Skip to content

add test for new union syntax on generic bounds #11431

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

Merged
merged 2 commits into from
Nov 3, 2021
Merged
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
30 changes: 29 additions & 1 deletion test-data/unit/check-generic-subtyping.test
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ class Y(Generic[T]):
return U() # E: Incompatible return value type (got "U", expected "T")


[case testTypeVarBoundToUnionAttributeAccess]
[case testTypeVarBoundToOldUnionAttributeAccess]
from typing import Union, TypeVar

class U:
Expand All @@ -844,6 +844,34 @@ main:15: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T
main:15: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"


[case testTypeVarBoundToNewUnionAttributeAccess]
# flags: --python-version 3.10
from typing import TypeVar

class U:
a: int
class V:
b: int
class W:
c: int

T = TypeVar("T", bound=U | V | W)

def f(x: T) -> None:
x.a # E
x.b = 1 # E
del x.c # E

[builtins fixtures/tuple.pyi]
[out]
main:14: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a"
main:14: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a"
main:15: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b"
main:15: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b"
main:16: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"
main:16: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"


[case testSubtypingIterableUnpacking1]
# https://github.com/python/mypy/issues/11138
from typing import Generic, Iterator, TypeVar
Expand Down