-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Fixes generic inference in functions with TypeGuard
, refs #11780
#11797
Conversation
Thanks! I have a similar use case with literals. I think this change helps a little, but my example runs into a different issue: from typing import Literal, TypeGuard, TypeVar, get_args
T = TypeVar('T')
def is_literal_value(value: str | int | None, literal_type: type[T]) -> TypeGuard[T]:
return value in get_args(literal_type)
value: str = input()
if is_literal_value(value, Literal['on', 'off']):
reveal_type(value) Expected result: Output in mypy master 😕
Output from this branch
|
Obligatory reference to #9773. |
Thanks @JelleZijlstra, I have been searching for discussion on this topic, but had not found that issue. |
Running into this problem, too. I get these errors:
When I run this code: from typing import Any, TypeGuard, Type, TypeVar
def do_stuff(foos: list[Foo]) -> None:
pass
class Foo:
pass
T = TypeVar("T")
def is_list_of(value: list, type: Type[T]) -> TypeGuard[list[T]]:
for item in value:
if isinstance(item, type) is False:
return False
return True
foos: list[Any] = [Foo()]
reveal_type(foos)
if is_list_of(foos, Foo):
reveal_type(foos)
do_stuff(foos) |
I think we can merge this. CC @JukkaL |
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.
This is great!
I think this might change behaviour if you have a function called edit: Github's diff view is out of date_
that is type guarded, but that's fine.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Closes #11780, closes #11428