-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What to do when the runtime lies? #11141
Comments
Generally we try to follow the actual implementation, until there is a good reason not to. Typing private modules is "better", but it was often neglected, because practically speaking there isn't too much difference, and is slightly more work. |
I think what I mean is that it's not obvious what "follow the implementation" means here, because different parts of the implementation are conflicting with each other. If we have:
Then we have followed the import structure accurately, but ReferenceType has a different qualname in typeshed and at runtime. If we have instead:
Now ReferenceType has a matching qualname in typeshed and at runtime, but we've had to reverse the import structure in order to do that. I'd make the case that the second version follows the implementation a little better, since the qualname is visible to mypy/stubcheck, while (I don't think) the import structure really is. I might be wrong about that? At any rate, I wasn't suggesting that we shouldn't make these available in the private module namespace. |
This is a great question. I don't have a strong opinion, but I lean towards:
I have this preference for two reasons:
This is also why in the "various inheritance" patch from #3968 I have a few cases of special casing underscore prefixed modules and classes: python/mypy@master...hauntsaninja:mypy:stubtestbaseclass#diff-55639a1f38efeacbb10988649399037c13ad11e9c82890b453d98ea2bed9caf8R267 |
The classes imported from the _ast module are defined in C, but set their __module__ to ast. This arrangement ensures that the type stubs have the same. related to python#3968 and the disccusion in python#11141
The classes imported from the _ast module are defined in C, but set their __module__ to ast. This arrangement ensures that the type stubs have the same. related to python#3968 and the discussion in python#11141
This improves fidelity of naming and inheritance on 3.11+ related to python#3968 and python#11141
This matches the name reported by the cass at runtime. related to python#11141
This matches the name reported by the cass at runtime. related to python#11141
This aligns with the implementation while giving greater fidelity to runtime naming and inheritance Related to python#3968 and python#11141
I made several MRs along these lines, but then I discovered that the _pydecimal module changes its Which got me thinking: what if mypy could detect cases where a module level I'm willing to take a run at that MR, if people think it's a reasonable idea. |
As it turned out, classes which set their own With that, typeshed could set |
There are a few pull requests that add missing If I understood correctly, we decided to do this (which IMO is a good idea for several reasons):
And if you have everything you'll ever need in Note that I'm talking about adding |
I created those while working on naming and inheritance - those all contain something which is a parent class of something else, so adding them brings the inheritance structure in typeshed closer to what it looks like at runtime. In other cases, it's a naming issue: the mod.Foo type calls itself _mod.Foo at runtime, and adding the It's a fair question whether that's justified in all cases or not. But that's not actually the subject of this ticket. Note that typeshed already has _weakref, and aligning the naming there would mean moving For another example and one more on subject: I've been investigating the issue of decoupling collections.abc from it's typing aliases (#6257). Runtime defines these classes in Typeshed has the same performance concerns; If we define them in |
I agree with @hauntsaninja above and nobody has forced disagreement, so let's go with that. I think that also aligns with my thinking as a CPython maintainer. For example, |
This matches the name reported by the cass at runtime. related to #11141 Co-authored-by: Jelle Zijlstra <[email protected]>
This improves fidelity of naming and inheritance on 3.11+ related to #3968 and #11141 Co-authored-by: Jelle Zijlstra <[email protected]>
I've been cleaning up inheritance mismatches. I've found that several modules with a C implementation fudge the module name of their classes. For example:
ReferenceType
is a class defined in the_weakref
C implementation, and then imported from there into theweakref
module. Because it's a C implementation, it's free to declare it's own qualified name, and calls itselfweakref.ReferenceType
: https://github.com/python/cpython/blob/main/Objects/weakrefobject.c#L368C1-L368C1The intention, I believe, being that this class canonically lives in the
weakref
module, and the fact that it's being defined in_weakref
is an implementation detail which is hidden at runtime. Typeshed declares the class in_weakref.pyi
, which means it gets a qualified name of_weakref.ReferenceType
I can see two possible arguments:
ReferenceType
is defined in_weakref
originally, so that's the correct place for it.OR
ReferenceType
considers itself to live in theweakref
module, that's where the majority of users will get it from, so that's the correct place for it.This pattern pops up in a lot of places: in
_ast
,_decimal
, and_sqlite3
are some of the others I've noticed. I suspect the current state of typeshed is inconsistent on this issue, since not all modules that have a C implementation currently have a_foo.pyi
stub file.Is there a consensus among typeshed's maintainers here? I'm a little biased towards option 2 at the moment, since I'm trying to make as many inheritances match as I can, but I think there's a good argument for either side, and this is, in the end, a very minor issue with not much practical effect.
The text was updated successfully, but these errors were encountered: