-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Make collections.abcs more consistent with runtime implementation #10816
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
Make collections.abcs more consistent with runtime implementation #10816
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Looking over these issues, all the aiohttp ones look like a mypy bug where it thinks that any annotated attribute is abstract the others hopefully might be easy enough fixes if you rename typing.Generator strings to types.GeneratorType, I'll see if a mypy PR can get rid of most of them. If that isn't possible would it be possible for mypy to selectively remove this commit? |
This comment has been minimized.
This comment has been minimized.
@@ -358,6 +358,12 @@ _ReturnT_co = TypeVar("_ReturnT_co", covariant=True) | |||
|
|||
@final | |||
class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be interested what happens if we just change types.pyi, but remove the typing
base classes to better match runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely sure what you're proposing, wouldn't that just crash mypy because typing.Generator wouldn't exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant just using class GeneratorType:
here (and adding the methods), not deleting typing.Generator
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typing.Generator hasn't been deleted it's just a protocol now
@@ -419,32 +419,16 @@ class Generator(Iterator[_YieldT_co], Generic[_YieldT_co, _SendT_contra, _Return | |||
@abstractmethod | |||
def throw(self, __typ: BaseException, __val: None = None, __tb: TracebackType | None = None) -> _YieldT_co: ... | |||
def close(self) -> None: ... | |||
def __iter__(self) -> Generator[_YieldT_co, _SendT_contra, _ReturnT_co]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure as to whether or not this should be -> Self the runtime has it as that in Iterator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon further consideration, not implementing it as returning Self is probably a bug so I'll change it in (Async)Iterator even
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typing.Generator.__iter__
shouldn't be annotated as returning Self
for the same reason that typing.Iterator.__iter__
shouldn't be annotated as returning Self
. 99% of all iterators have __iter__
methods that return Self
, and 99% of generators are the same. But if we hinted them as such in the stub, we'd be changing the very definition of Iterator/Generator such that the remaining 1% of iterators and generators would no longer be considered iterators or generators. And that wouldn't be accurate: it's not part of the contract that iterators have to return self
from their __iter__
methods at runtime in order for them to be considered iterators, they just have to return an iterable object.
With types.GeneratorType
, it's a different issue, because that class doesn't define an interface in the same way. We can just reflect what happens at runtime there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect removing this method is causing the mypy-primer fallout. Let's add it back.
@@ -358,6 +358,12 @@ _ReturnT_co = TypeVar("_ReturnT_co", covariant=True) | |||
|
|||
@final | |||
class GeneratorType(Generator[_YieldT_co, _SendT_contra, _ReturnT_co]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely sure what you're proposing, wouldn't that just crash mypy because typing.Generator wouldn't exist?
This change feels correct, let's see if we can get type checkers to agree. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cc @A5rocks regarding the primer changes in trio. I think they are technically correct; trio uses the Coroutine ABC, which does not guarantee the existence of attributes like |
This comment has been minimized.
This comment has been minimized.
I agree with these changes being technically correct, because
Anyways, I think it's better to be more correct. We have added asserts to check |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: trio (https://github.com/python-trio/trio)
+ src/trio/_core/_traps.py:307: error: "Coroutine[Any, Outcome[object], Any]" has no attribute "cr_running" [attr-defined]
+ src/trio/_core/_ki.py:179: error: "Coroutine[Any, Outcome[object], Any]" has no attribute "cr_frame" [attr-defined]
+ src/trio/_core/_run.py:1882: error: "Coroutine[object, Never, object]" has no attribute "cr_frame" [attr-defined]
+ src/trio/_core/_tests/test_asyncgen.py:91: error: "AsyncGenerator[int, None]" has no attribute "ag_frame" [attr-defined]
+ src/trio/_core/_tests/test_asyncgen.py:304: error: "AsyncGenerator[object, Never]" has no attribute "ag_frame" [attr-defined]
+ src/trio/_core/_tests/test_asyncgen.py:307: error: "AsyncGenerator[object, Never]" has no attribute "ag_frame" [attr-defined]
|
Interested to see how much breakage primer reports.
refs: python/typing#1480