-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Return Supports(A)Next from (a)iter #6035
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
We'll need to change the types of |
Regardless of whether we change |
This comment has been minimized.
This comment has been minimized.
stdlib/_typeshed/__init__.pyi
Outdated
def __next__(self) -> _T_co: ... | ||
|
||
# stable | ||
class SupportsANext(Protocol[_T_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.
Nitty: Maybe this should be SupportsAnext
?
We have SupportsAclose
elsewhere.
Anext is the camel case equivalent of anext
/ maybe reads more distinguishably from SupportsNext
? Don't feel strongly though!
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.
For what it's worth 3.10rc2 renamed PyAiter_Check to PyAIter_Check in the C API (https://docs.python.org/3.10/whatsnew/changelog.html#id2).
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
stdlib/builtins.pyi
Outdated
_SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=True) | ||
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True) | ||
|
||
class _Iterable2(Protocol[_T_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.
Why not HasIter
?
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've renamed to protocols (as _Supports...
). Do you think it would make sense to add them to _typeshed
?
This comment has been minimized.
This comment has been minimized.
I like how using a That said, the new error message from |
I prefer the correctness and the fact that it fixes two separate problems over the minor inconvenience that one particular type checker has a slightly obtuse error message in a complicated case. This really shouldn't be a factor. |
What two problems? I have seen no actual problems that this fixes, besides some theorethical correctness that really doesn't seem to apply in practice and therefore is meaningless to me.
It is true that mypy is just "one particular type checker", but it is an important type checker. In other areas of typeshed, we already have "unnecessary" overloads just to help mypy in complicated situations. I'm fine with merging this, even though I still don't see why it would be a good thing. |
That |
A non-concrete project_iter = iter(p for p in PROJECTS)
if ARGS.project_selector:
projects = [ ... ]
...
project_iter = iter(projects) If That said, this was previously considered not very important in #5145, and maybe we should go with that here too. It is also easy to fix by adding a type annotation, However, typeshed is not just for type checkers. What autocompletions do we want to get in this situation? iterator = iter(some_function_that_yields())
iterator.<Tab> I wouldn't want to want to see So again, I don't really like this change, but I'm fine with it getting merged. |
Diff from mypy_primer, showing the effect of this PR on open source code: mypy_primer (https://github.com/hauntsaninja/mypy_primer)
+ mypy_primer.py:557: error: Argument 1 to "iter" has incompatible type "List[Project]"; expected "_SupportsIter[Generator[Project, None, None]]"
|
Explore the impact of changing the return type, see #6030.