-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
builtins.filter
compat with typing.TypeGuard
#6726
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 change enables the following use-case: ```python def is_not_none(x: Optional[int]) -> TypeGuard[int]: return x is not None list_optional: list[Optional[int]] = [0, None, 1, None, 2] generate_ints: Iterable[int] = filter(is_not_none, list_optional) ```
This comment has been minimized.
This comment has been minimized.
It has been brought to my attention that this PR is related to issue #5661 and to PR #6140. |
It appears that the diff implemented in this PR is identical to that from #6140. |
Interesting, maybe the latest mypy release fixed things. cc @sobolevn. |
Yes, this was my PR to mypy: python/mypy#11314 after our initial attempt: #6140 |
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.
Can you please attach a simple manual test to show that it is working?
Yup, #6140 has the correct overload order. I think that wasn't tested against mypy 0.930, so worth trying again. |
Co-authored-by: Alex Waygood <[email protected]>
Diff from mypy_primer, showing the effect of this PR on open source code: ibis (https://github.com/ibis-project/ibis)
+ ibis/backends/csv/__init__.py:72: error: Argument 1 to "filter" has incompatible type "Callable[[Any], bool]"; expected "Callable[[Any], TypeGuard[bool]]"
+ ibis/backends/csv/__init__.py:78: error: "bool" has no attribute "name"
+ ibis/backends/csv/__init__.py:82: error: "bool" has no attribute "schema"
+ ibis/backends/csv/__init__.py:91: error: "bool" has no attribute "schema"
+ ibis/backends/csv/__init__.py:92: error: Dict entry 0 has incompatible type "bool": "Any"; expected "Node": "Any"
|
Huh, that's weird. I'm guessing mypy is failing to fall back to the third overload due to some strange |
This one is the most strange: It should just continue with other overload (probably this one: I will take a look into mypy itself 👍 |
I am unable to reproduce this locally. |
Going to merge this since the change is correct and mostly works with mypy. If it does expose a mypy bug, that can be tracked over on the mypy repo. |
Based on microsoft/pyright#2768.
This change enables the following use-case: