Fixes generic inference in functions with TypeGuard, refs #11780#11797
Merged
hauntsaninja merged 6 commits intopython:masterfrom May 8, 2022
sobolevn:issue-11780
Merged
Fixes generic inference in functions with TypeGuard, refs #11780#11797hauntsaninja merged 6 commits intopython:masterfrom sobolevn:issue-11780
TypeGuard, refs #11780#11797hauntsaninja merged 6 commits intopython:masterfrom
sobolevn:issue-11780
Conversation
Contributor
|
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 |
Member
|
Obligatory reference to #9773. |
Contributor
|
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) |
Member
Author
|
I think we can merge this. CC @JukkaL |
hauntsaninja
approved these changes
May 7, 2022
hauntsaninja
reviewed
May 7, 2022
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
hauntsaninja
reviewed
May 8, 2022
This comment has been minimized.
This comment has been minimized.
1 similar comment
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #11780, closes #11428