-
Notifications
You must be signed in to change notification settings - Fork 259
Using types
classes over collections.abc
's bases
#1480
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
Comments
I can't follow this sentence. Could you elaborate, maybe by mentioning the actual attributes you're referring to. |
Currently collections.abc.Generator defines itself as having these extra properties @property
def gi_code(self) -> CodeType: ...
@property
def gi_frame(self) -> FrameType: ...
@property
def gi_running(self) -> bool: ...
@property
def gi_yieldfrom(self) -> Generator[Any, Any, Any] | None: ... which aren't required by the actual runtime subclass check (which is just _check_methods(C, '__iter__', '__next__', 'send', 'throw', 'close') ) |
Oh, this is a typeshed issue. You should probably close this issue here and rely on the typeshed issue you just opened. |
It's not purely typeshed related because it does require changes to type checkers as well and things at runtime |
types
classes over collections.abc
's bases
What runtime changes do you propose? |
Python 3.13.0a0 (heads/more-pydoc-str-dirty:a6fedf2a0a, Oct 1 2023, 11:49:10) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import types
>>> types.CoroutineType[None, None, int]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'coroutine' is not subscriptable |
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 |
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 |
@erictraut would you mind explaining why you think doing this is a bad idea? |
The classes defined in If there are problems with the existing ABCs defined in |
I'd propose enforcing that the return types of things that are at runtime in the |
Uh oh!
There was an error while loading. Please reload this page.
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:
collections.abc.pyi
Protocol
s that should be based on their runtime implementation (they implement custom__subclasshook__
) and not the concrete types. (Make collections.abcs more consistent with runtime implementation typeshed#10816)types
classes subscriptable at runtime. (gh-110209: Add __class_getitem__ for generator and coroutine cpython#110212)def foo(): yield
astypes.GeneratorType
and not justcollections.abc.Generator
.(as a small aside it might be nice to move the type implementations from typing to collections.abc soon)
refs: microsoft/pyright#6053
The text was updated successfully, but these errors were encountered: