You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using types classes over collections.abc's bases #1480
Currently collections.abc.Generator/Coroutine/AsyncGenerator etc. have attributes that don't have to exist at runtime. I think it'd be wise to switch recommending to using the types concrete classes where possible over the abstract versions which have a smaller interface because having this weird sort of duplication where the 2 are basically the same is a wrinkle that's bitten me recently. To do this I think a few things need to happen:
I don't think that any runtime changes are required to address the problem you're talking about here. We simply need a protocol defined somewhere in typeshed (e.g. in the _typeshed module) that type checkers can use for this purpose. The SupportsKeysAndGetItem is analogous and is used by both pyright and mypy.
I think the protocol partly solves the problem but there is a bigger issue underlying all of this. The runtime changes at least to me seem like a good idea because the types classes are subscriptable at type time just not at runtime currently
The classes defined in types are runtime details. Unless you're writing tooling-related code that needs access to these details (e.g. a runtime type checker or debugger), you should not use these classes in type annotations. This is already a point of confusion for newcomers to typing, and my blanket guidance is "if you're importing a type from the types module and using it in a type annotation, you're probably doing something wrong." If we start to promote the use of some of the classes from the types module, it will create significant confusion. It will also require type checkers to expand existing special-case logic that knows about classes in the typing and collections.abc modules.
If there are problems with the existing ABCs defined in collections.abc, let's fix those rather than doing something more radical.
I'd propose enforcing that the return types of things that are at runtime in the types module as their collections.abc counterparts and then by structural type checks the types versions should be fine to return if you want the specific "correct" types. That shouldn't at least to my knowledge require much more special casing than currently present, which is why I don't see this change being problematic.
Activity
gvanrossum commentedon Oct 1, 2023
I can't follow this sentence. Could you elaborate, maybe by mentioning the actual attributes you're referring to.
Gobot1234 commentedon Oct 1, 2023
Currently collections.abc.Generator defines itself as having these extra properties
which aren't required by the actual runtime subclass check (which is just
)
gvanrossum commentedon Oct 1, 2023
Oh, this is a typeshed issue. You should probably close this issue here and rely on the typeshed issue you just opened.
Gobot1234 commentedon Oct 1, 2023
It's not purely typeshed related because it does require changes to type checkers as well and things at runtime
[-]types over collections.abc [/-][+]Using types classes over collections.abc's bases[/+][-]Using types classes over collections.abc's bases[/-][+]Using `types` classes over `collections.abc`'s bases[/+]gvanrossum commentedon Oct 1, 2023
What runtime changes do you propose?
Gobot1234 commentedon Oct 1, 2023
erictraut commentedon Oct 1, 2023
I don't think that any runtime changes are required to address the problem you're talking about here. We simply need a protocol defined somewhere in typeshed (e.g. in the
_typeshedmodule) that type checkers can use for this purpose. TheSupportsKeysAndGetItemis analogous and is used by both pyright and mypy.Gobot1234 commentedon Oct 1, 2023
I think the protocol partly solves the problem but there is a bigger issue underlying all of this. The runtime changes at least to me seem like a good idea because the
typesclasses are subscriptable at type time just not at runtime currentlyGobot1234 commentedon Oct 12, 2023
@erictraut would you mind explaining why you think doing this is a bad idea?
erictraut commentedon Oct 12, 2023
The classes defined in
typesare runtime details. Unless you're writing tooling-related code that needs access to these details (e.g. a runtime type checker or debugger), you should not use these classes in type annotations. This is already a point of confusion for newcomers to typing, and my blanket guidance is "if you're importing a type from thetypesmodule and using it in a type annotation, you're probably doing something wrong." If we start to promote the use of some of the classes from thetypesmodule, it will create significant confusion. It will also require type checkers to expand existing special-case logic that knows about classes in thetypingandcollections.abcmodules.If there are problems with the existing ABCs defined in
collections.abc, let's fix those rather than doing something more radical.Gobot1234 commentedon Oct 12, 2023
I'd propose enforcing that the return types of things that are at runtime in the
typesmodule as theircollections.abccounterparts and then by structural type checks the types versions should be fine to return if you want the specific "correct" types. That shouldn't at least to my knowledge require much more special casing than currently present, which is why I don't see this change being problematic.