-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
TypeGuard typechecking on filter
is too strict
#12682
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
NB: the new behavior was introduced in python/typeshed#5661 |
This seems like a mypy bug, not a typeshed bug. It's picking the wrong overload. |
Yeah, mypy isn't happy with this on 0.942 either, I think the bug is a type context related. |
Any word on getting this fixed? Maybe the same issue - #12996 |
Has a workaround been identified in the meantime? |
Although error message is bad (mypy sometimes randomly chooses "best" overload match to report error if none of overload matches), it is technically correct (assuming that best inferred type of the first argument in second and third call is from typing import Any, List, Optional
path_parts: List[Optional[str]] = ["foo", None, "bar"]
def always(x: Any) -> bool:
return True
"/".join(filter(always, path_parts)) So to support this we will need both:
Both are possible but non-trivial (and still a bit hacky). |
I was personally getting this error whenever I was attempting to filter a Narrowing the type with an assertion was enough to stop mypy from producing this error. table_name = self.table_specifier.specifier.get("table")
if not table_name:
raise ...
filtered = list(filter(lambda x: x in <>)) |
Another example snippet that triggers the issue. Here mypy is happy from typing_extensions import reveal_type
non_none_strings = list(filter(lambda x: x is not None, [None, "value"]))
reveal_type(non_none_strings) the revealed type is from typing_extensions import reveal_type
non_none_strings: list[str] = list(filter(lambda x: x is not None, [None, "value"]))
reveal_type(non_none_strings)
print(", ".join(non_none_strings)) which makes mypy complain with
The workaround I had to use was to NOT pass the lambda at all from typing_extensions import reveal_type
non_none_strings: list[str] = list(filter(None, [None, "value"]))
reveal_type(non_none_strings)
print(", ".join(non_none_strings)) |
In my use-case I needed the first item of a list of dicts where a particular key equals a particular value. At first I used item = next(filter(lambda v: v["my-key"] == "my-value", list_of_dicts), None)
if item is None:
...
else:
... I got 3 mypy errors for the same line:
My workaround was to use item = next((item for item in list_of_dicts if item["my-key"] == "my-value"), None)
if item is None:
...
else:
... |
As reported in python/mypy#12682, mypy didn't recognise `bool` as a `TypeGuard[T]` for `Optional[T]`. However it doesn't flag using `None` as a typeguard in `filter` which behaves exactly the same. * sphinx_polyversion/git.py
Bug Report
NB: This may be more properly an issue in Typeshed, or require changes in that project to fix, but our team encountered it via MyPy.
The type annotations on
builtins.filter
cause correct and idiomatic usages to fail to typecheck:To Reproduce
Expected:
Actual result:
Your Environment
$> mypy --version mypy 0.950 (compiled: yes)
$> python --version Python 3.10.4
The following is what my editor (VSCode) pulls up when I investigate the type hints for
filter
, though I doubt this is the same file that MyPy itself is consulting:Note that both offending lines conform to the third type signature, but fail the second.
The text was updated successfully, but these errors were encountered: