Skip to content
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

Fix false negatives involving Unions and generators or coroutines #14224

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ def get_generator_yield_type(self, return_type: Type, is_coroutine: bool) -> Typ

if isinstance(return_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
elif isinstance(return_type, UnionType):
return make_simplified_union(
[self.get_generator_yield_type(item, is_coroutine) for item in return_type.items]
)
elif not self.is_generator_return_type(
return_type, is_coroutine
) and not self.is_async_generator_return_type(return_type):
Expand Down Expand Up @@ -873,6 +877,10 @@ def get_generator_receive_type(self, return_type: Type, is_coroutine: bool) -> T

if isinstance(return_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
elif isinstance(return_type, UnionType):
return make_simplified_union(
[self.get_generator_receive_type(item, is_coroutine) for item in return_type.items]
)
elif not self.is_generator_return_type(
return_type, is_coroutine
) and not self.is_async_generator_return_type(return_type):
Expand Down Expand Up @@ -912,6 +920,10 @@ def get_generator_return_type(self, return_type: Type, is_coroutine: bool) -> Ty

if isinstance(return_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=return_type)
elif isinstance(return_type, UnionType):
return make_simplified_union(
[self.get_generator_return_type(item, is_coroutine) for item in return_type.items]
)
elif not self.is_generator_return_type(return_type, is_coroutine):
# If the function doesn't have a proper Generator (or
# Awaitable) return type, anything is permissible.
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,21 @@ async def f() -> AsyncGenerator[int, None]:

[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]

[case testAwaitUnion]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also fix type checking yield in generators? If yes, can you add a test case for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it does, will do!

from typing import overload, Union

class A: ...
class B: ...

@overload
async def foo(x: A) -> B: ...
@overload
async def foo(x: B) -> A: ...
async def foo(x): ...

async def bar(x: Union[A, B]) -> None:
reveal_type(await foo(x)) # N: Revealed type is "Union[__main__.B, __main__.A]"

[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
9 changes: 9 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -2206,3 +2206,12 @@ def foo():
x: int = "no" # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs
y = "no" # type: int # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs
z: int # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs

[case testGeneratorUnion]
from typing import Generator, Union

class A: pass
class B: pass

def foo(x: int) -> Union[Generator[A, None, None], Generator[B, None, None]]:
yield x # E: Incompatible types in "yield" (actual type "int", expected type "Union[A, B]")