diff --git a/mypy/test/helpers.py b/mypy/test/helpers.py index d5f1cd787953..658a87016e16 100644 --- a/mypy/test/helpers.py +++ b/mypy/test/helpers.py @@ -357,7 +357,7 @@ def parse_options( options = Options() options.error_summary = False options.hide_error_codes = True - options.force_union_syntax = True + options.overwrite_union_syntax = True # Allow custom python version to override testfile_pyversion. if all(flag.split("=")[0] != "--python-version" for flag in flag_list): diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index 96be1a044d91..155d7912bb9e 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -136,6 +136,7 @@ def run_case_once( options = parse_options(original_program_text, testcase, incremental_step) options.use_builtins_fixtures = True options.show_traceback = True + options.overwrite_union_syntax = True if options.num_workers: options.fixed_format_cache = True @@ -151,8 +152,6 @@ def run_case_once( options.hide_error_codes = False if "abstract" not in testcase.file: options.allow_empty_bodies = not testcase.name.endswith("_no_empty") - if "union-error" not in testcase.file and "Pep604" not in testcase.name: - options.force_union_syntax = True if incremental_step and options.incremental or options.num_workers > 0: # Don't overwrite # flags: --no-incremental in incremental test cases diff --git a/mypy/test/testfinegrained.py b/mypy/test/testfinegrained.py index b098c1fb0ad2..7899c5b611a1 100644 --- a/mypy/test/testfinegrained.py +++ b/mypy/test/testfinegrained.py @@ -152,6 +152,7 @@ def get_options(self, source: str, testcase: DataDrivenTestCase, build_cache: bo options.use_fine_grained_cache = self.use_cache and not build_cache options.cache_fine_grained = self.use_cache options.local_partial_types = True + options.overwrite_union_syntax = True options.export_types = "inspect" in testcase.file # Treat empty bodies safely for these test cases. options.allow_empty_bodies = not testcase.name.endswith("_no_empty") diff --git a/mypy/test/testmerge.py b/mypy/test/testmerge.py index c2c75f60be29..cd9edd0a4790 100644 --- a/mypy/test/testmerge.py +++ b/mypy/test/testmerge.py @@ -102,6 +102,7 @@ def build(self, source: str, testcase: DataDrivenTestCase) -> BuildResult | None options.export_types = True options.show_traceback = True options.allow_empty_bodies = True + options.overwrite_union_syntax = True main_path = os.path.join(test_temp_dir, "main") self.str_conv.options = options diff --git a/mypy/test/testparse.py b/mypy/test/testparse.py index d9d94a5edc38..324a38ee361d 100644 --- a/mypy/test/testparse.py +++ b/mypy/test/testparse.py @@ -38,6 +38,7 @@ def test_parser(testcase: DataDrivenTestCase) -> None: The argument contains the description of the test case. """ options = Options() + options.overwrite_union_syntax = True options.hide_error_codes = True if testcase.file.endswith("python310.test"): diff --git a/mypy/test/testsemanal.py b/mypy/test/testsemanal.py index 11cd36c83fd7..58924f1b8389 100644 --- a/mypy/test/testsemanal.py +++ b/mypy/test/testsemanal.py @@ -38,6 +38,7 @@ def get_semanal_options(program_text: str, testcase: DataDrivenTestCase) -> Opti options.semantic_analysis_only = True options.show_traceback = True options.python_version = PYTHON3_VERSION + options.overwrite_union_syntax = True return options diff --git a/mypy/test/testtransform.py b/mypy/test/testtransform.py index 48a3eeed2115..caac13244d98 100644 --- a/mypy/test/testtransform.py +++ b/mypy/test/testtransform.py @@ -38,6 +38,7 @@ def test_transform(testcase: DataDrivenTestCase) -> None: options.use_builtins_fixtures = True options.semantic_analysis_only = True options.show_traceback = True + options.overwrite_union_syntax = True result = build.build( sources=[BuildSource("main", None, src)], options=options, alt_lib_path=test_temp_dir ) diff --git a/mypy/test/testtypegen.py b/mypy/test/testtypegen.py index 42d831beeecc..38ac75df5f99 100644 --- a/mypy/test/testtypegen.py +++ b/mypy/test/testtypegen.py @@ -35,6 +35,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None: options.export_types = True options.preserve_asts = True options.allow_empty_bodies = True + options.overwrite_union_syntax = True result = build.build( sources=[BuildSource("main", None, src)], options=options, diff --git a/test-data/unit/check-annotated.test b/test-data/unit/check-annotated.test index 24f4a1d945c6..d4de3f7b5043 100644 --- a/test-data/unit/check-annotated.test +++ b/test-data/unit/check-annotated.test @@ -8,7 +8,7 @@ reveal_type(x) # N: Revealed type is "builtins.int" from typing import Union from typing_extensions import Annotated x: Annotated[Union[int, str], ...] -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testAnnotated2] @@ -51,7 +51,7 @@ reveal_type(x) # N: Revealed type is "builtins.int" from typing import Union from typing_extensions import Annotated x: Annotated[Annotated[Union[int, str], ...], ...] -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testAnnotatedNestedBadType] @@ -114,7 +114,7 @@ from typing_extensions import Annotated T = TypeVar('T') Alias = Annotated[Union[T, str], ...] x: Alias[int] -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testAnnotatedSecondParamNonType] diff --git a/test-data/unit/check-async-await.test b/test-data/unit/check-async-await.test index 979da62aca92..e4bd4568f8c8 100644 --- a/test-data/unit/check-async-await.test +++ b/test-data/unit/check-async-await.test @@ -205,7 +205,7 @@ async def f() -> None: pass async for z in C(): # type: Union[int, str] - reveal_type(z) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/async_await.pyi] [typing fixtures/typing-async.pyi] @@ -952,7 +952,7 @@ 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]" + reveal_type(await foo(x)) # N: Revealed type is "__main__.B | __main__.A" [builtins fixtures/async_await.pyi] [typing fixtures/typing-async.pyi] diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 07ed5fd77082..bfd5e4e59303 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -220,7 +220,7 @@ reveal_type(__file__) # N: Revealed type is "builtins.str" reveal_type(__package__) # N: Revealed type is "builtins.str" reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]" # This will actually reveal Union[importlib.machinery.ModuleSpec, None] -reveal_type(__spec__) # N: Revealed type is "Union[builtins.object, None]" +reveal_type(__spec__) # N: Revealed type is "builtins.object | None" import module reveal_type(module.__name__) # N: Revealed type is "builtins.str" @@ -416,7 +416,7 @@ D = TypedDict('D', {'y': int}) def foo() -> Optional[A]: b = True - return a.A() if b else None # E: Incompatible return value type (got "Optional[a.A]", expected "Optional[b.A]") + return a.A() if b else None # E: Incompatible return value type (got "a.A | None", expected "b.A | None") def bar() -> List[A]: l = [a.A()] @@ -424,10 +424,10 @@ def bar() -> List[A]: def baz() -> Union[A, int]: b = True - return a.A() if b else 10 # E: Incompatible return value type (got "Union[a.A, int]", expected "Union[b.A, int]") + return a.A() if b else 10 # E: Incompatible return value type (got "a.A | int", expected "b.A | int") def spam() -> Optional[A]: - return a.A() # E: Incompatible return value type (got "a.A", expected "Optional[b.A]") + return a.A() # E: Incompatible return value type (got "a.A", expected "b.A | None") def eggs() -> Sequence[A]: x = [a.A()] @@ -516,11 +516,11 @@ x: """ str """ -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" y: """( int | str ) """ -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" diff --git a/test-data/unit/check-callable.test b/test-data/unit/check-callable.test index 0157ff3d2c53..f3ec6ec5f939 100644 --- a/test-data/unit/check-callable.test +++ b/test-data/unit/check-callable.test @@ -47,7 +47,7 @@ x = 5 # type: Union[int, Callable[[], str], Callable[[], int]] if callable(x): y = x() + 2 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[str, int]" + # N: Left operand is of type "str | int" else: z = x + 6 @@ -62,7 +62,7 @@ if callable(x): y = x() + 'test' else: z = x + 6 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" [builtins fixtures/callable.pyi] @@ -156,7 +156,7 @@ if callable(x) and x() == 'test': x() else: x + 5 # E: Unsupported left operand type for + ("Callable[[], str]") \ - # N: Left operand is of type "Union[int, Callable[[], str]]" + # N: Left operand is of type "int | Callable[[], str]" [builtins fixtures/callable.pyi] @@ -280,9 +280,9 @@ T = Union[Union[int, Callable[[], int]], Union[str, Callable[[], str]]] def f(t: T) -> None: if callable(t): - reveal_type(t()) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(t()) # N: Revealed type is "builtins.int | builtins.str" else: - reveal_type(t) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(t) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/callable.pyi] diff --git a/test-data/unit/check-class-namedtuple.test b/test-data/unit/check-class-namedtuple.test index fe8a1551f81b..c9b5b0f4db1a 100644 --- a/test-data/unit/check-class-namedtuple.test +++ b/test-data/unit/check-class-namedtuple.test @@ -396,7 +396,7 @@ class HasNone(NamedTuple): x: int y: Optional[int] = None -reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, Union[builtins.int, None], fallback=__main__.HasNone]" +reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, builtins.int | None, fallback=__main__.HasNone]" class Parameterized(NamedTuple): x: int @@ -425,7 +425,7 @@ class HasNone(NamedTuple): x: int y: Optional[int] = None -reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, Union[builtins.int, None], fallback=__main__.HasNone]" +reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, builtins.int | None, fallback=__main__.HasNone]" HasNone(None) # E: Argument 1 to "HasNone" has incompatible type "None"; expected "int" HasNone(1, y=None) HasNone(1, y=2) diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 0bd400139822..6a0fda5a5eca 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -769,7 +769,7 @@ class A: class B(A): def __replace__(self, x: str) -> Self: pass # E: \ - # E: Argument 1 of "__replace__" is incompatible with supertype "A"; supertype defines the argument type as "Optional[str]" [override] \ + # E: Argument 1 of "__replace__" is incompatible with supertype "A"; supertype defines the argument type as "str | None" [override] \ # N: This violates the Liskov substitution principle \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides [builtins fixtures/tuple.pyi] @@ -899,18 +899,18 @@ class A: f4: Union[Callable[[str], str], str] class B(A): - def f1(self, x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[str], str]") + def f1(self, x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], str] | str", override has type "Callable[[str], str]") pass - def f2(self, x: object) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[object], str]") + def f2(self, x: object) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], str] | str", override has type "Callable[[object], str]") pass @classmethod - def f3(cls, x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[str], str]") + def f3(cls, x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], str] | str", override has type "Callable[[str], str]") pass @staticmethod - def f4(x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[str], str]") + def f4(x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], str] | str", override has type "Callable[[str], str]") pass [builtins fixtures/classmethod.pyi] @@ -1149,10 +1149,10 @@ T = TypeVar('T') def join(x: T, y: T) -> T: pass f1 = join(func, WithMetaclass) -reveal_type(f1()) # N: Revealed type is "Union[__main__.WithMetaclass, None]" +reveal_type(f1()) # N: Revealed type is "__main__.WithMetaclass | None" f2 = join(WithMetaclass, func) -reveal_type(f2()) # N: Revealed type is "Union[__main__.WithMetaclass, None]" +reveal_type(f2()) # N: Revealed type is "__main__.WithMetaclass | None" -- Attribute access in class body -- ------------------------------ @@ -2829,9 +2829,9 @@ a: Union[int, float] b: int c: float -reveal_type(a + a) # N: Revealed type is "Union[builtins.int, builtins.float]" -reveal_type(a + b) # N: Revealed type is "Union[builtins.int, builtins.float]" -reveal_type(b + a) # N: Revealed type is "Union[builtins.int, builtins.float]" +reveal_type(a + a) # N: Revealed type is "builtins.int | builtins.float" +reveal_type(a + b) # N: Revealed type is "builtins.int | builtins.float" +reveal_type(b + a) # N: Revealed type is "builtins.int | builtins.float" reveal_type(a + c) # N: Revealed type is "builtins.float" reveal_type(c + a) # N: Revealed type is "builtins.float" [builtins fixtures/ops.pyi] @@ -2877,16 +2877,16 @@ a + a # E: Unsupported operand types for + ("Foo" and "Bar") \ # N: Both left and right operands are unions a + b # E: Unsupported operand types for + ("Bar" and "Foo") \ - # N: Left operand is of type "Union[Foo, Bar]" + # N: Left operand is of type "Foo | Bar" b + a # E: Unsupported operand types for + ("Foo" and "Bar") \ - # N: Right operand is of type "Union[Foo, Bar]" + # N: Right operand is of type "Foo | Bar" a + c # E: Unsupported operand types for + ("Foo" and "Bar") \ - # N: Left operand is of type "Union[Foo, Bar]" + # N: Left operand is of type "Foo | Bar" c + a # E: Unsupported operand types for + ("Bar" and "Foo") \ - # N: Right operand is of type "Union[Foo, Bar]" + # N: Right operand is of type "Foo | Bar" [case testOperatorDoubleUnionNoRelationship2] from typing import Union @@ -2903,9 +2903,9 @@ a: Union[Foo, Bar] b: Foo c: Bar -reveal_type(a + a) # N: Revealed type is "Union[__main__.Foo, __main__.Bar]" -reveal_type(a + b) # N: Revealed type is "Union[__main__.Foo, __main__.Bar]" -reveal_type(b + a) # N: Revealed type is "Union[__main__.Foo, __main__.Bar]" +reveal_type(a + a) # N: Revealed type is "__main__.Foo | __main__.Bar" +reveal_type(a + b) # N: Revealed type is "__main__.Foo | __main__.Bar" +reveal_type(b + a) # N: Revealed type is "__main__.Foo | __main__.Bar" reveal_type(a + c) # N: Revealed type is "__main__.Bar" reveal_type(c + a) # N: Revealed type is "__main__.Bar" @@ -2946,11 +2946,11 @@ class D: x: Union[A, B] y: Union[C, D] -reveal_type(x + y) # N: Revealed type is "Union[__main__.Out3, __main__.Out1, __main__.Out2, __main__.Out4]" -reveal_type(A() + y) # N: Revealed type is "Union[__main__.Out3, __main__.Out1]" -reveal_type(B() + y) # N: Revealed type is "Union[__main__.Out2, __main__.Out4]" -reveal_type(x + C()) # N: Revealed type is "Union[__main__.Out3, __main__.Out2]" -reveal_type(x + D()) # N: Revealed type is "Union[__main__.Out1, __main__.Out4]" +reveal_type(x + y) # N: Revealed type is "__main__.Out3 | __main__.Out1 | __main__.Out2 | __main__.Out4" +reveal_type(A() + y) # N: Revealed type is "__main__.Out3 | __main__.Out1" +reveal_type(B() + y) # N: Revealed type is "__main__.Out2 | __main__.Out4" +reveal_type(x + C()) # N: Revealed type is "__main__.Out3 | __main__.Out2" +reveal_type(x + D()) # N: Revealed type is "__main__.Out1 | __main__.Out4" [case testOperatorDoubleUnionDivision] from typing import Union @@ -2970,8 +2970,8 @@ def sum(x: Iterable[T]) -> Union[T, int]: ... def len(x: Iterable[T]) -> int: ... x = [1.1, 2.2, 3.3] -reveal_type(sum(x)) # N: Revealed type is "Union[builtins.float, builtins.int]" -reveal_type(sum(x) / len(x)) # N: Revealed type is "Union[builtins.float, builtins.int]" +reveal_type(sum(x)) # N: Revealed type is "builtins.float | builtins.int" +reveal_type(sum(x) / len(x)) # N: Revealed type is "builtins.float | builtins.int" [builtins fixtures/floatdict.pyi] [case testOperatorWithEmptyListAndSum] @@ -3423,7 +3423,7 @@ class C: x = C(foo=12) x.a # E: "C" has no attribute "a" -C(foo='') # E: Argument "foo" to "C" has incompatible type "str"; expected "Optional[int]" +C(foo='') # E: Argument "foo" to "C" has incompatible type "str"; expected "int | None" [builtins fixtures/__new__.pyi] [case testConstructInstanceWithDynamicallyTyped__new__] @@ -3850,7 +3850,7 @@ def process(cls: Type[Union[BasicUser, ProUser]]): obj = cls() cls.bar(obj) cls.mro() # Defined in class type - cls.error # E: Item "type" of "Union[type[BasicUser], type[ProUser]]" has no attribute "error" + cls.error # E: Item "type" of "type[BasicUser] | type[ProUser]" has no attribute "error" [builtins fixtures/classmethod.pyi] [out] @@ -5608,7 +5608,7 @@ class Bar(NamedTuple): def foo(node: Node) -> int: x = node - reveal_type(node) # N: Revealed type is "Union[tuple[builtins.int, fallback=__main__.Foo], tuple[builtins.int, fallback=__main__.Bar]]" + reveal_type(node) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Foo] | tuple[builtins.int, fallback=__main__.Bar]" return x.x [builtins fixtures/tuple.pyi] [out] @@ -5686,7 +5686,7 @@ ForwardUnion = Union['TP', int] class TP(NamedTuple('TP', [('x', int)])): pass def f(x: ForwardUnion) -> None: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, fallback=__main__.TP], builtins.int]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.TP] | builtins.int" if isinstance(x, TP): reveal_type(x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.TP]" [builtins fixtures/isinstance.pyi] @@ -7147,7 +7147,7 @@ def test() -> None: x = Other else: return - reveal_type(x) # N: Revealed type is "Union[def () -> __main__.One, def () -> __main__.Other]" + reveal_type(x) # N: Revealed type is "def () -> __main__.One | def () -> __main__.Other" reveal_type(x.x) # N: Revealed type is "builtins.int" [builtins fixtures/isinstancelist.pyi] @@ -7362,10 +7362,10 @@ from typing import Tuple, Optional # Check for some cases that aren't allowed class X: - def __new__(cls) -> Optional[Y]: # E: "__new__" must return a class instance (got "Optional[Y]") + def __new__(cls) -> Optional[Y]: # E: "__new__" must return a class instance (got "Y | None") pass class Y: - def __new__(cls) -> Optional[int]: # E: "__new__" must return a class instance (got "Optional[int]") + def __new__(cls) -> Optional[int]: # E: "__new__" must return a class instance (got "int | None") pass @@ -7807,7 +7807,7 @@ class Foo: self.x = None self.y = [] -reveal_type(Foo().x) # N: Revealed type is "Union[Any, None]" +reveal_type(Foo().x) # N: Revealed type is "Any | None" reveal_type(Foo().y) # N: Revealed type is "builtins.list[Any]" [builtins fixtures/list.pyi] diff --git a/test-data/unit/check-classvar.test b/test-data/unit/check-classvar.test index 7918ccded2fe..e781e5fa11c1 100644 --- a/test-data/unit/check-classvar.test +++ b/test-data/unit/check-classvar.test @@ -165,8 +165,8 @@ A.x = 'a' A.x = B() reveal_type(A().x) [out] -main:8: error: Incompatible types in assignment (expression has type "B", variable has type "Union[int, str]") -main:9: note: Revealed type is "Union[builtins.int, builtins.str]" +main:8: error: Incompatible types in assignment (expression has type "B", variable has type "int | str") +main:9: note: Revealed type is "builtins.int | builtins.str" [case testOverrideWithNarrowedUnion] from typing import ClassVar, Union @@ -188,7 +188,7 @@ class D: class E(D): x = None # type: ClassVar[Union[A, B, C]] [out] -main:8: error: Incompatible types in assignment (expression has type "Union[A, B, C]", base class "D" defined the type as "Union[A, B]") +main:8: error: Incompatible types in assignment (expression has type "A | B | C", base class "D" defined the type as "A | B") [case testAssignmentToCallableRet] from typing import ClassVar @@ -320,8 +320,8 @@ A[int, str].x # E: Access to generic class variables is ambiguous class Bad(A[int, str]): pass reveal_type(Bad.x) # E: Access to generic class variables is ambiguous \ - # N: Revealed type is "Union[builtins.int, tuple[builtins.str, type[builtins.str]]]" -reveal_type(Bad().x) # N: Revealed type is "Union[builtins.int, tuple[builtins.str, type[builtins.str]]]" + # N: Revealed type is "builtins.int | tuple[builtins.str, type[builtins.str]]" +reveal_type(Bad().x) # N: Revealed type is "builtins.int | tuple[builtins.str, type[builtins.str]]" class Good(A[int, str]): x = 42 @@ -344,7 +344,7 @@ class C: def f(self) -> int: ... g: ClassVar[Union[Callable[[C], int], int]] = f -reveal_type(C().g) # N: Revealed type is "Union[def () -> builtins.int, builtins.int]" +reveal_type(C().g) # N: Revealed type is "def () -> builtins.int | builtins.int" [case testGenericSubclassAccessNoLeak] from typing import ClassVar, Generic, TypeVar diff --git a/test-data/unit/check-ctypes.test b/test-data/unit/check-ctypes.test index 88cb524035ba..da5c53afb479 100644 --- a/test-data/unit/check-ctypes.test +++ b/test-data/unit/check-ctypes.test @@ -15,8 +15,8 @@ a[1] = ctypes.c_int(42) a[2] = MyCInt(42) a[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "bytes" \ # N: Possible overload variants: \ - # N: def __setitem__(self, int, Union[c_int, int], /) -> None \ - # N: def __setitem__(self, slice, list[Union[c_int, int]], /) -> None + # N: def __setitem__(self, int, c_int | int, /) -> None \ + # N: def __setitem__(self, slice, list[c_int | int], /) -> None for x in a: reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/floatdict.pyi] @@ -39,19 +39,19 @@ reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[__main__.MyCInt]" mya[0] = 42 mya[1] = ctypes.c_int(42) # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "c_int" \ # N: Possible overload variants: \ - # N: def __setitem__(self, int, Union[MyCInt, int], /) -> None \ - # N: def __setitem__(self, slice, list[Union[MyCInt, int]], /) -> None + # N: def __setitem__(self, int, MyCInt | int, /) -> None \ + # N: def __setitem__(self, slice, list[MyCInt | int], /) -> None mya[2] = MyCInt(42) mya[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "bytes" \ # N: Possible overload variants: \ - # N: def __setitem__(self, int, Union[MyCInt, int], /) -> None \ - # N: def __setitem__(self, slice, list[Union[MyCInt, int]], /) -> None + # N: def __setitem__(self, int, MyCInt | int, /) -> None \ + # N: def __setitem__(self, slice, list[MyCInt | int], /) -> None for myx in mya: reveal_type(myx) # N: Revealed type is "__main__.MyCInt" myu: Union[ctypes.Array[ctypes.c_int], List[str]] for myi in myu: - reveal_type(myi) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(myi) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/floatdict.pyi] [typing fixtures/typing-medium.pyi] @@ -63,9 +63,9 @@ class MyCInt(ctypes.c_int): pass mya: ctypes.Array[Union[MyCInt, ctypes.c_uint]] -reveal_type(mya) # N: Revealed type is "_ctypes.Array[Union[__main__.MyCInt, ctypes.c_uint]]" -reveal_type(mya[0]) # N: Revealed type is "Union[__main__.MyCInt, builtins.int]" -reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[Union[__main__.MyCInt, builtins.int]]" +reveal_type(mya) # N: Revealed type is "_ctypes.Array[__main__.MyCInt | ctypes.c_uint]" +reveal_type(mya[0]) # N: Revealed type is "__main__.MyCInt | builtins.int" +reveal_type(mya[1:3]) # N: Revealed type is "builtins.list[__main__.MyCInt | builtins.int]" # The behavior here is not strictly correct, but intentional. # See the comment in mypy.plugins.ctypes._autoconvertible_to_cdata for details. mya[0] = 42 @@ -73,10 +73,10 @@ mya[1] = ctypes.c_uint(42) mya[2] = MyCInt(42) mya[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches argument types "int", "bytes" \ # N: Possible overload variants: \ - # N: def __setitem__(self, int, Union[MyCInt, int, c_uint], /) -> None \ - # N: def __setitem__(self, slice, list[Union[MyCInt, int, c_uint]], /) -> None + # N: def __setitem__(self, int, MyCInt | int | c_uint, /) -> None \ + # N: def __setitem__(self, slice, list[MyCInt | int | c_uint], /) -> None for myx in mya: - reveal_type(myx) # N: Revealed type is "Union[__main__.MyCInt, builtins.int]" + reveal_type(myx) # N: Revealed type is "__main__.MyCInt | builtins.int" [builtins fixtures/floatdict.pyi] [typing fixtures/typing-medium.pyi] @@ -111,8 +111,8 @@ import ctypes from typing import Union cua: ctypes.Array[Union[ctypes.c_char, ctypes.c_wchar]] -reveal_type(cua.value) # N: Revealed type is "Union[builtins.bytes, builtins.str]" -cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "Union[c_char, c_wchar]" +reveal_type(cua.value) # N: Revealed type is "builtins.bytes | builtins.str" +cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_char | c_wchar" [builtins fixtures/floatdict.pyi] [typing fixtures/typing-medium.pyi] @@ -121,7 +121,7 @@ import ctypes from typing import Any, Union caa: ctypes.Array[Union[ctypes.c_char, Any]] -reveal_type(caa.value) # N: Revealed type is "Union[builtins.bytes, Any]" +reveal_type(caa.value) # N: Revealed type is "builtins.bytes | Any" reveal_type(caa.raw) # N: Revealed type is "builtins.bytes" [builtins fixtures/floatdict.pyi] [typing fixtures/typing-medium.pyi] @@ -131,8 +131,8 @@ import ctypes from typing import Union cua: ctypes.Array[Union[ctypes.c_char, ctypes.c_int]] -cua.value # E: Array attribute "value" is only available with element type "c_char" or "c_wchar", not "Union[c_char, c_int]" -cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "Union[c_char, c_int]" +cua.value # E: Array attribute "value" is only available with element type "c_char" or "c_wchar", not "c_char | c_int" +cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_char | c_int" [builtins fixtures/floatdict.pyi] [typing fixtures/typing-medium.pyi] diff --git a/test-data/unit/check-custom-plugin.test b/test-data/unit/check-custom-plugin.test index 0c157510cb34..7b1a2c2a91d7 100644 --- a/test-data/unit/check-custom-plugin.test +++ b/test-data/unit/check-custom-plugin.test @@ -356,9 +356,9 @@ class C: z = AnotherAlias(int, required=False) c = C() -reveal_type(c.x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(c.x) # N: Revealed type is "builtins.int | None" reveal_type(c.y) # N: Revealed type is "builtins.int" -reveal_type(c.z) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(c.z) # N: Revealed type is "builtins.int | None" [file mod.py] from typing import Generic, TypeVar, Type diff --git a/test-data/unit/check-dataclass-transform.test b/test-data/unit/check-dataclass-transform.test index 89b8dc88c98f..aed91dc672a6 100644 --- a/test-data/unit/check-dataclass-transform.test +++ b/test-data/unit/check-dataclass-transform.test @@ -1026,11 +1026,11 @@ class C: c = C(x=b'x') c = C(x=None) -C(x=1) # E: Argument "x" to "C" has incompatible type "int"; expected "Optional[bytes]" +C(x=1) # E: Argument "x" to "C" has incompatible type "int"; expected "bytes | None" reveal_type(c.x) # N: Revealed type is "builtins.str" reveal_type(C.x) # N: Revealed type is "builtins.int" c.x = b'x' -c.x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Optional[bytes]") +c.x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "bytes | None") [typing fixtures/typing-full.pyi] [builtins fixtures/dataclasses.pyi] diff --git a/test-data/unit/check-dataclasses.test b/test-data/unit/check-dataclasses.test index c7744e4a82a9..3cc4a03ffb11 100644 --- a/test-data/unit/check-dataclasses.test +++ b/test-data/unit/check-dataclasses.test @@ -296,9 +296,9 @@ class Person: name: str age: int = field(init=None) # E: No overload variant of "field" matches argument type "None" \ # N: Possible overload variants: \ - # N: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T \ - # N: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T \ - # N: def field(*, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> Any + # N: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ + # N: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ + # N: def field(*, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> Any [builtins fixtures/dataclasses.pyi] @@ -329,7 +329,7 @@ class Person: enemy_names: List[str] nickname: Optional[str] = None -reveal_type(Person) # N: Revealed type is "def (name: builtins.str, friend_names: builtins.list[builtins.str], enemy_names: builtins.list[builtins.str], nickname: Union[builtins.str, None] =) -> __main__.Person" +reveal_type(Person) # N: Revealed type is "def (name: builtins.str, friend_names: builtins.list[builtins.str], enemy_names: builtins.list[builtins.str], nickname: builtins.str | None =) -> __main__.Person" [builtins fixtures/dataclasses.pyi] @@ -679,7 +679,7 @@ class Application: class SpecializedApplication(Application): rating: int = 0 -reveal_type(SpecializedApplication) # N: Revealed type is "def (id: Union[builtins.int, None], name: builtins.str, rating: builtins.int =) -> __main__.SpecializedApplication" +reveal_type(SpecializedApplication) # N: Revealed type is "def (id: builtins.int | None, name: builtins.str, rating: builtins.int =) -> __main__.SpecializedApplication" [builtins fixtures/dataclasses.pyi] @@ -1390,9 +1390,9 @@ class Foo: main:6: error: Unpacking **kwargs in "field()" is not supported main:6: error: No overload variant of "field" matches argument type "dict[str, bool]" main:6: note: Possible overload variants: -main:6: note: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T -main:6: note: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T -main:6: note: def field(*, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> Any +main:6: note: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T +main:6: note: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T +main:6: note: def field(*, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> Any [builtins fixtures/dataclasses.pyi] [case testDataclassFieldWithPositionalArguments] @@ -1403,9 +1403,9 @@ class C: x: int = field(0) # E: "field()" does not accept positional arguments \ # E: No overload variant of "field" matches argument type "int" \ # N: Possible overload variants: \ - # N: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T \ - # N: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T \ - # N: def field(*, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> Any + # N: def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ + # N: def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> _T \ + # N: def field(*, init: bool = ..., repr: bool = ..., hash: bool | None = ..., compare: bool = ..., metadata: Mapping[str, Any] | None = ..., kw_only: bool = ...) -> Any [builtins fixtures/dataclasses.pyi] [case testDataclassFieldWithTypedDictUnpacking] @@ -1712,8 +1712,8 @@ class Parent(Generic[T]): @dataclass class Child(Parent): y: Optional[int] -Child(x=1, y=1) # E: Argument "x" to "Child" has incompatible type "int"; expected "Optional[str]" -Child(x='', y='') # E: Argument "y" to "Child" has incompatible type "str"; expected "Optional[int]" +Child(x=1, y=1) # E: Argument "x" to "Child" has incompatible type "int"; expected "str | None" +Child(x='', y='') # E: Argument "y" to "Child" has incompatible type "str"; expected "int | None" Child(x='', y=1) Child(x=None, y=None) [builtins fixtures/dataclasses.pyi] @@ -1959,16 +1959,16 @@ class LinkedList(Generic[T]): next: Optional[Self] = None def meth(self) -> None: - reveal_type(self.next) # N: Revealed type is "Union[Self`0, None]" + reveal_type(self.next) # N: Revealed type is "Self`0 | None" l_int: LinkedList[int] = LinkedList(1, LinkedList("no", None)) # E: Argument 1 to "LinkedList" has incompatible type "str"; expected "int" @dataclass class SubLinkedList(LinkedList[int]): ... -lst = SubLinkedList(1, LinkedList(2)) # E: Argument 2 to "SubLinkedList" has incompatible type "LinkedList[int]"; expected "Optional[SubLinkedList]" -reveal_type(lst.next) # N: Revealed type is "Union[__main__.SubLinkedList, None]" -reveal_type(SubLinkedList) # N: Revealed type is "def (value: builtins.int, next: Union[__main__.SubLinkedList, None] =) -> __main__.SubLinkedList" +lst = SubLinkedList(1, LinkedList(2)) # E: Argument 2 to "SubLinkedList" has incompatible type "LinkedList[int]"; expected "SubLinkedList | None" +reveal_type(lst.next) # N: Revealed type is "__main__.SubLinkedList | None" +reveal_type(SubLinkedList) # N: Revealed type is "def (value: builtins.int, next: __main__.SubLinkedList | None =) -> __main__.SubLinkedList" [builtins fixtures/dataclasses.pyi] [case testNoCrashOnNestedGenericCallable] @@ -2097,10 +2097,10 @@ class B: a_or_b: Union[A[int], B] _ = replace(a_or_b, x=42, y=True, init_var=42) -_ = replace(a_or_b, x=42, y=True) # E: Missing named argument "init_var" for "replace" of "Union[A[int], B]" -_ = replace(a_or_b, x=42, y=True, z='42', init_var=42) # E: Argument "z" to "replace" of "Union[A[int], B]" has incompatible type "str"; expected "Never" -_ = replace(a_or_b, x=42, y=True, w={}, init_var=42) # E: Argument "w" to "replace" of "Union[A[int], B]" has incompatible type "dict[Never, Never]"; expected "Never" -_ = replace(a_or_b, y=42, init_var=42) # E: Argument "y" to "replace" of "Union[A[int], B]" has incompatible type "int"; expected "bool" +_ = replace(a_or_b, x=42, y=True) # E: Missing named argument "init_var" for "replace" of "A[int] | B" +_ = replace(a_or_b, x=42, y=True, z='42', init_var=42) # E: Argument "z" to "replace" of "A[int] | B" has incompatible type "str"; expected "Never" +_ = replace(a_or_b, x=42, y=True, w={}, init_var=42) # E: Argument "w" to "replace" of "A[int] | B" has incompatible type "dict[Never, Never]"; expected "Never" +_ = replace(a_or_b, y=42, init_var=42) # E: Argument "y" to "replace" of "A[int] | B" has incompatible type "int"; expected "bool" [builtins fixtures/tuple.pyi] @@ -2122,7 +2122,7 @@ TA = TypeVar('TA', bound=A) TB = TypeVar('TB', bound=B) def f(b_or_t: Union[TA, TB, int]) -> None: - a2 = replace(b_or_t) # E: Value of type variable "_DataclassT" of "replace" cannot be "Union[TA, TB, int]" + a2 = replace(b_or_t) # E: Value of type variable "_DataclassT" of "replace" cannot be "TA | TB | int" [builtins fixtures/tuple.pyi] @@ -2204,7 +2204,7 @@ from dataclasses import is_dataclass, replace def f(x: object) -> None: _ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object" if is_dataclass(x): - _ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "Union[DataclassInstance, type[DataclassInstance]]" + _ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "DataclassInstance | type[DataclassInstance]" if not isinstance(x, type): _ = replace(x) @@ -2611,11 +2611,11 @@ class Base: @dataclass class Child(Base): - a: int # E: Covariant override of a mutable attribute (base class "Base" defined the type as "Optional[int]", expression has type "int") + a: int # E: Covariant override of a mutable attribute (base class "Base" defined the type as "int | None", expression has type "int") @dataclass class Other(Base): - a: str # E: Incompatible types in assignment (expression has type "str", base class "Base" defined the type as "Optional[int]") + a: str # E: Incompatible types in assignment (expression has type "str", base class "Base" defined the type as "int | None") @dataclass_transform(kw_only_default=True) class DCMeta(type): ... @@ -2624,7 +2624,7 @@ class X(metaclass=DCMeta): a: Optional[int] class Y(X): - a: int # E: Covariant override of a mutable attribute (base class "X" defined the type as "Optional[int]", expression has type "int") + a: int # E: Covariant override of a mutable attribute (base class "X" defined the type as "int | None", expression has type "int") [builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-dynamic-typing.test b/test-data/unit/check-dynamic-typing.test index 166073dd1553..eeaa69affb78 100644 --- a/test-data/unit/check-dynamic-typing.test +++ b/test-data/unit/check-dynamic-typing.test @@ -101,9 +101,9 @@ d in a # E: Unsupported right operand type for in ("A") d and a d or a if int(): - c = d and b # E: Incompatible types in assignment (expression has type "Union[Any, bool]", variable has type "C") + c = d and b # E: Incompatible types in assignment (expression has type "Any | bool", variable has type "C") if int(): - c = d or b # E: Incompatible types in assignment (expression has type "Union[Any, bool]", variable has type "C") + c = d or b # E: Incompatible types in assignment (expression has type "Any | bool", variable has type "C") if int(): c = d + a @@ -165,9 +165,9 @@ a or d if int(): c = a in d # E: Incompatible types in assignment (expression has type "bool", variable has type "C") if int(): - c = b and d # E: Incompatible types in assignment (expression has type "Union[Literal[False], Any]", variable has type "C") + c = b and d # E: Incompatible types in assignment (expression has type "Literal[False] | Any", variable has type "C") if int(): - c = b or d # E: Incompatible types in assignment (expression has type "Union[Literal[True], Any]", variable has type "C") + c = b or d # E: Incompatible types in assignment (expression has type "Literal[True] | Any", variable has type "C") if int(): b = a + d if int(): diff --git a/test-data/unit/check-enum.test b/test-data/unit/check-enum.test index 3bcf9745a801..b33f583ad9bc 100644 --- a/test-data/unit/check-enum.test +++ b/test-data/unit/check-enum.test @@ -101,7 +101,7 @@ class Truth(Enum): false = False def infer_truth(truth: Truth) -> None: - reveal_type(truth.value) # N: Revealed type is "Union[Literal[True]?, Literal[False]?]" + reveal_type(truth.value) # N: Revealed type is "Literal[True]? | Literal[False]?" [builtins fixtures/bool.pyi] [case testEnumValueAllAuto] @@ -134,7 +134,7 @@ class Truth(Enum): return 'bar' def infer_truth(truth: Truth) -> None: - reveal_type(truth.value) # N: Revealed type is "Union[Literal[True]?, Literal[False]?]" + reveal_type(truth.value) # N: Revealed type is "Literal[True]? | Literal[False]?" [builtins fixtures/bool.pyi] [case testEnumValueCustomAuto] @@ -882,7 +882,7 @@ elif x is Foo.C: reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.C]" else: reveal_type(x) # No output here: this branch is unreachable -reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" +reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" if Foo.A is x: reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A]" @@ -892,7 +892,7 @@ elif Foo.C is x: reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.C]" else: reveal_type(x) # No output here: this branch is unreachable -reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" +reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" y: Foo if y is Foo.A: @@ -986,14 +986,14 @@ if x is y: reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A]" reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]" else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" + reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]" reveal_type(x) # N: Revealed type is "__main__.Foo" if y is x: reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A]" reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]" else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" + reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]" reveal_type(x) # N: Revealed type is "__main__.Foo" @@ -1002,7 +1002,7 @@ if x is z: reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?" accepts_foo_a(z) else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" + reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?" accepts_foo_a(z) reveal_type(x) # N: Revealed type is "__main__.Foo" @@ -1011,7 +1011,7 @@ if z is x: reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?" accepts_foo_a(z) else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" + reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?" accepts_foo_a(z) reveal_type(x) # N: Revealed type is "__main__.Foo" @@ -1048,17 +1048,17 @@ z: Literal[Foo.B, Foo.C] # For the sake of simplicity, no narrowing is done when the narrower type is a Union. if x is y: reveal_type(x) # N: Revealed type is "__main__.Foo" - reveal_type(y) # N: Revealed type is "Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]" + reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]" else: reveal_type(x) # N: Revealed type is "__main__.Foo" - reveal_type(y) # N: Revealed type is "Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]" + reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]" if y is z: - reveal_type(y) # N: Revealed type is "Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]" - reveal_type(z) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" + reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]" + reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" else: - reveal_type(y) # N: Revealed type is "Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]" - reveal_type(z) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" + reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]" + reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" [builtins fixtures/bool.pyi] [case testEnumReachabilityWithNone] @@ -1084,8 +1084,8 @@ else: if x is Foo.A: reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A]" else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C], None]" -reveal_type(x) # N: Revealed type is "Union[__main__.Foo, None]" + reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C] | None" +reveal_type(x) # N: Revealed type is "__main__.Foo | None" [builtins fixtures/enum.pyi] [case testEnumReachabilityWithMultipleEnums] @@ -1103,22 +1103,22 @@ x1: Union[Foo, Bar] if x1 is Foo.A: reveal_type(x1) # N: Revealed type is "Literal[__main__.Foo.A]" else: - reveal_type(x1) # N: Revealed type is "Union[Literal[__main__.Foo.B], __main__.Bar]" -reveal_type(x1) # N: Revealed type is "Union[__main__.Foo, __main__.Bar]" + reveal_type(x1) # N: Revealed type is "Literal[__main__.Foo.B] | __main__.Bar" +reveal_type(x1) # N: Revealed type is "__main__.Foo | __main__.Bar" x2: Union[Foo, Bar] if x2 is Bar.A: reveal_type(x2) # N: Revealed type is "Literal[__main__.Bar.A]" else: - reveal_type(x2) # N: Revealed type is "Union[__main__.Foo, Literal[__main__.Bar.B]]" -reveal_type(x2) # N: Revealed type is "Union[__main__.Foo, __main__.Bar]" + reveal_type(x2) # N: Revealed type is "__main__.Foo | Literal[__main__.Bar.B]" +reveal_type(x2) # N: Revealed type is "__main__.Foo | __main__.Bar" x3: Union[Foo, Bar] if x3 is Foo.A or x3 is Bar.A: - reveal_type(x3) # N: Revealed type is "Union[Literal[__main__.Foo.A], Literal[__main__.Bar.A]]" + reveal_type(x3) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Bar.A]" else: - reveal_type(x3) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Bar.B]]" -reveal_type(x3) # N: Revealed type is "Union[__main__.Foo, __main__.Bar]" + reveal_type(x3) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Bar.B]" +reveal_type(x3) # N: Revealed type is "__main__.Foo | __main__.Bar" [builtins fixtures/bool.pyi] @@ -1133,7 +1133,7 @@ _empty: Final = Empty.token def func(x: Union[int, None, Empty] = _empty) -> int: boom = x + 42 # E: Unsupported left operand type for + ("None") \ # E: Unsupported left operand type for + ("Empty") \ - # N: Left operand is of type "Union[int, Empty, None]" + # N: Left operand is of type "int | Empty | None" if x is _empty: reveal_type(x) # N: Revealed type is "Literal[__main__.Empty.token]" return 0 @@ -1178,7 +1178,7 @@ _empty = Empty.token def func(x: Union[int, None, Empty] = _empty) -> int: boom = x + 42 # E: Unsupported left operand type for + ("None") \ # E: Unsupported left operand type for + ("Empty") \ - # N: Left operand is of type "Union[int, Empty, None]" + # N: Left operand is of type "int | Empty | None" if x is _empty: reveal_type(x) # N: Revealed type is "Literal[__main__.Empty.token]" return 0 @@ -1209,7 +1209,7 @@ reveal_type(Empty.f) # N: Revealed type is "def (self: __main__.Empty) -> builti def func(x: Union[int, None, Empty] = _empty) -> int: boom = x + 42 # E: Unsupported left operand type for + ("None") \ # E: Unsupported left operand type for + ("Empty") \ - # N: Left operand is of type "Union[int, Empty, None]" + # N: Left operand is of type "int | Empty | None" if x is _empty: reveal_type(x) # N: Revealed type is "Literal[__main__.Empty.token]" return 0 @@ -1431,8 +1431,8 @@ class Foo(Enum): B = 2 a = Foo.A -reveal_type(a.value) # N: Revealed type is "Union[Literal[1]?, Literal[2]?]" -reveal_type(a._value_) # N: Revealed type is "Union[Literal[1]?, Literal[2]?]" +reveal_type(a.value) # N: Revealed type is "Literal[1]? | Literal[2]?" +reveal_type(a._value_) # N: Revealed type is "Literal[1]? | Literal[2]?" [builtins fixtures/enum.pyi] [case testNewSetsUnexpectedValueType] @@ -1494,7 +1494,7 @@ def f(x: Foo): return x if x is Foo.B: pass - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]" + reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]" [builtins fixtures/bool.pyi] @@ -2369,7 +2369,7 @@ reveal_type(My.b) # N: Revealed type is "Literal[__main__.My.b]?" reveal_type(My.c) # N: Revealed type is "builtins.int" def accepts_my(my: My): - reveal_type(my.value) # N: Revealed type is "Union[Literal[1]?, Literal[2]?]" + reveal_type(my.value) # N: Revealed type is "Literal[1]? | Literal[2]?" class Other(Enum): a = 1 @@ -2397,7 +2397,7 @@ reveal_type(A.y) # N: Revealed type is "Literal[__main__.A.y]?" reveal_type(A.y.value) # N: Revealed type is "Literal[2]?" def some_a(a: A): - reveal_type(a.value) # N: Revealed type is "Union[Literal[1]?, Literal[2]?]" + reveal_type(a.value) # N: Revealed type is "Literal[1]? | Literal[2]?" [builtins fixtures/dict.pyi] diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index b1009f8fe085..31b9c526b6e5 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -371,7 +371,7 @@ class A: class B: y: str a: Union[A, B] -a.x # E: Item "B" of "Union[A, B]" has no attribute "x" [union-attr] +a.x # E: Item "B" of "A | B" has no attribute "x" [union-attr] [case testErrorCodeFunctionHasNoAnnotation] # flags: --disallow-untyped-defs @@ -623,7 +623,7 @@ B() # E: Cannot instantiate abstract class "B" with abstract attribute "f" [ab [case testErrorCodeNewTypeNotSubclassable] from typing import Union, NewType -X = NewType('X', Union[int, str]) # E: Argument 2 to NewType(...) must be subclassable (got "Union[int, str]") [valid-newtype] +X = NewType('X', Union[int, str]) # E: Argument 2 to NewType(...) must be subclassable (got "int | str") [valid-newtype] [case testErrorCodeOverloadVariant] from typing import overload @@ -682,7 +682,7 @@ def g() -> int: '{!x}'.format('Hm...') # E: Invalid conversion type "x", must be one of "r", "s" or "a" [str-format] '}{'.format() # E: Invalid conversion specifier in format string: unexpected } [str-format] -'%d' % 'no' # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]") [str-format] +'%d' % 'no' # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsInt") [str-format] '%d + %d' % (1, 2, 3) # E: Not all arguments converted during string formatting [str-format] '{}'.format(b'abc') # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes [str-bytes-safe] @@ -927,12 +927,12 @@ if not good_union: not good_union bad_union: Union[Foo, Bar] = Foo() -if bad_union: # E: "__main__.bad_union" has type "Union[Foo, Bar]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] +if bad_union: # E: "__main__.bad_union" has type "Foo | Bar" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] pass -if not bad_union: # E: "__main__.bad_union" has type "Union[Foo, Bar]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] +if not bad_union: # E: "__main__.bad_union" has type "Foo | Bar" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] pass -not bad_union # E: "__main__.bad_union" has type "Union[Foo, Bar]" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] +not bad_union # E: "__main__.bad_union" has type "Foo | Bar" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] # 'object' is special and is treated as potentially falsy obj: object = Foo() diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index fd7c29b5febb..8a969fc54428 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -316,13 +316,13 @@ if int(): if int(): b = b or b if int(): - b = b and a # E: Incompatible types in assignment (expression has type "Union[Literal[False], A]", variable has type "bool") + b = b and a # E: Incompatible types in assignment (expression has type "Literal[False] | A", variable has type "bool") if int(): - b = a and b # E: Incompatible types in assignment (expression has type "Union[A, bool]", variable has type "bool") + b = a and b # E: Incompatible types in assignment (expression has type "A | bool", variable has type "bool") if int(): - b = b or a # E: Incompatible types in assignment (expression has type "Union[Literal[True], A]", variable has type "bool") + b = b or a # E: Incompatible types in assignment (expression has type "Literal[True] | A", variable has type "bool") if int(): - b = a or b # E: Incompatible types in assignment (expression has type "Union[A, bool]", variable has type "bool") + b = a or b # E: Incompatible types in assignment (expression has type "A | bool", variable has type "bool") class A: pass [builtins fixtures/bool.pyi] @@ -1478,9 +1478,9 @@ if int(): [case testConditionalExpressionUnion] from typing import Union -reveal_type(1 if bool() else 2) # N: Revealed type is "Union[Literal[1]?, Literal[2]?]" -reveal_type(1 if bool() else '') # N: Revealed type is "Union[Literal[1]?, Literal['']?]" -x: Union[int, str] = reveal_type(1 if bool() else '') # N: Revealed type is "Union[Literal[1]?, Literal['']?]" +reveal_type(1 if bool() else 2) # N: Revealed type is "Literal[1]? | Literal[2]?" +reveal_type(1 if bool() else '') # N: Revealed type is "Literal[1]? | Literal['']?" +x: Union[int, str] = reveal_type(1 if bool() else '') # N: Revealed type is "Literal[1]? | Literal['']?" class A: pass class B(A): @@ -1494,17 +1494,17 @@ b = B() c = C() d = D() reveal_type(a if bool() else b) # N: Revealed type is "__main__.A" -reveal_type(b if bool() else c) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(c if bool() else b) # N: Revealed type is "Union[__main__.C, __main__.B]" -reveal_type(c if bool() else a) # N: Revealed type is "Union[__main__.C, __main__.A]" -reveal_type(d if bool() else b) # N: Revealed type is "Union[__main__.D, __main__.B]" +reveal_type(b if bool() else c) # N: Revealed type is "__main__.B | __main__.C" +reveal_type(c if bool() else b) # N: Revealed type is "__main__.C | __main__.B" +reveal_type(c if bool() else a) # N: Revealed type is "__main__.C | __main__.A" +reveal_type(d if bool() else b) # N: Revealed type is "__main__.D | __main__.B" [builtins fixtures/bool.pyi] [case testConditionalExpressionUnionWithAny] from typing import Union, Any a: Any -x: Union[int, str] = reveal_type(a if int() else 1) # N: Revealed type is "Union[Any, Literal[1]?]" -reveal_type(a if int() else 1) # N: Revealed type is "Union[Any, Literal[1]?]" +x: Union[int, str] = reveal_type(a if int() else 1) # N: Revealed type is "Any | Literal[1]?" +reveal_type(a if int() else 1) # N: Revealed type is "Any | Literal[1]?" [case testConditionalExpressionStatementNoReturn] from typing import List, Union @@ -1654,7 +1654,7 @@ from typing import Generator def g() -> Generator[int, None, None]: x = yield from () # E: Function does not return a value (it only ever returns None) x = yield from (0, 1, 2) # E: Function does not return a value (it only ever returns None) - x = yield from (0, "ERROR") # E: Incompatible types in "yield from" (actual type "Union[int, str]", expected type "int") \ + x = yield from (0, "ERROR") # E: Incompatible types in "yield from" (actual type "int | str", expected type "int") \ # E: Function does not return a value (it only ever returns None) x = yield from ("ERROR",) # E: Incompatible types in "yield from" (actual type "str", expected type "int") \ # E: Function does not return a value (it only ever returns None) @@ -2008,16 +2008,16 @@ a: Union[int, str] b: Union[A, B] a == int() -b == int() # E: Non-overlapping equality check (left operand type: "Union[A, B]", right operand type: "int") +b == int() # E: Non-overlapping equality check (left operand type: "A | B", right operand type: "int") a is int() -b is int() # E: Non-overlapping identity check (left operand type: "Union[A, B]", right operand type: "int") +b is int() # E: Non-overlapping identity check (left operand type: "A | B", right operand type: "int") ca: Union[Container[int], Container[str]] cb: Union[Container[A], Container[B]] 42 in ca -42 in cb # E: Non-overlapping container check (element type: "int", container item type: "Union[A, B]") +42 in cb # E: Non-overlapping container check (element type: "int", container item type: "A | B") [builtins fixtures/bool.pyi] [typing fixtures/typing-full.pyi] @@ -2099,7 +2099,7 @@ from typing import Optional x: Optional[str] y: Optional[int] -if x == y: # E: Non-overlapping equality check (left operand type: "Optional[str]", right operand type: "Optional[int]") +if x == y: # E: Non-overlapping equality check (left operand type: "str | None", right operand type: "int | None") ... [builtins fixtures/bool.pyi] diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test index e7417d049442..f1c0716e9fb8 100644 --- a/test-data/unit/check-fastparse.test +++ b/test-data/unit/check-fastparse.test @@ -121,9 +121,9 @@ def f(a, # type: A **kwargs # type: F ): reveal_type(a) # N: Revealed type is "__main__.A" - reveal_type(b) # N: Revealed type is "Union[__main__.B, None]" + reveal_type(b) # N: Revealed type is "__main__.B | None" reveal_type(args) # N: Revealed type is "builtins.tuple[__main__.C, ...]" - reveal_type(d) # N: Revealed type is "Union[__main__.D, None]" + reveal_type(d) # N: Revealed type is "__main__.D | None" reveal_type(e) # N: Revealed type is "__main__.E" reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]" [builtins fixtures/dict.pyi] @@ -147,9 +147,9 @@ def f(a, # type: A ): # type: (...) -> int reveal_type(a) # N: Revealed type is "__main__.A" - reveal_type(b) # N: Revealed type is "Union[__main__.B, None]" + reveal_type(b) # N: Revealed type is "__main__.B | None" reveal_type(args) # N: Revealed type is "builtins.tuple[__main__.C, ...]" - reveal_type(d) # N: Revealed type is "Union[__main__.D, None]" + reveal_type(d) # N: Revealed type is "__main__.D | None" reveal_type(e) # N: Revealed type is "__main__.E" reveal_type(kwargs) # N: Revealed type is "builtins.dict[builtins.str, __main__.F]" return "not an int" # E: Incompatible return value type (got "str", expected "int") diff --git a/test-data/unit/check-final.test b/test-data/unit/check-final.test index e3fc4614fc06..5d2a5cf25289 100644 --- a/test-data/unit/check-final.test +++ b/test-data/unit/check-final.test @@ -1200,7 +1200,7 @@ class B: class C: def __len__(self) -> Literal[1]: return 1 -reveal_type(A() and 42) # N: Revealed type is "Union[__main__.A, Literal[42]?]" +reveal_type(A() and 42) # N: Revealed type is "__main__.A | Literal[42]?" reveal_type(B() and 42) # N: Revealed type is "Literal[42]?" reveal_type(C() and 42) # N: Revealed type is "Literal[42]?" @@ -1224,7 +1224,7 @@ class B: class C: def __len__(self) -> Literal[0]: return 0 -reveal_type(A() and 42) # N: Revealed type is "Union[__main__.A, Literal[42]?]" +reveal_type(A() and 42) # N: Revealed type is "__main__.A | Literal[42]?" reveal_type(B() and 42) # N: Revealed type is "Literal[42]?" reveal_type(C() and 42) # N: Revealed type is "__main__.C" diff --git a/test-data/unit/check-formatting.test b/test-data/unit/check-formatting.test index b5b37f8d2976..b2593014c3ee 100644 --- a/test-data/unit/check-formatting.test +++ b/test-data/unit/check-formatting.test @@ -12,10 +12,10 @@ t: Tuple[int] '%f' % f '%s' % s '%d' % (f,) -'%d' % (s,) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]") +'%d' % (s,) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsInt") '%d' % t -'%d' % s # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]") -'%f' % s # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsFloat]") +'%d' % s # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsInt") +'%f' % s # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsFloat") '%x' % f # E: Incompatible types in string interpolation (expression has type "float", placeholder has type "int") '%i' % f '%o' % f # E: Incompatible types in string interpolation (expression has type "float", placeholder has type "int") @@ -45,7 +45,7 @@ xs: str '%d %d' % (1, 2, 3) # E: Not all arguments converted during string formatting t = 1, 's' '%d %s' % t -'%s %d' % t # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]") +'%s %d' % t # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsInt") '%d' % t # E: Not all arguments converted during string formatting [builtins fixtures/primitives.pyi] [typing fixtures/typing-medium.pyi] @@ -115,7 +115,7 @@ a = None # type: Any [case testStringInterpolationMappingTypes] '%(a)d %(b)s' % {'a': 1, 'b': 's'} -'%(a)d %(b)s' % {'a': 's', 'b': 1} # E: Incompatible types in string interpolation (expression has type "str", placeholder with key 'a' has type "Union[int, float, SupportsInt]") +'%(a)d %(b)s' % {'a': 's', 'b': 1} # E: Incompatible types in string interpolation (expression has type "str", placeholder with key 'a' has type "int | float | SupportsInt") b'%(x)s' % {b'x': b'data'} [builtins fixtures/primitives.pyi] [typing fixtures/typing-medium.pyi] @@ -176,8 +176,8 @@ b'%()s' % BytesThing() [builtins fixtures/primitives.pyi] [typing fixtures/typing-medium.pyi] [out] -main:3: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsFloat]") -main:4: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsFloat]") +main:3: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsFloat") +main:4: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsFloat") [case testStringInterpolationSpaceKey] '%( )s' % {' ': 'foo'} @@ -370,7 +370,7 @@ kwargs: Dict[str, str] = {} '{one}, {two}'.format(one=1, two=2, **kwargs) # Don't flag this because args may be empty '{stuff:.3d}'.format(**kwargs) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int") -'{stuff[0]:f}, {other}'.format(**kwargs) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]") +'{stuff[0]:f}, {other}'.format(**kwargs) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float") '{stuff[0]:c}'.format(**kwargs) [builtins fixtures/primitives.pyi] @@ -395,8 +395,8 @@ x: Union[Good, Bad] '{:E}'.format(42) '{:g}'.format(42) '{:x}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int") -'{:E}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]") -'{:g}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]") +'{:E}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float") +'{:g}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float") '{:n}'.format(3.14) '{:d}'.format(3.14) # E: Incompatible types in string interpolation (expression has type "float", placeholder has type "int") @@ -414,7 +414,7 @@ x: Union[Good, Bad] class C: ... -'{:c}'.format(C()) # E: Incompatible types in string interpolation (expression has type "C", placeholder has type "Union[int, str]") +'{:c}'.format(C()) # E: Incompatible types in string interpolation (expression has type "C", placeholder has type "int | str") x: str '{:c}'.format(x) [builtins fixtures/primitives.pyi] @@ -549,7 +549,7 @@ class User(TypedDict): name: str u: User -'{user[name]:.3f}'.format(user=u) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]") +'{user[name]:.3f}'.format(user=u) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float") def f() -> str: ... '{[f()]}'.format(u) # E: Invalid index expression in format field accessor "[f()]" diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index 4bdb7e1f8173..f131420cdf38 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -501,7 +501,7 @@ a, b = None, None # type: (A, B) if int(): a = f() # E: Incompatible types in assignment (expression has type "B", variable has type "A") if int(): - b = f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "Optional[A]" + b = f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "A | None" if int(): b = f(a, a) # E: Too many arguments for "f" @@ -1453,7 +1453,7 @@ else: # N: Original: \ # N: def [T1] f(x: T1) -> T1 \ # N: Redefinition: \ - # N: def [T2: Union[int, str]] f(x: T2) -> T2 + # N: def [T2: int | str] f(x: T2) -> T2 [case testConditionalFunctionDefinitionUsingDecorator1] from typing import Callable @@ -2293,7 +2293,7 @@ def f() -> None: def g(x: int) -> None: pass h = f if bool() else g -reveal_type(h) # N: Revealed type is "Union[def (), def (x: builtins.int)]" +reveal_type(h) # N: Revealed type is "def () | def (x: builtins.int)" h(7) # E: Too many arguments for "f" T = TypeVar("T") @@ -2476,26 +2476,26 @@ def fn( from typing import Union, Dict, List def f() -> List[Union[str, int]]: x = ['a'] - return x # E: Incompatible return value type (got "list[str]", expected "list[Union[str, int]]") \ + return x # E: Incompatible return value type (got "list[str]", expected "list[str | int]") \ # N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ # N: Consider using "Sequence" instead, which is covariant \ - # N: Perhaps you need a type annotation for "x"? Suggestion: "list[Union[str, int]]" + # N: Perhaps you need a type annotation for "x"? Suggestion: "list[str | int]" def g() -> Dict[str, Union[str, int]]: x = {'a': 'a'} - return x # E: Incompatible return value type (got "dict[str, str]", expected "dict[str, Union[str, int]]") \ + return x # E: Incompatible return value type (got "dict[str, str]", expected "dict[str, str | int]") \ # N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ # N: Consider using "Mapping" instead, which is covariant in the value type \ - # N: Perhaps you need a type annotation for "x"? Suggestion: "dict[str, Union[str, int]]" + # N: Perhaps you need a type annotation for "x"? Suggestion: "dict[str, str | int]" def h() -> Dict[Union[str, int], str]: x = {'a': 'a'} - return x # E: Incompatible return value type (got "dict[str, str]", expected "dict[Union[str, int], str]") \ -# N: Perhaps you need a type annotation for "x"? Suggestion: "dict[Union[str, int], str]" + return x # E: Incompatible return value type (got "dict[str, str]", expected "dict[str | int, str]") \ +# N: Perhaps you need a type annotation for "x"? Suggestion: "dict[str | int, str]" def i() -> List[Union[int, float]]: x: List[int] = [1] - return x # E: Incompatible return value type (got "list[int]", expected "list[Union[int, float]]") \ + return x # E: Incompatible return value type (got "list[int]", expected "list[int | float]") \ # N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ # N: Consider using "Sequence" instead, which is covariant @@ -2505,11 +2505,11 @@ def i() -> List[Union[int, float]]: from typing import Union, List def f() -> List[Union[int, float]]: x = ['a'] - return x # E: Incompatible return value type (got "list[str]", expected "list[Union[int, float]]") + return x # E: Incompatible return value type (got "list[str]", expected "list[int | float]") def g() -> List[Union[str, int]]: x = ('a', 2) - return x # E: Incompatible return value type (got "tuple[str, int]", expected "list[Union[str, int]]") + return x # E: Incompatible return value type (got "tuple[str, int]", expected "list[str | int]") [builtins fixtures/list.pyi] @@ -2517,7 +2517,7 @@ def g() -> List[Union[str, int]]: from typing import Union, Dict, List def f() -> Dict[str, Union[str, int]]: x = {'a': 'a', 'b': 2} - return x # E: Incompatible return value type (got "dict[str, object]", expected "dict[str, Union[str, int]]") + return x # E: Incompatible return value type (got "dict[str, object]", expected "dict[str, str | int]") def g() -> Dict[str, Union[str, int]]: x: Dict[str, Union[str, int]] = {'a': 'a', 'b': 2} @@ -2525,7 +2525,7 @@ def g() -> Dict[str, Union[str, int]]: def h() -> List[Union[str, int]]: x = ['a', 2] - return x # E: Incompatible return value type (got "list[object]", expected "list[Union[str, int]]") + return x # E: Incompatible return value type (got "list[object]", expected "list[str | int]") def i() -> List[Union[str, int]]: x: List[Union[str, int]] = ['a', 2] @@ -3366,7 +3366,7 @@ def f(x: T, y: S) -> Union[T, S]: ... def g(x: T, y: S) -> Union[T, S]: ... x = [f, g] -reveal_type(x) # N: Revealed type is "builtins.list[def [T, S] (x: T`4, y: S`5) -> Union[T`4, S`5]]" +reveal_type(x) # N: Revealed type is "builtins.list[def [T, S] (x: T`4, y: S`5) -> T`4 | S`5]" [builtins fixtures/list.pyi] [case testTypeVariableClashErrorMessage] @@ -3397,7 +3397,7 @@ class Foo: class Bar(Foo): @property - def method(self) -> Callable[[int, A], None]: # E: Argument 2 of "method" is incompatible with supertype "Foo"; supertype defines the argument type as "Union[Callable[[C], None], Callable[[D], None]]" \ + def method(self) -> Callable[[int, A], None]: # E: Argument 2 of "method" is incompatible with supertype "Foo"; supertype defines the argument type as "Callable[[C], None] | Callable[[D], None]" \ # N: This violates the Liskov substitution principle \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides ... @@ -3458,7 +3458,7 @@ for factory in ( lambda: var, func, ): - reveal_type(factory) # N: Revealed type is "def () -> Union[builtins.str, None]" + reveal_type(factory) # N: Revealed type is "def () -> builtins.str | None" var = factory() [builtins fixtures/tuple.pyi] @@ -3544,7 +3544,7 @@ from mypy_extensions import Arg def f(x: Callable[[Arg(int, 'x')], None]) -> None: pass y: Callable[[Union[int, str]], None] -f(y) # E: Argument 1 to "f" has incompatible type "Callable[[Union[int, str]], None]"; expected "def (x: int) -> None" +f(y) # E: Argument 1 to "f" has incompatible type "Callable[[int | str], None]"; expected "def (x: int) -> None" [builtins fixtures/tuple.pyi] [case testAbstractOverloadsWithoutImplementationAllowed] diff --git a/test-data/unit/check-functools.test b/test-data/unit/check-functools.test index 650928b1a5ed..ffd0a97b6988 100644 --- a/test-data/unit/check-functools.test +++ b/test-data/unit/check-functools.test @@ -348,12 +348,12 @@ fn1: Union[Callable[[int], int], Callable[[int], int]] reveal_type(functools.partial(fn1, 2)()) # N: Revealed type is "builtins.int" fn2: Union[Callable[[int], int], Callable[[int], str]] -reveal_type(functools.partial(fn2, 2)()) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(functools.partial(fn2, 2)()) # N: Revealed type is "builtins.int | builtins.str" fn3: Union[Callable[[int], int], str] reveal_type(functools.partial(fn3, 2)()) # E: "str" not callable \ # N: Revealed type is "builtins.int" \ - # E: Argument 1 to "partial" has incompatible type "Union[Callable[[int], int], str]"; expected "Callable[..., int]" + # E: Argument 1 to "partial" has incompatible type "Callable[[int], int] | str"; expected "Callable[..., int]" [builtins fixtures/tuple.pyi] [case testFunctoolsPartialUnionOfTypeAndCallable] diff --git a/test-data/unit/check-generic-subtyping.test b/test-data/unit/check-generic-subtyping.test index ee5bcccf8ace..3dd117826493 100644 --- a/test-data/unit/check-generic-subtyping.test +++ b/test-data/unit/check-generic-subtyping.test @@ -863,12 +863,12 @@ def f(x: T) -> None: del x.c # E [out] -main:13: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a" -main:13: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a" -main:14: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b" -main:14: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b" -main:15: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c" -main:15: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c" +main:13: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "a" +main:13: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "a" +main:14: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "b" +main:14: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "b" +main:15: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "c" +main:15: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "c" [case testTypeVarBoundToNewUnionAttributeAccess] @@ -891,12 +891,12 @@ def f(x: T) -> None: [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" +main:14: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "a" +main:14: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "a" +main:15: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "b" +main:15: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "b" +main:16: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "c" +main:16: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "c" [case testSubtypingIterableUnpacking1] diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 3b535ab4a1c0..32975350e20a 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -658,7 +658,7 @@ reveal_type(f2('a')) # N: Revealed type is "__main__.Node[builtins.list[builtins def f3() -> Third: return Node([1], ['x']) -reveal_type(f3()) # N: Revealed type is "Union[builtins.int, __main__.Node[builtins.list[builtins.int], builtins.list[builtins.str]]]" +reveal_type(f3()) # N: Revealed type is "builtins.int | __main__.Node[builtins.list[builtins.int], builtins.list[builtins.str]]" [builtins fixtures/list.pyi] @@ -793,7 +793,7 @@ UNode = Union[int, Node[T]] x = 1 # type: UNode[int] x + 1 # E: Unsupported left operand type for + ("Node[int]") \ - # N: Left operand is of type "Union[int, Node[int]]" + # N: Left operand is of type "int | Node[int]" if not isinstance(x, Node): x + 1 @@ -807,17 +807,17 @@ def f(x: T) -> UNode[T]: else: return 1 -reveal_type(f(1)) # N: Revealed type is "Union[builtins.int, __main__.Node[builtins.int]]" +reveal_type(f(1)) # N: Revealed type is "builtins.int | __main__.Node[builtins.int]" TNode = Union[T, Node[int]] -s = 1 # type: TNode[str] # E: Incompatible types in assignment (expression has type "int", variable has type "Union[str, Node[int]]") +s = 1 # type: TNode[str] # E: Incompatible types in assignment (expression has type "int", variable has type "str | Node[int]") if not isinstance(s, str): s.x = 1 z = None # type: TNode # Same as TNode[Any] z.x -z.foo() # E: Item "Node[int]" of "Union[Any, Node[int]]" has no attribute "foo" +z.foo() # E: Item "Node[int]" of "Any | Node[int]" has no attribute "foo" [builtins fixtures/isinstance.pyi] @@ -999,7 +999,7 @@ U = Union[int] x: O y: U -reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(x) # N: Revealed type is "builtins.int | None" reveal_type(y) # N: Revealed type is "builtins.int" U[int] # E: Type application targets a non-generic function or class @@ -1028,7 +1028,7 @@ class C: def h(self, x: c) -> None: pass # E: Variable "__main__.C.c" is not valid as a type \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases x: b - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [out] [case testGenericTypeAliasesRuntimeExpressionsInstance] @@ -2985,7 +2985,7 @@ def lift(f: F[T]) -> F[Optional[T]]: ... def g(x: T) -> T: return x -reveal_type(lift(g)) # N: Revealed type is "def [T] (Union[T`1, None]) -> Union[T`1, None]" +reveal_type(lift(g)) # N: Revealed type is "def [T] (T`1 | None) -> T`1 | None" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericSplitOrder] @@ -3576,9 +3576,9 @@ def draw( colors3: A[str] | B[str] | C[int] | D[int | str], ) -> None: for c1, c2, c3 in zip2(colors1, colors2, colors3): - reveal_type(c1) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(c2) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(c3) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(c1) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(c2) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(c3) # N: Revealed type is "builtins.int | builtins.str" def takes_int_str_none(x: int | str | None) -> None: ... diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index ad1168ce83dc..46cf828ef52c 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -1259,8 +1259,8 @@ reveal_type(x) y: Alias[int] reveal_type(y) [out2] -tmp/a.py:3: note: Revealed type is "Union[builtins.int, builtins.str]" -tmp/a.py:5: note: Revealed type is "Union[builtins.int, builtins.int]" +tmp/a.py:3: note: Revealed type is "builtins.int | builtins.str" +tmp/a.py:5: note: Revealed type is "builtins.int | builtins.int" [case testIncrementalSilentImportsWithBlatantError] # cmd: mypy -m main @@ -2936,7 +2936,7 @@ def foo() -> Optional[int]: return 0 [out1] [out2] main:2: error: Unsupported operand types for + ("int" and "None") -main:2: note: Right operand is of type "Optional[int]" +main:2: note: Right operand is of type "int | None" [case testAttrsIncrementalSubclassingCached] from a import A @@ -3214,9 +3214,9 @@ class A: [builtins fixtures/list.pyi] [out1] -main:2: note: Revealed type is "def (x: Union[builtins.int, None]) -> a.a.A" +main:2: note: Revealed type is "def (x: builtins.int | None) -> a.a.A" [out2] -main:2: note: Revealed type is "def (x: Union[builtins.int, None]) -> a.a.A" +main:2: note: Revealed type is "def (x: builtins.int | None) -> a.a.A" [case testAttrsIncrementalConverterManyStyles] import a @@ -3354,14 +3354,14 @@ class SubBB(SubBase): [builtins fixtures/list.pyi] [out1] [out2] -tmp/a.py:3: error: Argument 2 to "Base" has incompatible type "int"; expected "Optional[str]" -tmp/a.py:6: error: Argument 2 to "A" has incompatible type "int"; expected "Optional[str]" -tmp/a.py:7: error: Argument 5 to "B" has incompatible type "int"; expected "Optional[str]" -tmp/a.py:10: error: Argument 2 to "SubBase" has incompatible type "int"; expected "Optional[str]" -tmp/a.py:13: error: Argument 2 to "AA" has incompatible type "int"; expected "Optional[str]" -tmp/a.py:14: error: Argument 5 to "BB" has incompatible type "int"; expected "Optional[str]" -tmp/a.py:17: error: Argument 2 to "SubAA" has incompatible type "int"; expected "Optional[str]" -tmp/a.py:18: error: Argument 5 to "SubBB" has incompatible type "int"; expected "Optional[str]" +tmp/a.py:3: error: Argument 2 to "Base" has incompatible type "int"; expected "str | None" +tmp/a.py:6: error: Argument 2 to "A" has incompatible type "int"; expected "str | None" +tmp/a.py:7: error: Argument 5 to "B" has incompatible type "int"; expected "str | None" +tmp/a.py:10: error: Argument 2 to "SubBase" has incompatible type "int"; expected "str | None" +tmp/a.py:13: error: Argument 2 to "AA" has incompatible type "int"; expected "str | None" +tmp/a.py:14: error: Argument 5 to "BB" has incompatible type "int"; expected "str | None" +tmp/a.py:17: error: Argument 2 to "SubAA" has incompatible type "int"; expected "str | None" +tmp/a.py:18: error: Argument 5 to "SubBB" has incompatible type "int"; expected "str | None" [case testAttrsIncrementalConverterInFunction] import attrs @@ -3502,7 +3502,7 @@ class Baz: [out] [out2] tmp/a.py:3: error: Unsupported operand types for + ("int" and "None") -tmp/a.py:3: note: Right operand is of type "Optional[int]" +tmp/a.py:3: note: Right operand is of type "int | None" [case testIncrementalMetaclassUpdate] import a @@ -5503,7 +5503,7 @@ reveal_type(y.c) [builtins fixtures/isinstance.pyi] [out] [out2] -tmp/c.py:2: note: Revealed type is "Union[a., a.]" +tmp/c.py:2: note: Revealed type is "a. | a." tmp/c.py:3: note: Revealed type is "builtins.int" tmp/c.py:5: note: Revealed type is "builtins.str" @@ -5853,9 +5853,9 @@ reveal_type(a.n) [out] [out2] [out3] -tmp/c.py:4: note: Revealed type is "tuple[Union[tuple[Union[..., None], builtins.int, fallback=b.M], None], builtins.int, fallback=a.N]" -tmp/c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int") -tmp/c.py:7: note: Revealed type is "tuple[Union[tuple[Union[..., None], builtins.int, fallback=b.M], None], builtins.int, fallback=a.N]" +tmp/c.py:4: note: Revealed type is "tuple[tuple[... | None, builtins.int, fallback=b.M] | None, builtins.int, fallback=a.N]" +tmp/c.py:5: error: Incompatible types in assignment (expression has type "N | None", variable has type "int") +tmp/c.py:7: note: Revealed type is "tuple[tuple[... | None, builtins.int, fallback=b.M] | None, builtins.int, fallback=a.N]" [case testTupleTypeUpdateNonRecursiveToRecursiveCoarse] import c @@ -5886,8 +5886,8 @@ def f(x: a.N) -> None: [out] [out2] [out3] -tmp/c.py:4: note: Revealed type is "tuple[Union[tuple[Union[..., None], builtins.int, fallback=b.M], None], builtins.int, fallback=a.N]" -tmp/c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int") +tmp/c.py:4: note: Revealed type is "tuple[tuple[... | None, builtins.int, fallback=b.M] | None, builtins.int, fallback=a.N]" +tmp/c.py:5: error: Incompatible types in assignment (expression has type "N | None", variable has type "int") [case testTypeAliasUpdateNonRecursiveToRecursiveCoarse] import c @@ -5918,8 +5918,8 @@ def f(x: a.N) -> None: [out] [out2] [out3] -tmp/c.py:4: note: Revealed type is "tuple[Union[tuple[Union[..., None], builtins.int], None], builtins.int]" -tmp/c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int") +tmp/c.py:4: note: Revealed type is "tuple[tuple[... | None, builtins.int] | None, builtins.int]" +tmp/c.py:5: error: Incompatible types in assignment (expression has type "N | None", variable has type "int") [case testTypedDictUpdateNonRecursiveToRecursiveCoarse] import c @@ -5960,9 +5960,9 @@ reveal_type(a.n) [out] [out2] [out3] -tmp/c.py:4: note: Revealed type is "TypedDict('a.N', {'r': Union[TypedDict('b.M', {'r': Union[..., None], 'x': builtins.int}), None], 'x': builtins.int})" -tmp/c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int") -tmp/c.py:7: note: Revealed type is "TypedDict('a.N', {'r': Union[TypedDict('b.M', {'r': Union[..., None], 'x': builtins.int}), None], 'x': builtins.int})" +tmp/c.py:4: note: Revealed type is "TypedDict('a.N', {'r': TypedDict('b.M', {'r': ... | None, 'x': builtins.int}) | None, 'x': builtins.int})" +tmp/c.py:5: error: Incompatible types in assignment (expression has type "N | None", variable has type "int") +tmp/c.py:7: note: Revealed type is "TypedDict('a.N', {'r': TypedDict('b.M', {'r': ... | None, 'x': builtins.int}) | None, 'x': builtins.int})" [case testIncrementalAddClassMethodPlugin] # flags: --config-file tmp/mypy.ini @@ -7359,22 +7359,22 @@ reveal_type(d.get(x_or_y_or_z, u)) [typing fixtures/typing-typeddict.pyi] [out] [out2] -tmp/impl.py:13: note: Revealed type is "Union[builtins.int, None]" -tmp/impl.py:14: note: Revealed type is "Union[builtins.str, None]" +tmp/impl.py:13: note: Revealed type is "builtins.int | None" +tmp/impl.py:14: note: Revealed type is "builtins.str | None" tmp/impl.py:15: note: Revealed type is "builtins.object" -tmp/impl.py:16: note: Revealed type is "Union[builtins.int, lib.Unrelated]" +tmp/impl.py:16: note: Revealed type is "builtins.int | lib.Unrelated" tmp/impl.py:17: note: Revealed type is "builtins.int" -tmp/impl.py:18: note: Revealed type is "Union[builtins.str, None]" -tmp/impl.py:21: note: Revealed type is "Union[builtins.int, None]" -tmp/impl.py:22: note: Revealed type is "Union[builtins.str, None]" +tmp/impl.py:18: note: Revealed type is "builtins.str | None" +tmp/impl.py:21: note: Revealed type is "builtins.int | None" +tmp/impl.py:22: note: Revealed type is "builtins.str | None" tmp/impl.py:23: note: Revealed type is "builtins.object" -tmp/impl.py:24: note: Revealed type is "Union[builtins.int, builtins.str, None]" +tmp/impl.py:24: note: Revealed type is "builtins.int | builtins.str | None" tmp/impl.py:25: note: Revealed type is "builtins.object" tmp/impl.py:26: note: Revealed type is "builtins.object" -tmp/impl.py:29: note: Revealed type is "Union[builtins.int, lib.Unrelated]" -tmp/impl.py:30: note: Revealed type is "Union[builtins.str, lib.Unrelated]" +tmp/impl.py:29: note: Revealed type is "builtins.int | lib.Unrelated" +tmp/impl.py:30: note: Revealed type is "builtins.str | lib.Unrelated" tmp/impl.py:31: note: Revealed type is "builtins.object" -tmp/impl.py:32: note: Revealed type is "Union[builtins.int, builtins.str, lib.Unrelated]" +tmp/impl.py:32: note: Revealed type is "builtins.int | builtins.str | lib.Unrelated" tmp/impl.py:33: note: Revealed type is "builtins.object" tmp/impl.py:34: note: Revealed type is "builtins.object" @@ -7434,13 +7434,13 @@ tmp/impl.py:18: note: Revealed type is "builtins.str" tmp/impl.py:21: note: Revealed type is "builtins.int" tmp/impl.py:22: note: Revealed type is "builtins.str" tmp/impl.py:23: note: Revealed type is "builtins.object" -tmp/impl.py:24: note: Revealed type is "Union[builtins.int, builtins.str]" +tmp/impl.py:24: note: Revealed type is "builtins.int | builtins.str" tmp/impl.py:25: note: Revealed type is "builtins.object" tmp/impl.py:26: note: Revealed type is "builtins.object" tmp/impl.py:29: note: Revealed type is "builtins.int" tmp/impl.py:30: note: Revealed type is "builtins.str" tmp/impl.py:31: note: Revealed type is "builtins.object" -tmp/impl.py:32: note: Revealed type is "Union[builtins.int, builtins.str]" +tmp/impl.py:32: note: Revealed type is "builtins.int | builtins.str" tmp/impl.py:33: note: Revealed type is "builtins.object" tmp/impl.py:34: note: Revealed type is "builtins.object" @@ -7494,21 +7494,21 @@ reveal_type(d.get(x_or_y_or_z, u)) [out] [out2] tmp/impl.py:13: note: Revealed type is "builtins.int" -tmp/impl.py:14: note: Revealed type is "Union[builtins.str, None]" +tmp/impl.py:14: note: Revealed type is "builtins.str | None" tmp/impl.py:15: note: Revealed type is "builtins.object" tmp/impl.py:16: note: Revealed type is "builtins.int" tmp/impl.py:17: note: Revealed type is "builtins.int" -tmp/impl.py:18: note: Revealed type is "Union[builtins.str, None]" +tmp/impl.py:18: note: Revealed type is "builtins.str | None" tmp/impl.py:21: note: Revealed type is "builtins.int" -tmp/impl.py:22: note: Revealed type is "Union[builtins.str, None]" +tmp/impl.py:22: note: Revealed type is "builtins.str | None" tmp/impl.py:23: note: Revealed type is "builtins.object" -tmp/impl.py:24: note: Revealed type is "Union[builtins.int, builtins.str, None]" +tmp/impl.py:24: note: Revealed type is "builtins.int | builtins.str | None" tmp/impl.py:25: note: Revealed type is "builtins.object" tmp/impl.py:26: note: Revealed type is "builtins.object" tmp/impl.py:29: note: Revealed type is "builtins.int" -tmp/impl.py:30: note: Revealed type is "Union[builtins.str, lib.Unrelated]" +tmp/impl.py:30: note: Revealed type is "builtins.str | lib.Unrelated" tmp/impl.py:31: note: Revealed type is "builtins.object" -tmp/impl.py:32: note: Revealed type is "Union[builtins.int, builtins.str, lib.Unrelated]" +tmp/impl.py:32: note: Revealed type is "builtins.int | builtins.str | lib.Unrelated" tmp/impl.py:33: note: Revealed type is "builtins.object" tmp/impl.py:34: note: Revealed type is "builtins.object" diff --git a/test-data/unit/check-inference-context.test b/test-data/unit/check-inference-context.test index b5b5d778d90f..b9e7e6996ade 100644 --- a/test-data/unit/check-inference-context.test +++ b/test-data/unit/check-inference-context.test @@ -389,7 +389,7 @@ a: A b: B def f(): aa, ab, ao # Prevent redefinition -aa = [b] # E: List item 0 has incompatible type "B"; expected "Optional[A]" +aa = [b] # E: List item 0 has incompatible type "B"; expected "A | None" ab = [a] # E: List item 0 has incompatible type "A"; expected "B" aa = [a] @@ -700,7 +700,7 @@ class A: pass class B(A): pass class C(A): pass def f(func: Callable[[T], S], *z: T, r: Optional[S] = None) -> S: pass -reveal_type(f(lambda x: 0 if isinstance(x, B) else 1)) # N: Revealed type is "Union[Literal[0]?, Literal[1]?]" +reveal_type(f(lambda x: 0 if isinstance(x, B) else 1)) # N: Revealed type is "Literal[0]? | Literal[1]?" f(lambda x: 0 if isinstance(x, B) else 1, A())() # E: "int" not callable f(lambda x: x if isinstance(x, B) else B(), A(), r=B())() # E: "B" not callable f( @@ -769,9 +769,9 @@ if int(): if int(): b = b or [C()] if int(): - a = a or b # E: Incompatible types in assignment (expression has type "Union[list[A], list[B]]", variable has type "list[A]") + a = a or b # E: Incompatible types in assignment (expression has type "list[A] | list[B]", variable has type "list[A]") if int(): - b = b or c # E: Incompatible types in assignment (expression has type "Union[list[B], list[C]]", variable has type "list[B]") + b = b or c # E: Incompatible types in assignment (expression has type "list[B] | list[C]", variable has type "list[B]") [builtins fixtures/list.pyi] @@ -855,7 +855,7 @@ T = TypeVar('T') def f(x: Union[List[T], str]) -> None: pass f([1]) f('') -f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "Union[list[Never], str]" +f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "list[Never] | str" [builtins fixtures/isinstancelist.pyi] [case testIgnoringInferenceContext] @@ -933,9 +933,9 @@ from typing import TypeVar, Union, List T = TypeVar('T') def f(x: Union[T, List[int]]) -> Union[T, List[int]]: pass -reveal_type(f(1)) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]" +reveal_type(f(1)) # N: Revealed type is "builtins.int | builtins.list[builtins.int]" reveal_type(f([])) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(f(None)) # N: Revealed type is "Union[None, builtins.list[builtins.int]]" +reveal_type(f(None)) # N: Revealed type is "None | builtins.list[builtins.int]" [builtins fixtures/list.pyi] [case testUnionWithGenericTypeItemContextAndStrictOptional] @@ -944,9 +944,9 @@ from typing import TypeVar, Union, List T = TypeVar('T') def f(x: Union[T, List[int]]) -> Union[T, List[int]]: pass -reveal_type(f(1)) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]" +reveal_type(f(1)) # N: Revealed type is "builtins.int | builtins.list[builtins.int]" reveal_type(f([])) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(f(None)) # N: Revealed type is "Union[None, builtins.list[builtins.int]]" +reveal_type(f(None)) # N: Revealed type is "None | builtins.list[builtins.int]" [builtins fixtures/list.pyi] [case testUnionWithGenericTypeItemContextInMethod] @@ -959,10 +959,10 @@ class C(Generic[T]): def f(self, x: Union[T, S]) -> Union[T, S]: pass c = C[List[int]]() -reveal_type(c.f('')) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.str]" +reveal_type(c.f('')) # N: Revealed type is "builtins.list[builtins.int] | builtins.str" reveal_type(c.f([1])) # N: Revealed type is "builtins.list[builtins.int]" reveal_type(c.f([])) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(c.f(None)) # N: Revealed type is "Union[builtins.list[builtins.int], None]" +reveal_type(c.f(None)) # N: Revealed type is "builtins.list[builtins.int] | None" [builtins fixtures/list.pyi] [case testGenericMethodCalledInGenericContext] @@ -1404,7 +1404,7 @@ from typing import Union, List, Any def f(x: Union[List[str], Any]) -> None: a = x if x else [] - reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.str], Any, builtins.list[Union[builtins.str, Any]]]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.str] | Any | builtins.list[builtins.str | Any]" [builtins fixtures/list.pyi] [case testConditionalExpressionWithEmptyIteableAndUnionWithAny] @@ -1412,7 +1412,7 @@ from typing import Union, Iterable, Any def f(x: Union[Iterable[str], Any]) -> None: a = x if x else [] - reveal_type(a) # N: Revealed type is "Union[typing.Iterable[builtins.str], Any, builtins.list[Union[builtins.str, Any]]]" + reveal_type(a) # N: Revealed type is "typing.Iterable[builtins.str] | Any | builtins.list[builtins.str | Any]" [builtins fixtures/list.pyi] [case testInferMultipleAnyUnionCovariant] @@ -1493,7 +1493,7 @@ from typing import Any, Tuple, Union i: Union[Tuple[Any, ...], int] b: Any i = i if isinstance(i, int) else b -reveal_type(i) # N: Revealed type is "Union[Any, builtins.int]" +reveal_type(i) # N: Revealed type is "Any | builtins.int" [builtins fixtures/isinstance.pyi] [case testLambdaInferenceUsesNarrowedTypes] diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 8b900fc5a24e..e0e853c3dbf9 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -340,10 +340,10 @@ for var in [a, b, c, d, e, f, s, t, u, v, w, x, y, z, aa]: reveal_type(var) # N: Revealed type is "builtins.int" for var2 in [g, h, i, j, k, l]: - reveal_type(var2) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(var2) # N: Revealed type is "builtins.int | builtins.str" for var3 in [m, n, o, p, q, r]: - reveal_type(var3) # N: Revealed type is "Union[builtins.int, Any]" + reveal_type(var3) # N: Revealed type is "builtins.int | Any" T = TypeVar("T", bound=Type[Foo]) @@ -358,7 +358,7 @@ T2 = TypeVar("T2", bound=Type[Union[Foo, Bar]]) def check2(x: T2) -> T2: a, b, c = x for var in [a, b, c]: - reveal_type(var) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(var) # N: Revealed type is "builtins.int | builtins.str" return x T3 = TypeVar("T3", bound=Union[Type[Foo], Type[Bar]]) @@ -366,7 +366,7 @@ T3 = TypeVar("T3", bound=Union[Type[Foo], Type[Bar]]) def check3(x: T3) -> T3: a, b, c = x for var in [a, b, c]: - reveal_type(var) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(var) # N: Revealed type is "builtins.int | builtins.str" return x [out] @@ -972,7 +972,7 @@ from typing import TypeVar, Union, List T = TypeVar('T') def f() -> List[T]: pass d1 = f() # type: Union[List[int], str] -d2 = f() # type: Union[int, str] # E: Incompatible types in assignment (expression has type "list[Never]", variable has type "Union[int, str]") +d2 = f() # type: Union[int, str] # E: Incompatible types in assignment (expression has type "list[Never]", variable has type "int | str") def g(x: T) -> List[T]: pass d3 = g(1) # type: Union[List[int], List[str]] [builtins fixtures/list.pyi] @@ -988,7 +988,7 @@ a = k2 if int(): a = k2 if int(): - a = k1 # E: Incompatible types in assignment (expression has type "Callable[[int, list[T@k1]], list[Union[T@k1, int]]]", variable has type "Callable[[S, list[T@k2]], list[Union[T@k2, int]]]") + a = k1 # E: Incompatible types in assignment (expression has type "Callable[[int, list[T@k1]], list[T@k1 | int]]", variable has type "Callable[[S, list[T@k2]], list[T@k2 | int]]") b = k1 if int(): b = k1 @@ -1248,7 +1248,7 @@ class X(TypedDict): x: X for a in ("hourly", "daily"): - reveal_type(a) # N: Revealed type is "Union[Literal['hourly']?, Literal['daily']?]" + reveal_type(a) # N: Revealed type is "Literal['hourly']? | Literal['daily']?" reveal_type(x[a]) # N: Revealed type is "builtins.int" reveal_type(a.upper()) # N: Revealed type is "builtins.str" c = a @@ -1467,17 +1467,17 @@ class Wrapper: def f(cond: bool) -> Any: f = Wrapper if cond else lambda x: x - reveal_type(f) # N: Revealed type is "Union[def (x: Any) -> __main__.Wrapper, def (x: Any) -> Any]" + reveal_type(f) # N: Revealed type is "def (x: Any) -> __main__.Wrapper | def (x: Any) -> Any" return f(3) def g(cond: bool) -> Any: f = lambda x: x if cond else Wrapper - reveal_type(f) # N: Revealed type is "def (x: Any) -> Union[Any, def (x: Any) -> __main__.Wrapper]" + reveal_type(f) # N: Revealed type is "def (x: Any) -> Any | def (x: Any) -> __main__.Wrapper" return f(3) def h(cond: bool) -> Any: f = (lambda x: x) if cond else Wrapper - reveal_type(f) # N: Revealed type is "Union[def (x: Any) -> Any, def (x: Any) -> __main__.Wrapper]" + reveal_type(f) # N: Revealed type is "def (x: Any) -> Any | def (x: Any) -> __main__.Wrapper" return f(3) -- Boolean operators @@ -1850,7 +1850,7 @@ class C: self.a = None if bool(): self.a = 1 -reveal_type(C().a) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(C().a) # N: Revealed type is "builtins.int | None" [case testInferAttributeInitializedToEmptyNonSelf] class C: @@ -1928,7 +1928,7 @@ class C: a = None def __init__(self) -> None: self.a = 1 -reveal_type(C().a) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(C().a) # N: Revealed type is "builtins.int | None" [case testInferListTypeFromEmptyListAndAny] def f(): @@ -2415,7 +2415,7 @@ b: Union[str, tuple] def f(): pass def g(x: Union[int, str]): pass c = a if f() else b -g(c) # E: Argument 1 to "g" has incompatible type "Union[int, str, tuple[Any, ...]]"; expected "Union[int, str]" +g(c) # E: Argument 1 to "g" has incompatible type "int | str | tuple[Any, ...]"; expected "int | str" [builtins fixtures/tuple.pyi] [case testUnificationMultipleInheritance] @@ -2570,7 +2570,7 @@ def check() -> None: if int(): x = Foo() reveal_type(x) # N: Revealed type is "__main__.Foo[Any]" - reveal_type(x) # N: Revealed type is "Union[__main__.Foo[Any], None]" + reveal_type(x) # N: Revealed type is "__main__.Foo[Any] | None" [case testRejectsPartialWithUninhabited2] from typing import Generic, TypeVar @@ -2585,7 +2585,7 @@ def check() -> None: x = Foo() reveal_type(x) # N: Revealed type is "__main__.Foo[Any]" -reveal_type(x) # N: Revealed type is "Union[__main__.Foo[Any], None]" +reveal_type(x) # N: Revealed type is "__main__.Foo[Any] | None" [case testRejectsPartialWithUninhabited3] # Without force-rejecting Partial, this crashes: @@ -2601,10 +2601,10 @@ def check() -> None: if client := Foo(): reveal_type(client) # N: Revealed type is "__main__.Foo[Any]" - reveal_type(client) # N: Revealed type is "Union[__main__.Foo[Any], None]" + reveal_type(client) # N: Revealed type is "__main__.Foo[Any] | None" - client = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "Optional[Foo[Any]]") - reveal_type(client) # N: Revealed type is "Union[__main__.Foo[Any], None]" + client = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "Foo[Any] | None") + reveal_type(client) # N: Revealed type is "__main__.Foo[Any] | None" [case testRejectsPartialWithUninhabitedIndependently] from typing import Generic, TypeVar @@ -2621,8 +2621,8 @@ def bad() -> None: def good() -> None: global client - client = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Optional[Foo[Any]]") - reveal_type(client) # N: Revealed type is "Union[__main__.Foo[Any], None]" + client = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Foo[Any] | None") + reveal_type(client) # N: Revealed type is "__main__.Foo[Any] | None" def bad2() -> None: global client @@ -2698,7 +2698,7 @@ if bool(): [case testLocalPartialTypesWithGlobalInitializedToNone] # flags: --local-partial-types -x = None # E: Need type annotation for "x" (hint: "x: Optional[] = ...") +x = None # E: Need type annotation for "x" (hint: "x: | None = ...") def f() -> None: global x @@ -2709,7 +2709,7 @@ reveal_type(x) # N: Revealed type is "None" [case testLocalPartialTypesWithGlobalInitializedToNone2] # flags: --local-partial-types -x = None # E: Need type annotation for "x" (hint: "x: Optional[] = ...") +x = None # E: Need type annotation for "x" (hint: "x: | None = ...") def f(): global x @@ -2735,11 +2735,11 @@ x = None def f() -> None: global x - x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Optional[str]") + x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str | None") x = '' def g() -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" [case testLocalPartialTypesWithGlobalInitializedToNone4] # flags: --local-partial-types --no-strict-optional @@ -2758,7 +2758,7 @@ reveal_type(a) # N: Revealed type is "builtins.str" [case testLocalPartialTypesWithClassAttributeInitializedToNone] # flags: --local-partial-types class A: - x = None # E: Need type annotation for "x" (hint: "x: Optional[] = ...") + x = None # E: Need type annotation for "x" (hint: "x: | None = ...") def f(self) -> None: self.x = 1 @@ -2932,7 +2932,7 @@ class B(A): x = None x = Y() -reveal_type(B.x) # N: Revealed type is "Union[__main__.Y, None]" +reveal_type(B.x) # N: Revealed type is "__main__.Y | None" [case testLocalPartialTypesBinderSpecialCase] # flags: --local-partial-types @@ -2941,7 +2941,7 @@ from typing import List def f(x): pass class A: - x = None # E: Need type annotation for "x" (hint: "x: Optional[] = ...") + x = None # E: Need type annotation for "x" (hint: "x: | None = ...") def f(self, p: List[str]) -> None: self.x = f(p) @@ -2951,18 +2951,18 @@ class A: [case testLocalPartialTypesAccessPartialNoneAttribute] # flags: --local-partial-types class C: - a = None # E: Need type annotation for "a" (hint: "a: Optional[] = ...") + a = None # E: Need type annotation for "a" (hint: "a: | None = ...") def f(self, x) -> None: - C.a.y # E: Item "None" of "Optional[Any]" has no attribute "y" + C.a.y # E: Item "None" of "Any | None" has no attribute "y" [case testLocalPartialTypesAccessPartialNoneAttribute2] # flags: --local-partial-types class C: - a = None # E: Need type annotation for "a" (hint: "a: Optional[] = ...") + a = None # E: Need type annotation for "a" (hint: "a: | None = ...") def f(self, x) -> None: - self.a.y # E: Item "None" of "Optional[Any]" has no attribute "y" + self.a.y # E: Item "None" of "Any | None" has no attribute "y" -- Special case for assignment to '_' -- ---------------------------------- @@ -3100,7 +3100,7 @@ class B(A): class C(A): x = '12' -reveal_type(A.x) # N: Revealed type is "Union[Any, None]" +reveal_type(A.x) # N: Revealed type is "Any | None" reveal_type(B.x) # N: Revealed type is "builtins.int" reveal_type(C.x) # N: Revealed type is "builtins.str" @@ -3529,7 +3529,7 @@ class C(NamedTuple): t: Optional[C] d: Dict[C, bytes] x = t and d[t] -reveal_type(x) # N: Revealed type is "Union[None, builtins.bytes]" +reveal_type(x) # N: Revealed type is "None | builtins.bytes" if x: reveal_type(x) # N: Revealed type is "builtins.bytes" [builtins fixtures/dict.pyi] @@ -3578,7 +3578,7 @@ class B: class C(B): x = [1] -reveal_type(C().x) # N: Revealed type is "builtins.list[Union[builtins.int, None]]" +reveal_type(C().x) # N: Revealed type is "builtins.list[builtins.int | None]" [builtins fixtures/list.pyi] [case testUseSupertypeAsInferenceContextInvalidType] @@ -3881,7 +3881,7 @@ def foo() -> Dict[str, Any]: return empty # E: Incompatible return value type (got "dict[Never, Never]", expected "dict[str, Any]") def bar() -> Union[Dict[str, Any], Dict[int, Any]]: - return empty # E: Incompatible return value type (got "dict[Never, Never]", expected "Union[dict[str, Any], dict[int, Any]]") + return empty # E: Incompatible return value type (got "dict[Never, Never]", expected "dict[str, Any] | dict[int, Any]") [builtins fixtures/dict.pyi] [case testUpperBoundInferenceFallbackNotOverused] @@ -3913,15 +3913,15 @@ def dec(f: Callable[[S], T]) -> Callable[[S], List[T]]: ... @dec def func(arg: T) -> Union[T, str]: ... -reveal_type(func) # N: Revealed type is "def [S] (S`1) -> builtins.list[Union[S`1, builtins.str]]" -reveal_type(func(42)) # N: Revealed type is "builtins.list[Union[builtins.int, builtins.str]]" +reveal_type(func) # N: Revealed type is "def [S] (S`1) -> builtins.list[S`1 | builtins.str]" +reveal_type(func(42)) # N: Revealed type is "builtins.list[builtins.int | builtins.str]" def dec2(f: Callable[[S], List[T]]) -> Callable[[S], T]: ... @dec2 def func2(arg: T) -> List[Union[T, str]]: ... -reveal_type(func2) # N: Revealed type is "def [S] (S`4) -> Union[S`4, builtins.str]" -reveal_type(func2(42)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(func2) # N: Revealed type is "def [S] (S`4) -> S`4 | builtins.str" +reveal_type(func2(42)) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericCallbackProtoMultiple] @@ -4045,7 +4045,7 @@ x: Union[int, str] x = "abc" for x in list[int](): reveal_type(x) # N: Revealed type is "builtins.int" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testForLoopIndexVaribaleNarrowing2] # flags: --enable-error-code=redundant-expr @@ -4054,7 +4054,7 @@ x: Union[int, str] x = "abc" for x in list[int](): reveal_type(x) # N: Revealed type is "builtins.int" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNarrowInFunctionDefer] from typing import Optional, Callable, TypeVar @@ -4118,7 +4118,7 @@ class Mapping(Generic[_K, _V]): def check(mapping: Mapping[str, _T]) -> None: ok1 = mapping.get("", "") - reveal_type(ok1) # N: Revealed type is "Union[_T`-1, builtins.str]" + reveal_type(ok1) # N: Revealed type is "_T`-1 | builtins.str" ok2: Union[_T, str] = mapping.get("", "") [builtins fixtures/tuple.pyi] @@ -4132,7 +4132,7 @@ def check_and(maybe: bool) -> None: if maybe and (foo := Foo(True)).value: reveal_type(foo) # N: Revealed type is "__main__.Foo" else: - reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]" + reveal_type(foo) # N: Revealed type is "__main__.Foo | None" def check_and_nested(maybe: bool) -> None: foo = None @@ -4143,14 +4143,14 @@ def check_and_nested(maybe: bool) -> None: reveal_type(bar) # N: Revealed type is "__main__.Foo" reveal_type(baz) # N: Revealed type is "__main__.Foo" else: - reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]" - reveal_type(bar) # N: Revealed type is "Union[__main__.Foo, None]" - reveal_type(baz) # N: Revealed type is "Union[__main__.Foo, None]" + reveal_type(foo) # N: Revealed type is "__main__.Foo | None" + reveal_type(bar) # N: Revealed type is "__main__.Foo | None" + reveal_type(baz) # N: Revealed type is "__main__.Foo | None" def check_or(maybe: bool) -> None: foo = None if maybe or (foo := Foo(True)).value: - reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]" + reveal_type(foo) # N: Revealed type is "__main__.Foo | None" else: reveal_type(foo) # N: Revealed type is "__main__.Foo" @@ -4163,9 +4163,9 @@ def check_or_nested(maybe: bool) -> None: reveal_type(bar) # N: Revealed type is "__main__.Foo" reveal_type(baz) # N: Revealed type is "__main__.Foo" else: - reveal_type(foo) # N: Revealed type is "Union[__main__.Foo, None]" - reveal_type(bar) # N: Revealed type is "Union[__main__.Foo, None]" - reveal_type(baz) # N: Revealed type is "Union[__main__.Foo, None]" + reveal_type(foo) # N: Revealed type is "__main__.Foo | None" + reveal_type(bar) # N: Revealed type is "__main__.Foo | None" + reveal_type(baz) # N: Revealed type is "__main__.Foo | None" [case testInferWalrusAssignmentIndexInCondition] def check_and(maybe: bool) -> None: @@ -4175,8 +4175,8 @@ def check_and(maybe: bool) -> None: reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]" reveal_type(bar) # N: Revealed type is "builtins.int" else: - reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]" - reveal_type(bar) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int] | None" + reveal_type(bar) # N: Revealed type is "builtins.int | None" def check_and_nested(maybe: bool) -> None: foo = None @@ -4187,16 +4187,16 @@ def check_and_nested(maybe: bool) -> None: reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int]" reveal_type(baz) # N: Revealed type is "builtins.list[builtins.int]" else: - reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]" - reveal_type(bar) # N: Revealed type is "Union[builtins.list[builtins.int], None]" - reveal_type(baz) # N: Revealed type is "Union[builtins.list[builtins.int], None]" + reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int] | None" + reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int] | None" + reveal_type(baz) # N: Revealed type is "builtins.list[builtins.int] | None" def check_or(maybe: bool) -> None: foo = None bar = None if maybe or (foo := [1])[(bar := 0)]: - reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]" - reveal_type(bar) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int] | None" + reveal_type(bar) # N: Revealed type is "builtins.int | None" else: reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]" reveal_type(bar) # N: Revealed type is "builtins.int" @@ -4206,9 +4206,9 @@ def check_or_nested(maybe: bool) -> None: bar = None baz = None if maybe or (foo := (bar := (baz := [1])))[0]: - reveal_type(foo) # N: Revealed type is "Union[builtins.list[builtins.int], None]" - reveal_type(bar) # N: Revealed type is "Union[builtins.list[builtins.int], None]" - reveal_type(baz) # N: Revealed type is "Union[builtins.list[builtins.int], None]" + reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int] | None" + reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int] | None" + reveal_type(baz) # N: Revealed type is "builtins.list[builtins.int] | None" else: reveal_type(foo) # N: Revealed type is "builtins.list[builtins.int]" reveal_type(bar) # N: Revealed type is "builtins.list[builtins.int]" diff --git a/test-data/unit/check-isinstance.test b/test-data/unit/check-isinstance.test index acd4b588f98c..ff403a9f5d8c 100644 --- a/test-data/unit/check-isinstance.test +++ b/test-data/unit/check-isinstance.test @@ -37,16 +37,16 @@ from typing import Union, List, Tuple, Dict def f(x: Union[int, str, List]) -> None: if isinstance(x, (str, (int,))): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" - x[1] # E: Value of type "Union[int, str]" is not indexable + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" + x[1] # E: Value of type "int | str" is not indexable else: reveal_type(x) # N: Revealed type is "builtins.list[Any]" x[1] - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" if isinstance(x, (str, (list,))): - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.list[Any]" x[1] - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" [builtins fixtures/isinstancelist.pyi] [case testClassAttributeInitialization] @@ -191,7 +191,7 @@ def bar() -> None: x + 'a' while bool(): x + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = foo() if bool(): continue @@ -422,17 +422,17 @@ def f(x: Union[List[int], List[str], int]) -> None: a + 'x' # E: Unsupported operand types for + ("int" and "str") # type of a? - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" x + 1 # E: Unsupported operand types for + ("list[int]" and "int") \ # E: Unsupported operand types for + ("list[str]" and "int") \ - # N: Left operand is of type "Union[list[int], list[str]]" + # N: Left operand is of type "list[int] | list[str]" else: x[0] # E: Value of type "int" is not indexable x + 1 - x[0] # E: Value of type "Union[list[int], list[str], int]" is not indexable + x[0] # E: Value of type "list[int] | list[str] | int" is not indexable x + 1 # E: Unsupported operand types for + ("list[int]" and "int") \ # E: Unsupported operand types for + ("list[str]" and "int") \ - # N: Left operand is of type "Union[list[int], list[str], int]" + # N: Left operand is of type "list[int] | list[str] | int" [builtins fixtures/isinstancelist.pyi] [case testUnionListIsinstance2] @@ -465,7 +465,7 @@ if int(): x = x + 1 x = foo() x = x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if isinstance(x, str): x = x + 1 # E: Unsupported operand types for + ("str" and "int") x = 1 @@ -530,9 +530,9 @@ while bool(): if isinstance(h.pet.paws, str): x = h.pet.paws + 'a' y = h.pet.paws + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" z = h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if isinstance(h.pet.paws, str): x = h.pet.paws + 'a' break @@ -578,7 +578,7 @@ v = A() # type: Union[A, B, C] if isinstance(v, (B, C)): v.method2(123) - v.method3('xyz') # E: Item "B" of "Union[B, C]" has no attribute "method3" + v.method3('xyz') # E: Item "B" of "B | C" has no attribute "method3" [builtins fixtures/isinstance.pyi] [case testIsinstanceNeverWidens] @@ -594,7 +594,7 @@ reveal_type(a) # N: Revealed type is "__main__.A" b = A() # type: Union[A, B] assert isinstance(b, (A, B, C)) -reveal_type(b) # N: Revealed type is "Union[__main__.A, __main__.B]" +reveal_type(b) # N: Revealed type is "__main__.A | __main__.B" [builtins fixtures/isinstance.pyi] [case testMemberAssignmentChanges] @@ -606,7 +606,7 @@ class Dog: pet = Dog() pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" pet.paws = 'a' pet.paws + 'a' pet.paws = 1 @@ -635,7 +635,7 @@ if isinstance(h.pet, Dog): for i in [1]: # TODO: should we allow this if iterable is of length one or zero? h.pet.paws + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if bool(): break h.pet.paws = 1 @@ -678,7 +678,7 @@ def foo() -> None: else: pass y = x + 'asdad' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" foo() [builtins fixtures/isinstancelist.pyi] @@ -697,11 +697,11 @@ while bool(): x + [1] x + 'a' # E: Unsupported operand types for + ("int" and "str") \ # E: Unsupported operand types for + ("list[int]" and "str") \ - # N: Left operand is of type "Union[int, str, list[int]]" + # N: Left operand is of type "int | str | list[int]" x + [1] # E: Unsupported operand types for + ("int" and "list[int]") \ # E: Unsupported operand types for + ("str" and "list[int]") \ - # N: Left operand is of type "Union[int, str, list[int]]" + # N: Left operand is of type "int | str | list[int]" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceThreeUnion2] @@ -718,7 +718,7 @@ while bool(): x + 'a' # E: Unsupported operand types for + ("list[int]" and "str") x + [1] # E: Unsupported operand types for + ("int" and "list[int]") \ # E: Unsupported operand types for + ("str" and "list[int]") \ - # N: Left operand is of type "Union[int, str, list[int]]" + # N: Left operand is of type "int | str | list[int]" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceThreeUnion3] @@ -738,7 +738,7 @@ while bool(): x + 'a' x + [1] # E: Unsupported operand types for + ("int" and "list[int]") \ # E: Unsupported operand types for + ("str" and "list[int]") \ - # N: Left operand is of type "Union[int, str, list[int]]" + # N: Left operand is of type "int | str | list[int]" [builtins fixtures/isinstancelist.pyi] [case testRemovingTypeRepeatedly] @@ -749,27 +749,27 @@ def foo() -> Union[int, str]: pass for i in [1, 2]: x = foo() x + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if isinstance(x, int): break x + 'a' x = foo() x + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if isinstance(x, int): break x + 'a' x = foo() x + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if isinstance(x, int): break x + 'a' x + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" [builtins fixtures/isinstancelist.pyi] [case testModifyRepeatedly] @@ -780,9 +780,9 @@ def foo() -> Union[int, str]: pass x = foo() def f(): x # Prevent redefinition x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 1 x + 1 @@ -794,9 +794,9 @@ x + 'a' x = foo() x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x + 'a' # E: Unsupported operand types for + ("int" and "str") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" [builtins fixtures/isinstancelist.pyi] [case testModifyLoop] @@ -807,7 +807,7 @@ def foo() -> Union[int, str]: pass x = foo() def f(): x # Prevent redefinition x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 'a' x + 1 # E: Unsupported operand types for + ("str" and "int") x = 1 @@ -815,7 +815,7 @@ x + 1 while bool(): x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 'a' [builtins fixtures/isinstancelist.pyi] @@ -827,7 +827,7 @@ def foo() -> Union[int, str]: pass x = foo() def f(): x # Prevent redefinition x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 'a' x + 1 # E: Unsupported operand types for + ("str" and "int") x = 1 @@ -837,7 +837,7 @@ for i in [1]: x = 'a' x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" [builtins fixtures/isinstancelist.pyi] [case testModifyLoop3] @@ -856,7 +856,7 @@ while bool(): else: x + 1 x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 1 for y in [1]: x + 1 @@ -865,7 +865,7 @@ for y in [1]: else: x + 1 x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" [builtins fixtures/isinstancelist.pyi] [case testModifyLoopWhile4] @@ -889,13 +889,13 @@ x + 'a' x = 1 while bool(): x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if bool(): x = 'a' continue else: x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 'a' x + 'a' [builtins fixtures/isinstancelist.pyi] @@ -921,13 +921,13 @@ x + 'a' x = 1 for y in [1]: x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" if bool(): x = 'a' continue else: x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 'a' x + 'a' [builtins fixtures/isinstancelist.pyi] @@ -950,7 +950,7 @@ for y in [1]: else: x + 1 x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" x = 1 while bool(): while bool(): @@ -961,7 +961,7 @@ while bool(): else: x + 1 x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" [builtins fixtures/isinstancelist.pyi] [case testModifyLoopLong] @@ -974,7 +974,7 @@ def foo() -> Union[int, str, A]: pass def bar() -> None: x = foo() x + 1 # E: Unsupported left operand type for + ("A") \ - # N: Left operand is of type "Union[int, str, A]" \ + # N: Left operand is of type "int | str | A" \ # E: Unsupported operand types for + ("str" and "int") if isinstance(x, A): x.a @@ -1007,7 +1007,7 @@ def bar() -> None: if isinstance(x, int): x + 1 else: - x.a # E: Item "str" of "Union[str, A]" has no attribute "a" + x.a # E: Item "str" of "str | A" has no attribute "a" x = 'a' [builtins fixtures/isinstancelist.pyi] @@ -1032,7 +1032,7 @@ while isinstance(x, int): x = 'a' else: reveal_type(x) # N: Revealed type is "builtins.str" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/isinstance.pyi] [case testWhileLinkedList] @@ -1295,7 +1295,7 @@ from typing import Optional def h(a: bool, x: object) -> Optional[int]: if a or isinstance(x, int): return None - return x # E: Incompatible return value type (got "object", expected "Optional[int]") + return x # E: Incompatible return value type (got "object", expected "int | None") [builtins fixtures/isinstance.pyi] [case testIsinstanceWithOverlappingUnionType] @@ -1337,7 +1337,7 @@ def f1(x: Union[float, int]) -> None: def f2(x: Union[FloatLike, IntLike]) -> None: # ...but not regular subtyping relationships if isinstance(x, FloatLike): - reveal_type(x) # N: Revealed type is "Union[__main__.FloatLike, __main__.IntLike]" + reveal_type(x) # N: Revealed type is "__main__.FloatLike | __main__.IntLike" [builtins fixtures/isinstance.pyi] [case testIsinstanceOfSuperclass] @@ -1397,7 +1397,7 @@ def f(x: Union[List[int], str]) -> None: x[0]() # E: "int" not callable else: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.int] | builtins.str" [builtins fixtures/isinstancelist.pyi] [case testIsinstanceOrIsinstance] @@ -1411,7 +1411,7 @@ class C(A): x1 = A() if isinstance(x1, B) or isinstance(x1, C): - reveal_type(x1) # N: Revealed type is "Union[__main__.B, __main__.C]" + reveal_type(x1) # N: Revealed type is "__main__.B | __main__.C" f = x1.flag # type: int else: reveal_type(x1) # N: Revealed type is "__main__.A" @@ -1462,11 +1462,11 @@ from typing import Union def f(x: Union[int, str], typ: type) -> None: if isinstance(x, (typ, int)): x + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + # N: Left operand is of type "int | str" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceWithBoundedType] @@ -1476,10 +1476,10 @@ class A: pass def f(x: Union[int, A], a: Type[A]) -> None: if isinstance(x, (a, int)): - reveal_type(x) # N: Revealed type is "Union[builtins.int, __main__.A]" + reveal_type(x) # N: Revealed type is "builtins.int | __main__.A" else: reveal_type(x) # N: Revealed type is "__main__.A" - reveal_type(x) # N: Revealed type is "Union[builtins.int, __main__.A]" + reveal_type(x) # N: Revealed type is "builtins.int | __main__.A" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceWithEmtpy2ndArg] @@ -1490,7 +1490,7 @@ def f(x: Union[int, str]) -> None: if isinstance(x, ()): reveal_type(x) # E: Statement is unreachable else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceWithTypeObject] @@ -1505,7 +1505,7 @@ def f(x: Union[int, A], a: Type[A]) -> None: reveal_type(x) # N: Revealed type is "builtins.int" else: reveal_type(x) # N: Revealed type is "__main__.A" - reveal_type(x) # N: Revealed type is "Union[builtins.int, __main__.A]" + reveal_type(x) # N: Revealed type is "builtins.int | __main__.A" [builtins fixtures/isinstancelist.pyi] [case testIssubclassUnreachable] @@ -1521,7 +1521,7 @@ class Z(X): pass a: Union[Type[Y], Type[Z]] if issubclass(a, X): - reveal_type(a) # N: Revealed type is "Union[type[__main__.Y], type[__main__.Z]]" + reveal_type(a) # N: Revealed type is "type[__main__.Y] | type[__main__.Z]" else: reveal_type(a) # unreachable block [builtins fixtures/isinstancelist.pyi] @@ -1530,21 +1530,21 @@ else: from typing import Union, List, Tuple, Dict, Type def f(x: Union[Type[int], Type[str], Type[List]]) -> None: if issubclass(x, (str, (int,))): - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str]" - x()[1] # E: Value of type "Union[int, str]" is not indexable + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str" + x()[1] # E: Value of type "int | str" is not indexable else: reveal_type(x) # N: Revealed type is "type[builtins.list[Any]]" reveal_type(x()) # N: Revealed type is "builtins.list[Any]" x()[1] - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" if issubclass(x, (str, (list,))): - reveal_type(x) # N: Revealed type is "Union[type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.str | builtins.list[Any]" x()[1] - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" [builtins fixtures/isinstancelist.pyi] [case testIssubclasDestructuringUnions2] @@ -1552,45 +1552,45 @@ from typing import Union, List, Tuple, Dict, Type def f(x: Type[Union[int, str, List]]) -> None: if issubclass(x, (str, (int,))): - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str]" - x()[1] # E: Value of type "Union[int, str]" is not indexable + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str" + x()[1] # E: Value of type "int | str" is not indexable else: reveal_type(x) # N: Revealed type is "type[builtins.list[Any]]" reveal_type(x()) # N: Revealed type is "builtins.list[Any]" x()[1] - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" if issubclass(x, (str, (list,))): - reveal_type(x) # N: Revealed type is "Union[type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.str | builtins.list[Any]" x()[1] - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" [builtins fixtures/isinstancelist.pyi] [case testIssubclasDestructuringUnions3] from typing import Union, List, Tuple, Dict, Type def f(x: Type[Union[int, str, List]]) -> None: - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" if issubclass(x, (str, (int,))): - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str]" - x()[1] # E: Value of type "Union[int, str]" is not indexable + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str" + x()[1] # E: Value of type "int | str" is not indexable else: reveal_type(x) # N: Revealed type is "type[builtins.list[Any]]" reveal_type(x()) # N: Revealed type is "builtins.list[Any]" x()[1] - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" if issubclass(x, (str, (list,))): - reveal_type(x) # N: Revealed type is "Union[type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.str | builtins.list[Any]" x()[1] - reveal_type(x) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], type[builtins.list[Any]]]" - reveal_type(x()) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "type[builtins.int] | type[builtins.str] | type[builtins.list[Any]]" + reveal_type(x()) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]" [builtins fixtures/isinstancelist.pyi] [case testIssubclass] @@ -1720,7 +1720,7 @@ def test_issubclass(cls: Type[Mob]) -> None: ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance if issubclass(cls, (GoblinDigger, GoblinAmbusher)): - reveal_type(cls) # N: Revealed type is "Union[type[__main__.GoblinDigger], type[__main__.GoblinAmbusher]]" + reveal_type(cls) # N: Revealed type is "type[__main__.GoblinDigger] | type[__main__.GoblinAmbusher]" cls.level cls.job g = cls() @@ -1859,8 +1859,8 @@ def f(x: T) -> T: reveal_type(x) # N: Revealed type is "T`-1" x.a # E: "T" has no attribute "a" x.b - x.a # E: Item "B" of the upper bound "Union[A, B]" of type variable "T" has no attribute "a" - x.b # E: Item "A" of the upper bound "Union[A, B]" of type variable "T" has no attribute "b" + x.a # E: Item "B" of the upper bound "A | B" of type variable "T" has no attribute "a" + x.b # E: Item "A" of the upper bound "A | B" of type variable "T" has no attribute "b" return x [builtins fixtures/isinstance.pyi] @@ -1898,13 +1898,13 @@ U = (list, T) x: object = None if isinstance(x, T): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" if isinstance(x, U): - reveal_type(x) # N: Revealed type is "Union[builtins.list[Any], builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.list[Any] | builtins.int | builtins.str" if isinstance(x, (set, (list, T))): - reveal_type(x) # N: Revealed type is "Union[builtins.set[Any], builtins.list[Any], builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.set[Any] | builtins.list[Any] | builtins.int | builtins.str" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceTooFewArgs] @@ -1932,7 +1932,7 @@ if issubclass(y): # E: Missing positional argument "t" in call to "issubclass" [case testIsInstanceTooManyArgs] isinstance(1, 1, 1) # E: Too many arguments for "isinstance" \ - # E: Argument 2 to "isinstance" has incompatible type "int"; expected "Union[type, tuple[Any, ...]]" + # E: Argument 2 to "isinstance" has incompatible type "int"; expected "type | tuple[Any, ...]" x: object if isinstance(x, str, 1): # E: Too many arguments for "isinstance" reveal_type(x) # N: Revealed type is "builtins.object" @@ -1973,9 +1973,9 @@ y: Optional[int] if y in x: reveal_type(y) # N: Revealed type is "builtins.int" else: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" if y not in x: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: reveal_type(y) # N: Revealed type is "builtins.int" [builtins fixtures/list.pyi] @@ -1988,9 +1988,9 @@ x: List[Optional[int]] y: Optional[int] if y not in x: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" [builtins fixtures/list.pyi] [out] @@ -2001,9 +2001,9 @@ x: List[str] y: Optional[int] if y in x: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" [builtins fixtures/list.pyi] [out] @@ -2017,7 +2017,7 @@ nested_any: List[List[Any]] if lst in nested_any: reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int]" if x in nested_any: - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | None" [builtins fixtures/list.pyi] [out] @@ -2031,7 +2031,7 @@ y: Optional[B] if y in (B(), C()): reveal_type(y) # N: Revealed type is "__main__.B" else: - reveal_type(y) # N: Revealed type is "Union[__main__.B, None]" + reveal_type(y) # N: Revealed type is "__main__.B | None" [builtins fixtures/tuple.pyi] [out] @@ -2044,7 +2044,7 @@ nt: NT y: Optional[int] if y not in nt: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: reveal_type(y) # N: Revealed type is "builtins.int" [builtins fixtures/tuple.pyi] @@ -2058,9 +2058,9 @@ y: Optional[str] if y in x: reveal_type(y) # N: Revealed type is "builtins.str" else: - reveal_type(y) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(y) # N: Revealed type is "builtins.str | None" if y not in x: - reveal_type(y) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(y) # N: Revealed type is "builtins.str | None" else: reveal_type(y) # N: Revealed type is "builtins.str" [builtins fixtures/dict.pyi] @@ -2073,14 +2073,14 @@ z: List[object] y: Optional[int] if y in x: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" if y not in z: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" [typing fixtures/typing-medium.pyi] [builtins fixtures/list.pyi] [out] @@ -2095,13 +2095,13 @@ class C(Container[int]): y: Optional[int] # We never trust user defined types if y in C(): - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" if y not in C(): - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" else: - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" [typing fixtures/typing-full.pyi] [builtins fixtures/list.pyi] [out] @@ -2114,9 +2114,9 @@ y: Optional[str] if y in {'a', 'b', 'c'}: reveal_type(y) # N: Revealed type is "builtins.str" else: - reveal_type(y) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(y) # N: Revealed type is "builtins.str | None" if y not in s: - reveal_type(y) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(y) # N: Revealed type is "builtins.str | None" else: reveal_type(y) # N: Revealed type is "builtins.str" [builtins fixtures/set.pyi] @@ -2154,7 +2154,7 @@ from typing import Any, Union class A: ... B: Any x: Union[A, B] -reveal_type(x) # N: Revealed type is "Union[__main__.A, Any]" +reveal_type(x) # N: Revealed type is "__main__.A | Any" assert isinstance(x, B) reveal_type(x) # N: Revealed type is "Any" [builtins fixtures/isinstance.pyi] @@ -2163,7 +2163,7 @@ reveal_type(x) # N: Revealed type is "Any" from typing import Union from foo import A # type: ignore def f(x: Union[A, str]) -> None: - x.method_only_in_a() # E: Item "str" of "Union[Any, str]" has no attribute "method_only_in_a" + x.method_only_in_a() # E: Item "str" of "Any | str" has no attribute "method_only_in_a" if isinstance(x, A): x.method_only_in_a() [builtins fixtures/isinstance.pyi] @@ -2284,14 +2284,14 @@ from typing import Union, Optional, List # correctly ignores 'None' in unions. def foo(x: Optional[List[str]]) -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], None]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | None" assert isinstance(x, list) reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]" def bar(x: Union[List[str], List[int], None]) -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], builtins.list[builtins.int], None]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int] | None" assert isinstance(x, list) - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int]" [builtins fixtures/isinstancelist.pyi] [case testNoneAndGenericTypesOverlapStrictOptional] @@ -2302,30 +2302,30 @@ from typing import Union, Optional, List # of completeness. def foo(x: Optional[List[str]]) -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], None]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | None" assert isinstance(x, list) reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]" def bar(x: Union[List[str], List[int], None]) -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], builtins.list[builtins.int], None]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int] | None" assert isinstance(x, list) - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int]" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceWithStarExpression] from typing import Union, List, Tuple def f(var: Union[List[str], Tuple[str, str], str]) -> None: - reveal_type(var) # N: Revealed type is "Union[builtins.list[builtins.str], tuple[builtins.str, builtins.str], builtins.str]" + reveal_type(var) # N: Revealed type is "builtins.list[builtins.str] | tuple[builtins.str, builtins.str] | builtins.str" if isinstance(var, (list, *(str, int))): - reveal_type(var) # N: Revealed type is "Union[builtins.list[builtins.str], builtins.str]" + reveal_type(var) # N: Revealed type is "builtins.list[builtins.str] | builtins.str" [builtins fixtures/isinstancelist.pyi] [case testIsInstanceWithStarExpressionAndVariable] from typing import Union def f(var: Union[int, str]) -> None: - reveal_type(var) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(var) # N: Revealed type is "builtins.int | builtins.str" some_types = (str, tuple) another_type = list if isinstance(var, (*some_types, another_type)): @@ -2440,7 +2440,7 @@ class B: def t3(self) -> None: if isinstance(self, (A1, A2)): - reveal_type(self) # N: Revealed type is "Union[__main__., __main__.]" + reveal_type(self) # N: Revealed type is "__main__. | __main__." x0: Literal[0] = self.f() # E: Incompatible types in assignment (expression has type "Literal[1, 2]", variable has type "Literal[0]") x1: Literal[1] = self.f() # E: Incompatible types in assignment (expression has type "Literal[1, 2]", variable has type "Literal[1]") @@ -2625,15 +2625,15 @@ class D: pass v1: A if isinstance(v1, (B, C)): - reveal_type(v1) # N: Revealed type is "Union[__main__., __main__.]" + reveal_type(v1) # N: Revealed type is "__main__. | __main__." v2: Union[A, B] if isinstance(v2, C): - reveal_type(v2) # N: Revealed type is "Union[__main__., __main__.]" + reveal_type(v2) # N: Revealed type is "__main__. | __main__." v3: Union[A, B] if isinstance(v3, (C, D)): - reveal_type(v3) # N: Revealed type is "Union[__main__., __main__., __main__., __main__.]" + reveal_type(v3) # N: Revealed type is "__main__. | __main__. | __main__. | __main__." [builtins fixtures/isinstance.pyi] [case testIsInstanceAdHocIntersectionSameNames] @@ -2757,7 +2757,7 @@ x: Union[int, str] if type(x) is int: reveal_type(x) # N: Revealed type is "builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testTypeEqualsMultipleTypesShouldntNarrow] # make sure we don't do any narrowing if there are multiple types being compared @@ -2766,9 +2766,9 @@ from typing import Union x: Union[int, str] if type(x) == int == str: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" # mypy thinks int isn't defined unless we include this [builtins fixtures/primitives.pyi] @@ -2778,7 +2778,7 @@ from typing import Union x: Union[int, str] if type(x) != int: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: reveal_type(x) # N: Revealed type is "builtins.int" @@ -2790,7 +2790,7 @@ from typing import Union x: Union[int, str] if type(x) is not int: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: reveal_type(x) # N: Revealed type is "builtins.int" @@ -2922,8 +2922,8 @@ class B: xu: Union[A, B] if hasattr(xu, "x"): - reveal_type(xu) # N: Revealed type is "Union[__main__.A, __main__.B]" - reveal_type(xu.x) # N: Revealed type is "Union[Any, builtins.int]" + reveal_type(xu) # N: Revealed type is "__main__.A | __main__.B" + reveal_type(xu.x) # N: Revealed type is "Any | builtins.int" else: reveal_type(xu) # N: Revealed type is "__main__.A" [builtins fixtures/isinstance.pyi] @@ -3013,13 +3013,13 @@ from typing import Any var: Any reveal_type(var) # N: Revealed type is "Any" assert isinstance(var, (bool, str)) -reveal_type(var) # N: Revealed type is "Union[builtins.bool, builtins.str]" +reveal_type(var) # N: Revealed type is "builtins.bool | builtins.str" if isinstance(var, bool): reveal_type(var) # N: Revealed type is "builtins.bool" # Type of var shouldn't fall back to Any -reveal_type(var) # N: Revealed type is "Union[builtins.bool, builtins.str]" +reveal_type(var) # N: Revealed type is "builtins.bool | builtins.str" [builtins fixtures/isinstance.pyi] [case testReuseIntersectionForRepeatedIsinstanceCalls] diff --git a/test-data/unit/check-kwargs.test b/test-data/unit/check-kwargs.test index 708b6662611e..7c9af8eeb11b 100644 --- a/test-data/unit/check-kwargs.test +++ b/test-data/unit/check-kwargs.test @@ -34,10 +34,10 @@ f(a=A()) f(b=B()) f(c=C()) f(b=B(), c=C()) -f(a=B()) # E: Argument "a" to "f" has incompatible type "B"; expected "Optional[A]" -f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "Optional[B]" -f(c=B()) # E: Argument "c" to "f" has incompatible type "B"; expected "Optional[C]" -f(b=B(), c=A()) # E: Argument "c" to "f" has incompatible type "A"; expected "Optional[C]" +f(a=B()) # E: Argument "a" to "f" has incompatible type "B"; expected "A | None" +f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" +f(c=B()) # E: Argument "c" to "f" has incompatible type "B"; expected "C | None" +f(b=B(), c=A()) # E: Argument "c" to "f" has incompatible type "A"; expected "C | None" [case testBothPositionalAndKeywordArguments] import typing class A: pass @@ -230,7 +230,7 @@ f(A(), b=B()) f(A(), A(), b=B()) f(B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A" f(A(), B()) # E: Argument 2 to "f" has incompatible type "B"; expected "A" -f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "Optional[B]" +f(b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" [builtins fixtures/list.pyi] [case testKeywordArgAfterVarArgsWithBothCallerAndCalleeVarArgs] @@ -246,8 +246,8 @@ f(b=B()) f(*a, b=B()) f(A(), *a, b=B()) f(A(), B()) # E: Argument 2 to "f" has incompatible type "B"; expected "A" -f(A(), b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "Optional[B]" -f(*a, b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "Optional[B]" +f(A(), b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" +f(*a, b=A()) # E: Argument "b" to "f" has incompatible type "A"; expected "B | None" [builtins fixtures/list.pyi] [case testCallingDynamicallyTypedFunctionWithKeywordArgs] @@ -357,7 +357,7 @@ d = {} # type: Dict[A, A] f(**d) # E: Keywords must be strings f(**A()) # E: Argument after ** must be a mapping, not "A" kwargs: Optional[Any] -f(**kwargs) # E: Argument after ** must be a mapping, not "Optional[Any]" +f(**kwargs) # E: Argument after ** must be a mapping, not "Any | None" def g(a: int) -> None: pass g(a=1, **4) # E: Argument after ** must be a mapping, not "int" diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index ce0ae2844bae..d4774420ad89 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -16,7 +16,7 @@ def h2(x: 'A|int') -> None: pass # E: Name "A" is not defined def i2(x: Literal['A|B']) -> None: pass reveal_type(f2) # N: Revealed type is "def (x: Any)" reveal_type(g2) # N: Revealed type is "def (x: Literal['A B'])" -reveal_type(h2) # N: Revealed type is "def (x: Union[Any, builtins.int])" +reveal_type(h2) # N: Revealed type is "def (x: Any | builtins.int)" reveal_type(i2) # N: Revealed type is "def (x: Literal['A|B'])" [builtins fixtures/tuple.pyi] [out] @@ -88,8 +88,8 @@ y = None # type: Optional[Tuple[Literal[2]]] def bar(x): # type: (Tuple[Literal[2]]) -> None pass -reveal_type(x) # N: Revealed type is "Union[tuple[Any], None]" -reveal_type(y) # N: Revealed type is "Union[tuple[Literal[2]], None]" +reveal_type(x) # N: Revealed type is "tuple[Any] | None" +reveal_type(y) # N: Revealed type is "tuple[Literal[2]] | None" reveal_type(bar) # N: Revealed type is "def (x: tuple[Literal[2]])" [builtins fixtures/tuple.pyi] [out] @@ -677,14 +677,14 @@ b: Literal["a", "b", "c"] c: Literal[1, "b", True, None] d: Literal[1, 1, 1] e: Literal[None, None, None] -reveal_type(a) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3]]" -reveal_type(b) # N: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]" -reveal_type(c) # N: Revealed type is "Union[Literal[1], Literal['b'], Literal[True], None]" +reveal_type(a) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3]" +reveal_type(b) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']" +reveal_type(c) # N: Revealed type is "Literal[1] | Literal['b'] | Literal[True] | None" # Note: I was thinking these should be simplified, but it seems like # mypy doesn't simplify unions with duplicate values with other types. -reveal_type(d) # N: Revealed type is "Union[Literal[1], Literal[1], Literal[1]]" -reveal_type(e) # N: Revealed type is "Union[None, None, None]" +reveal_type(d) # N: Revealed type is "Literal[1] | Literal[1] | Literal[1]" +reveal_type(e) # N: Revealed type is "None | None | None" [builtins fixtures/bool.pyi] [out] @@ -694,8 +694,8 @@ from typing import Literal # Literal[1, 2, 3]. So we treat the two as being equivalent for now. a: Literal[1, 2, 3] b: Literal[(1, 2, 3)] -reveal_type(a) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3]]" -reveal_type(b) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3]]" +reveal_type(a) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3]" +reveal_type(b) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3]" [builtins fixtures/tuple.pyi] [out] @@ -703,7 +703,7 @@ reveal_type(b) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3]] from typing import Literal a: Literal[Literal[3], 4, Literal["foo"]] -reveal_type(a) # N: Revealed type is "Union[Literal[3], Literal[4], Literal['foo']]" +reveal_type(a) # N: Revealed type is "Literal[3] | Literal[4] | Literal['foo']" alias_for_literal = Literal[5] b: Literal[alias_for_literal] @@ -711,12 +711,12 @@ reveal_type(b) # N: Revealed type is "Literal[5]" another_alias = Literal[1, None] c: Literal[alias_for_literal, another_alias, "r"] -reveal_type(c) # N: Revealed type is "Union[Literal[5], Literal[1], None, Literal['r']]" +reveal_type(c) # N: Revealed type is "Literal[5] | Literal[1] | None | Literal['r']" basic_mode = Literal["r", "w", "a"] basic_with_plus = Literal["r+", "w+", "a+"] combined: Literal[basic_mode, basic_with_plus] -reveal_type(combined) # N: Revealed type is "Union[Literal['r'], Literal['w'], Literal['a'], Literal['r+'], Literal['w+'], Literal['a+']]" +reveal_type(combined) # N: Revealed type is "Literal['r'] | Literal['w'] | Literal['a'] | Literal['r+'] | Literal['w+'] | Literal['a+']" [builtins fixtures/tuple.pyi] [out] @@ -754,7 +754,7 @@ d: "Literal['Foo']" reveal_type(d) # N: Revealed type is "Literal['Foo']" e: Literal[Foo, 'Foo'] -reveal_type(e) # N: Revealed type is "Union[Literal[5], Literal['Foo']]" +reveal_type(e) # N: Revealed type is "Literal[5] | Literal['Foo']" Foo = Literal[5] [builtins fixtures/tuple.pyi] @@ -1034,9 +1034,9 @@ x = b # E: Incompatible types in assignment (expression has type "str", variabl y = c # E: Incompatible types in assignment (expression has type "bool", variable has type "Literal[True]") z = d # This is ok: Literal[None] and None are equivalent. -combined = a # E: Incompatible types in assignment (expression has type "int", variable has type "Optional[Literal[1, 'foo', True]]") -combined = b # E: Incompatible types in assignment (expression has type "str", variable has type "Optional[Literal[1, 'foo', True]]") -combined = c # E: Incompatible types in assignment (expression has type "bool", variable has type "Optional[Literal[1, 'foo', True]]") +combined = a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[1, 'foo', True] | None") +combined = b # E: Incompatible types in assignment (expression has type "str", variable has type "Literal[1, 'foo', True] | None") +combined = c # E: Incompatible types in assignment (expression has type "bool", variable has type "Literal[1, 'foo', True] | None") combined = d # Also ok, for similar reasons. e: Literal[1] = 1 @@ -1151,11 +1151,11 @@ h: List[Literal[1]] = [] reveal_type(a) # N: Revealed type is "builtins.list[Literal[1]]" reveal_type(b) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(c) # N: Revealed type is "builtins.list[Union[Literal[1], Literal[2], Literal[3]]]" +reveal_type(c) # N: Revealed type is "builtins.list[Literal[1] | Literal[2] | Literal[3]]" reveal_type(d) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(e) # N: Revealed type is "builtins.list[Union[Literal[1], Literal['x']]]" +reveal_type(e) # N: Revealed type is "builtins.list[Literal[1] | Literal['x']]" reveal_type(f) # N: Revealed type is "builtins.list[builtins.object]" -reveal_type(g) # N: Revealed type is "builtins.list[builtins.list[builtins.list[Union[Literal[1], Literal[2], Literal[3]]]]]" +reveal_type(g) # N: Revealed type is "builtins.list[builtins.list[builtins.list[Literal[1] | Literal[2] | Literal[3]]]]" reveal_type(h) # N: Revealed type is "builtins.list[Literal[1]]" lit1: Literal[1] @@ -1280,9 +1280,9 @@ d: Literal[6, 7] e: int f: Literal[7, "bar"] -reveal_type(func(a)) # N: Revealed type is "Union[__main__.A, __main__.C]" +reveal_type(func(a)) # N: Revealed type is "__main__.A | __main__.C" reveal_type(func(b)) # N: Revealed type is "__main__.B" -reveal_type(func(c)) # N: Revealed type is "Union[__main__.B, __main__.A]" +reveal_type(func(c)) # N: Revealed type is "__main__.B | __main__.A" reveal_type(func(d)) # N: Revealed type is "__main__.B" \ # E: Argument 1 to "func" has incompatible type "Literal[6, 7]"; expected "Literal[3, 4, 5, 6]" @@ -1477,13 +1477,13 @@ Alias = Literal[3] isinstance(3, Literal[3]) # E: Cannot use isinstance() with Literal type isinstance(3, Alias) # E: Cannot use isinstance() with Literal type \ - # E: Argument 2 to "isinstance" has incompatible type ""; expected "Union[type, tuple[Any, ...]]" + # E: Argument 2 to "isinstance" has incompatible type ""; expected "type | tuple[Any, ...]" isinstance(3, Renamed[3]) # E: Cannot use isinstance() with Literal type isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with Literal type issubclass(int, Literal[3]) # E: Cannot use issubclass() with Literal type issubclass(int, Alias) # E: Cannot use issubclass() with Literal type \ - # E: Argument 2 to "issubclass" has incompatible type ""; expected "Union[type, tuple[Any, ...]]" + # E: Argument 2 to "issubclass" has incompatible type ""; expected "type | tuple[Any, ...]" issubclass(int, Renamed[3]) # E: Cannot use issubclass() with Literal type issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with Literal type [builtins fixtures/isinstancelist.pyi] @@ -1568,7 +1568,7 @@ T = TypeVar('T') def identity(x: T) -> T: return x a: Union[int, Literal['foo']] = identity('foo') -b: Union[int, Literal['foo']] = identity('bar') # E: Argument 1 to "identity" has incompatible type "Literal['bar']"; expected "Union[int, Literal['foo']]" +b: Union[int, Literal['foo']] = identity('bar') # E: Argument 1 to "identity" has incompatible type "Literal['bar']"; expected "int | Literal['foo']" [builtins fixtures/tuple.pyi] [out] @@ -1836,16 +1836,16 @@ idx_final: Final = 2 tup1: Tuple[A, B, Optional[C], D, E] reveal_type(tup1[idx0]) # N: Revealed type is "__main__.A" reveal_type(tup1[idx1]) # N: Revealed type is "__main__.B" -reveal_type(tup1[idx2]) # N: Revealed type is "Union[__main__.C, None]" -reveal_type(tup1[idx_final]) # N: Revealed type is "Union[__main__.C, None]" +reveal_type(tup1[idx2]) # N: Revealed type is "__main__.C | None" +reveal_type(tup1[idx_final]) # N: Revealed type is "__main__.C | None" reveal_type(tup1[idx3]) # N: Revealed type is "__main__.D" reveal_type(tup1[idx4]) # N: Revealed type is "__main__.E" reveal_type(tup1[idx_neg1]) # N: Revealed type is "__main__.E" tup1[idx5] # E: Tuple index out of range -reveal_type(tup1[idx2:idx4]) # N: Revealed type is "tuple[Union[__main__.C, None], __main__.D]" -reveal_type(tup1[::idx2]) # N: Revealed type is "tuple[__main__.A, Union[__main__.C, None], __main__.E]" +reveal_type(tup1[idx2:idx4]) # N: Revealed type is "tuple[__main__.C | None, __main__.D]" +reveal_type(tup1[::idx2]) # N: Revealed type is "tuple[__main__.A, __main__.C | None, __main__.E]" if tup1[idx2] is not None: - reveal_type(tup1[idx2]) # N: Revealed type is "Union[__main__.C, None]" + reveal_type(tup1[idx2]) # N: Revealed type is "__main__.C | None" if tup1[idx_final] is not None: reveal_type(tup1[idx_final]) # N: Revealed type is "__main__.C" @@ -1885,7 +1885,7 @@ reveal_type(d[b_key]) # N: Revealed type is "builtins.str" d[c_key] # E: TypedDict "Outer" has no key "c" reveal_type(d.get(a_key, u)) # N: Revealed type is "builtins.int" -reveal_type(d.get(b_key, u)) # N: Revealed type is "Union[builtins.str, __main__.Unrelated]" +reveal_type(d.get(b_key, u)) # N: Revealed type is "builtins.str | __main__.Unrelated" reveal_type(d.get(c_key, u)) # N: Revealed type is "builtins.object" reveal_type(d.pop(a_key)) # N: Revealed type is "builtins.int" \ @@ -1955,14 +1955,14 @@ tup1: Tuple[A, B, C, D, E] Tup2Class = NamedTuple('Tup2Class', [('a', A), ('b', B), ('c', C), ('d', D), ('e', E)]) tup2: Tup2Class -reveal_type(tup1[idx1]) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(tup1[idx1:idx2]) # N: Revealed type is "Union[tuple[__main__.B, __main__.C], tuple[__main__.B, __main__.C, __main__.D], tuple[__main__.C], tuple[__main__.C, __main__.D]]" -reveal_type(tup1[0::idx1]) # N: Revealed type is "Union[tuple[__main__.A, __main__.B, __main__.C, __main__.D, __main__.E], tuple[__main__.A, __main__.C, __main__.E]]" +reveal_type(tup1[idx1]) # N: Revealed type is "__main__.B | __main__.C" +reveal_type(tup1[idx1:idx2]) # N: Revealed type is "tuple[__main__.B, __main__.C] | tuple[__main__.B, __main__.C, __main__.D] | tuple[__main__.C] | tuple[__main__.C, __main__.D]" +reveal_type(tup1[0::idx1]) # N: Revealed type is "tuple[__main__.A, __main__.B, __main__.C, __main__.D, __main__.E] | tuple[__main__.A, __main__.C, __main__.E]" tup1[idx_bad] # E: Tuple index out of range -reveal_type(tup2[idx1]) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(tup2[idx1:idx2]) # N: Revealed type is "Union[tuple[__main__.B, __main__.C], tuple[__main__.B, __main__.C, __main__.D], tuple[__main__.C], tuple[__main__.C, __main__.D]]" -reveal_type(tup2[0::idx1]) # N: Revealed type is "Union[tuple[__main__.A, __main__.B, __main__.C, __main__.D, __main__.E], tuple[__main__.A, __main__.C, __main__.E]]" +reveal_type(tup2[idx1]) # N: Revealed type is "__main__.B | __main__.C" +reveal_type(tup2[idx1:idx2]) # N: Revealed type is "tuple[__main__.B, __main__.C] | tuple[__main__.B, __main__.C, __main__.D] | tuple[__main__.C] | tuple[__main__.C, __main__.D]" +reveal_type(tup2[0::idx1]) # N: Revealed type is "tuple[__main__.A, __main__.B, __main__.C, __main__.D, __main__.E] | tuple[__main__.A, __main__.C, __main__.E]" tup2[idx_bad] # E: Tuple index out of range [builtins fixtures/slice.pyi] [out] @@ -1992,12 +1992,12 @@ good_keys: Literal["a", "b"] optional_keys: Literal["d", "e"] bad_keys: Literal["a", "bad"] -reveal_type(test[good_keys]) # N: Revealed type is "Union[__main__.A, __main__.B]" -reveal_type(test.get(good_keys)) # N: Revealed type is "Union[__main__.A, __main__.B]" -reveal_type(test.get(good_keys, 3)) # N: Revealed type is "Union[__main__.A, __main__.B]" -reveal_type(test.pop(optional_keys)) # N: Revealed type is "Union[__main__.D, __main__.E]" -reveal_type(test.pop(optional_keys, 3)) # N: Revealed type is "Union[__main__.D, __main__.E, Literal[3]?]" -reveal_type(test.setdefault(good_keys, AAndB())) # N: Revealed type is "Union[__main__.A, __main__.B]" +reveal_type(test[good_keys]) # N: Revealed type is "__main__.A | __main__.B" +reveal_type(test.get(good_keys)) # N: Revealed type is "__main__.A | __main__.B" +reveal_type(test.get(good_keys, 3)) # N: Revealed type is "__main__.A | __main__.B" +reveal_type(test.pop(optional_keys)) # N: Revealed type is "__main__.D | __main__.E" +reveal_type(test.pop(optional_keys, 3)) # N: Revealed type is "__main__.D | __main__.E | Literal[3]?" +reveal_type(test.setdefault(good_keys, AAndB())) # N: Revealed type is "__main__.A | __main__.B" reveal_type(test.get(bad_keys)) # N: Revealed type is "builtins.object" reveal_type(test.get(bad_keys, 3)) # N: Revealed type is "builtins.object" del test[optional_keys] @@ -2044,9 +2044,9 @@ bad_keys: Literal['e', 'f'] x[mixed_keys] # E: TypedDict "D1" has no key "d" \ # E: TypedDict "D2" has no key "a" -reveal_type(x[good_keys]) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(x.get(good_keys)) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(x.get(good_keys, 3)) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(x[good_keys]) # N: Revealed type is "__main__.B | __main__.C" +reveal_type(x.get(good_keys)) # N: Revealed type is "__main__.B | __main__.C" +reveal_type(x.get(good_keys, 3)) # N: Revealed type is "__main__.B | __main__.C" reveal_type(x.get(mixed_keys)) # N: Revealed type is "builtins.object" reveal_type(x.get(mixed_keys, 3)) # N: Revealed type is "builtins.object" reveal_type(x.get(bad_keys)) # N: Revealed type is "builtins.object" @@ -2230,7 +2230,7 @@ from typing import Final var1: Final = [0, None] var2: Final = (0, None) -reveal_type(var1) # N: Revealed type is "builtins.list[Union[builtins.int, None]]" +reveal_type(var1) # N: Revealed type is "builtins.list[builtins.int | None]" reveal_type(var2) # N: Revealed type is "tuple[Literal[0]?, None]" [builtins fixtures/tuple.pyi] @@ -2839,28 +2839,28 @@ else: y: Union[Truth, AlsoTruth, Lie] if y: - reveal_type(y) # N: Revealed type is "Union[__main__.Truth, __main__.AlsoTruth]" + reveal_type(y) # N: Revealed type is "__main__.Truth | __main__.AlsoTruth" else: reveal_type(y) # N: Revealed type is "__main__.Lie" z: Union[Truth, AnyAnswer] if z: - reveal_type(z) # N: Revealed type is "Union[__main__.Truth, __main__.AnyAnswer]" + reveal_type(z) # N: Revealed type is "__main__.Truth | __main__.AnyAnswer" else: reveal_type(z) # N: Revealed type is "__main__.AnyAnswer" q: Union[Truth, NoAnswerSpecified] if q: - reveal_type(q) # N: Revealed type is "Union[__main__.Truth, __main__.NoAnswerSpecified]" + reveal_type(q) # N: Revealed type is "__main__.Truth | __main__.NoAnswerSpecified" else: reveal_type(q) # N: Revealed type is "__main__.NoAnswerSpecified" w: Union[Truth, AlsoTruth] if w: - reveal_type(w) # N: Revealed type is "Union[__main__.Truth, __main__.AlsoTruth]" + reveal_type(w) # N: Revealed type is "__main__.Truth | __main__.AlsoTruth" else: reveal_type(w) # E: Statement is unreachable @@ -2879,7 +2879,7 @@ def f() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]: else: return (False, 'oops') -reveal_type(f()) # N: Revealed type is "Union[tuple[Literal[True], builtins.int], tuple[Literal[False], builtins.str]]" +reveal_type(f()) # N: Revealed type is "tuple[Literal[True], builtins.int] | tuple[Literal[False], builtins.str]" def does_work() -> Tuple[Literal[1]]: x: Final = (1,) @@ -2899,15 +2899,15 @@ def invalid_literal_type() -> Tuple[Literal[1]]: def incorrect_return1() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]: if x: - return (False, 5) # E: Incompatible return value type (got "tuple[bool, int]", expected "Union[tuple[Literal[True], int], tuple[Literal[False], str]]") + return (False, 5) # E: Incompatible return value type (got "tuple[bool, int]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") else: - return (True, 'oops') # E: Incompatible return value type (got "tuple[bool, str]", expected "Union[tuple[Literal[True], int], tuple[Literal[False], str]]") + return (True, 'oops') # E: Incompatible return value type (got "tuple[bool, str]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") def incorrect_return2() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]: if x: - return (bool(), 5) # E: Incompatible return value type (got "tuple[bool, int]", expected "Union[tuple[Literal[True], int], tuple[Literal[False], str]]") + return (bool(), 5) # E: Incompatible return value type (got "tuple[bool, int]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") else: - return (bool(), 'oops') # E: Incompatible return value type (got "tuple[bool, str]", expected "Union[tuple[Literal[True], int], tuple[Literal[False], str]]") + return (bool(), 'oops') # E: Incompatible return value type (got "tuple[bool, str]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]") [builtins fixtures/bool.pyi] [case testLiteralSubtypeContext] @@ -2929,7 +2929,7 @@ class A: class B(A): foo = ['spam'] -reveal_type(B().foo) # N: Revealed type is "builtins.list[Union[Literal['bar'], Literal['spam']]]" +reveal_type(B().foo) # N: Revealed type is "builtins.list[Literal['bar'] | Literal['spam']]" [builtins fixtures/tuple.pyi] [case testLiteralSubtypeContextGeneric] @@ -2968,7 +2968,7 @@ class C(Base): ) -> int: if sep is None: sep = "a" if int() else "b" - reveal_type(sep) # N: Revealed type is "Union[Literal['a'], Literal['b']]" + reveal_type(sep) # N: Revealed type is "Literal['a'] | Literal['b']" return super().feed_data(sep) [builtins fixtures/primitives.pyi] diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index a9070247c97d..8faacb1a95f8 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -534,7 +534,7 @@ Both = Union[int, str] def foo(x: int, y: int = ...) -> int: ... @overload def foo(x: str, y: str = ...) -> str: ... -def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for argument "y" (default has type "ellipsis", argument has type "Union[int, str]") +def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for argument "y" (default has type "ellipsis", argument has type "int | str") return x @overload @@ -2581,7 +2581,7 @@ reveal_type(x) class Two: pass [out] -tmp/m/two.py:3: note: Revealed type is "Union[builtins.int, builtins.str]" +tmp/m/two.py:3: note: Revealed type is "builtins.int | builtins.str" [case testImportCycleSpecialCase] import p diff --git a/test-data/unit/check-namedtuple.test b/test-data/unit/check-namedtuple.test index 6be3f2b3e953..a24a3964692f 100644 --- a/test-data/unit/check-namedtuple.test +++ b/test-data/unit/check-namedtuple.test @@ -144,7 +144,7 @@ X(0, 1) # ok X(0, 1, 2) # E: Too many arguments for "X" Y = namedtuple('Y', ['x', 'y'], defaults=(1, 2, 3)) # E: Too many defaults given in call to "namedtuple()" -Z = namedtuple('Z', ['x', 'y'], defaults='not a tuple') # E: List or tuple literal expected as the defaults argument to namedtuple() # E: Argument "defaults" to "namedtuple" has incompatible type "str"; expected "Optional[Iterable[Any]]" +Z = namedtuple('Z', ['x', 'y'], defaults='not a tuple') # E: List or tuple literal expected as the defaults argument to namedtuple() # E: Argument "defaults" to "namedtuple" has incompatible type "str"; expected "Iterable[Any] | None" [builtins fixtures/list.pyi] @@ -764,13 +764,13 @@ def test() -> None: class B(NamedTuple('B', [('val', object)])): pass exp: Exp - reveal_type(exp) # N: Revealed type is "Union[Any, tuple[builtins.object, fallback=__main__.B@6]]" + reveal_type(exp) # N: Revealed type is "Any | tuple[builtins.object, fallback=__main__.B@6]" if isinstance(exp, A): - reveal_type(exp[0][0]) # N: Revealed type is "Union[Any, tuple[builtins.object, fallback=__main__.B@6]]" - reveal_type(exp.attr[0]) # N: Revealed type is "Union[Any, tuple[builtins.object, fallback=__main__.B@6]]" + reveal_type(exp[0][0]) # N: Revealed type is "Any | tuple[builtins.object, fallback=__main__.B@6]" + reveal_type(exp.attr[0]) # N: Revealed type is "Any | tuple[builtins.object, fallback=__main__.B@6]" if isinstance(exp, B): reveal_type(exp.val) # N: Revealed type is "builtins.object" - reveal_type(A([B(1), B(2)])) # N: Revealed type is "tuple[builtins.list[Union[Any, tuple[builtins.object, fallback=__main__.B@6]]], fallback=__main__.A@5]" + reveal_type(A([B(1), B(2)])) # N: Revealed type is "tuple[builtins.list[Any | tuple[builtins.object, fallback=__main__.B@6]], fallback=__main__.A@5]" [builtins fixtures/isinstancelist.pyi] [out] @@ -1241,7 +1241,7 @@ nti: NT[int] reveal_type(nti * x) # N: Revealed type is "builtins.tuple[builtins.int, ...]" nts: NT[str] -reveal_type(nts * x) # N: Revealed type is "builtins.tuple[Union[builtins.int, builtins.str], ...]" +reveal_type(nts * x) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-namedtuple.pyi] @@ -1302,9 +1302,9 @@ reveal_type(foo(nti, nts)) # N: Revealed type is "tuple[builtins.int, builtins. reveal_type(foo(nts, nti)) # N: Revealed type is "tuple[builtins.int, builtins.object, fallback=__main__.NT[builtins.object]]" reveal_type(foo(nti, x)) # N: Revealed type is "builtins.tuple[builtins.int, ...]" -reveal_type(foo(nts, x)) # N: Revealed type is "builtins.tuple[Union[builtins.int, builtins.str], ...]" +reveal_type(foo(nts, x)) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]" reveal_type(foo(x, nti)) # N: Revealed type is "builtins.tuple[builtins.int, ...]" -reveal_type(foo(x, nts)) # N: Revealed type is "builtins.tuple[Union[builtins.int, builtins.str], ...]" +reveal_type(foo(x, nts)) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-namedtuple.pyi] @@ -1499,7 +1499,7 @@ def g(x: Union[A, B, str]) -> Union[A, B, str]: if isinstance(x, str): return x else: - reveal_type(x) # N: Revealed type is "Union[tuple[tuple[builtins.str, fallback=__main__.AKey], fallback=__main__.A], tuple[tuple[builtins.str, fallback=__main__.BKey], fallback=__main__.B]]" + reveal_type(x) # N: Revealed type is "tuple[tuple[builtins.str, fallback=__main__.AKey], fallback=__main__.A] | tuple[tuple[builtins.str, fallback=__main__.BKey], fallback=__main__.B]" return x._replace() # no errors should be raised above. @@ -1516,7 +1516,7 @@ class C(T): return True c: Union[C, Any] -reveal_type(c.f()) # N: Revealed type is "Union[builtins.bool, Any]" +reveal_type(c.f()) # N: Revealed type is "builtins.bool | Any" [builtins fixtures/tuple.pyi] [case testNamedTupleAsClassMemberNoCrash] diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index 00d33c86414f..8dd8ba01eca8 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -255,17 +255,17 @@ x: Union[Object1, Object2] if x.key is Key.A: reveal_type(x) # N: Revealed type is "__main__.Object1" else: - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, __main__.Object2]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | __main__.Object2" if x.key is Key.C: - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, __main__.Object2]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | __main__.Object2" else: - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, __main__.Object2]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | __main__.Object2" if x.key is Key.D: reveal_type(x) # E: Statement is unreachable else: - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, __main__.Object2]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | __main__.Object2" [builtins fixtures/tuple.pyi] [case testNarrowingTypedDictParentMultipleKeys] @@ -279,19 +279,19 @@ class TypedDict2(TypedDict): x: Union[TypedDict1, TypedDict2] if x['key'] == 'A': - reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key': Union[Literal['A'], Literal['C']]})" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key': Literal['A'] | Literal['C']})" else: - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key': Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key': Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key': Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key': Literal['B'] | Literal['C']})" if x['key'] == 'C': - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key': Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key': Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key': Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key': Literal['B'] | Literal['C']})" else: - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key': Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key': Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key': Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key': Literal['B'] | Literal['C']})" if x['key'] == 'D': reveal_type(x) # E: Statement is unreachable else: - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key': Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key': Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key': Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key': Literal['B'] | Literal['C']})" [builtins fixtures/primitives.pyi] [typing fixtures/typing-typeddict.pyi] @@ -306,19 +306,19 @@ class TypedDict2(TypedDict, total=False): x: Union[TypedDict1, TypedDict2] if x['key'] == 'A': - reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key'?: Union[Literal['A'], Literal['C']]})" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key'?: Literal['A'] | Literal['C']})" else: - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key'?: Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key'?: Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key'?: Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key'?: Literal['B'] | Literal['C']})" if x['key'] == 'C': - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key'?: Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key'?: Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key'?: Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key'?: Literal['B'] | Literal['C']})" else: - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key'?: Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key'?: Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key'?: Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key'?: Literal['B'] | Literal['C']})" if x['key'] == 'D': reveal_type(x) # E: Statement is unreachable else: - reveal_type(x) # N: Revealed type is "Union[TypedDict('__main__.TypedDict1', {'key'?: Union[Literal['A'], Literal['C']]}), TypedDict('__main__.TypedDict2', {'key'?: Union[Literal['B'], Literal['C']]})]" + reveal_type(x) # N: Revealed type is "TypedDict('__main__.TypedDict1', {'key'?: Literal['A'] | Literal['C']}) | TypedDict('__main__.TypedDict2', {'key'?: Literal['B'] | Literal['C']})" [builtins fixtures/primitives.pyi] [typing fixtures/typing-typeddict.pyi] @@ -339,13 +339,13 @@ class Y(TypedDict): unknown: Union[X, Y] if unknown['inner']['key'] == 'A': - reveal_type(unknown) # N: Revealed type is "TypedDict('__main__.X', {'inner': Union[TypedDict('__main__.A', {'key': Literal['A']}), TypedDict('__main__.B', {'key': Literal['B']})]})" + reveal_type(unknown) # N: Revealed type is "TypedDict('__main__.X', {'inner': TypedDict('__main__.A', {'key': Literal['A']}) | TypedDict('__main__.B', {'key': Literal['B']})})" reveal_type(unknown['inner']) # N: Revealed type is "TypedDict('__main__.A', {'key': Literal['A']})" if unknown['inner']['key'] == 'B': - reveal_type(unknown) # N: Revealed type is "Union[TypedDict('__main__.X', {'inner': Union[TypedDict('__main__.A', {'key': Literal['A']}), TypedDict('__main__.B', {'key': Literal['B']})]}), TypedDict('__main__.Y', {'inner': Union[TypedDict('__main__.B', {'key': Literal['B']}), TypedDict('__main__.C', {'key': Literal['C']})]})]" + reveal_type(unknown) # N: Revealed type is "TypedDict('__main__.X', {'inner': TypedDict('__main__.A', {'key': Literal['A']}) | TypedDict('__main__.B', {'key': Literal['B']})}) | TypedDict('__main__.Y', {'inner': TypedDict('__main__.B', {'key': Literal['B']}) | TypedDict('__main__.C', {'key': Literal['C']})})" reveal_type(unknown['inner']) # N: Revealed type is "TypedDict('__main__.B', {'key': Literal['B']})" if unknown['inner']['key'] == 'C': - reveal_type(unknown) # N: Revealed type is "TypedDict('__main__.Y', {'inner': Union[TypedDict('__main__.B', {'key': Literal['B']}), TypedDict('__main__.C', {'key': Literal['C']})]})" + reveal_type(unknown) # N: Revealed type is "TypedDict('__main__.Y', {'inner': TypedDict('__main__.B', {'key': Literal['B']}) | TypedDict('__main__.C', {'key': Literal['C']})})" reveal_type(unknown['inner']) # N: Revealed type is "TypedDict('__main__.C', {'key': Literal['C']})" [builtins fixtures/primitives.pyi] [typing fixtures/typing-typeddict.pyi] @@ -372,12 +372,12 @@ x: Union[Object1, Object2, Object3, Object4] if x.key is Key.A: reveal_type(x) # N: Revealed type is "__main__.Object1" else: - reveal_type(x) # N: Revealed type is "Union[__main__.Object2, __main__.Object3, __main__.Object4]" + reveal_type(x) # N: Revealed type is "__main__.Object2 | __main__.Object3 | __main__.Object4" if isinstance(x.key, str): reveal_type(x) # N: Revealed type is "__main__.Object4" else: - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, __main__.Object2, __main__.Object3]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | __main__.Object2 | __main__.Object3" [builtins fixtures/isinstance.pyi] [case testNarrowingParentsWithGenerics] @@ -417,29 +417,29 @@ else: reveal_type(ok_mixture) # N: Revealed type is "tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]" impossible_mixture: Union[KeyedObject, KeyedTypedDict] -if impossible_mixture.key is Key.A: # E: Item "KeyedTypedDict" of "Union[KeyedObject, KeyedTypedDict]" has no attribute "key" - reveal_type(impossible_mixture) # N: Revealed type is "Union[__main__.KeyedObject, TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})]" +if impossible_mixture.key is Key.A: # E: Item "KeyedTypedDict" of "KeyedObject | KeyedTypedDict" has no attribute "key" + reveal_type(impossible_mixture) # N: Revealed type is "__main__.KeyedObject | TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})" else: - reveal_type(impossible_mixture) # N: Revealed type is "Union[__main__.KeyedObject, TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})]" + reveal_type(impossible_mixture) # N: Revealed type is "__main__.KeyedObject | TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})" -if impossible_mixture["key"] is Key.A: # E: Value of type "Union[KeyedObject, KeyedTypedDict]" is not indexable - reveal_type(impossible_mixture) # N: Revealed type is "Union[__main__.KeyedObject, TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})]" +if impossible_mixture["key"] is Key.A: # E: Value of type "KeyedObject | KeyedTypedDict" is not indexable + reveal_type(impossible_mixture) # N: Revealed type is "__main__.KeyedObject | TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})" else: - reveal_type(impossible_mixture) # N: Revealed type is "Union[__main__.KeyedObject, TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})]" + reveal_type(impossible_mixture) # N: Revealed type is "__main__.KeyedObject | TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]})" weird_mixture: Union[KeyedTypedDict, KeyedNamedTuple] if weird_mixture["key"] is Key.B: # E: No overload variant of "__getitem__" of "tuple" matches argument type "str" \ # N: Possible overload variants: \ # N: def __getitem__(self, int, /) -> Literal[Key.C] \ # N: def __getitem__(self, slice, /) -> tuple[Literal[Key.C], ...] - reveal_type(weird_mixture) # N: Revealed type is "Union[TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}), tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]]" + reveal_type(weird_mixture) # N: Revealed type is "TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}) | tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]" else: - reveal_type(weird_mixture) # N: Revealed type is "Union[TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}), tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]]" + reveal_type(weird_mixture) # N: Revealed type is "TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}) | tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]" if weird_mixture[0] is Key.B: # E: TypedDict key must be a string literal; expected one of ("key") - reveal_type(weird_mixture) # N: Revealed type is "Union[TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}), tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]]" + reveal_type(weird_mixture) # N: Revealed type is "TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}) | tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]" else: - reveal_type(weird_mixture) # N: Revealed type is "Union[TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}), tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]]" + reveal_type(weird_mixture) # N: Revealed type is "TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}) | tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] @@ -465,7 +465,7 @@ class Object3: x: Union[Object1, Object2, Object3] if x.key is Key.A: - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, __main__.Object2]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | __main__.Object2" else: reveal_type(x) # N: Revealed type is "__main__.Object3" [builtins fixtures/property.pyi] @@ -488,11 +488,11 @@ class Object2: x: Union[Object1, Object2, Any] if x.key is Key.A: reveal_type(x.key) # N: Revealed type is "Literal[__main__.Key.A]" - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, Any]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | Any" else: # TODO: Is this a bug? Should we skip inferring Any for singleton types? - reveal_type(x.key) # N: Revealed type is "Union[Any, Literal[__main__.Key.B]]" - reveal_type(x) # N: Revealed type is "Union[__main__.Object1, __main__.Object2, Any]" + reveal_type(x.key) # N: Revealed type is "Any | Literal[__main__.Key.B]" + reveal_type(x) # N: Revealed type is "__main__.Object1 | __main__.Object2 | Any" [builtins fixtures/tuple.pyi] [case testNarrowingParentsHierarchy] @@ -523,17 +523,17 @@ class Child3: x: Union[Parent1, Parent2, Parent3] if x.child.main is Key.A: - reveal_type(x) # N: Revealed type is "Union[__main__.Parent1, __main__.Parent3]" + reveal_type(x) # N: Revealed type is "__main__.Parent1 | __main__.Parent3" reveal_type(x.child) # N: Revealed type is "__main__.Child1" else: - reveal_type(x) # N: Revealed type is "Union[__main__.Parent1, __main__.Parent2, __main__.Parent3]" - reveal_type(x.child) # N: Revealed type is "Union[__main__.Child2, __main__.Child3]" + reveal_type(x) # N: Revealed type is "__main__.Parent1 | __main__.Parent2 | __main__.Parent3" + reveal_type(x.child) # N: Revealed type is "__main__.Child2 | __main__.Child3" if x.child.same_for_1_and_2 is Key.A: - reveal_type(x) # N: Revealed type is "Union[__main__.Parent1, __main__.Parent2, __main__.Parent3]" - reveal_type(x.child) # N: Revealed type is "Union[__main__.Child1, __main__.Child2]" + reveal_type(x) # N: Revealed type is "__main__.Parent1 | __main__.Parent2 | __main__.Parent3" + reveal_type(x.child) # N: Revealed type is "__main__.Child1 | __main__.Child2" else: - reveal_type(x) # N: Revealed type is "Union[__main__.Parent2, __main__.Parent3]" + reveal_type(x) # N: Revealed type is "__main__.Parent2 | __main__.Parent3" reveal_type(x.child) # N: Revealed type is "__main__.Child3" y: Union[Parent1, Parent2] @@ -541,12 +541,12 @@ if y.child.main is Key.A: reveal_type(y) # N: Revealed type is "__main__.Parent1" reveal_type(y.child) # N: Revealed type is "__main__.Child1" else: - reveal_type(y) # N: Revealed type is "Union[__main__.Parent1, __main__.Parent2]" - reveal_type(y.child) # N: Revealed type is "Union[__main__.Child2, __main__.Child3]" + reveal_type(y) # N: Revealed type is "__main__.Parent1 | __main__.Parent2" + reveal_type(y.child) # N: Revealed type is "__main__.Child2 | __main__.Child3" if y.child.same_for_1_and_2 is Key.A: - reveal_type(y) # N: Revealed type is "Union[__main__.Parent1, __main__.Parent2]" - reveal_type(y.child) # N: Revealed type is "Union[__main__.Child1, __main__.Child2]" + reveal_type(y) # N: Revealed type is "__main__.Parent1 | __main__.Parent2" + reveal_type(y.child) # N: Revealed type is "__main__.Child1 | __main__.Child2" else: reveal_type(y) # N: Revealed type is "__main__.Parent2" reveal_type(y.child) # N: Revealed type is "__main__.Child3" @@ -609,8 +609,8 @@ if y["model"]["key"] is Key.C: reveal_type(y) # E: Statement is unreachable reveal_type(y["model"]) else: - reveal_type(y) # N: Revealed type is "Union[TypedDict('__main__.Parent1', {'model': TypedDict('__main__.Model1', {'key': Literal[__main__.Key.A]}), 'foo': builtins.int}), TypedDict('__main__.Parent2', {'model': TypedDict('__main__.Model2', {'key': Literal[__main__.Key.B]}), 'bar': builtins.str})]" - reveal_type(y["model"]) # N: Revealed type is "Union[TypedDict('__main__.Model1', {'key': Literal[__main__.Key.A]}), TypedDict('__main__.Model2', {'key': Literal[__main__.Key.B]})]" + reveal_type(y) # N: Revealed type is "TypedDict('__main__.Parent1', {'model': TypedDict('__main__.Model1', {'key': Literal[__main__.Key.A]}), 'foo': builtins.int}) | TypedDict('__main__.Parent2', {'model': TypedDict('__main__.Model2', {'key': Literal[__main__.Key.B]}), 'bar': builtins.str})" + reveal_type(y["model"]) # N: Revealed type is "TypedDict('__main__.Model1', {'key': Literal[__main__.Key.A]}) | TypedDict('__main__.Model2', {'key': Literal[__main__.Key.B]})" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -645,8 +645,8 @@ if y["model"]["key"] == 'C': reveal_type(y) # E: Statement is unreachable reveal_type(y["model"]) else: - reveal_type(y) # N: Revealed type is "Union[TypedDict('__main__.Parent1', {'model': TypedDict('__main__.Model1', {'key': Literal['A']}), 'foo': builtins.int}), TypedDict('__main__.Parent2', {'model': TypedDict('__main__.Model2', {'key': Literal['B']}), 'bar': builtins.str})]" - reveal_type(y["model"]) # N: Revealed type is "Union[TypedDict('__main__.Model1', {'key': Literal['A']}), TypedDict('__main__.Model2', {'key': Literal['B']})]" + reveal_type(y) # N: Revealed type is "TypedDict('__main__.Parent1', {'model': TypedDict('__main__.Model1', {'key': Literal['A']}), 'foo': builtins.int}) | TypedDict('__main__.Parent2', {'model': TypedDict('__main__.Model2', {'key': Literal['B']}), 'bar': builtins.str})" + reveal_type(y["model"]) # N: Revealed type is "TypedDict('__main__.Model1', {'key': Literal['A']}) | TypedDict('__main__.Model2', {'key': Literal['B']})" [builtins fixtures/primitives.pyi] [typing fixtures/typing-typeddict.pyi] @@ -772,14 +772,14 @@ x_union: Literal["A", "B", None] if x_union == A_final: reveal_type(x_union) # N: Revealed type is "Literal['A']" else: - reveal_type(x_union) # N: Revealed type is "Union[Literal['B'], None]" -reveal_type(x_union) # N: Revealed type is "Union[Literal['A'], Literal['B'], None]" + reveal_type(x_union) # N: Revealed type is "Literal['B'] | None" +reveal_type(x_union) # N: Revealed type is "Literal['A'] | Literal['B'] | None" if x_union == A_literal: reveal_type(x_union) # N: Revealed type is "Literal['A']" else: - reveal_type(x_union) # N: Revealed type is "Union[Literal['B'], None]" -reveal_type(x_union) # N: Revealed type is "Union[Literal['A'], Literal['B'], None]" + reveal_type(x_union) # N: Revealed type is "Literal['B'] | None" +reveal_type(x_union) # N: Revealed type is "Literal['A'] | Literal['B'] | None" [builtins fixtures/primitives.pyi] [case testNarrowingEqualityRequiresExplicitEnumLiteral] @@ -834,15 +834,15 @@ class Default: pass x1: Union[Custom, Literal[1], Literal[2]] if x1 == 1: - reveal_type(x1) # N: Revealed type is "Union[__main__.Custom, Literal[1], Literal[2]]" + reveal_type(x1) # N: Revealed type is "__main__.Custom | Literal[1] | Literal[2]" else: - reveal_type(x1) # N: Revealed type is "Union[__main__.Custom, Literal[1], Literal[2]]" + reveal_type(x1) # N: Revealed type is "__main__.Custom | Literal[1] | Literal[2]" x2: Union[Default, Literal[1], Literal[2]] if x2 == 1: reveal_type(x2) # N: Revealed type is "Literal[1]" else: - reveal_type(x2) # N: Revealed type is "Union[__main__.Default, Literal[2]]" + reveal_type(x2) # N: Revealed type is "__main__.Default | Literal[2]" class CustomEnum(Enum): A = 1 @@ -885,18 +885,18 @@ z: Default # enough to declare itself to be equal to None and so permit this narrowing, # since it's often convenient in practice. if 1 == x == y: - reveal_type(x) # N: Revealed type is "Union[Literal[1], Literal[2]]" + reveal_type(x) # N: Revealed type is "Literal[1] | Literal[2]" reveal_type(y) # N: Revealed type is "__main__.Custom" else: - reveal_type(x) # N: Revealed type is "Union[Literal[1], Literal[2], None]" + reveal_type(x) # N: Revealed type is "Literal[1] | Literal[2] | None" reveal_type(y) # N: Revealed type is "__main__.Custom" # No contamination here -if 1 == x == z: # E: Non-overlapping equality check (left operand type: "Optional[Literal[1, 2]]", right operand type: "Default") +if 1 == x == z: # E: Non-overlapping equality check (left operand type: "Literal[1, 2] | None", right operand type: "Default") reveal_type(x) # E: Statement is unreachable reveal_type(z) else: - reveal_type(x) # N: Revealed type is "Union[Literal[1], Literal[2], None]" + reveal_type(x) # N: Revealed type is "Literal[1] | Literal[2] | None" reveal_type(z) # N: Revealed type is "__main__.Default" [builtins fixtures/primitives.pyi] @@ -914,8 +914,8 @@ if a == b == c: reveal_type(c) else: reveal_type(a) # N: Revealed type is "Literal[1]" - reveal_type(b) # N: Revealed type is "Union[Literal[1], Literal[2]]" - reveal_type(c) # N: Revealed type is "Union[Literal[2], Literal[3]]" + reveal_type(b) # N: Revealed type is "Literal[1] | Literal[2]" + reveal_type(c) # N: Revealed type is "Literal[2] | Literal[3]" if a == a == a: reveal_type(a) # N: Revealed type is "Literal[1]" @@ -943,8 +943,8 @@ if b == 2 == c: reveal_type(b) # N: Revealed type is "Literal[2]" reveal_type(c) # N: Revealed type is "Literal[2]" else: - reveal_type(b) # N: Revealed type is "Union[Literal[1], Literal[2]]" - reveal_type(c) # N: Revealed type is "Union[Literal[2], Literal[3]]" + reveal_type(b) # N: Revealed type is "Literal[1] | Literal[2]" + reveal_type(c) # N: Revealed type is "Literal[2] | Literal[3]" [builtins fixtures/primitives.pyi] [case testNarrowingUnreachableCases2] @@ -968,8 +968,8 @@ elif a == b == 4: reveal_type(b) # N: Revealed type is "Literal[4]" else: # This branch is reachable if a == 1 and b == 2, for example. - reveal_type(a) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3], Literal[4]]" - reveal_type(b) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3], Literal[4]]" + reveal_type(a) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3] | Literal[4]" + reveal_type(b) # N: Revealed type is "Literal[1] | Literal[2] | Literal[3] | Literal[4]" if a == a == 1: reveal_type(a) # N: Revealed type is "Literal[1]" @@ -994,7 +994,7 @@ str_or_false: Union[Literal[False], str] if str_or_false: reveal_type(str_or_false) # N: Revealed type is "builtins.str" else: - reveal_type(str_or_false) # N: Revealed type is "Union[Literal[False], Literal['']]" + reveal_type(str_or_false) # N: Revealed type is "Literal[False] | Literal['']" true_or_false: Literal[True, False] @@ -1019,7 +1019,7 @@ if not b: if not c: reveal_type(c) # N: Revealed type is "Literal[0]" if not d: - reveal_type(d) # N: Revealed type is "Union[Literal[''], Literal[b''], Literal[0]]" + reveal_type(d) # N: Revealed type is "Literal[''] | Literal[b''] | Literal[0]" [case testNarrowingIsInstanceFinalSubclass] # flags: --warn-unreachable @@ -1083,7 +1083,7 @@ if isinstance(n_f2, F1): # E: Subclass of "N" and "F1" cannot exist: "F1" is fi # E: Subclass of "F2" and "F1" cannot exist: "F1" is final reveal_type(n_f2) # E: Statement is unreachable else: - reveal_type(n_f2) # N: Revealed type is "Union[__main__.N, __main__.F2]" + reveal_type(n_f2) # N: Revealed type is "__main__.N | __main__.F2" if isinstance(f1_f2, F1): reveal_type(f1_f2) # N: Revealed type is "__main__.F1" @@ -1149,7 +1149,7 @@ else: str_or_bool_literal: Union[Literal[False], Literal[True], str] if str_or_bool_literal is not True: - reveal_type(str_or_bool_literal) # N: Revealed type is "Union[Literal[False], builtins.str]" + reveal_type(str_or_bool_literal) # N: Revealed type is "Literal[False] | builtins.str" else: reveal_type(str_or_bool_literal) # N: Revealed type is "Literal[True]" @@ -1175,7 +1175,7 @@ if opt_bool_val is not None: reveal_type(opt_bool_val) # N: Revealed type is "builtins.bool" if opt_bool_val is not False: - reveal_type(opt_bool_val) # N: Revealed type is "Union[Literal[True], None]" + reveal_type(opt_bool_val) # N: Revealed type is "Literal[True] | None" else: reveal_type(opt_bool_val) # N: Revealed type is "Literal[False]" [builtins fixtures/primitives.pyi] @@ -1196,8 +1196,8 @@ opt_bool_val: Optional[bool] if opt_bool_val: reveal_type(opt_bool_val) # N: Revealed type is "Literal[True]" else: - reveal_type(opt_bool_val) # N: Revealed type is "Union[Literal[False], None]" -reveal_type(opt_bool_val) # N: Revealed type is "Union[builtins.bool, None]" + reveal_type(opt_bool_val) # N: Revealed type is "Literal[False] | None" +reveal_type(opt_bool_val) # N: Revealed type is "builtins.bool | None" [builtins fixtures/primitives.pyi] [case testNarrowingBooleanBoolOp] @@ -1331,7 +1331,7 @@ def all_parts_covered(x: Union[str, List[str], List[int], int]) -> None: if isinstance(x, str): reveal_type(x) # N: Revealed type is "builtins.str" elif isinstance(x, list): - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.str], builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int]" else: reveal_type(x) # N: Revealed type is "builtins.int" @@ -1339,7 +1339,7 @@ def two_type_vars(x: Union[str, Dict[str, int], Dict[bool, object], int]) -> Non if isinstance(x, str): reveal_type(x) # N: Revealed type is "builtins.str" elif isinstance(x, dict): - reveal_type(x) # N: Revealed type is "Union[builtins.dict[builtins.str, builtins.int], builtins.dict[builtins.bool, builtins.object]]" + reveal_type(x) # N: Revealed type is "builtins.dict[builtins.str, builtins.int] | builtins.dict[builtins.bool, builtins.object]" else: reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/dict.pyi] @@ -1364,22 +1364,22 @@ class A: ... val: Optional[A] if val == None: - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(val) # N: Revealed type is "__main__.A | None" else: reveal_type(val) # N: Revealed type is "__main__.A" if val != None: reveal_type(val) # N: Revealed type is "__main__.A" else: - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(val) # N: Revealed type is "__main__.A | None" if val in (None,): - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(val) # N: Revealed type is "__main__.A | None" else: - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(val) # N: Revealed type is "__main__.A | None" if val not in (None,): - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(val) # N: Revealed type is "__main__.A | None" else: - reveal_type(val) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(val) # N: Revealed type is "__main__.A | None" [builtins fixtures/primitives.pyi] [case testNarrowingWithTupleOfTypes] @@ -1419,13 +1419,13 @@ impls: tuple[type[Base], ...] = (Impl1, Impl2) if isinstance(some, impls): reveal_type(some) # N: Revealed type is "__main__.Base" else: - reveal_type(some) # N: Revealed type is "Union[builtins.int, __main__.Base]" + reveal_type(some) # N: Revealed type is "builtins.int | __main__.Base" raw: tuple[type, ...] if isinstance(some, raw): - reveal_type(some) # N: Revealed type is "Union[builtins.int, __main__.Base]" + reveal_type(some) # N: Revealed type is "builtins.int | __main__.Base" else: - reveal_type(some) # N: Revealed type is "Union[builtins.int, __main__.Base]" + reveal_type(some) # N: Revealed type is "builtins.int | __main__.Base" [builtins fixtures/dict.pyi] [case testNarrowingWithAnyOps] @@ -1437,7 +1437,7 @@ tp: Any c: C if isinstance(c, tp) or isinstance(c, D): - reveal_type(c) # N: Revealed type is "Union[Any, __main__.D]" + reveal_type(c) # N: Revealed type is "Any | __main__.D" else: reveal_type(c) # N: Revealed type is "__main__.C" reveal_type(c) # N: Revealed type is "__main__.C" @@ -1451,7 +1451,7 @@ reveal_type(c1) # N: Revealed type is "__main__.C" c2: C if isinstance(c2, D) or isinstance(c2, tp): - reveal_type(c2) # N: Revealed type is "Union[__main__.D, Any]" + reveal_type(c2) # N: Revealed type is "__main__.D | Any" else: reveal_type(c2) # N: Revealed type is "__main__.C" reveal_type(c2) # N: Revealed type is "__main__.C" @@ -1517,9 +1517,9 @@ from typing import Union, List x: Union[str, List[int]] if len(x) == 3: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.list[builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.list[builtins.int]" [builtins fixtures/len.pyi] [case testNarrowingLenAnyListElseNotAffected] @@ -1563,22 +1563,22 @@ VarTuple = Union[Tuple[int], Tuple[int, int], Tuple[int, int, int]] x: VarTuple if len(x) > 1: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" else: reveal_type(x) # N: Revealed type is "tuple[builtins.int]" if len(x) < 2: reveal_type(x) # N: Revealed type is "tuple[builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" if len(x) >= 2: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" else: reveal_type(x) # N: Revealed type is "tuple[builtins.int]" if len(x) <= 2: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int], tuple[builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int] | tuple[builtins.int, builtins.int]" else: reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.int]" [builtins fixtures/len.pyi] @@ -1595,9 +1595,9 @@ VarTuple = Union[ x: VarTuple if 2 <= len(x) <= 3: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int], tuple[builtins.int, builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int] | tuple[builtins.int, builtins.int, builtins.int, builtins.int]" [builtins fixtures/len.pyi] [case testNarrowingLenGreaterThanHomogeneousTupleShort] @@ -1608,7 +1608,7 @@ VarTuple = Tuple[int, ...] x: VarTuple if len(x) < 3: - reveal_type(x) # N: Revealed type is "Union[tuple[()], tuple[builtins.int], tuple[builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, builtins.int]" else: reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]" reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...]" @@ -1633,9 +1633,9 @@ from typing import Tuple x: Tuple[int, ...] if 1 < len(x) < 4: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[()], tuple[builtins.int], tuple[builtins.int, builtins.int, builtins.int, builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]]" + reveal_type(x) # N: Revealed type is "tuple[()] | tuple[builtins.int] | tuple[builtins.int, builtins.int, builtins.int, builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]" reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...]" [builtins fixtures/len.pyi] @@ -1647,12 +1647,12 @@ x: Union[Tuple[int, int], Tuple[int, int, int]] if len(x) >= 4: reveal_type(x) # E: Statement is unreachable else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" if len(x) < 2: reveal_type(x) # E: Statement is unreachable else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" [builtins fixtures/len.pyi] [case testNarrowingLenMixedTypes] @@ -1661,17 +1661,17 @@ from typing import Tuple, List, Union x: Union[Tuple[int, int], Tuple[int, int, int], List[int]] a = b = c = 0 if len(x) == 3: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int, builtins.int], builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.int] | builtins.list[builtins.int]" a, b, c = x else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.list[builtins.int]" a, b = x if len(x) != 3: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.list[builtins.int]" a, b = x else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int, builtins.int], builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.int] | builtins.list[builtins.int]" a, b, c = x [builtins fixtures/len.pyi] @@ -1773,7 +1773,7 @@ def foo(x: Tuple[int, Unpack[Tuple[float, ...]], str]) -> None: if len(x) > 3: reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.float, builtins.float, Unpack[builtins.tuple[builtins.float, ...]], builtins.str]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.str], tuple[builtins.int, builtins.float, builtins.str]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str] | tuple[builtins.int, builtins.float, builtins.str]" reveal_type(x) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.str]" if len(x) < 3: @@ -1838,7 +1838,7 @@ x: Optional[Tuple[int, ...]] if x: reveal_type(x) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[()], None]" + reveal_type(x) # N: Revealed type is "tuple[()] | None" [builtins fixtures/len.pyi] [case testNarrowingLenBareExpressionWithNoneImprecise] @@ -1848,7 +1848,7 @@ x: Optional[Tuple[int, ...]] if x: reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...]" else: - reveal_type(x) # N: Revealed type is "Union[builtins.tuple[builtins.int, ...], None]" + reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...] | None" [builtins fixtures/len.pyi] [case testNarrowingLenMixWithAnyPrecise] @@ -1857,14 +1857,14 @@ from typing import Any x: Any if isinstance(x, (list, tuple)) and len(x) == 0: - reveal_type(x) # N: Revealed type is "Union[tuple[()], builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "tuple[()] | builtins.list[Any]" else: reveal_type(x) # N: Revealed type is "Any" reveal_type(x) # N: Revealed type is "Any" x1: Any if isinstance(x1, (list, tuple)) and len(x1) > 1: - reveal_type(x1) # N: Revealed type is "Union[tuple[Any, Any, Unpack[builtins.tuple[Any, ...]]], builtins.list[Any]]" + reveal_type(x1) # N: Revealed type is "tuple[Any, Any, Unpack[builtins.tuple[Any, ...]]] | builtins.list[Any]" else: reveal_type(x1) # N: Revealed type is "Any" reveal_type(x1) # N: Revealed type is "Any" @@ -1875,14 +1875,14 @@ from typing import Any x: Any if isinstance(x, (list, tuple)) and len(x) == 0: - reveal_type(x) # N: Revealed type is "Union[tuple[()], builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "tuple[()] | builtins.list[Any]" else: reveal_type(x) # N: Revealed type is "Any" reveal_type(x) # N: Revealed type is "Any" x1: Any if isinstance(x1, (list, tuple)) and len(x1) > 1: - reveal_type(x1) # N: Revealed type is "Union[builtins.tuple[Any, ...], builtins.list[Any]]" + reveal_type(x1) # N: Revealed type is "builtins.tuple[Any, ...] | builtins.list[Any]" else: reveal_type(x1) # N: Revealed type is "Any" reveal_type(x1) # N: Revealed type is "Any" @@ -1902,13 +1902,13 @@ supported: Literal[2] if len(x) == supported: reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" not_supported_yet: Literal[2, 3] if len(x) == not_supported_yet: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int], tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int] | tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int], tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int] | tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" [builtins fixtures/len.pyi] [case testNarrowingLenUnionOfVariadicTuples] @@ -1916,9 +1916,9 @@ from typing import Tuple, Union x: Union[Tuple[int, ...], Tuple[str, ...]] if len(x) == 2: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.str, builtins.str]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.str, builtins.str]" else: - reveal_type(x) # N: Revealed type is "Union[builtins.tuple[builtins.int, ...], builtins.tuple[builtins.str, ...]]" + reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...] | builtins.tuple[builtins.str, ...]" [builtins fixtures/len.pyi] [case testNarrowingLenUnionOfNamedTuples] @@ -1991,15 +1991,15 @@ x: Union[Tuple[int, int], Tuple[int, int, int]] n: int if len(x) == n: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" a: Any if len(x) == a: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.int, builtins.int, builtins.int]]" + reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.int, builtins.int, builtins.int]" [builtins fixtures/len.pyi] [case testNarrowingLenUnionWithUnreachable] @@ -2144,9 +2144,9 @@ def f3(x: object) -> None: def f4(x: int | Any) -> None: if x == IE.X: - reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" + reveal_type(x) # N: Revealed type is "builtins.int | Any" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" + reveal_type(x) # N: Revealed type is "builtins.int | Any" def f5(x: int) -> None: if x is IE.X: @@ -2187,21 +2187,21 @@ class E(Enum): def f1(x: IE | MyDecimal) -> None: if x == IE.X: - reveal_type(x) # N: Revealed type is "Union[__main__.IE, __main__.MyDecimal]" + reveal_type(x) # N: Revealed type is "__main__.IE | __main__.MyDecimal" else: - reveal_type(x) # N: Revealed type is "Union[__main__.IE, __main__.MyDecimal]" + reveal_type(x) # N: Revealed type is "__main__.IE | __main__.MyDecimal" def f2(x: E | bytes) -> None: if x == E.X: reveal_type(x) # N: Revealed type is "Literal[__main__.E.X]" else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.E.Y], builtins.bytes]" + reveal_type(x) # N: Revealed type is "Literal[__main__.E.Y] | builtins.bytes" def f3(x: IE | IE2) -> None: if x == IE.X: - reveal_type(x) # N: Revealed type is "Union[__main__.IE, __main__.IE2]" + reveal_type(x) # N: Revealed type is "__main__.IE | __main__.IE2" else: - reveal_type(x) # N: Revealed type is "Union[__main__.IE, __main__.IE2]" + reveal_type(x) # N: Revealed type is "__main__.IE | __main__.IE2" def f4(x: IE | E) -> None: if x == IE.X: @@ -2209,25 +2209,25 @@ def f4(x: IE | E) -> None: elif x == E.X: reveal_type(x) # N: Revealed type is "Literal[__main__.E.X]" else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.IE.Y], Literal[__main__.E.Y]]" + reveal_type(x) # N: Revealed type is "Literal[__main__.IE.Y] | Literal[__main__.E.Y]" def f5(x: E | str | int) -> None: if x == E.X: reveal_type(x) # N: Revealed type is "Literal[__main__.E.X]" else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.E.Y], builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "Literal[__main__.E.Y] | builtins.str | builtins.int" def f6(x: IE | Any) -> None: if x == IE.X: - reveal_type(x) # N: Revealed type is "Union[__main__.IE, Any]" + reveal_type(x) # N: Revealed type is "__main__.IE | Any" else: - reveal_type(x) # N: Revealed type is "Union[__main__.IE, Any]" + reveal_type(x) # N: Revealed type is "__main__.IE | Any" def f7(x: IE | None) -> None: if x == IE.X: reveal_type(x) # N: Revealed type is "Literal[__main__.IE.X]" else: - reveal_type(x) # N: Revealed type is "Union[Literal[__main__.IE.Y], None]" + reveal_type(x) # N: Revealed type is "Literal[__main__.IE.Y] | None" def f8(x: IE | None) -> None: if x is None: @@ -2277,12 +2277,12 @@ def f4(x: SE) -> None: # https://github.com/python/mypy/issues/17864 def f(x: str | int) -> None: if x == "x": - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" y = x if x in ["x"]: # TODO: we should fix this reveal https://github.com/python/mypy/issues/3229 - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" y = x z = x z = y @@ -2340,15 +2340,15 @@ for _ in range(2): if x is not None: reveal_type(x) # N: Revealed type is "builtins.int" x = 1 -reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(x) # N: Revealed type is "builtins.int | None" def f() -> bool: ... y = None while f(): - reveal_type(y) # N: Revealed type is "Union[None, builtins.int]" + reveal_type(y) # N: Revealed type is "None | builtins.int" y = 1 -reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(y) # N: Revealed type is "builtins.int | None" z = [] # E: Need type annotation for "z" (hint: "z: list[] = ...") def g() -> None: @@ -2398,7 +2398,7 @@ def main_no_cast(p: Processor) -> None: ed: ProcessorReturnValue ed = cast(str, ...) while True: - ed = p(ed) # E: Argument 1 has incompatible type "Union[str, int]"; expected "str" + ed = p(ed) # E: Argument 1 has incompatible type "str | int"; expected "str" [builtins fixtures/bool.pyi] [case testAvoidFalseUnreachableInLoop1] @@ -2453,10 +2453,10 @@ from typing import Union x: Union[str, float, None] y: Union[int, str] x = y -reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" z: Union[complex, str] z = x -reveal_type(z) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/primitives.pyi] @@ -2472,7 +2472,7 @@ x: Optional[float] while b(): x = None while b(): - reveal_type(x) # N: Revealed type is "Union[None, builtins.int]" + reveal_type(x) # N: Revealed type is "None | builtins.int" if x is None or b(): x = i() reveal_type(x) # N: Revealed type is "builtins.int" @@ -2491,10 +2491,10 @@ def f() -> None: x = None return finally: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" if isinstance(x, str): reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" [builtins fixtures/isinstancelist.pyi] @@ -2556,7 +2556,7 @@ def test(x: T) -> T: if not isinstance(x, (A, B)): return x reveal_type(x) # N: Revealed type is "T`-1" - reveal_type(x.x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x.x) # N: Revealed type is "builtins.int | builtins.str" if isinstance(x, A): reveal_type(x) # N: Revealed type is "T`-1" reveal_type(x.x) # N: Revealed type is "builtins.int" @@ -2576,7 +2576,7 @@ class A(Generic[T]): pass def check_a(obj: "A[T] | str") -> None: - reveal_type(obj) # N: Revealed type is "Union[__main__.A[T`-1], builtins.str]" + reveal_type(obj) # N: Revealed type is "__main__.A[T`-1] | builtins.str" if isinstance(obj, A): reveal_type(obj) # N: Revealed type is "__main__.A[T`-1]" else: @@ -2592,7 +2592,7 @@ class B(Generic[T]): pass def check_b(obj: "B[T] | str") -> None: - reveal_type(obj) # N: Revealed type is "Union[__main__.B[T`-1], builtins.str]" + reveal_type(obj) # N: Revealed type is "__main__.B[T`-1] | builtins.str" if isinstance(obj, B): reveal_type(obj) # N: Revealed type is "__main__.B[T`-1]" else: @@ -2608,7 +2608,7 @@ class C(Generic[T]): pass def check_c(obj: "C[T] | str") -> None: - reveal_type(obj) # N: Revealed type is "Union[__main__.C[T`-1], builtins.str]" + reveal_type(obj) # N: Revealed type is "__main__.C[T`-1] | builtins.str" if isinstance(obj, C): reveal_type(obj) # N: Revealed type is "__main__.C[T`-1]" else: @@ -2637,7 +2637,7 @@ def baz(item: Base) -> None: if not isinstance(item, (FooMixin, BarMixin)): raise - reveal_type(item) # N: Revealed type is "Union[__main__., __main__.]" + reveal_type(item) # N: Revealed type is "__main__. | __main__." if isinstance(item, FooMixin): reveal_type(item) # N: Revealed type is "__main__." item.foo() diff --git a/test-data/unit/check-native-int.test b/test-data/unit/check-native-int.test index 2f852ca522c5..c0b5d2b9b64c 100644 --- a/test-data/unit/check-native-int.test +++ b/test-data/unit/check-native-int.test @@ -196,37 +196,37 @@ def narrow_i64(x: Union[str, i64]) -> None: reveal_type(x) # N: Revealed type is "mypy_extensions.i64" else: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.str, mypy_extensions.i64]" + reveal_type(x) # N: Revealed type is "builtins.str | mypy_extensions.i64" if isinstance(x, str): reveal_type(x) # N: Revealed type is "builtins.str" else: reveal_type(x) # N: Revealed type is "mypy_extensions.i64" - reveal_type(x) # N: Revealed type is "Union[builtins.str, mypy_extensions.i64]" + reveal_type(x) # N: Revealed type is "builtins.str | mypy_extensions.i64" if isinstance(x, int): reveal_type(x) # N: Revealed type is "mypy_extensions.i64" else: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.str, mypy_extensions.i64]" + reveal_type(x) # N: Revealed type is "builtins.str | mypy_extensions.i64" def narrow_i32(x: Union[str, i32]) -> None: if isinstance(x, i32): reveal_type(x) # N: Revealed type is "mypy_extensions.i32" else: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.str, mypy_extensions.i32]" + reveal_type(x) # N: Revealed type is "builtins.str | mypy_extensions.i32" if isinstance(x, str): reveal_type(x) # N: Revealed type is "builtins.str" else: reveal_type(x) # N: Revealed type is "mypy_extensions.i32" - reveal_type(x) # N: Revealed type is "Union[builtins.str, mypy_extensions.i32]" + reveal_type(x) # N: Revealed type is "builtins.str | mypy_extensions.i32" if isinstance(x, int): reveal_type(x) # N: Revealed type is "mypy_extensions.i32" else: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.str, mypy_extensions.i32]" + reveal_type(x) # N: Revealed type is "builtins.str | mypy_extensions.i32" [builtins fixtures/primitives.pyi] diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test index 00c2d899f231..73c304defb0d 100644 --- a/test-data/unit/check-newsemanal.test +++ b/test-data/unit/check-newsemanal.test @@ -471,7 +471,7 @@ def main() -> None: def g(x: str) -> str: ... def g(x: Union[int, str]) -> Union[int, str]: reveal_type(f(int())) # N: Revealed type is "builtins.int" - return float() # E: Incompatible return value type (got "float", expected "Union[int, str]") + return float() # E: Incompatible return value type (got "float", expected "int | str") [case testNewAnalyzerNestedClassInMethod] class C: @@ -588,7 +588,7 @@ G = D[T, C] c: C2 reveal_type(c) # N: Revealed type is "__main__.C" u: U -reveal_type(u) # N: Revealed type is "Union[__main__.C, builtins.int]" +reveal_type(u) # N: Revealed type is "__main__.C | builtins.int" g: G[int] reveal_type(g) # N: Revealed type is "__main__.D[builtins.int, __main__.C]" [case testNewAnalyzerTypeAlias2] @@ -598,7 +598,7 @@ class C(D): pass A = Union[C, int] x: A -reveal_type(x) # N: Revealed type is "Union[__main__.C, builtins.int]" +reveal_type(x) # N: Revealed type is "__main__.C | builtins.int" class D: pass @@ -646,7 +646,7 @@ def f(x: int) -> int: ... @overload def f(x: str) -> str: ... def f(x: Union[int, str]) -> Union[int, str]: - return 1.0 # E: Incompatible return value type (got "float", expected "Union[int, str]") + return 1.0 # E: Incompatible return value type (got "float", expected "int | str") f(1) f('') @@ -664,7 +664,7 @@ class A: @overload def f(self, x: str) -> str: ... def f(self, x: Union[int, str]) -> Union[int, str]: - return 1.0 # E: Incompatible return value type (got "float", expected "Union[int, str]") + return 1.0 # E: Incompatible return value type (got "float", expected "int | str") a = A() a.f(1) @@ -1460,8 +1460,8 @@ B = A[List[T]] class C(List[B[int]]): pass y: C -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]" -reveal_type(y[0]) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.list[builtins.int]" +reveal_type(y[0]) # N: Revealed type is "builtins.int | builtins.list[builtins.int]" [builtins fixtures/list.pyi] [case testNewAnalyzerForwardAliasFromUnion] @@ -1558,8 +1558,8 @@ class C(List[A]): pass A = Union[B, C] -reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(x[0]) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" +reveal_type(x[0]) # N: Revealed type is "__main__.B | __main__.C" [builtins fixtures/list.pyi] [case testNewAnalyzerTrickyAliasInFuncDef] @@ -1827,7 +1827,7 @@ from typing import List, Optional x: Optional[List] = None y: List[str] -reveal_type(x) # N: Revealed type is "Union[builtins.list[Any], None]" +reveal_type(x) # N: Revealed type is "builtins.list[Any] | None" x = ['a', 'b'] reveal_type(x) # N: Revealed type is "builtins.list[Any]" x.extend(y) @@ -1946,18 +1946,18 @@ class NTStr(NamedTuple): y: str t1: T -reveal_type(t1.__iter__) # N: Revealed type is "def () -> typing.Iterator[Union[__main__.B, __main__.C]]" +reveal_type(t1.__iter__) # N: Revealed type is "def () -> typing.Iterator[__main__.B | __main__.C]" t2: NTInt reveal_type(t2.__iter__) # N: Revealed type is "def () -> typing.Iterator[builtins.int]" nt: Union[NTInt, NTStr] -reveal_type(nt.__iter__) # N: Revealed type is "Union[def () -> typing.Iterator[builtins.int], def () -> typing.Iterator[builtins.str]]" +reveal_type(nt.__iter__) # N: Revealed type is "def () -> typing.Iterator[builtins.int] | def () -> typing.Iterator[builtins.str]" for nx in nt: - reveal_type(nx) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(nx) # N: Revealed type is "builtins.int | builtins.str" t: Union[Tuple[int, int], Tuple[str, str]] for x in t: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/for.pyi] [case testNewAnalyzerFallbackUpperBoundCheckAndFallbacks] @@ -3252,7 +3252,7 @@ from typing import Optional, Callable, TypeVar class C(B): def a(self) -> None: - reveal_type(self._foo) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(self._foo) # N: Revealed type is "builtins.int | None" self._foo = defer() class B: diff --git a/test-data/unit/check-newtype.test b/test-data/unit/check-newtype.test index f7219e721222..1e2775b3aaa6 100644 --- a/test-data/unit/check-newtype.test +++ b/test-data/unit/check-newtype.test @@ -286,7 +286,7 @@ A = NewType('A', Any) # E: Argument 2 to NewType(...) must be subclassable (got [case testNewTypeWithUnionsFails] from typing import NewType, Union -Foo = NewType('Foo', Union[int, float]) # E: Argument 2 to NewType(...) must be subclassable (got "Union[int, float]") +Foo = NewType('Foo', Union[int, float]) # E: Argument 2 to NewType(...) must be subclassable (got "int | float") [out] [case testNewTypeWithTypeTypeFails] diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index 47f75c82c1e3..4cbd39ecbd87 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -36,7 +36,7 @@ main:1: error: Incompatible types in assignment (expression has type "int", vari from typing import Optional def f(a: int) -> None: pass x = None # type: Optional[int] -f(x) # E: Argument 1 to "f" has incompatible type "Optional[int]"; expected "int" +f(x) # E: Argument 1 to "f" has incompatible type "int | None"; expected "int" [case testIsinstanceCases] from typing import Optional @@ -53,14 +53,14 @@ x = None # type: Optional[int] if x: reveal_type(x) # N: Revealed type is "builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[Literal[0], None]" + reveal_type(x) # N: Revealed type is "Literal[0] | None" [builtins fixtures/bool.pyi] [case testIfNotCases] from typing import Optional x = None # type: Optional[int] if not x: - reveal_type(x) # N: Revealed type is "Union[Literal[0], None]" + reveal_type(x) # N: Revealed type is "Literal[0] | None" else: reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/bool.pyi] @@ -81,7 +81,7 @@ if x is None: reveal_type(x) # N: Revealed type is "None" else: reveal_type(x) # N: Revealed type is "builtins.int" -reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(x) # N: Revealed type is "builtins.int | None" [builtins fixtures/bool.pyi] [case testAnyCanBeNone] @@ -99,23 +99,23 @@ x = None # type: Optional[str] y1 = x or 'a' reveal_type(y1) # N: Revealed type is "builtins.str" y2 = x or 1 -reveal_type(y2) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(y2) # N: Revealed type is "builtins.str | builtins.int" z1 = 'a' or x -reveal_type(z1) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(z1) # N: Revealed type is "builtins.str | None" z2 = int() or x -reveal_type(z2) # N: Revealed type is "Union[builtins.int, builtins.str, None]" +reveal_type(z2) # N: Revealed type is "builtins.int | builtins.str | None" [case testAndCases] from typing import Optional x = None # type: Optional[str] y1 = x and 'b' -reveal_type(y1) # N: Revealed type is "Union[Literal[''], None, builtins.str]" +reveal_type(y1) # N: Revealed type is "Literal[''] | None | builtins.str" y2 = x and 1 # x could be '', so... -reveal_type(y2) # N: Revealed type is "Union[Literal[''], None, builtins.int]" +reveal_type(y2) # N: Revealed type is "Literal[''] | None | builtins.int" z1 = 'b' and x -reveal_type(z1) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(z1) # N: Revealed type is "builtins.str | None" z2 = int() and x -reveal_type(z2) # N: Revealed type is "Union[Literal[0], builtins.str, None]" +reveal_type(z2) # N: Revealed type is "Literal[0] | builtins.str | None" [case testLambdaReturningNone] f = lambda: None @@ -130,7 +130,7 @@ f(None) # flags: --implicit-optional def f(x: int = None) -> None: x + 1 # E: Unsupported left operand type for + ("None") \ - # N: Left operand is of type "Optional[int]" + # N: Left operand is of type "int | None" f(None) [out] @@ -147,7 +147,7 @@ def f(x: int = None) -> None: # E: Incompatible default for argument "x" (defau def f(x=None): # type: (int) -> None x + 1 # E: Unsupported left operand type for + ("None") \ - # N: Left operand is of type "Optional[int]" + # N: Left operand is of type "int | None" f(None) [out] @@ -168,7 +168,7 @@ if bool(): # in scope of the assignment, x is an int reveal_type(x) # N: Revealed type is "builtins.int" # out of scope of the assignment, it's an Optional[int] -reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(x) # N: Revealed type is "builtins.int | None" [builtins fixtures/bool.pyi] [case testInferOptionalTypeLocallyBound] @@ -183,7 +183,7 @@ a = None # type: Any if bool(): x = a reveal_type(x) # N: Revealed type is "Any" -reveal_type(x) # N: Revealed type is "Union[Any, None]" +reveal_type(x) # N: Revealed type is "Any | None" [builtins fixtures/bool.pyi] [case testInferOptionalTypeFromOptional] @@ -191,7 +191,7 @@ from typing import Optional y = None # type: Optional[int] x = None x = y -reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(x) # N: Revealed type is "builtins.int | None" [case testInferOptionalListType] x = [None] @@ -274,22 +274,22 @@ def f(a: Optional[int], b: Optional[int]) -> None: def g(a: int, b: Optional[int]) -> None: reveal_type(a or b) [out] -main:3: note: Revealed type is "Union[builtins.int, None]" -main:5: note: Revealed type is "Union[builtins.int, None]" +main:3: note: Revealed type is "builtins.int | None" +main:5: note: Revealed type is "builtins.int | None" [case testOptionalTypeOrTypeComplexUnion] from typing import Union def f(a: Union[int, str, None]) -> None: reveal_type(a or 'default') [out] -main:3: note: Revealed type is "Union[builtins.int, builtins.str]" +main:3: note: Revealed type is "builtins.int | builtins.str" [case testOptionalTypeOrTypeNoTriggerPlain] from typing import Optional def f(a: Optional[int], b: int) -> int: return b or a [out] -main:3: error: Incompatible return value type (got "Optional[int]", expected "int") +main:3: error: Incompatible return value type (got "int | None", expected "int") [case testOptionalTypeOrTypeNoTriggerTypeVar] from typing import Optional, TypeVar @@ -297,7 +297,7 @@ T = TypeVar('T') def f(a: Optional[T], b: T) -> T: return b or a [out] -main:4: error: Incompatible return value type (got "Optional[T]", expected "T") +main:4: error: Incompatible return value type (got "T | None", expected "T") [case testNoneOrStringIsString] def f() -> str: @@ -399,11 +399,11 @@ def lookup_field(name, obj): attr = None [case testTernaryWithNone] -reveal_type(None if bool() else 0) # N: Revealed type is "Union[None, Literal[0]?]" +reveal_type(None if bool() else 0) # N: Revealed type is "None | Literal[0]?" [builtins fixtures/bool.pyi] [case testListWithNone] -reveal_type([0, None, 0]) # N: Revealed type is "builtins.list[Union[builtins.int, None]]" +reveal_type([0, None, 0]) # N: Revealed type is "builtins.list[builtins.int | None]" [builtins fixtures/list.pyi] [case testNoneContextInference] @@ -447,50 +447,50 @@ x = '' # type: Optional[str] if x == '': reveal_type(x) # N: Revealed type is "builtins.str" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" if x is '': reveal_type(x) # N: Revealed type is "builtins.str" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" [builtins fixtures/ops.pyi] [case testInferEqualsNotOptionalWithUnion] from typing import Union x = '' # type: Union[str, int, None] if x == '': - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" if x is '': - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" [builtins fixtures/ops.pyi] [case testInferEqualsNotOptionalWithOverlap] from typing import Union x = '' # type: Union[str, int, None] if x == object(): - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" if x is object(): - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" [builtins fixtures/ops.pyi] [case testInferEqualsStillOptionalWithNoOverlap] from typing import Optional x = '' # type: Optional[str] if x == 0: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" if x is 0: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" [builtins fixtures/ops.pyi] [case testInferEqualsStillOptionalWithBothOptional] @@ -498,13 +498,13 @@ from typing import Union x = '' # type: Union[str, int, None] y = '' # type: Union[str, None] if x == y: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" if x is y: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None" [builtins fixtures/ops.pyi] [case testInferEqualsNotOptionalWithMultipleArgs] @@ -515,8 +515,8 @@ if x == y == 1: reveal_type(x) # N: Revealed type is "builtins.int" reveal_type(y) # N: Revealed type is "builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | None" + reveal_type(y) # N: Revealed type is "builtins.int | None" class A: pass a: Optional[A] @@ -525,8 +525,8 @@ if a == b == object(): reveal_type(a) # N: Revealed type is "__main__.A" reveal_type(b) # N: Revealed type is "__main__.A" else: - reveal_type(a) # N: Revealed type is "Union[__main__.A, None]" - reveal_type(b) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(a) # N: Revealed type is "__main__.A | None" + reveal_type(b) # N: Revealed type is "__main__.A | None" [builtins fixtures/ops.pyi] [case testInferInWithErasedTypes] @@ -568,7 +568,7 @@ if int(): if int(): x = f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int" -x.x = 1 # E: Item "None" of "Optional[Node[int]]" has no attribute "x" +x.x = 1 # E: Item "None" of "Node[int] | None" has no attribute "x" if x is not None: x.x = 1 # OK here @@ -606,14 +606,14 @@ def u(x: T, y: S) -> Union[S, T]: pass a = None # type: Any # Test both orders -reveal_type(u(C(), None)) # N: Revealed type is "Union[None, __main__.C]" -reveal_type(u(None, C())) # N: Revealed type is "Union[__main__.C, None]" +reveal_type(u(C(), None)) # N: Revealed type is "None | __main__.C" +reveal_type(u(None, C())) # N: Revealed type is "__main__.C | None" -reveal_type(u(a, None)) # N: Revealed type is "Union[None, Any]" -reveal_type(u(None, a)) # N: Revealed type is "Union[Any, None]" +reveal_type(u(a, None)) # N: Revealed type is "None | Any" +reveal_type(u(None, a)) # N: Revealed type is "Any | None" -reveal_type(u(1, None)) # N: Revealed type is "Union[None, builtins.int]" -reveal_type(u(None, 1)) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(u(1, None)) # N: Revealed type is "None | builtins.int" +reveal_type(u(None, 1)) # N: Revealed type is "builtins.int | None" [case testOptionalAndAnyBaseClass] from typing import Any, Optional @@ -621,7 +621,7 @@ A = None # type: Any class C(A): pass x = None # type: Optional[C] -x.foo() # E: Item "None" of "Optional[C]" has no attribute "foo" +x.foo() # E: Item "None" of "C | None" has no attribute "foo" [case testIsinstanceAndOptionalAndAnyBase] from typing import Any, Optional @@ -630,21 +630,21 @@ B = None # type: Any class A(B): pass def f(a: Optional[A]): - reveal_type(a) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(a) # N: Revealed type is "__main__.A | None" if a is not None: reveal_type(a) # N: Revealed type is "__main__.A" else: reveal_type(a) # N: Revealed type is "None" - reveal_type(a) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(a) # N: Revealed type is "__main__.A | None" [builtins fixtures/isinstance.pyi] [case testFlattenOptionalUnion] from typing import Optional, Union x: Optional[Union[int, str]] -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" y: Optional[Union[int, None]] -reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(y) # N: Revealed type is "builtins.int | None" [case testOverloadWithNoneAndOptional] from typing import overload, Optional @@ -656,9 +656,9 @@ def f(x: Optional[int]) -> Optional[str]: ... def f(x): return x reveal_type(f(1)) # N: Revealed type is "builtins.str" -reveal_type(f(None)) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(f(None)) # N: Revealed type is "builtins.str | None" x: Optional[int] -reveal_type(f(x)) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(f(x)) # N: Revealed type is "builtins.str | None" [case testUnionTruthinessTracking] from typing import Optional, Any @@ -674,7 +674,7 @@ from typing import Optional x: object y: Optional[int] x = y -reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(x) # N: Revealed type is "builtins.int | None" [out] [case testNarrowOptionalOutsideLambda] @@ -726,8 +726,8 @@ def g(x: Optional[int]) -> int: reveal_type(x) # N: Revealed type is "None" # As a special case for Unions containing None, during x = f() - reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" - reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" + reveal_type(x) # N: Revealed type is "builtins.int | Any" + reveal_type(x) # N: Revealed type is "builtins.int | Any" return x [builtins fixtures/bool.pyi] @@ -743,8 +743,8 @@ def g(x: Optional[int]) -> int: reveal_type(x) # N: Revealed type is "builtins.int" # Same as above, even after we've assigned to x x = f() - reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" - reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" + reveal_type(x) # N: Revealed type is "builtins.int | Any" + reveal_type(x) # N: Revealed type is "builtins.int | Any" return x [builtins fixtures/bool.pyi] @@ -758,7 +758,7 @@ def g(x: Optional[int]) -> int: return x reveal_type(x) # N: Revealed type is "None" x = f() - reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" + reveal_type(x) # N: Revealed type is "builtins.int | Any" return x [builtins fixtures/bool.pyi] @@ -782,7 +782,7 @@ asdf(x) \[mypy-a] strict_optional = False [out] -main:4: error: Argument 1 to "asdf" has incompatible type "list[str]"; expected "list[Optional[str]]" +main:4: error: Argument 1 to "asdf" has incompatible type "list[str]"; expected "list[str | None]" main:4: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance main:4: note: Consider using "Sequence" instead, which is covariant [builtins fixtures/list.pyi] @@ -797,7 +797,7 @@ def f1(b: bool) -> Optional[int]: else: z = None reveal_type(z) # N: Revealed type is "None" - reveal_type(z) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(z) # N: Revealed type is "builtins.int | None" return z def f2(b: bool) -> int: @@ -805,7 +805,7 @@ def f2(b: bool) -> int: z = 10 else: z = None - return z # E: Incompatible return value type (got "Optional[int]", expected "int") + return z # E: Incompatible return value type (got "int | None", expected "int") def f3(b: bool) -> int: # XXX: This one is a little questionable! Maybe we *do* want to allow this? @@ -904,7 +904,7 @@ def f15() -> None: z = f2(True) except Exception: z = None - reveal_type(z) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(z) # N: Revealed type is "builtins.int | None" def f16(z: Any) -> None: for x in z: @@ -913,7 +913,7 @@ def f16(z: Any) -> None: break else: y = None - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" def f17(b: bool, c: bool, d: bool) -> None: if b: @@ -922,7 +922,7 @@ def f17(b: bool, c: bool, d: bool) -> None: z = None elif d: z = 3 - reveal_type(z) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(z) # N: Revealed type is "builtins.int | None" def f18(b: bool, c: bool, d: bool) -> None: if b: @@ -932,7 +932,7 @@ def f18(b: bool, c: bool, d: bool) -> None: z = 5 else: z = None - reveal_type(z) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(z) # N: Revealed type is "builtins.int | None" def f19(b: bool, c: bool, d: bool) -> None: if b: @@ -941,7 +941,7 @@ def f19(b: bool, c: bool, d: bool) -> None: z = None if c: z = 6 - reveal_type(z) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(z) # N: Revealed type is "builtins.int | None" def f20(b: bool) -> None: if b: @@ -989,7 +989,7 @@ def f2(b: bool) -> None: x.append(1) else: x = None - reveal_type(x) # N: Revealed type is "Union[builtins.list[builtins.int], None]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.int] | None" [builtins fixtures/list.pyi] @@ -1025,7 +1025,7 @@ def f1(b: bool) -> Optional[int]: Defer().defer z = None reveal_type(z) # N: Revealed type is "None" - reveal_type(z) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(z) # N: Revealed type is "builtins.int | None" return z class Defer: @@ -1076,7 +1076,7 @@ def cannot_narrow_if_reassigned(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") if int(): x = None nested() @@ -1098,8 +1098,8 @@ x = "y" def narrowing_global_at_top_level_not_propagated() -> str: def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") [case testNarrowedVariableInNestedFunctionMore1] from typing import Optional, overload @@ -1111,14 +1111,14 @@ def attribute_narrowing(c: C) -> None: # This case is not supported, since we can't keep track of assignments to attributes. c.a = "x" def nested() -> str: - return c.a # E: Incompatible return value type (got "Optional[str]", expected "str") + return c.a # E: Incompatible return value type (got "str | None", expected "str") nested() def assignment_in_for(x: Optional[str]) -> None: if x is None: x = "e" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") for x in ["x"]: pass @@ -1128,7 +1128,7 @@ def assignment_in_with(x: Optional[str]) -> None: if x is None: x = "e" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") with foo() as x: pass @@ -1150,7 +1150,7 @@ def assign_to_nonlocal(x: Optional[str]) -> None: x = "a" def nested2() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") return nested2() nested() @@ -1172,7 +1172,7 @@ def decorated_outer_bad(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") x = None nested() @@ -1189,7 +1189,7 @@ def decorated_inner_bad(x: Optional[str]) -> None: x = "a" @dec def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") x = None nested() @@ -1212,7 +1212,7 @@ def overloaded_outer_bad(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") x = None nested() @@ -1244,7 +1244,7 @@ def narrow_multiple_partial(x: Optional[str], y: Optional[int]) -> None: def nested() -> None: a: str = x b: int = y - c: str = z # E: Incompatible types in assignment (expression has type "Optional[str]", variable has type "str") + c: str = z # E: Incompatible types in assignment (expression has type "str | None", variable has type "str") z = None nested() @@ -1288,7 +1288,7 @@ def narrow_with_multi_lvalue_2(x: Optional[str]) -> None: x = "" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") x = y = None @@ -1297,7 +1297,7 @@ def narrow_with_multi_lvalue_3(x: Optional[str]) -> None: x = "" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") y = x = None @@ -1315,7 +1315,7 @@ def narrow_with_multi_assign_2(x: Optional[str]) -> None: x = "" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") x, y = None, None @@ -1324,7 +1324,7 @@ def narrow_with_multi_assign_3(x: Optional[str]) -> None: x = "" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") y, x = None, None diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 1830a0c5ce3c..68eccb46ab21 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -1455,12 +1455,12 @@ foo(g) main:17: note: Revealed type is "builtins.int" main:18: note: Revealed type is "builtins.str" main:19: note: Revealed type is "Any" -main:20: note: Revealed type is "Union[builtins.int, builtins.str]" +main:20: note: Revealed type is "builtins.int | builtins.str" main:21: error: Argument 1 to "foo" has incompatible type "list[bool]"; expected "list[int]" main:21: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance main:21: note: Consider using "Sequence" instead, which is covariant main:22: error: Argument 1 to "foo" has incompatible type "list[object]"; expected "list[int]" -main:23: error: Argument 1 to "foo" has incompatible type "list[Union[int, str]]"; expected "list[int]" +main:23: error: Argument 1 to "foo" has incompatible type "list[int | str]"; expected "list[int]" [case testOverloadAgainstEmptyCollections] from typing import overload, List @@ -1891,7 +1891,7 @@ def g(x): pass a: Any reveal_type(f(a)) # N: Revealed type is "builtins.str" -reveal_type(g(a)) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(g(a)) # N: Revealed type is "builtins.str | builtins.int" [case testOverloadWithOverlappingItemsAndAnyArgument16] from typing import overload, Any, Union, Callable @@ -2050,7 +2050,7 @@ class Child4(Parent): @overload def f(self, arg: str) -> str: ... def f(self, arg: Union[int, str]) -> Union[int, str]: - return b'' # E: Incompatible return value type (got "bytes", expected "Union[int, str]") + return b'' # E: Incompatible return value type (got "bytes", expected "int | str") [builtins fixtures/tuple.pyi] @@ -2247,9 +2247,9 @@ oi: Optional[int] reveal_type(foo(None, None)) # N: Revealed type is "builtins.str" reveal_type(foo(None, 42)) # N: Revealed type is "builtins.int" reveal_type(foo(42, 42)) # N: Revealed type is "builtins.int" -reveal_type(foo(oi, None)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(foo(oi, None)) # N: Revealed type is "builtins.int | builtins.str" reveal_type(foo(oi, 42)) # N: Revealed type is "builtins.int" -reveal_type(foo(oi, oi)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(foo(oi, oi)) # N: Revealed type is "builtins.int | builtins.str" @overload def foo_list(x: None) -> None: ... @@ -2257,7 +2257,7 @@ def foo_list(x: None) -> None: ... def foo_list(x: T) -> List[T]: ... def foo_list(x): ... -reveal_type(foo_list(oi)) # N: Revealed type is "Union[builtins.list[builtins.int], None]" +reveal_type(foo_list(oi)) # N: Revealed type is "builtins.list[builtins.int] | None" # What if 'T' is 'object'? @overload @@ -3201,10 +3201,10 @@ def f1(x: C) -> D: ... def f1(x): ... arg1: Union[A, C] -reveal_type(f1(arg1)) # N: Revealed type is "Union[__main__.B, __main__.D]" +reveal_type(f1(arg1)) # N: Revealed type is "__main__.B | __main__.D" arg2: Union[A, B] -f1(arg2) # E: Argument 1 to "f1" has incompatible type "Union[A, B]"; expected "A" +f1(arg2) # E: Argument 1 to "f1" has incompatible type "A | B"; expected "A" @overload def f2(x: A) -> B: ... @@ -3242,12 +3242,12 @@ reveal_type(f2(arg1, C())) [out] main:15: note: Revealed type is "__main__.B" -main:15: error: Argument 1 to "f1" has incompatible type "Union[A, C]"; expected "A" -main:15: error: Argument 2 to "f1" has incompatible type "Union[A, C]"; expected "C" +main:15: error: Argument 1 to "f1" has incompatible type "A | C"; expected "A" +main:15: error: Argument 2 to "f1" has incompatible type "A | C"; expected "C" main:23: note: Revealed type is "__main__.B" -main:23: error: Argument 1 to "f2" has incompatible type "Union[A, C]"; expected "A" -main:23: error: Argument 2 to "f2" has incompatible type "Union[A, C]"; expected "C" -main:24: note: Revealed type is "Union[__main__.B, __main__.D]" +main:23: error: Argument 1 to "f2" has incompatible type "A | C"; expected "A" +main:23: error: Argument 2 to "f2" has incompatible type "A | C"; expected "C" +main:24: note: Revealed type is "__main__.B | __main__.D" [case testOverloadInferUnionRespectsVariance] from typing import overload, TypeVar, Union, Generic @@ -3269,10 +3269,10 @@ def foo(x: WrapperContra[B]) -> str: ... def foo(x): pass compat: Union[WrapperCo[C], WrapperContra[A]] -reveal_type(foo(compat)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(foo(compat)) # N: Revealed type is "builtins.int | builtins.str" not_compat: Union[WrapperCo[A], WrapperContra[C]] -foo(not_compat) # E: Argument 1 to "foo" has incompatible type "Union[WrapperCo[A], WrapperContra[C]]"; expected "WrapperCo[B]" +foo(not_compat) # E: Argument 1 to "foo" has incompatible type "WrapperCo[A] | WrapperContra[C]"; expected "WrapperCo[B]" [case testOverloadInferUnionIfParameterNamesAreDifferent] from typing import overload, Union @@ -3290,7 +3290,7 @@ def f(x): ... x: Union[A, B] reveal_type(f(A())) # N: Revealed type is "__main__.B" reveal_type(f(B())) # N: Revealed type is "__main__.C" -reveal_type(f(x)) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(f(x)) # N: Revealed type is "__main__.B | __main__.C" [case testOverloadInferUnionReturnFunctionsWithKwargs] from typing import overload, Union, Optional @@ -3312,7 +3312,7 @@ reveal_type(f(A(), B())) # N: Revealed type is "__main__.C" reveal_type(f(A(), C())) # N: Revealed type is "__main__.B" arg: Union[B, C] -reveal_type(f(A(), arg)) # N: Revealed type is "Union[__main__.C, __main__.B]" +reveal_type(f(A(), arg)) # N: Revealed type is "__main__.C | __main__.B" reveal_type(f(A())) # N: Revealed type is "__main__.D" [builtins fixtures/tuple.pyi] @@ -3334,7 +3334,7 @@ def f(*args): ... x: Union[A, B] reveal_type(f(x)) # N: Revealed type is "__main__.Parent" -f(x, B()) # E: Argument 1 to "f" has incompatible type "Union[A, B]"; expected "B" +f(x, B()) # E: Argument 1 to "f" has incompatible type "A | B"; expected "B" [builtins fixtures/tuple.pyi] [case testOverloadInferUnionWithMixOfPositionalAndOptionalArgs] @@ -3352,9 +3352,9 @@ def f(*args): ... x: Union[A, B] y: Optional[A] z: Union[A, Optional[B]] -reveal_type(f(x)) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(f(y)) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(f(z)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(f(x)) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(f(y)) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(f(z)) # N: Revealed type is "builtins.int | builtins.str" reveal_type(f()) # N: Revealed type is "builtins.str" [builtins fixtures/tuple.pyi] @@ -3381,7 +3381,7 @@ x: Union[A, B] reveal_type(obj.f(A())) # N: Revealed type is "__main__.C" reveal_type(obj.f(B())) # N: Revealed type is "__main__.B" -reveal_type(obj.f(x)) # N: Revealed type is "Union[__main__.C, __main__.B]" +reveal_type(obj.f(x)) # N: Revealed type is "__main__.C | __main__.B" [case testOverloadingInferUnionReturnWithFunctionTypevarReturn] from typing import overload, Union, TypeVar, Generic @@ -3411,10 +3411,10 @@ def wrapper() -> None: obj2: Union[W1[A], W2[B]] - reveal_type(foo(obj2)) # N: Revealed type is "Union[__main__.A, __main__.B]" + reveal_type(foo(obj2)) # N: Revealed type is "__main__.A | __main__.B" bar(obj2) # E: Cannot infer value of type parameter "T" of "bar" - b1_overload: A = foo(obj2) # E: Incompatible types in assignment (expression has type "Union[A, B]", variable has type "A") + b1_overload: A = foo(obj2) # E: Incompatible types in assignment (expression has type "A | B", variable has type "A") b1_union: A = bar(obj2) # E: Cannot infer value of type parameter "T" of "bar" [case testOverloadingInferUnionReturnWithObjectTypevarReturn] @@ -3444,11 +3444,11 @@ def wrapper() -> None: # Note: These should be fine, but mypy has an unrelated bug # that makes them error out? - a2_overload: A = SomeType().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "Union[W1[A], W2[A]]"; expected "W1[Never]" - a2_union: A = SomeType().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "Union[W1[A], W2[A]]"; expected "Union[W1[Never], W2[Never]]" + a2_overload: A = SomeType().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "W1[A] | W2[A]"; expected "W1[Never]" + a2_union: A = SomeType().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "W1[A] | W2[A]"; expected "W1[Never] | W2[Never]" - SomeType().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "Union[W1[A], W2[A]]"; expected "W1[Never]" - SomeType().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "Union[W1[A], W2[A]]"; expected "Union[W1[Never], W2[Never]]" + SomeType().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "W1[A] | W2[A]"; expected "W1[Never]" + SomeType().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "W1[A] | W2[A]"; expected "W1[Never] | W2[Never]" [case testOverloadingInferUnionReturnWithBadObjectTypevarReturn] from typing import overload, Union, TypeVar, Generic @@ -3472,14 +3472,14 @@ class SomeType(Generic[T]): def wrapper(mysterious: T) -> T: obj1: Union[W1[A], W2[B]] - SomeType().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "Union[W1[A], W2[B]]"; expected "W1[Never]" - SomeType().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "Union[W1[A], W2[B]]"; expected "Union[W1[Never], W2[Never]]" + SomeType().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "W1[A] | W2[B]"; expected "W1[Never]" + SomeType().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "W1[A] | W2[B]"; expected "W1[Never] | W2[Never]" - SomeType[A]().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "Union[W1[A], W2[B]]"; expected "W1[A]" - SomeType[A]().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "Union[W1[A], W2[B]]"; expected "Union[W1[A], W2[A]]" + SomeType[A]().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "W1[A] | W2[B]"; expected "W1[A]" + SomeType[A]().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "W1[A] | W2[B]"; expected "W1[A] | W2[A]" - SomeType[T]().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "Union[W1[A], W2[B]]"; expected "W1[T]" - SomeType[T]().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "Union[W1[A], W2[B]]"; expected "Union[W1[T], W2[T]]" + SomeType[T]().foo(obj1) # E: Argument 1 to "foo" of "SomeType" has incompatible type "W1[A] | W2[B]"; expected "W1[T]" + SomeType[T]().bar(obj1) # E: Argument 1 to "bar" of "SomeType" has incompatible type "W1[A] | W2[B]"; expected "W1[T] | W2[T]" return mysterious @@ -3505,7 +3505,7 @@ T1 = TypeVar('T1', bound=A) def t_is_same_bound(arg1: T1, arg2: S) -> Tuple[T1, S]: x1: Union[List[S], List[Tuple[T1, S]]] y1: S - reveal_type(Dummy[T1]().foo(x1, y1)) # N: Revealed type is "Union[S`-2, T1`-1]" + reveal_type(Dummy[T1]().foo(x1, y1)) # N: Revealed type is "S`-2 | T1`-1" x2: Union[List[T1], List[Tuple[T1, T1]]] y2: T1 @@ -3539,12 +3539,12 @@ def t_is_same_bound(arg1: T1, arg2: S) -> Tuple[T1, S]: x3: Union[List[S], List[Tuple[S, T1]]] y3: S Dummy[T1]().foo(x3, y3) # E: Cannot infer value of type parameter "S" of "foo" of "Dummy" \ - # E: Argument 1 to "foo" of "Dummy" has incompatible type "Union[list[S], list[tuple[S, T1]]]"; expected "list[tuple[T1, Any]]" + # E: Argument 1 to "foo" of "Dummy" has incompatible type "list[S] | list[tuple[S, T1]]"; expected "list[tuple[T1, Any]]" x4: Union[List[int], List[Tuple[C, int]]] y4: int - reveal_type(Dummy[C]().foo(x4, y4)) # N: Revealed type is "Union[builtins.int, __main__.C]" - Dummy[A]().foo(x4, y4) # E: Argument 1 to "foo" of "Dummy" has incompatible type "Union[list[int], list[tuple[C, int]]]"; expected "list[tuple[A, int]]" + reveal_type(Dummy[C]().foo(x4, y4)) # N: Revealed type is "builtins.int | __main__.C" + Dummy[A]().foo(x4, y4) # E: Argument 1 to "foo" of "Dummy" has incompatible type "list[int] | list[tuple[C, int]]"; expected "list[tuple[A, int]]" return arg1, arg2 @@ -3572,7 +3572,7 @@ T1 = TypeVar('T1', bound=B) def t_is_tighter_bound(arg1: T1, arg2: S) -> Tuple[T1, S]: x1: Union[List[S], List[Tuple[T1, S]]] y1: S - reveal_type(Dummy[T1]().foo(x1, y1)) # N: Revealed type is "Union[S`-2, T1`-1]" + reveal_type(Dummy[T1]().foo(x1, y1)) # N: Revealed type is "S`-2 | T1`-1" x2: Union[List[T1], List[Tuple[T1, T1]]] y2: T1 @@ -3614,8 +3614,8 @@ def t_is_compatible_bound(arg1: T3, arg2: S) -> Tuple[T3, S]: [builtins fixtures/list.pyi] [out] -main:22: note: Revealed type is "Union[S`-2, __main__.B]" -main:22: note: Revealed type is "Union[S`-2, __main__.C]" +main:22: note: Revealed type is "S`-2 | __main__.B" +main:22: note: Revealed type is "S`-2 | __main__.C" main:26: note: Revealed type is "__main__.B" main:26: note: Revealed type is "__main__.C" @@ -3695,7 +3695,7 @@ b: int c: Optional[int] reveal_type(g(a)) # N: Revealed type is "builtins.int" reveal_type(g(b)) # N: Revealed type is "builtins.str" -reveal_type(g(c)) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(g(c)) # N: Revealed type is "builtins.str | builtins.int" [case testOverloadsNoneAndTypeVarsWithNoStrictOptional] # flags: --no-strict-optional @@ -3741,7 +3741,7 @@ f3: Optional[Callable[[int], str]] reveal_type(mymap(f1, seq)) # N: Revealed type is "typing.Iterable[builtins.str]" reveal_type(mymap(f2, seq)) # N: Revealed type is "typing.Iterable[builtins.int]" -reveal_type(mymap(f3, seq)) # N: Revealed type is "Union[typing.Iterable[builtins.str], typing.Iterable[builtins.int]]" +reveal_type(mymap(f3, seq)) # N: Revealed type is "typing.Iterable[builtins.str] | typing.Iterable[builtins.int]" [builtins fixtures/list.pyi] [typing fixtures/typing-medium.pyi] @@ -4586,7 +4586,7 @@ x: Union[int, str] reveal_type(Parent().foo(3)) # N: Revealed type is "__main__.Parent" reveal_type(Child().foo(3)) # N: Revealed type is "__main__.Child" reveal_type(Child().foo("...")) # N: Revealed type is "builtins.str" -reveal_type(Child().foo(x)) # N: Revealed type is "Union[__main__.Child, builtins.str]" +reveal_type(Child().foo(x)) # N: Revealed type is "__main__.Child | builtins.str" reveal_type(Child().foo(3).child_only()) # N: Revealed type is "builtins.int" [case testOverloadAndSelfTypesGenericNoOverlap] @@ -4634,7 +4634,7 @@ x: Union[int, str] reveal_type(Parent.foo(3)) # N: Revealed type is "type[__main__.Parent]" reveal_type(Child.foo(3)) # N: Revealed type is "type[__main__.Child]" reveal_type(Child.foo("...")) # N: Revealed type is "builtins.str" -reveal_type(Child.foo(x)) # N: Revealed type is "Union[type[__main__.Child], builtins.str]" +reveal_type(Child.foo(x)) # N: Revealed type is "type[__main__.Child] | builtins.str" reveal_type(Child.foo(3)().child_only()) # N: Revealed type is "builtins.int" [builtins fixtures/classmethod.pyi] @@ -4670,8 +4670,8 @@ x: Union[int, str] y: Union[int, str] f(x, y) [out] -main:12: error: Argument 1 to "f" has incompatible type "Union[int, str]"; expected "int" -main:12: error: Argument 2 to "f" has incompatible type "Union[int, str]"; expected "int" +main:12: error: Argument 1 to "f" has incompatible type "int | str"; expected "int" +main:12: error: Argument 2 to "f" has incompatible type "int | str"; expected "int" [case testUnionMathTrickyOverload2] from typing import overload, Union, Any @@ -4688,7 +4688,7 @@ class D(C): x: D y: Union[D, Any] -reveal_type(x.f(y)) # N: Revealed type is "Union[__main__.D, Any]" +reveal_type(x.f(y)) # N: Revealed type is "__main__.D | Any" [out] [case testManyUnionsInOverload] @@ -4710,7 +4710,7 @@ class B: pass x: Union[int, str, A, B] y = f(x, x, x, x, x, x, x, x) # 8 args -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str, __main__.A, __main__.B]" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str | __main__.A | __main__.B" [builtins fixtures/dict.pyi] [out] @@ -4774,14 +4774,14 @@ f(x, x, x, x, x, x, x, x) [builtins fixtures/tuple.pyi] [out] main:11: error: Not all union combinations were tried because there are too many unions -main:11: error: Argument 1 to "f" has incompatible type "Union[int, str]"; expected "int" -main:11: error: Argument 2 to "f" has incompatible type "Union[int, str]"; expected "int" -main:11: error: Argument 3 to "f" has incompatible type "Union[int, str]"; expected "int" -main:11: error: Argument 4 to "f" has incompatible type "Union[int, str]"; expected "int" -main:11: error: Argument 5 to "f" has incompatible type "Union[int, str]"; expected "int" -main:11: error: Argument 6 to "f" has incompatible type "Union[int, str]"; expected "int" -main:11: error: Argument 7 to "f" has incompatible type "Union[int, str]"; expected "int" -main:11: error: Argument 8 to "f" has incompatible type "Union[int, str]"; expected "int" +main:11: error: Argument 1 to "f" has incompatible type "int | str"; expected "int" +main:11: error: Argument 2 to "f" has incompatible type "int | str"; expected "int" +main:11: error: Argument 3 to "f" has incompatible type "int | str"; expected "int" +main:11: error: Argument 4 to "f" has incompatible type "int | str"; expected "int" +main:11: error: Argument 5 to "f" has incompatible type "int | str"; expected "int" +main:11: error: Argument 6 to "f" has incompatible type "int | str"; expected "int" +main:11: error: Argument 7 to "f" has incompatible type "int | str"; expected "int" +main:11: error: Argument 8 to "f" has incompatible type "int | str"; expected "int" [case testSafeDunderOverlapInSubclass] from typing import overload @@ -5208,7 +5208,7 @@ def foo() -> None: ... def foo(iterable: Iterable[_T]) -> None: ... def foo(iterable = None) -> None: pass -foo(bar('lol').foo()) # E: Item "int" of "Union[A, int]" has no attribute "foo" \ +foo(bar('lol').foo()) # E: Item "int" of "A | int" has no attribute "foo" \ # E: Argument 1 to "bar" has incompatible type "str"; expected "int" @@ -5344,7 +5344,7 @@ def compose(f: Callable[[U], V], g: Callable[[W], U]) -> Callable[[W], V]: ID = NewType("ID", fakeint) compose(ID, fakeint)("test") -reveal_type(compose(ID, fakeint)) # N: Revealed type is "def (Union[builtins.str, builtins.bytes]) -> __main__.ID" +reveal_type(compose(ID, fakeint)) # N: Revealed type is "def (builtins.str | builtins.bytes) -> __main__.ID" [builtins fixtures/tuple.pyi] @@ -6863,5 +6863,5 @@ if isinstance(headers, dict): (always_bytes(k), always_bytes(v)) for k, v in headers.items() ) -reveal_type(headers) # N: Revealed type is "Union[__main__.Headers, typing.Iterable[tuple[builtins.bytes, builtins.bytes]]]" +reveal_type(headers) # N: Revealed type is "__main__.Headers | typing.Iterable[tuple[builtins.bytes, builtins.bytes]]" [builtins fixtures/isinstancelist.pyi] diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index bffd34782f51..f494f943ace0 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -1509,9 +1509,9 @@ def foo(x: int) -> str: ... def foo2(__x: int) -> Callable[[int], str]: ... x: C[[int, str]] -reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> Union[builtins.int, ...]" +reveal_type(x) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int | ..." y: C[int, str] -reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> Union[builtins.int, ...]" +reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int | ..." [builtins fixtures/paramspec.pyi] [case testParamSpecAliasInRuntimeContext] @@ -2171,15 +2171,15 @@ def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ... @dec def func(arg: T) -> Union[T, str]: ... -reveal_type(func) # N: Revealed type is "def [T] (arg: T`-1) -> builtins.list[Union[T`-1, builtins.str]]" -reveal_type(func(42)) # N: Revealed type is "builtins.list[Union[builtins.int, builtins.str]]" +reveal_type(func) # N: Revealed type is "def [T] (arg: T`-1) -> builtins.list[T`-1 | builtins.str]" +reveal_type(func(42)) # N: Revealed type is "builtins.list[builtins.int | builtins.str]" def dec2(f: Callable[P, List[T]]) -> Callable[P, T]: ... @dec2 def func2(arg: T) -> List[Union[T, str]]: ... -reveal_type(func2) # N: Revealed type is "def [T] (arg: T`-1) -> Union[T`-1, builtins.str]" -reveal_type(func2(42)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(func2) # N: Revealed type is "def [T] (arg: T`-1) -> T`-1 | builtins.str" +reveal_type(func2(42)) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/paramspec.pyi] [case testParamSpecPreciseKindsUsedIfPossible] @@ -2311,7 +2311,7 @@ def capture( def fn() -> str: return '' def err() -> NoReturn: ... -reveal_type(capture(fn)) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(capture(fn)) # N: Revealed type is "builtins.str | builtins.int" reveal_type(capture(err)) # N: Revealed type is "builtins.int" [builtins fixtures/paramspec.pyi] @@ -2554,7 +2554,7 @@ class ServerErrorMiddleware(App): def fn(f: MiddlewareFactory[P]) -> Capture[P]: ... -reveal_type(fn(ServerErrorMiddleware)) # N: Revealed type is "__main__.Capture[[handler: Union[builtins.str, None] =, debug: builtins.bool =]]" +reveal_type(fn(ServerErrorMiddleware)) # N: Revealed type is "__main__.Capture[[handler: builtins.str | None =, debug: builtins.bool =]]" [builtins fixtures/paramspec.pyi] [case testRunParamSpecDuplicateArgsKwargs] @@ -2595,7 +2595,7 @@ def run3(predicate: Callable[Concatenate[int, str, _P], None], *args: _P.args, * base_ok: tuple[int, str] predicate(*base_ok, *args, **kwargs) base_bad: tuple[Union[int, str], ...] - predicate(*base_bad, *args, **kwargs) # E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "int" \ - # E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "str" \ - # E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "_P.args" + predicate(*base_bad, *args, **kwargs) # E: Argument 1 has incompatible type "*tuple[int | str, ...]"; expected "int" \ + # E: Argument 1 has incompatible type "*tuple[int | str, ...]"; expected "str" \ + # E: Argument 1 has incompatible type "*tuple[int | str, ...]"; expected "_P.args" [builtins fixtures/paramspec.pyi] diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index 91c53f0125a3..6cdee6fa578a 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -818,7 +818,7 @@ class B: AOrB = Union[A, B] -reveal_type(A) # N: Revealed type is "def (frob: builtins.list[Union[__main__.A, __main__.B]]) -> __main__.A" +reveal_type(A) # N: Revealed type is "def (frob: builtins.list[__main__.A | __main__.B]) -> __main__.A" reveal_type(B) # N: Revealed type is "def () -> __main__.B" A([B()]) @@ -2166,8 +2166,8 @@ class B: a_or_b: A[int] | B a2 = attrs.evolve(a_or_b, x=42, y=True) -a2 = attrs.evolve(a_or_b, x=42, y=True, z='42') # E: Argument "z" to "evolve" of "Union[A[int], B]" has incompatible type "str"; expected "Never" -a2 = attrs.evolve(a_or_b, x=42, y=True, w={}) # E: Argument "w" to "evolve" of "Union[A[int], B]" has incompatible type "dict[Never, Never]"; expected "Never" +a2 = attrs.evolve(a_or_b, x=42, y=True, z='42') # E: Argument "z" to "evolve" of "A[int] | B" has incompatible type "str"; expected "Never" +a2 = attrs.evolve(a_or_b, x=42, y=True, w={}) # E: Argument "w" to "evolve" of "A[int] | B" has incompatible type "dict[Never, Never]"; expected "Never" [builtins fixtures/plugin_attrs.pyi] @@ -2191,8 +2191,8 @@ TA = TypeVar('TA', bound=A) TB = TypeVar('TB', bound=B) def f(b_or_t: TA | TB | int) -> None: - a2 = attrs.evolve(b_or_t) # E: Argument 1 to "evolve" has type "Union[TA, TB, int]" whose item "TB" is not bound to an attrs class \ - # E: Argument 1 to "evolve" has incompatible type "Union[TA, TB, int]" whose item "int" is not an attrs class + a2 = attrs.evolve(b_or_t) # E: Argument 1 to "evolve" has type "TA | TB | int" whose item "TB" is not bound to an attrs class \ + # E: Argument 1 to "evolve" has incompatible type "TA | TB | int" whose item "int" is not an attrs class [builtins fixtures/plugin_attrs.pyi] @@ -2489,5 +2489,5 @@ class Child(Parent[float]): Parent(run_type = None) c = Child(run_type = None) -reveal_type(c.run_type) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(c.run_type) # N: Revealed type is "builtins.int | None" [builtins fixtures/plugin_attrs.pyi] diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 99fd94a69e4d..b2f751bf3572 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -755,10 +755,10 @@ class D(A, B): pass x: P = D() # Same as P[Any, Any] -var: P[Union[int, P], Union[P, str]] = C() # E: Incompatible types in assignment (expression has type "C", variable has type "P[Union[int, P[Any, Any]], Union[P[Any, Any], str]]") \ +var: P[Union[int, P], Union[P, str]] = C() # E: Incompatible types in assignment (expression has type "C", variable has type "P[int | P[Any, Any], P[Any, Any] | str]") \ # N: Following member(s) of "C" have conflicts: \ - # N: attr1: expected "Union[int, P[Any, Any]]", got "int" \ - # N: attr2: expected "Union[P[Any, Any], str]", got "str" + # N: attr1: expected "int | P[Any, Any]", got "int" \ + # N: attr2: expected "P[Any, Any] | str", got "str" [case testGenericSubProtocolsExtensionCovariant] from typing import TypeVar, Protocol, Union @@ -780,14 +780,14 @@ class C: var: P[Union[int, P], Union[P, str]] = C() # OK for covariant var2: P[Union[str, P], Union[P, int]] = C() [out] -main:18: error: Incompatible types in assignment (expression has type "C", variable has type "P[Union[str, P[Any, Any]], Union[P[Any, Any], int]]") +main:18: error: Incompatible types in assignment (expression has type "C", variable has type "P[str | P[Any, Any], P[Any, Any] | int]") main:18: note: Following member(s) of "C" have conflicts: main:18: note: Expected: -main:18: note: def attr1(self) -> Union[str, P[Any, Any]] +main:18: note: def attr1(self) -> str | P[Any, Any] main:18: note: Got: main:18: note: def attr1(self) -> int main:18: note: Expected: -main:18: note: def attr2(self) -> Union[P[Any, Any], int] +main:18: note: def attr2(self) -> P[Any, Any] | int main:18: note: Got: main:18: note: def attr2(self) -> str @@ -1019,7 +1019,7 @@ main:14: note: b main:15: error: Incompatible types in assignment (expression has type "P1[Any]", variable has type "P2[Any]") main:15: note: Following member(s) of "P1[Any]" have conflicts: main:15: note: Expected: -main:15: note: def [T2] a(self, other: Union[P1[T2], P2[T2]]) -> Any +main:15: note: def [T2] a(self, other: P1[T2] | P2[T2]) -> Any main:15: note: Got: main:15: note: def [T2] a(self, other: P1[T2]) -> Any @@ -1286,9 +1286,9 @@ main:18: error: Incompatible types in assignment (expression has type "D", varia main:18: note: Following member(s) of "D" have conflicts: main:18: note: Expected: main:18: note: @overload -main:18: note: def f(self, x: int) -> Optional[int] +main:18: note: def f(self, x: int) -> int | None main:18: note: @overload -main:18: note: def f(self, x: str) -> Optional[str] +main:18: note: def f(self, x: str) -> str | None main:18: note: Got: main:18: note: def f(self, x: int) -> None @@ -1564,7 +1564,7 @@ x = C1() if int(): x = C2() x = C() - x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "Union[P1, P2]") + x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "P1 | P2") [case testUnionsOfNormalClassesWithProtocols] from typing import Protocol, Union @@ -1593,11 +1593,11 @@ x: Union[C1, C2] y: Union[C1, D1] z: Union[C, D1] -f1(x) # E: Argument 1 to "f1" has incompatible type "Union[C1, C2]"; expected "P1" +f1(x) # E: Argument 1 to "f1" has incompatible type "C1 | C2"; expected "P1" f1(y) f1(z) -f2(x) # E: Argument 1 to "f2" has incompatible type "Union[C1, C2]"; expected "P2" -f2(z) # E: Argument 1 to "f2" has incompatible type "Union[C, D1]"; expected "P2" +f2(x) # E: Argument 1 to "f2" has incompatible type "C1 | C2"; expected "P2" +f2(z) # E: Argument 1 to "f2" has incompatible type "C | D1"; expected "P2" -- Type[] with protocol types -- -------------------------- @@ -2304,7 +2304,7 @@ main:14: note: Expected: main:14: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None main:14: note: Got: main:14: note: def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None -main:15: error: Argument 1 to "func2" has incompatible type "B"; expected "Optional[A]" +main:15: error: Argument 1 to "func2" has incompatible type "B"; expected "A | None" main:15: note: Following member(s) of "B" have conflicts: main:15: note: Expected: main:15: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None @@ -2393,7 +2393,7 @@ main:10: note: def meth(self, x: int, *args: str) -> None main:10: note: Got: main:10: note: def meth(self) -> int main:10: note: Expected: -main:10: note: def other(self, *args: Any, hint: Optional[str] = ..., **kwargs: str) -> None +main:10: note: def other(self, *args: Any, hint: str | None = ..., **kwargs: str) -> None main:10: note: Got: main:10: note: def other(self) -> int @@ -2573,7 +2573,7 @@ def func(caller: Caller) -> None: pass func(call) -func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[Union[int, str]], Union[int, str]]"; expected "Caller" \ +func(bad) # E: Argument 1 to "func" has incompatible type "Callable[[int | str], int | str]"; expected "Caller" \ # N: "Caller.__call__" has type overloaded function [out] @@ -3561,7 +3561,7 @@ test(C) # E: Argument 1 to "test" has incompatible type "type[C]"; expected "P" # N: Expected: \ # N: def [T] foo(arg: T) -> T \ # N: Got: \ - # N: def [T] foo(self: T) -> Union[T, int] + # N: def [T] foo(self: T) -> T | int [case testProtocolClassObjectSelfTypeClassMethod] from typing import Protocol, Type, TypeVar @@ -3755,7 +3755,7 @@ test(c) # E: Argument 1 to "test" has incompatible type "type[C]"; expected "P" # N: Expected: \ # N: def [T] foo(arg: T) -> T \ # N: Got: \ - # N: def [T] foo(self: T) -> Union[T, int] + # N: def [T] foo(self: T) -> T | int [case testProtocolClassObjectInference] from typing import Any, Protocol, TypeVar diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 8bc781d091c3..6a6409f7aee6 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -311,7 +311,7 @@ m: Tuple[int, str, float, bool] match m: case [a, *b, c]: reveal_type(a) # N: Revealed type is "builtins.int" - reveal_type(b) # N: Revealed type is "builtins.list[Union[builtins.str, builtins.float]]" + reveal_type(b) # N: Revealed type is "builtins.list[builtins.str | builtins.float]" reveal_type(c) # N: Revealed type is "builtins.bool" reveal_type(m) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.float, builtins.bool]" [builtins fixtures/list.pyi] @@ -1338,7 +1338,7 @@ m: object match m: case 1 | 2 as n: - reveal_type(n) # N: Revealed type is "Union[Literal[1], Literal[2]]" + reveal_type(n) # N: Revealed type is "Literal[1] | Literal[2]" [case testMatchAsPatternAlreadyNarrower] m: bool @@ -1354,21 +1354,21 @@ m: object match m: case 1 | 2: - reveal_type(m) # N: Revealed type is "Union[Literal[1], Literal[2]]" + reveal_type(m) # N: Revealed type is "Literal[1] | Literal[2]" [case testMatchOrPatternNarrowsStr] m: object match m: case "foo" | "bar": - reveal_type(m) # N: Revealed type is "Union[Literal['foo'], Literal['bar']]" + reveal_type(m) # N: Revealed type is "Literal['foo'] | Literal['bar']" [case testMatchOrPatternNarrowsUnion] m: object match m: case 1 | "foo": - reveal_type(m) # N: Revealed type is "Union[Literal[1], Literal['foo']]" + reveal_type(m) # N: Revealed type is "Literal[1] | Literal['foo']" [case testMatchOrPatternCapturesMissing] from typing import List @@ -1376,7 +1376,7 @@ m: List[int] match m: case [x, y] | list(x): # E: Alternative patterns bind different names - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.list[builtins.int]" reveal_type(y) # N: Revealed type is "builtins.int" [builtins fixtures/list.pyi] @@ -1385,7 +1385,7 @@ m: object match m: case list(x) | dict(x): - reveal_type(x) # N: Revealed type is "Union[builtins.list[Any], builtins.dict[Any, Any]]" + reveal_type(x) # N: Revealed type is "builtins.list[Any] | builtins.dict[Any, Any]" [builtins fixtures/dict.pyi] -- Interactions -- @@ -1399,7 +1399,7 @@ match m: case str(x): reveal_type(x) # N: Revealed type is "builtins.str" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testMatchCapturePatternMultipleCaptures] from typing import Iterable @@ -1463,7 +1463,7 @@ def main() -> None: match func2(True): case c: reveal_type(a) # N: Revealed type is "builtins.str" - reveal_type(c) # N: Revealed type is "Union[builtins.bytes, builtins.int]" + reveal_type(c) # N: Revealed type is "builtins.bytes | builtins.int" reveal_type(a) # N: Revealed type is "builtins.str" case a: reveal_type(a) # N: Revealed type is "builtins.int" @@ -1478,7 +1478,7 @@ async def main() -> None: match await func2(True): case c: reveal_type(a) # N: Revealed type is "builtins.str" - reveal_type(c) # N: Revealed type is "Union[builtins.bytes, builtins.int]" + reveal_type(c) # N: Revealed type is "builtins.bytes | builtins.int" reveal_type(a) # N: Revealed type is "builtins.str" case a: reveal_type(a) # N: Revealed type is "builtins.int" @@ -1555,7 +1555,7 @@ def func() -> Optional[str]: ... match m := func(): case _ if not m: - reveal_type(m) # N: Revealed type is "Union[Literal[''], None]" + reveal_type(m) # N: Revealed type is "Literal[''] | None" case _: reveal_type(m) # N: Revealed type is "builtins.str" @@ -1581,8 +1581,8 @@ m: Union[str, bytes, int] match m: case str(a) | bytes(a): - reveal_type(a) # N: Revealed type is "Union[builtins.str, builtins.bytes]" - reveal_type(m) # N: Revealed type is "Union[builtins.str, builtins.bytes]" + reveal_type(a) # N: Revealed type is "builtins.str | builtins.bytes" + reveal_type(m) # N: Revealed type is "builtins.str | builtins.bytes" case b: reveal_type(b) # N: Revealed type is "builtins.int" @@ -1689,7 +1689,7 @@ match m1: case [int()]: reveal_type(m1) # N: Revealed type is "typing.Sequence[builtins.int]" case r: - reveal_type(m1) # N: Revealed type is "typing.Sequence[Union[builtins.int, builtins.str]]" + reveal_type(m1) # N: Revealed type is "typing.Sequence[builtins.int | builtins.str]" m2: Tuple[int | str] @@ -1705,7 +1705,7 @@ match m3: case (1,): reveal_type(m3) # N: Revealed type is "tuple[Literal[1]]" case r2: - reveal_type(m3) # N: Revealed type is "tuple[Union[builtins.int, builtins.str]]" + reveal_type(m3) # N: Revealed type is "tuple[builtins.int | builtins.str]" m4: Tuple[Literal[1], int] @@ -1721,9 +1721,9 @@ m5: Tuple[Literal[1, 2], Literal["a", "b"]] match m5: case (1, str()): - reveal_type(m5) # N: Revealed type is "tuple[Literal[1], Union[Literal['a'], Literal['b']]]" + reveal_type(m5) # N: Revealed type is "tuple[Literal[1], Literal['a'] | Literal['b']]" case _: - reveal_type(m5) # N: Revealed type is "tuple[Literal[2], Union[Literal['a'], Literal['b']]]" + reveal_type(m5) # N: Revealed type is "tuple[Literal[2], Literal['a'] | Literal['b']]" m6: Tuple[Literal[1, 2], Literal["a", "b"]] @@ -1731,7 +1731,7 @@ match m6: case (1, "a"): reveal_type(m6) # N: Revealed type is "tuple[Literal[1], Literal['a']]" case _: - reveal_type(m6) # N: Revealed type is "tuple[Union[Literal[1], Literal[2]], Union[Literal['a'], Literal['b']]]" + reveal_type(m6) # N: Revealed type is "tuple[Literal[1] | Literal[2], Literal['a'] | Literal['b']]" [builtins fixtures/tuple.pyi] @@ -1769,7 +1769,7 @@ def f(m: Medal) -> int: reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.gold]" return 0 case _: - reveal_type(m) # N: Revealed type is "Union[Literal[__main__.Medal.silver], Literal[__main__.Medal.bronze]]" + reveal_type(m) # N: Revealed type is "Literal[__main__.Medal.silver] | Literal[__main__.Medal.bronze]" return 1 def g(m: Medal) -> int: @@ -1933,7 +1933,7 @@ def union(x: str | bool) -> None: match x: case True: return - reveal_type(x) # N: Revealed type is "Union[builtins.str, Literal[False]]" + reveal_type(x) # N: Revealed type is "builtins.str | Literal[False]" [builtins fixtures/tuple.pyi] [case testMatchNarrowDownUnionUsingClassPattern] @@ -2020,7 +2020,7 @@ def f(x: int | str | None) -> None: case str(): break reveal_type(x) # N: Revealed type is "None" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" [case testMatchNarrowDownWithStarred-skip] from typing import List @@ -2155,9 +2155,9 @@ var: tuple[int, int] | tuple[str, str] # TODO: we can infer better here. match var: case (42, a): - reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" case ("yes", b): - reveal_type(b) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(b) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testMatchNamedAndKeywordsAreTheSame] @@ -2371,7 +2371,7 @@ def f() -> None: match x := returns_a_or_none(): case A(): reveal_type(x.a) # N: Revealed type is "builtins.int" - reveal_type(x) # N: Revealed type is "Union[__main__.A, None]" + reveal_type(x) # N: Revealed type is "__main__.A | None" match x := returns_a(): case A(): reveal_type(x.a) # N: Revealed type is "builtins.int" @@ -2388,7 +2388,7 @@ def match_stmt_error1(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") match object(): case str(x): pass @@ -2410,7 +2410,7 @@ def match_stmt_error2(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") match [None]: case [x]: pass @@ -2420,7 +2420,7 @@ def match_stmt_error3(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") match {'a': None}: case {'a': x}: pass @@ -2430,7 +2430,7 @@ def match_stmt_error4(x: Optional[list[str]]) -> None: if x is None: x = ["a"] def nested() -> list[str]: - return x # E: Incompatible return value type (got "Optional[list[str]]", expected "list[str]") + return x # E: Incompatible return value type (got "list[str] | None", expected "list[str]") match ["a"]: case [*x]: pass @@ -2443,7 +2443,7 @@ def match_stmt_error5(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") match C(): case C(a=x): pass @@ -2533,7 +2533,7 @@ def f4(e: int | str | bytes) -> int: return 0 # E: Statement is unreachable case x if isinstance(x, str): return 0 - reveal_type(e) # N: Revealed type is "Union[builtins.int, builtins.bytes]" + reveal_type(e) # N: Revealed type is "builtins.int | builtins.bytes" return 0 [builtins fixtures/primitives.pyi] @@ -2584,7 +2584,7 @@ m3: Tuple[int, int, Unpack[Tuple[str, ...]], int, int] match m3: case [a3, *b3, c3]: reveal_type(a3) # N: Revealed type is "builtins.int" - reveal_type(b3) # N: Revealed type is "builtins.list[Union[builtins.int, builtins.str]]" + reveal_type(b3) # N: Revealed type is "builtins.list[builtins.int | builtins.str]" reveal_type(c3) # N: Revealed type is "builtins.int" [builtins fixtures/tuple.pyi] @@ -2759,7 +2759,7 @@ def fn1(x: Some | int | str) -> None: def value(): return 1 reveal_type(value) # N: Revealed type is "def () -> Any" - case Some(value): # E: Incompatible types in capture pattern (pattern captures type "Union[int, str]", variable has type "Callable[[], Any]") + case Some(value): # E: Incompatible types in capture pattern (pattern captures type "int | str", variable has type "Callable[[], Any]") pass def fn2(x: Some | int | str) -> None: @@ -2776,7 +2776,7 @@ def fn2(x: Some | int | str) -> None: # N: def value() -> int return 1 reveal_type(value) # N: Revealed type is "def () -> builtins.str" - case Some(value): # E: Incompatible types in capture pattern (pattern captures type "Union[int, str]", variable has type "Callable[[], str]") + case Some(value): # E: Incompatible types in capture pattern (pattern captures type "int | str", variable has type "Callable[[], str]") pass [builtins fixtures/dict.pyi] @@ -2932,7 +2932,7 @@ def f1(x: int | str | list[bytes]) -> None: reveal_type(y) # N: Revealed type is "builtins.str" case [y]: reveal_type(y) # N: Revealed type is "builtins.bytes" - reveal_type(y) # N: Revealed type is "Union[builtins.str, builtins.bytes]" + reveal_type(y) # N: Revealed type is "builtins.str | builtins.bytes" [case testNewRedefineLoopWithMatch] # flags: --allow-redefinition-new --local-partial-types diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index c2a0bb09810a..b44af8936b97 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -27,7 +27,7 @@ class Custom(Exception): ... try: pass except* (RuntimeError, Custom) as e: - reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Union[builtins.RuntimeError, __main__.Custom]]" + reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.RuntimeError | __main__.Custom]" [builtins fixtures/exception.pyi] [case testTryStarInvalidType] @@ -49,7 +49,7 @@ except* ExceptionGroup as e: # E: Exception type in except* cannot derive from try: pass except* (RuntimeError, ExceptionGroup) as e: # E: Exception type in except* cannot derive from BaseExceptionGroup - reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Union[builtins.RuntimeError, Any]]" + reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.RuntimeError | Any]" [builtins fixtures/exception.pyi] [case testBasicTypeVarTupleGeneric] diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index a67ed4abcffa..0521722b47c1 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -59,7 +59,7 @@ Ts = TypeVarTuple("Ts") TestType = TypeAliasType("TestType", int | str) x: TestType = 42 y: TestType = 'a' -z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]") +z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int | str") BadAlias1 = TypeAliasType("BadAlias1", tuple[*Ts]) # E: TypeVarTuple "Ts" is not included in type_params ba1: BadAlias1[int] # E: Bad number of arguments for type alias, expected 0, given 1 @@ -122,7 +122,7 @@ class C[T]: a: C[int] = C[object]() # E: Incompatible types in assignment (expression has type "C[object]", variable has type "C[int]") b: C[object] = C[int]() -reveal_type(C[str]().m(1)) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(C[str]().m(1)) # N: Revealed type is "builtins.str | builtins.int" [case testPEP695InferVarianceSimpleFromMethod] class Invariant[T]: @@ -602,7 +602,7 @@ reveal_type(a3) # N: Revealed type is "__main__.D[builtins.str, __main__.C[buil type A4 = int | str a4: A4 -reveal_type(a4) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(a4) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/type.pyi] [case testPEP695TypeAliasNotValidAsBaseClass] @@ -703,7 +703,7 @@ reveal_type(a) # N: Revealed type is "Any" type A = int | 1 # E: Invalid type: try using Literal[1] instead? a: A -reveal_type(a) # N: Revealed type is "Union[builtins.int, Any]" +reveal_type(a) # N: Revealed type is "builtins.int | Any" type B = int + str # E: Invalid type alias: expression is not a valid type b: B reveal_type(b) # N: Revealed type is "Any" @@ -905,7 +905,7 @@ from typing import Callable type C[**P] = Callable[P, int] f: C[[str, int | None]] -reveal_type(f) # N: Revealed type is "def (builtins.str, Union[builtins.int, None]) -> builtins.int" +reveal_type(f) # N: Revealed type is "def (builtins.str, builtins.int | None) -> builtins.int" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] @@ -959,7 +959,7 @@ def g[T: int, S: (str, None)](x: T, y: S) -> T | S: [out2] tmp/a.py:2: note: Revealed type is "builtins.int" -tmp/a.py:3: note: Revealed type is "Union[builtins.int, builtins.str]" +tmp/a.py:3: note: Revealed type is "builtins.int | builtins.str" tmp/a.py:4: error: Value of type variable "T" of "g" cannot be "str" tmp/a.py:5: error: Value of type variable "S" of "g" cannot be "int" @@ -1279,13 +1279,13 @@ class C[T]: [case testPEP695RecursiceTypeAlias] type A = str | list[A] a: A -reveal_type(a) # N: Revealed type is "Union[builtins.str, builtins.list[...]]" +reveal_type(a) # N: Revealed type is "builtins.str | builtins.list[...]" class C[T]: pass type B[T] = C[T] | list[B[T]] b: B[int] -reveal_type(b) # N: Revealed type is "Union[__main__.C[builtins.int], builtins.list[...]]" +reveal_type(b) # N: Revealed type is "__main__.C[builtins.int] | builtins.list[...]" [builtins fixtures/type.pyi] [case testPEP695BadRecursiveTypeAlias] @@ -1303,7 +1303,7 @@ def f(a: A) -> None: if isinstance(a, str): reveal_type(a) # N: Revealed type is "builtins.str" else: - reveal_type(a) # N: Revealed type is "__main__.C[Union[builtins.str, __main__.C[...]]]" + reveal_type(a) # N: Revealed type is "__main__.C[builtins.str | __main__.C[...]]" type A = str | C[A] @@ -1523,14 +1523,14 @@ class C: reveal_type(v) # N: Revealed type is "builtins.int" reveal_type(C.a) # N: Revealed type is "builtins.int" -reveal_type(C.b) # N: Revealed type is "Union[builtins.list[builtins.str], None]" +reveal_type(C.b) # N: Revealed type is "builtins.list[builtins.str] | None" C.A = str # E: Incompatible types in assignment (expression has type "type[str]", variable has type "TypeAliasType") x: C.A y: C.B[int] reveal_type(x) # N: Revealed type is "builtins.int" -reveal_type(y) # N: Revealed type is "Union[builtins.list[builtins.int], None]" +reveal_type(y) # N: Revealed type is "builtins.list[builtins.int] | None" def f() -> None: type A = int @@ -1540,7 +1540,7 @@ def f() -> None: def g() -> None: b: B[int] - reveal_type(b) # N: Revealed type is "Union[builtins.list[builtins.int], None]" + reveal_type(b) # N: Revealed type is "builtins.list[builtins.int] | None" class D: def __init__(self) -> None: @@ -1763,7 +1763,7 @@ type I3 = None | C[TD] # flags: --enable-incomplete-feature=InlineTypedDict type X[T] = {"item": T, "other": X[T] | None} x: X[str] -reveal_type(x) # N: Revealed type is "TypedDict({'item': builtins.str, 'other': Union[..., None]})" +reveal_type(x) # N: Revealed type is "TypedDict({'item': builtins.str, 'other': ... | None})" if x["other"] is not None: reveal_type(x["other"]["item"]) # N: Revealed type is "builtins.str" @@ -1829,7 +1829,7 @@ y: B z: C zz: D reveal_type(x) # N: Revealed type is "builtins.int" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" reveal_type(z) # N: Revealed type is "builtins.int" reveal_type(zz) # N: Revealed type is "builtins.str" [builtins fixtures/tuple.pyi] @@ -2217,10 +2217,10 @@ from collections.abc import Hashable type HashableArg = int | tuple[Hashable | HashableArg] x: HashableArg -reveal_type(x) # N: Revealed type is "Union[builtins.int, tuple[Union[typing.Hashable, ...]]]" +reveal_type(x) # N: Revealed type is "builtins.int | tuple[typing.Hashable | ...]" if isinstance(x, tuple): y, = x - reveal_type(y) # N: Revealed type is "Union[typing.Hashable, Union[builtins.int, tuple[Union[typing.Hashable, ...]]]]" + reveal_type(y) # N: Revealed type is "typing.Hashable | builtins.int | tuple[typing.Hashable | ...]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index dd3f793fd02b..c32dec94a62d 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -316,13 +316,13 @@ def check_final() -> None: def check_binder(x: Optional[int], y: Optional[int], z: Optional[int], a: Optional[int], b: Optional[int]) -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | None" (x := 1) reveal_type(x) # N: Revealed type is "builtins.int" if x or (y := 1): - reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(y) # N: Revealed type is "builtins.int | None" if x and (y := 1): reveal_type(y) # N: Revealed type is "builtins.int" @@ -341,7 +341,7 @@ def check_partial() -> None: if bool() and (x := 2): pass - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | None" def check_narrow(x: Optional[int], s: List[int]) -> None: if (y := x): @@ -421,7 +421,7 @@ else: reveal_type(is_str) # N: Revealed type is "Literal[False]" reveal_type(maybe_str) # N: Revealed type is "None" -reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" [builtins fixtures/bool.pyi] [case testWalrusConditionalTypeCheck2] @@ -431,12 +431,12 @@ maybe_str: Optional[str] if (x := maybe_str) is not None: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" else: reveal_type(x) # N: Revealed type is "None" - reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" -reveal_type(maybe_str) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(maybe_str) # N: Revealed type is "builtins.str | None" [builtins fixtures/bool.pyi] [case testWalrusPartialTypes] @@ -750,7 +750,7 @@ def walrus_with_nested_error(x: Optional[str]) -> None: if x is None: x = "a" def nested() -> str: - return x # E: Incompatible return value type (got "Optional[str]", expected "str") + return x # E: Incompatible return value type (got "str | None", expected "str") if x := None: pass nested() diff --git a/test-data/unit/check-recursive-types.test b/test-data/unit/check-recursive-types.test index c09f1e6b90c0..a3af3d475f6e 100644 --- a/test-data/unit/check-recursive-types.test +++ b/test-data/unit/check-recursive-types.test @@ -7,12 +7,12 @@ JSON = Union[str, List[JSON], Dict[str, JSON]] x: JSON = ["foo", {"bar": "baz"}] -reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.list[...], builtins.dict[builtins.str, ...]]" +reveal_type(x) # N: Revealed type is "builtins.str | builtins.list[...] | builtins.dict[builtins.str, ...]" if isinstance(x, list): x = x[0] class Bad: ... -x = ["foo", {"bar": [Bad()]}] # E: List item 0 has incompatible type "Bad"; expected "Union[str, list[JSON], dict[str, JSON]]" +x = ["foo", {"bar": [Bad()]}] # E: List item 0 has incompatible type "Bad"; expected "str | list[JSON] | dict[str, JSON]" [builtins fixtures/isinstancelist.pyi] [case testRecursiveAliasBasicGenericSubtype] @@ -54,7 +54,7 @@ reveal_type(flatten([1, [2, [3]]])) # N: Revealed type is "builtins.list[builti class Bad: ... x: Nested[int] = [1, [2, [3]]] -x = [1, [Bad()]] # E: List item 0 has incompatible type "Bad"; expected "Union[int, Nested[int]]" +x = [1, [Bad()]] # E: List item 0 has incompatible type "Bad"; expected "int | Nested[int]" [builtins fixtures/isinstancelist.pyi] [case testRecursiveAliasGenericInferenceNested] @@ -77,7 +77,7 @@ from test import A x: A if isinstance(x, list): - reveal_type(x[0]) # N: Revealed type is "Union[builtins.int, builtins.list[Union[builtins.int, builtins.list[...]]]]" + reveal_type(x[0]) # N: Revealed type is "builtins.int | builtins.list[builtins.int | builtins.list[...]]" else: reveal_type(x) # N: Revealed type is "builtins.int" @@ -95,7 +95,7 @@ A = Union[B, int] B = Callable[[C], int] C = Type[A] x: A -reveal_type(x) # N: Revealed type is "Union[def (Union[type[def (...) -> builtins.int], type[builtins.int]]) -> builtins.int, builtins.int]" +reveal_type(x) # N: Revealed type is "def (type[def (...) -> builtins.int] | type[builtins.int]) -> builtins.int | builtins.int" [case testRecursiveAliasesProhibited-skip] from typing import Type, Callable, Union @@ -189,7 +189,7 @@ class A(NamedTuple('A', [('attr', List[Exp])])): pass class B(NamedTuple('B', [('val', object)])): pass def my_eval(exp: Exp) -> int: - reveal_type(exp) # N: Revealed type is "Union[tuple[builtins.list[...], fallback=__main__.A], tuple[builtins.object, fallback=__main__.B]]" + reveal_type(exp) # N: Revealed type is "tuple[builtins.list[...], fallback=__main__.A] | tuple[builtins.object, fallback=__main__.B]" if isinstance(exp, A): my_eval(exp[0][0]) return my_eval(exp.attr[0]) @@ -217,7 +217,7 @@ def union(a: T, b: S) -> Union[T, S]: ... x: int y = union(a, b) -x = y # E: Incompatible types in assignment (expression has type "Sequence[Union[A, NestedA]]", variable has type "int") +x = y # E: Incompatible types in assignment (expression has type "Sequence[A | NestedA]", variable has type "int") [builtins fixtures/isinstancelist.pyi] [case testRecursiveAliasesJoins] @@ -238,11 +238,11 @@ def join(a: T, b: T) -> T: ... x: int y1 = join(a, b) -x = y1 # E: Incompatible types in assignment (expression has type "Sequence[Union[A, NestedA]]", variable has type "int") +x = y1 # E: Incompatible types in assignment (expression has type "Sequence[A | NestedA]", variable has type "int") y2 = join(a, lb) -x = y2 # E: Incompatible types in assignment (expression has type "Sequence[Union[A, NestedA]]", variable has type "int") +x = y2 # E: Incompatible types in assignment (expression has type "Sequence[A | NestedA]", variable has type "int") y3 = join(la, b) -x = y3 # E: Incompatible types in assignment (expression has type "Sequence[Union[Sequence[A], B, NestedB]]", variable has type "int") +x = y3 # E: Incompatible types in assignment (expression has type "Sequence[Sequence[A] | B | NestedB]", variable has type "int") [builtins fixtures/isinstancelist.pyi] [case testRecursiveAliasesRestrictions] @@ -254,9 +254,9 @@ B = Mapping[int, Union[int, B]] x: int y: Union[A, B] if isinstance(y, Sequence): - x = y # E: Incompatible types in assignment (expression has type "Sequence[Union[int, A]]", variable has type "int") + x = y # E: Incompatible types in assignment (expression has type "Sequence[int | A]", variable has type "int") else: - x = y # E: Incompatible types in assignment (expression has type "Mapping[int, Union[int, B]]", variable has type "int") + x = y # E: Incompatible types in assignment (expression has type "Mapping[int, int | B]", variable has type "int") [builtins fixtures/isinstancelist.pyi] [case testRecursiveAliasesRestrictions2] @@ -275,11 +275,11 @@ aa: NestedA x: int x = a # E: Incompatible types in assignment (expression has type "NestedA", variable has type "int") a = b -x = a # E: Incompatible types in assignment (expression has type "Sequence[Union[B, NestedB]]", variable has type "int") +x = a # E: Incompatible types in assignment (expression has type "Sequence[B | NestedB]", variable has type "int") b = aa # E: Incompatible types in assignment (expression has type "NestedA", variable has type "NestedB") if isinstance(b[0], Sequence): a = b[0] - x = a # E: Incompatible types in assignment (expression has type "Sequence[Union[B, NestedB]]", variable has type "int") + x = a # E: Incompatible types in assignment (expression has type "Sequence[B | NestedB]", variable has type "int") [builtins fixtures/isinstancelist.pyi] [case testRecursiveAliasWithRecursiveInstance] @@ -298,8 +298,8 @@ a = [[b]] # OK b = aa # E: Incompatible types in assignment (expression has type "Nested[A]", variable has type "B") def join(a: T, b: T) -> T: ... -reveal_type(join(a, b)) # N: Revealed type is "typing.Sequence[Union[__main__.A, typing.Sequence[Union[__main__.A, ...]]]]" -reveal_type(join(b, a)) # N: Revealed type is "typing.Sequence[Union[__main__.A, typing.Sequence[Union[__main__.A, ...]]]]" +reveal_type(join(a, b)) # N: Revealed type is "typing.Sequence[__main__.A | typing.Sequence[__main__.A | ...]]" +reveal_type(join(b, a)) # N: Revealed type is "typing.Sequence[__main__.A | typing.Sequence[__main__.A | ...]]" [builtins fixtures/isinstancelist.pyi] [case testRecursiveAliasWithRecursiveInstanceInference] @@ -377,7 +377,7 @@ from typing import Optional, Sequence A = Sequence[Optional[A]] x: A -y: str = x[0] # E: Incompatible types in assignment (expression has type "Optional[A]", variable has type "str") +y: str = x[0] # E: Incompatible types in assignment (expression has type "A | None", variable has type "str") [case testRecursiveAliasesProhibitBadAliases] # flags: --disable-error-code used-before-def @@ -407,7 +407,7 @@ def local() -> None: L = List[Union[int, L]] # E: Cannot resolve name "L" (possible cyclic definition) \ # N: Recursive types are not allowed at function scope x: L - reveal_type(x) # N: Revealed type is "builtins.list[Union[builtins.int, Any]]" + reveal_type(x) # N: Revealed type is "builtins.list[builtins.int | Any]" S = Type[S] # E: Type[...] can't contain "Type[...]" U = Type[Union[int, U]] # E: Type[...] can't contain "Union[Type[...], Type[...]]" \ @@ -427,12 +427,12 @@ from typing import NamedTuple, Optional NT = NamedTuple("NT", [("x", Optional[NT]), ("y", int)]) nt: NT -reveal_type(nt) # N: Revealed type is "tuple[Union[..., None], builtins.int, fallback=__main__.NT]" -reveal_type(nt.x) # N: Revealed type is "Union[tuple[Union[..., None], builtins.int, fallback=__main__.NT], None]" -reveal_type(nt[0]) # N: Revealed type is "Union[tuple[Union[..., None], builtins.int, fallback=__main__.NT], None]" +reveal_type(nt) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT]" +reveal_type(nt.x) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" +reveal_type(nt[0]) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" y: str if nt.x is not None: - y = nt.x[0] # E: Incompatible types in assignment (expression has type "Optional[NT]", variable has type "str") + y = nt.x[0] # E: Incompatible types in assignment (expression has type "NT | None", variable has type "str") [builtins fixtures/tuple.pyi] [case testBasicRecursiveNamedTupleSpecial] @@ -464,12 +464,12 @@ class NT(NamedTuple): y: int nt: NT -reveal_type(nt) # N: Revealed type is "tuple[Union[..., None], builtins.int, fallback=__main__.NT]" -reveal_type(nt.x) # N: Revealed type is "Union[tuple[Union[..., None], builtins.int, fallback=__main__.NT], None]" -reveal_type(nt[0]) # N: Revealed type is "Union[tuple[Union[..., None], builtins.int, fallback=__main__.NT], None]" +reveal_type(nt) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT]" +reveal_type(nt.x) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" +reveal_type(nt[0]) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" y: str if nt.x is not None: - y = nt.x[0] # E: Incompatible types in assignment (expression has type "Optional[NT]", variable has type "str") + y = nt.x[0] # E: Incompatible types in assignment (expression has type "NT | None", variable has type "str") [builtins fixtures/tuple.pyi] [case testRecursiveRegularTupleClass] @@ -522,7 +522,7 @@ T = TypeVar("T") S = TypeVar("S") def foo(arg: Tuple[T, S]) -> Union[T, S]: ... x = foo(n) -y: str = x # E: Incompatible types in assignment (expression has type "Union[str, tuple[B, ...]]", variable has type "str") +y: str = x # E: Incompatible types in assignment (expression has type "str | tuple[B, ...]", variable has type "str") [builtins fixtures/tuple.pyi] [case testMutuallyRecursiveNamedTuplesJoin] @@ -605,8 +605,8 @@ class NT(NamedTuple, Generic[T]): class A: ... class B(A): ... -nti: NT[int] = NT(key=0, value=NT(key=1, value=A())) # E: Argument "value" to "NT" has incompatible type "A"; expected "Union[int, NT[int]]" -reveal_type(nti) # N: Revealed type is "tuple[builtins.int, Union[builtins.int, ...], fallback=__main__.NT[builtins.int]]" +nti: NT[int] = NT(key=0, value=NT(key=1, value=A())) # E: Argument "value" to "NT" has incompatible type "A"; expected "int | NT[int]" +reveal_type(nti) # N: Revealed type is "tuple[builtins.int, builtins.int | ..., fallback=__main__.NT[builtins.int]]" nta: NT[A] ntb: NT[B] @@ -690,12 +690,12 @@ class TD(TypedDict, total=False): y: TD td: TD -reveal_type(td.get("y")) # N: Revealed type is "Union[TypedDict('__main__.TD', {'x'?: builtins.int, 'y'?: ...}), None]" +reveal_type(td.get("y")) # N: Revealed type is "TypedDict('__main__.TD', {'x'?: builtins.int, 'y'?: ...}) | None" td["y"] = {"x": 0, "y": {}} td["y"] = {"x": 0, "y": {"x": 0, "y": 42}} # E: Incompatible types (expression has type "int", TypedDict item "y" has type "TD") -reveal_type(td.get("y")) # N: Revealed type is "Union[TypedDict('__main__.TD', {'x'?: builtins.int, 'y'?: ...}), None]" -s: str = td.get("y") # E: Incompatible types in assignment (expression has type "Optional[TD]", variable has type "str") +reveal_type(td.get("y")) # N: Revealed type is "TypedDict('__main__.TD', {'x'?: builtins.int, 'y'?: ...}) | None" +s: str = td.get("y") # E: Incompatible types in assignment (expression has type "TD | None", variable has type "str") td.update({"x": 0, "y": {"x": 1, "y": {}}}) td.update({"x": 0, "y": {"x": 1, "y": {"x": 2, "y": 42}}}) # E: Incompatible types (expression has type "int", TypedDict item "y" has type "TD") @@ -791,7 +791,7 @@ from typing import Union, Sequence class A: Children = Union[Sequence['Children'], 'A', None] x: A.Children -reveal_type(x) # N: Revealed type is "Union[typing.Sequence[...], __main__.A, None]" +reveal_type(x) # N: Revealed type is "typing.Sequence[...] | __main__.A | None" class B: Foo = Sequence[Bar] @@ -808,11 +808,11 @@ Tree2 = Union[str, Tuple[Tree2, Tree2]] Tree3 = Union[str, Tuple[Tree3, Tree3, Tree3]] def test1() -> Tree1: - return 42 # E: Incompatible return value type (got "int", expected "Union[str, tuple[Tree1]]") + return 42 # E: Incompatible return value type (got "int", expected "str | tuple[Tree1]") def test2() -> Tree2: - return 42 # E: Incompatible return value type (got "int", expected "Union[str, tuple[Tree2, Tree2]]") + return 42 # E: Incompatible return value type (got "int", expected "str | tuple[Tree2, Tree2]") def test3() -> Tree3: - return 42 # E: Incompatible return value type (got "int", expected "Union[str, tuple[Tree3, Tree3, Tree3]]") + return 42 # E: Incompatible return value type (got "int", expected "str | tuple[Tree3, Tree3, Tree3]") [builtins fixtures/tuple.pyi] [case testRecursiveDoubleUnionNoCrash] @@ -864,7 +864,7 @@ class Sub(Base[Union[ValueT, Recursive[ValueT]]]): pass x: Iterable[str] -reveal_type(Sub) # N: Revealed type is "def [ValueT] (element: Union[ValueT`1, __main__.Recursive[ValueT`1]]) -> __main__.Sub[ValueT`1]" +reveal_type(Sub) # N: Revealed type is "def [ValueT] (element: ValueT`1 | __main__.Recursive[ValueT`1]) -> __main__.Sub[ValueT`1]" reveal_type(Sub(x)) # N: Revealed type is "__main__.Sub[typing.Iterable[builtins.str]]" [case testNoRecursiveExpandInstanceUnionCrashInference] @@ -906,7 +906,7 @@ def dummy() -> None: def bar(x: T) -> T: pass - reveal_type(bar) # N: Revealed type is "def [T <: Union[builtins.str, builtins.dict[builtins.str, Any]]] (x: T`-1) -> T`-1" + reveal_type(bar) # N: Revealed type is "def [T <: builtins.str | builtins.dict[builtins.str, Any]] (x: T`-1) -> T`-1" [builtins fixtures/dict.pyi] [case testForwardBoundFunctionScopeWorks] @@ -933,7 +933,7 @@ x: A[int, str] *_, last = x if last is not None: - reveal_type(last) # N: Revealed type is "tuple[builtins.int, builtins.str, Union[tuple[builtins.int, builtins.str, Union[..., None]], None]]" + reveal_type(last) # N: Revealed type is "tuple[builtins.int, builtins.str, tuple[builtins.int, builtins.str, ... | None] | None]" [builtins fixtures/tuple.pyi] [case testRecursiveAliasLiteral] @@ -957,8 +957,8 @@ X = Union[A[Optional[X]], B[Optional[X]]] z: Z x: X -reveal_type(z) # N: Revealed type is "Union[__main__.A[...], __main__.B[Union[..., None]]]" -reveal_type(x) # N: Revealed type is "Union[__main__.A[Union[..., None]], __main__.B[Union[..., None]]]" +reveal_type(z) # N: Revealed type is "__main__.A[...] | __main__.B[... | None]" +reveal_type(x) # N: Revealed type is "__main__.A[... | None] | __main__.B[... | None]" [case testRecursiveTupleFallback1] from typing import NewType, Tuple, Union diff --git a/test-data/unit/check-redefine.test b/test-data/unit/check-redefine.test index 4bcbaf50298d..f760d0582a44 100644 --- a/test-data/unit/check-redefine.test +++ b/test-data/unit/check-redefine.test @@ -57,10 +57,10 @@ T = TypeVar('T') def f(x: int) -> None: x = g(x) - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" y = 1 y = g(y) - reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" def g(x: T) -> Union[T, str]: pass @@ -383,7 +383,7 @@ def f() -> None: x = 1 if int(): x = '' - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" x = '' reveal_type(x) # N: Revealed type is "builtins.str" if int(): @@ -477,7 +477,7 @@ x = 0 reveal_type(x) # N: Revealed type is "builtins.int" for x in f(x): pass -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNoRedefinitionIfOnlyInitialized] # flags: --allow-redefinition --no-strict-optional diff --git a/test-data/unit/check-redefine2.test b/test-data/unit/check-redefine2.test index 4d99aa17f804..6e9441151c93 100644 --- a/test-data/unit/check-redefine2.test +++ b/test-data/unit/check-redefine2.test @@ -25,14 +25,14 @@ def f1() -> None: x = 0 else: x = '' - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f2() -> None: if int(): x = 0 else: x = None - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | None" [case testNewRedefineMergeConditionalLocal2] # flags: --allow-redefinition-new --local-partial-types @@ -42,14 +42,14 @@ def nested_ifs() -> None: x = 0 else: x = '' - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: if int(): x = None else: x = b"" - reveal_type(x) # N: Revealed type is "Union[None, builtins.bytes]" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None, builtins.bytes]" + reveal_type(x) # N: Revealed type is "None | builtins.bytes" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None | builtins.bytes" [case testNewRedefineUninitializedCodePath1] # flags: --allow-redefinition-new --local-partial-types @@ -67,7 +67,7 @@ from typing import Union def f1() -> None: if int(): x: Union[int, str] = 0 - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" x = "" reveal_type(x) # N: Revealed type is "builtins.str" @@ -80,7 +80,7 @@ def f1() -> None: x = 0 elif int(): x = "" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineUninitializedCodePath4] # flags: --allow-redefinition-new --local-partial-types @@ -89,7 +89,7 @@ from typing import Union def f1() -> None: if int(): x: Union[int, str] = 0 - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineUninitializedCodePath5] # flags: --allow-redefinition-new --local-partial-types @@ -101,7 +101,7 @@ def f1() -> None: x = "" reveal_type(x) # N: Revealed type is "builtins.str" x = None - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | None" [case testNewRedefineUninitializedCodePath6] # flags: --allow-redefinition-new --local-partial-types @@ -112,7 +112,7 @@ x: Union[str, None] def f1() -> None: if x is not None: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" [case testNewRedefineGlobalVariableSimple] # flags: --allow-redefinition-new --local-partial-types @@ -122,18 +122,18 @@ if int(): else: x = "" reveal_type(x) # N: Revealed type is "builtins.str" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f1() -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f2() -> None: global x - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" x = 0 reveal_type(x) # N: Revealed type is "builtins.int" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineGlobalVariableNoneInit] # flags: --allow-redefinition-new --local-partial-types @@ -152,7 +152,7 @@ reveal_type(x) # N: Revealed type is "None" from typing import Optional def f1(x: Optional[str] = None) -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.str | None" if x is None: x = "" reveal_type(x) # N: Revealed type is "builtins.str" @@ -176,9 +176,9 @@ class C: else: x = "" reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" -reveal_type(C.x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(C.x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineNestedFunctionBasics] # flags: --allow-redefinition-new --local-partial-types @@ -189,7 +189,7 @@ def f1() -> None: x = "" def nested() -> None: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f2() -> None: if int(): @@ -199,11 +199,11 @@ def f2() -> None: def nested() -> None: nonlocal x - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" x = 0 reveal_type(x) # N: Revealed type is "builtins.int" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineLambdaBasics] # flags: --allow-redefinition-new --local-partial-types @@ -211,12 +211,12 @@ def f1() -> None: x = 0 if int(): x = None - f = lambda: reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" - reveal_type(f) # N: Revealed type is "def () -> Union[builtins.int, None]" + f = lambda: reveal_type(x) # N: Revealed type is "builtins.int | None" + reveal_type(f) # N: Revealed type is "def () -> builtins.int | None" if x is None: x = "" - f = lambda: reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(f) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]" + f = lambda: reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(f) # N: Revealed type is "def () -> builtins.int | builtins.str" [case testNewRedefineAssignmentExpression] # flags: --allow-redefinition-new --local-partial-types @@ -225,7 +225,7 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.int" elif x := str(): reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f2() -> None: if x := int(): @@ -234,12 +234,12 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "builtins.str" else: pass - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f3() -> None: if (x := int()) or (x := str()): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineOperatorAssignment] # flags: --allow-redefinition-new --local-partial-types @@ -251,7 +251,7 @@ c = C() if int(): c += C() reveal_type(c) # N: Revealed type is "__main__.D" -reveal_type(c) # N: Revealed type is "Union[__main__.C, __main__.D]" +reveal_type(c) # N: Revealed type is "__main__.C | __main__.D" [case testNewRedefineImportFrom-xfail] # flags: --allow-redefinition-new --local-partial-types @@ -294,7 +294,7 @@ def f1() -> None: x = None if int(): x = "" - reveal_type(x) # N: Revealed type is "Union[None, builtins.str]" + reveal_type(x) # N: Revealed type is "None | builtins.str" def f2() -> None: if int(): @@ -303,14 +303,14 @@ def f2() -> None: x = "" else: x = 1 - reveal_type(x) # N: Revealed type is "Union[None, builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "None | builtins.str | builtins.int" def f3() -> None: if int(): x = None else: x = "" - reveal_type(x) # N: Revealed type is "Union[None, builtins.str]" + reveal_type(x) # N: Revealed type is "None | builtins.str" def f4() -> None: x = None @@ -319,7 +319,7 @@ def f4() -> None: y = None if int(): y = 1 -reveal_type(y) # N: Revealed type is "Union[None, builtins.int]" +reveal_type(y) # N: Revealed type is "None | builtins.int" if int(): z = None @@ -327,7 +327,7 @@ elif int(): z = 1 else: z = "" -reveal_type(z) # N: Revealed type is "Union[None, builtins.int, builtins.str]" +reveal_type(z) # N: Revealed type is "None | builtins.int | builtins.str" [case testNewRedefinePartialTypeForInstanceVariable] # flags: --allow-redefinition-new --local-partial-types @@ -337,9 +337,9 @@ class C1: if int(): self.x = 1 reveal_type(self.x) # N: Revealed type is "builtins.int" - reveal_type(self.x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(self.x) # N: Revealed type is "builtins.int | None" -reveal_type(C1().x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(C1().x) # N: Revealed type is "builtins.int | None" class C2: def __init__(self) -> None: @@ -356,10 +356,10 @@ class C3: if int(): self.x = 1 else: - self.x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "Optional[int]") - reveal_type(self.x) # N: Revealed type is "Union[builtins.int, None]" + self.x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int | None") + reveal_type(self.x) # N: Revealed type is "builtins.int | None" -reveal_type(C3().x) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(C3().x) # N: Revealed type is "builtins.int | None" class C4: def __init__(self) -> None: @@ -378,7 +378,7 @@ class C5: self.x = [""] reveal_type(self.x) # N: Revealed type is "builtins.list[builtins.str]" -reveal_type(C5().x) # N: Revealed type is "Union[builtins.list[builtins.str], None]" +reveal_type(C5().x) # N: Revealed type is "builtins.list[builtins.str] | None" [builtins fixtures/list.pyi] [case testNewRedefinePartialGenericTypes] @@ -420,7 +420,7 @@ def f5() -> None: b = [""] a = b reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]" - reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" def f6() -> None: a = [] @@ -465,9 +465,9 @@ def f2(x: Optional[str]) -> None: def f3() -> None: a: list[Optional[str]] = [""] - reveal_type(a) # N: Revealed type is "builtins.list[Union[builtins.str, None]]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.str | None]" a = [""] - reveal_type(a) # N: Revealed type is "builtins.list[Union[builtins.str, None]]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.str | None]" class C: x: Optional[str] @@ -489,7 +489,7 @@ def f1() -> None: else: x = a() reveal_type(x) # N: Revealed type is "Any" - reveal_type(x) # N: Revealed type is "Union[builtins.str, Any]" + reveal_type(x) # N: Revealed type is "builtins.str | Any" x = 1 reveal_type(x) # N: Revealed type is "builtins.int" @@ -499,7 +499,7 @@ def f2() -> None: else: x = "" reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[Any, builtins.str]" + reveal_type(x) # N: Revealed type is "Any | builtins.str" x = 1 reveal_type(x) # N: Revealed type is "builtins.int" @@ -525,7 +525,7 @@ def f5() -> None: elif int(): x = "" reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[Any, builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "Any | builtins.int | builtins.str" def f6() -> None: x = a() @@ -533,7 +533,7 @@ def f6() -> None: x = 1 else: x = "" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f7() -> None: x: int @@ -622,7 +622,7 @@ def f() -> None: reveal_type(x) # N: Revealed type is "None" x = b"" reveal_type(x) # N: Revealed type is "builtins.bytes" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.bytes]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.bytes" x = [1] reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]" @@ -633,15 +633,15 @@ def f1() -> None: while int(): if int(): x = "" - reveal_type(x) # N: Revealed type is "Union[None, builtins.str]" + reveal_type(x) # N: Revealed type is "None | builtins.str" def f2() -> None: x = None while int(): - reveal_type(x) # N: Revealed type is "Union[None, builtins.str]" + reveal_type(x) # N: Revealed type is "None | builtins.str" if int(): x = "" - reveal_type(x) # N: Revealed type is "Union[None, builtins.str]" + reveal_type(x) # N: Revealed type is "None | builtins.str" [case testNewRedefineWhileLoopPartialType] # flags: --allow-redefinition-new --local-partial-types @@ -713,19 +713,19 @@ def b() -> None: break reveal_type(x) # N: Revealed type is "builtins.str" x = None - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | None" def c() -> None: x = 0 while int(): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" if int(): x = "" continue else: x = None reveal_type(x) # N: Revealed type is "None" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" [case testNewRedefineUnderscore] # flags: --allow-redefinition-new --local-partial-types @@ -736,7 +736,7 @@ def f() -> None: else: _ = "" reveal_type(_) # N: Revealed type is "builtins.str" - reveal_type(_) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(_) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineWithStatement] # flags: --allow-redefinition-new --local-partial-types @@ -760,7 +760,7 @@ def f2() -> None: else: with D() as x: reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNewRedefineTryStatement] # flags: --allow-redefinition-new --local-partial-types @@ -776,12 +776,12 @@ def f1() -> None: reveal_type(x) # N: Revealed type is "builtins.str" except RuntimeError as e: reveal_type(e) # N: Revealed type is "builtins.RuntimeError" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" except E as e: reveal_type(e) # N: Revealed type is "__main__.E" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" reveal_type(e) # N: Revealed type is "" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f2() -> None: try: @@ -790,7 +790,7 @@ def f2() -> None: x = "" return except Exception: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" return reveal_type(x) # N: Revealed type is "builtins.int" @@ -801,7 +801,7 @@ def f3() -> None: x = "" return finally: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" reveal_type(x) # N: Revealed type is "builtins.int" def f4() -> None: @@ -817,8 +817,8 @@ def f4() -> None: x = None break finally: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" + reveal_type(x) # N: Revealed type is "builtins.int | None" [builtins fixtures/exception.pyi] [case testNewRedefineRaiseStatement] @@ -831,7 +831,7 @@ def f1() -> None: raise Exception() else: x = 1 - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" def f2() -> None: try: @@ -841,8 +841,8 @@ def f2() -> None: raise Exception() reveal_type(x) # N: Revealed type is "builtins.int" except Exception: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/exception.pyi] @@ -865,8 +865,8 @@ def f2() -> None: x, y = None, 2 reveal_type(x) # N: Revealed type is "None" reveal_type(y) # N: Revealed type is "builtins.int" - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" - reveal_type(y) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.int | None" + reveal_type(y) # N: Revealed type is "builtins.str | builtins.int" [case testNewRedefineForLoopBasics] # flags: --allow-redefinition-new --local-partial-types @@ -886,8 +886,8 @@ def f2() -> None: reveal_type(x) # N: Revealed type is "None" reveal_type(y) # N: Revealed type is "builtins.int" - reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" - reveal_type(y) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.int | None" + reveal_type(y) # N: Revealed type is "builtins.str | builtins.int" [builtins fixtures/for.pyi] [case testNewRedefineForLoop1] @@ -899,7 +899,7 @@ def f1() -> None: x = "" for x in l(): reveal_type(x) # N: Revealed type is "builtins.int" - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" def f2() -> None: for x in [1, 2]: @@ -910,7 +910,7 @@ def f3() -> None: for x in [1, 2]: if int(): x = "x" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/for.pyi] [case testNewRedefineForLoop2] @@ -933,7 +933,7 @@ class X(TypedDict): x: X for a in ("hourly", "daily"): - reveal_type(a) # N: Revealed type is "Union[Literal['hourly']?, Literal['daily']?]" + reveal_type(a) # N: Revealed type is "Literal['hourly']? | Literal['daily']?" reveal_type(x[a]) # N: Revealed type is "builtins.int" reveal_type(a.upper()) # N: Revealed type is "builtins.str" c = a @@ -970,7 +970,7 @@ def f2() -> None: break x = "" reveal_type(x) # N: Revealed type is "builtins.str" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" def f3() -> None: if int(): @@ -990,8 +990,8 @@ def f1() -> None: continue elif e is not None and int(): break - reveal_type(e) # N: Revealed type is "Union[builtins.str, None]" - reveal_type(e) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(e) # N: Revealed type is "builtins.str | None" + reveal_type(e) # N: Revealed type is "builtins.str | None" def f2(e: Optional[str]) -> None: for x in ["a"]: @@ -1000,8 +1000,8 @@ def f2(e: Optional[str]) -> None: continue elif e is not None and int(): break - reveal_type(e) # N: Revealed type is "Union[builtins.str, None]" - reveal_type(e) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(e) # N: Revealed type is "builtins.str | None" + reveal_type(e) # N: Revealed type is "builtins.str | None" [case testNewRedefineLoopAndPartialTypesSpecialCase] # flags: --allow-redefinition-new --local-partial-types @@ -1044,7 +1044,7 @@ if int(): x = 0 else: x = "" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [file b.py] if int(): @@ -1083,7 +1083,7 @@ def f() -> None: while int(): x = [x] - reveal_type(x) # N: Revealed type is "Union[Any, builtins.list[Any]]" + reveal_type(x) # N: Revealed type is "Any | builtins.list[Any]" [case testNewRedefinePartialNoneEmptyList] # flags: --allow-redefinition-new --local-partial-types @@ -1093,7 +1093,7 @@ def func() -> None: if int(): l = [] # E: Need type annotation for "l" l.append(1) - reveal_type(l) # N: Revealed type is "Union[None, builtins.list[Any]]" + reveal_type(l) # N: Revealed type is "None | builtins.list[Any]" [builtins fixtures/list.pyi] [case testNewRedefineNarrowingSpecialCase] @@ -1104,10 +1104,10 @@ def get() -> Union[tuple[Any, Any], tuple[None, None]]: ... def f() -> None: x, _ = get() - reveal_type(x) # N: Revealed type is "Union[Any, None]" + reveal_type(x) # N: Revealed type is "Any | None" if x and int(): reveal_type(x) # N: Revealed type is "Any" - reveal_type(x) # N: Revealed type is "Union[Any, None]" + reveal_type(x) # N: Revealed type is "Any | None" if x and int(): reveal_type(x) # N: Revealed type is "Any" [builtins fixtures/tuple.pyi] @@ -1196,4 +1196,4 @@ import pkg x = 0 if int(): x = "" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" diff --git a/test-data/unit/check-selftype.test b/test-data/unit/check-selftype.test index 53754bf3c217..60fd517a7baf 100644 --- a/test-data/unit/check-selftype.test +++ b/test-data/unit/check-selftype.test @@ -1135,7 +1135,7 @@ class C: def same(self: T) -> T: ... x: Union[A, C] -reveal_type(x.same) # N: Revealed type is "Union[builtins.int, def () -> __main__.C]" +reveal_type(x.same) # N: Revealed type is "builtins.int | def () -> __main__.C" [case testSelfTypeOnUnionClassMethod] from typing import TypeVar, Union, Type @@ -1150,7 +1150,7 @@ class C: def same(cls: Type[T]) -> T: ... x: Union[A, C] -reveal_type(x.same) # N: Revealed type is "Union[builtins.int, def () -> __main__.C]" +reveal_type(x.same) # N: Revealed type is "builtins.int | def () -> __main__.C" [builtins fixtures/classmethod.pyi] [case SelfTypeOverloadedClassMethod] @@ -1201,9 +1201,9 @@ class B(A): ... class C(A): ... t: Type[Union[B, C]] -reveal_type(t.meth) # N: Revealed type is "Union[def () -> __main__.B, def () -> __main__.C]" +reveal_type(t.meth) # N: Revealed type is "def () -> __main__.B | def () -> __main__.C" x = t.meth() -reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" [builtins fixtures/classmethod.pyi] [case testSelfTypeClassMethodOnUnionGeneric] @@ -1218,7 +1218,7 @@ class A(Generic[T]): t: Type[Union[A[int], A[str]]] x = t.meth() -reveal_type(x) # N: Revealed type is "Union[__main__.A[builtins.int], __main__.A[builtins.str]]" +reveal_type(x) # N: Revealed type is "__main__.A[builtins.int] | __main__.A[builtins.str]" [builtins fixtures/classmethod.pyi] [case testSelfTypeClassMethodOnUnionList] @@ -1234,7 +1234,7 @@ class C(A): ... t: Type[Union[B, C]] x = t.meth()[0] -reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" [builtins fixtures/isinstancelist.pyi] [case testSelfTypeClassMethodOverloadedOnInstance] @@ -1258,14 +1258,14 @@ class AClass: ... def foo(x: Type[AClass]) -> None: - reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])" + reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int | None, def (id: __main__.AClass, id2: None =) -> builtins.int | None)" y = x() - reveal_type(y.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])" + reveal_type(y.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int | None, def (id: __main__.AClass, id2: None =) -> builtins.int | None)" y.delete(10, 20) y.delete(y) def bar(x: AClass) -> None: - reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])" + reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int | None, def (id: __main__.AClass, id2: None =) -> builtins.int | None)" x.delete(10, 20) [builtins fixtures/classmethod.pyi] @@ -2117,7 +2117,7 @@ class C: class D: x: int x: Union[C, D] -reveal_type(x.x) # N: Revealed type is "Union[__main__.C, builtins.int]" +reveal_type(x.x) # N: Revealed type is "__main__.C | builtins.int" [case testCallableProtocolTypingSelf] from typing import Protocol, Self @@ -2227,19 +2227,19 @@ class X: _inst: Optional[Self] = None @classmethod def default(cls) -> Self: - reveal_type(cls._inst) # N: Revealed type is "Union[Self`0, None]" + reveal_type(cls._inst) # N: Revealed type is "Self`0 | None" if cls._inst is None: cls._inst = cls() return cls._inst reveal_type(X._inst) # E: Access to generic instance variables via class is ambiguous \ - # N: Revealed type is "Union[__main__.X, None]" -reveal_type(X()._inst) # N: Revealed type is "Union[__main__.X, None]" + # N: Revealed type is "__main__.X | None" +reveal_type(X()._inst) # N: Revealed type is "__main__.X | None" class Y(X): ... reveal_type(Y._inst) # E: Access to generic instance variables via class is ambiguous \ - # N: Revealed type is "Union[__main__.Y, None]" -reveal_type(Y()._inst) # N: Revealed type is "Union[__main__.Y, None]" + # N: Revealed type is "__main__.Y | None" +reveal_type(Y()._inst) # N: Revealed type is "__main__.Y | None" [builtins fixtures/tuple.pyi] [case testSelfInFuncDecoratedClassmethod] diff --git a/test-data/unit/check-serialize.test b/test-data/unit/check-serialize.test index 1498c8d82826..3be96bb53ee1 100644 --- a/test-data/unit/check-serialize.test +++ b/test-data/unit/check-serialize.test @@ -754,8 +754,8 @@ from typing import Optional x: Optional[int] def f(x: int) -> None: pass [out2] -tmp/a.py:2: note: Revealed type is "Union[builtins.int, None]" -tmp/a.py:3: error: Argument 1 to "f" has incompatible type "Optional[int]"; expected "int" +tmp/a.py:2: note: Revealed type is "builtins.int | None" +tmp/a.py:3: error: Argument 1 to "f" has incompatible type "int | None"; expected "int" -- -- # type: ignore @@ -993,7 +993,7 @@ Ty2 = type [out2] tmp/a.py:9: note: Revealed type is "b.DD" tmp/a.py:10: note: Revealed type is "Any" -tmp/a.py:11: note: Revealed type is "Union[builtins.int, builtins.str]" +tmp/a.py:11: note: Revealed type is "builtins.int | builtins.str" tmp/a.py:12: note: Revealed type is "builtins.list[builtins.int]" tmp/a.py:13: note: Revealed type is "tuple[builtins.int, builtins.str]" tmp/a.py:14: note: Revealed type is "def (builtins.int) -> builtins.str" diff --git a/test-data/unit/check-singledispatch.test b/test-data/unit/check-singledispatch.test index e63d4c073e86..36c99b288bbe 100644 --- a/test-data/unit/check-singledispatch.test +++ b/test-data/unit/check-singledispatch.test @@ -151,7 +151,7 @@ def h(arg: C) -> None: pass x: Union[B, C, int] -f(x) # E: Argument 1 to "f" has incompatible type "Union[B, C, int]"; expected "Union[A, C]" +f(x) # E: Argument 1 to "f" has incompatible type "B | C | int"; expected "A | C" [builtins fixtures/args.pyi] diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index 477d582c7b7e..880df82671d4 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -176,7 +176,7 @@ for z in x: # type: int pass for w in x: # type: Union[int, str] - reveal_type(w) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(w) # N: Revealed type is "builtins.int | builtins.str" for v in x: # type: int, int # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn) pass @@ -746,8 +746,8 @@ except (E1, E1_1, E1_2) as e1: z = e1 # type: E1_2 # E: Incompatible types in assignment (expression has type "E1", variable has type "E1_2") except (E1_1, E1_2) as e2: a = e2 # type: E1 - b = e2 # type: E1_1 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_1") - c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_2") + b = e2 # type: E1_1 # E: Incompatible types in assignment (expression has type "E1_1 | E1_2", variable has type "E1_1") + c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "E1_1 | E1_2", variable has type "E1_2") [builtins fixtures/exception.pyi] [case testExceptWithMultipleTypes4] @@ -767,25 +767,25 @@ def union(exc: Union[Type[E1], Type[E2]]) -> None: try: pass except exc as e: - reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2]" + reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2" def tuple_in_union(exc: Union[Type[E1], Tuple[Type[E2], Type[E3]]]) -> None: try: pass except exc as e: - reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]" + reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3" def variadic_in_union(exc: Union[Type[E1], Tuple[Type[E2], ...]]) -> None: try: pass except exc as e: - reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2]" + reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2" def nested_union(exc: Union[Type[E1], Union[Type[E2], Type[E3]]]) -> None: try: pass except exc as e: - reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]" + reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3" def error_in_union(exc: Union[Type[E1], int]) -> None: try: @@ -812,19 +812,19 @@ def union_in_variadic(exc: Tuple[Union[Type[E1], Type[E2]], ...]) -> None: try: pass except exc as e: - reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2]" + reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2" def nested_union_in_variadic(exc: Tuple[Union[Type[E1], Union[Type[E2], Type[E3]]], ...]) -> None: try: pass except exc as e: - reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]" + reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3" def union_in_tuple(exc: Tuple[Union[Type[E1], Type[E2]], Type[E3]]) -> None: try: pass except exc as e: - reveal_type(e) # N: Revealed type is "Union[__main__.E1, __main__.E2, __main__.E3]" + reveal_type(e) # N: Revealed type is "__main__.E1 | __main__.E2 | __main__.E3" def error_in_variadic_union(exc: Tuple[Union[Type[E1], int], ...]) -> None: try: @@ -864,11 +864,11 @@ try: except BaseException as e1: reveal_type(e1) # N: Revealed type is "builtins.BaseException" except (E1, BaseException) as e2: - reveal_type(e2) # N: Revealed type is "Union[Any, builtins.BaseException]" + reveal_type(e2) # N: Revealed type is "Any | builtins.BaseException" except (E1, E2) as e3: - reveal_type(e3) # N: Revealed type is "Union[Any, __main__.E2]" + reveal_type(e3) # N: Revealed type is "Any | __main__.E2" except (E1, E2, BaseException) as e4: - reveal_type(e4) # N: Revealed type is "Union[Any, builtins.BaseException]" + reveal_type(e4) # N: Revealed type is "Any | builtins.BaseException" try: pass except E1 as e1: @@ -1066,8 +1066,8 @@ exs2 = (E1_1, E1_2) try: pass except exs2 as e2: a = e2 # type: E1 - b = e2 # type: E1_1 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_1") - c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "Union[E1_1, E1_2]", variable has type "E1_2") + b = e2 # type: E1_1 # E: Incompatible types in assignment (expression has type "E1_1 | E1_2", variable has type "E1_1") + c = e2 # type: E1_2 # E: Incompatible types in assignment (expression has type "E1_1 | E1_2", variable has type "E1_2") exs3 = (E1, (E1_1, (E1_2,))) try: pass @@ -1397,7 +1397,7 @@ from typing import Generator def g() -> Generator[int, None, None]: yield from () yield from (0, 1, 2) - yield from (0, "ERROR") # E: Incompatible types in "yield from" (actual type "Union[int, str]", expected type "int") + yield from (0, "ERROR") # E: Incompatible types in "yield from" (actual type "int | str", expected type "int") yield from ("ERROR",) # E: Incompatible types in "yield from" (actual type "str", expected type "int") [builtins fixtures/tuple.pyi] @@ -1512,7 +1512,7 @@ with A() as c: # type: int, int # E: Syntax error in type annotation # N: Sugg pass with A() as d: # type: Union[int, str] - reveal_type(d) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(d) # N: Revealed type is "builtins.int | builtins.str" [case testWithStmtTupleTypeComment] @@ -2285,7 +2285,7 @@ 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]") + yield x # E: Incompatible types in "yield" (actual type "int", expected type "A | B") [case testYieldFromUnionOfGenerators] from typing import Generator, Union @@ -2302,7 +2302,7 @@ class A: pass class B: pass def foo(arg: Union[A, B]) -> Generator[Union[int, str], None, A]: - return (yield from arg) # E: "yield from" can't be applied to "Union[A, B]" + return (yield from arg) # E: "yield from" can't be applied to "A | B" [case testYieldFromUnionOfGeneratorWithIterableStr] from typing import Generator, Union, Iterable, Optional @@ -2311,7 +2311,7 @@ def foo(arg: Union[Generator[int, None, bytes], Iterable[str]]) -> Generator[Uni return (yield from arg) def bar(arg: Generator[str, None, str]) -> Generator[str, None, str]: - return foo(arg) # E: Incompatible return value type (got "Generator[Union[int, str], None, Optional[bytes]]", expected "Generator[str, None, str]") + return foo(arg) # E: Incompatible return value type (got "Generator[int | str, None, bytes | None]", expected "Generator[str, None, str]") def launder(arg: Iterable[str]) -> Generator[Union[int, str], None, Optional[bytes]]: return foo(arg) diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index b4d86b8007e6..3dccb6ce9834 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -324,8 +324,8 @@ if int(): t1[2] # E: Tuple index out of range t1[3] # E: Tuple index out of range t2[1] # E: Tuple index out of range -reveal_type(t1[n]) # N: Revealed type is "Union[__main__.A, __main__.B]" -reveal_type(t3[n:]) # N: Revealed type is "builtins.tuple[Union[__main__.A, __main__.B, __main__.C, __main__.D, __main__.E], ...]" +reveal_type(t1[n]) # N: Revealed type is "__main__.A | __main__.B" +reveal_type(t3[n:]) # N: Revealed type is "builtins.tuple[__main__.A | __main__.B | __main__.C | __main__.D | __main__.E, ...]" if int(): b = t1[(0)] # E: Incompatible types in assignment (expression has type "A", variable has type "B") @@ -1187,7 +1187,7 @@ from typing import Tuple, Union Pair = Tuple[int, int] Variant = Union[int, Pair] def tuplify(v: Variant) -> None: - reveal_type(v) # N: Revealed type is "Union[builtins.int, tuple[builtins.int, builtins.int]]" + reveal_type(v) # N: Revealed type is "builtins.int | tuple[builtins.int, builtins.int]" if not isinstance(v, tuple): reveal_type(v) # N: Revealed type is "builtins.int" v = (v, v) @@ -1209,10 +1209,10 @@ def tuplify2(v: Variant2) -> None: from typing import Tuple, Union def good(blah: Union[Tuple[int, int], int]) -> None: - reveal_type(blah) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], builtins.int]" + reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.int" if isinstance(blah, tuple): reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int]" - reveal_type(blah) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], builtins.int]" + reveal_type(blah) # N: Revealed type is "tuple[builtins.int, builtins.int] | builtins.int" [builtins fixtures/tuple.pyi] [out] @@ -1431,11 +1431,11 @@ f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "tuple[()]" t = (0, "") x = 0 y = "" -reveal_type(t[x]) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(t[x]) # N: Revealed type is "builtins.int | builtins.str" t[y] # E: No overload variant of "__getitem__" of "tuple" matches argument type "str" \ # N: Possible overload variants: \ - # N: def __getitem__(self, int, /) -> Union[int, str] \ - # N: def __getitem__(self, slice, /) -> tuple[Union[int, str], ...] + # N: def __getitem__(self, int, /) -> int | str \ + # N: def __getitem__(self, slice, /) -> tuple[int | str, ...] [builtins fixtures/tuple.pyi] @@ -1443,7 +1443,7 @@ t[y] # E: No overload variant of "__getitem__" of "tuple" matches argument type t = (0, "") x = 0 y = "" -reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[Union[builtins.int, builtins.str], ...]" +reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]" t[y:] # E: Slice index must be an integer, SupportsIndex or None [builtins fixtures/tuple.pyi] @@ -1543,7 +1543,7 @@ x: Optional[Tuple[B]] if x in possibles: reveal_type(x) # N: Revealed type is "tuple[__main__.B]" else: - reveal_type(x) # N: Revealed type is "Union[tuple[__main__.B], None]" + reveal_type(x) # N: Revealed type is "tuple[__main__.B] | None" [builtins fixtures/tuple.pyi] @@ -1552,10 +1552,10 @@ from typing import Union, Tuple tup: Union[Tuple[int, str], Tuple[int, int, str]] reveal_type(tup[0]) # N: Revealed type is "builtins.int" -reveal_type(tup[1]) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(tup[1]) # N: Revealed type is "builtins.str | builtins.int" reveal_type(tup[2]) # E: Tuple index out of range \ - # N: Revealed type is "Union[Any, builtins.str]" -reveal_type(tup[:]) # N: Revealed type is "Union[tuple[builtins.int, builtins.str], tuple[builtins.int, builtins.int, builtins.str]]" + # N: Revealed type is "Any | builtins.str" +reveal_type(tup[:]) # N: Revealed type is "tuple[builtins.int, builtins.str] | tuple[builtins.int, builtins.int, builtins.str]" [builtins fixtures/tuple.pyi] @@ -1564,10 +1564,10 @@ from typing import Union, Tuple, List tup: Union[Tuple[int, str], List[int]] reveal_type(tup[0]) # N: Revealed type is "builtins.int" -reveal_type(tup[1]) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(tup[1]) # N: Revealed type is "builtins.str | builtins.int" reveal_type(tup[2]) # E: Tuple index out of range \ - # N: Revealed type is "Union[Any, builtins.int]" -reveal_type(tup[:]) # N: Revealed type is "Union[tuple[builtins.int, builtins.str], builtins.list[builtins.int]]" + # N: Revealed type is "Any | builtins.int" +reveal_type(tup[:]) # N: Revealed type is "tuple[builtins.int, builtins.str] | builtins.list[builtins.int]" [builtins fixtures/tuple.pyi] @@ -1650,9 +1650,9 @@ class A: ) [out] main:18: error: Incompatible return value type (6 tuple items are incompatible; 3 items are omitted) -main:18: note: Expression tuple item 6 has type "Union[str, int]"; "str" expected; -main:18: note: Expression tuple item 7 has type "Union[str, float]"; "str" expected; -main:18: note: Expression tuple item 8 has type "Optional[str]"; "str" expected; +main:18: note: Expression tuple item 6 has type "str | int"; "str" expected; +main:18: note: Expression tuple item 7 has type "str | float"; "str" expected; +main:18: note: Expression tuple item 8 has type "str | None"; "str" expected; [builtins fixtures/property.pyi] [case testPropertyLongTupleReturnTypeMismatchUnionWiderExpected] @@ -1690,7 +1690,7 @@ class A: [out] main:18: error: Incompatible return value type (2 tuple items are incompatible) main:18: note: Expression tuple item 2 has type "str"; "int" expected; -main:18: note: Expression tuple item 11 has type "Union[float, int]"; "str" expected; +main:18: note: Expression tuple item 11 has type "float | int"; "str" expected; [builtins fixtures/property.pyi] [case testTupleWithStarExpr] diff --git a/test-data/unit/check-type-aliases.test b/test-data/unit/check-type-aliases.test index 3023575179dd..0d2e6b5f0c9d 100644 --- a/test-data/unit/check-type-aliases.test +++ b/test-data/unit/check-type-aliases.test @@ -12,7 +12,7 @@ U = Union[int, str] def f(x: U) -> None: pass f(1) f('') -f(()) # E: Argument 1 to "f" has incompatible type "tuple[()]"; expected "Union[int, str]" +f(()) # E: Argument 1 to "f" has incompatible type "tuple[()]"; expected "int | str" [targets __main__, __main__.f] [builtins fixtures/tuple.pyi] @@ -64,7 +64,7 @@ from _m import U def f(x: U) -> None: pass f(1) f('x') -f(()) # E: Argument 1 to "f" has incompatible type "tuple[()]"; expected "Union[int, str]" +f(()) # E: Argument 1 to "f" has incompatible type "tuple[()]"; expected "int | str" [file _m.py] from typing import Union U = Union[int, str] @@ -224,7 +224,7 @@ main:6: note: Recursive types are not allowed at function scope main:6: error: Cannot resolve name "C" (possible cyclic definition) main:7: error: Cannot resolve name "C" (possible cyclic definition) main:7: note: Recursive types are not allowed at function scope -main:9: note: Revealed type is "Union[Any, builtins.int]" +main:9: note: Revealed type is "Any | builtins.int" [case testDoubleForwardAlias] # flags: --disable-error-code=used-before-def @@ -307,7 +307,7 @@ void = type(None) x: void reveal_type(x) # N: Revealed type is "None" y: Union[int, void] -reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(y) # N: Revealed type is "builtins.int | None" [builtins fixtures/bool.pyi] [case testNoneAliasStrict] @@ -316,7 +316,7 @@ void = type(None) x: int y: Union[int, void] z: Optional[int] -x = y # E: Incompatible types in assignment (expression has type "Optional[int]", variable has type "int") +x = y # E: Incompatible types in assignment (expression has type "int | None", variable has type "int") y = z [builtins fixtures/bool.pyi] @@ -463,8 +463,8 @@ class C: class D(C): ... -reveal_type(D.meth(1)) # N: Revealed type is "Union[__main__.D, builtins.int]" -reveal_type(D().meth(1)) # N: Revealed type is "Union[__main__.D, builtins.int]" +reveal_type(D.meth(1)) # N: Revealed type is "__main__.D | builtins.int" +reveal_type(D().meth(1)) # N: Revealed type is "__main__.D | builtins.int" [builtins fixtures/classmethod.pyi] [out] @@ -687,7 +687,7 @@ reveal_type(b) # N: Revealed type is "builtins.str" z: TypeAlias = "int | str" c: z -reveal_type(c) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(c) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testForwardRefPep613] @@ -859,8 +859,8 @@ def plausible_top_3() -> SpecialImplicit: pass def plausible_top_4() -> SpecialExplicit: pass reveal_type(plausible_top_1) # N: Revealed type is "def () -> __main__.Foo" reveal_type(plausible_top_2) # N: Revealed type is "def () -> __main__.Foo" -reveal_type(plausible_top_3) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]" -reveal_type(plausible_top_4) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]" +reveal_type(plausible_top_3) # N: Revealed type is "def () -> builtins.int | builtins.str" +reveal_type(plausible_top_4) # N: Revealed type is "def () -> builtins.int | builtins.str" def plausible_parent_1() -> Parent.NormalImplicit: pass # E: Variable "__main__.Parent.NormalImplicit" is not valid as a type \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases @@ -869,8 +869,8 @@ def plausible_parent_3() -> Parent.SpecialImplicit: pass def plausible_parent_4() -> Parent.SpecialExplicit: pass reveal_type(plausible_parent_1) # N: Revealed type is "def () -> Parent.NormalImplicit?" reveal_type(plausible_parent_2) # N: Revealed type is "def () -> __main__.Foo" -reveal_type(plausible_parent_3) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]" -reveal_type(plausible_parent_4) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]" +reveal_type(plausible_parent_3) # N: Revealed type is "def () -> builtins.int | builtins.str" +reveal_type(plausible_parent_4) # N: Revealed type is "def () -> builtins.int | builtins.str" def plausible_child_1() -> Child.NormalImplicit: pass # E: Variable "__main__.Parent.NormalImplicit" is not valid as a type \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases @@ -879,8 +879,8 @@ def plausible_child_3() -> Child.SpecialImplicit: pass def plausible_child_4() -> Child.SpecialExplicit: pass reveal_type(plausible_child_1) # N: Revealed type is "def () -> Child.NormalImplicit?" reveal_type(plausible_child_2) # N: Revealed type is "def () -> __main__.Foo" -reveal_type(plausible_child_3) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]" -reveal_type(plausible_child_4) # N: Revealed type is "def () -> Union[builtins.int, builtins.str]" +reveal_type(plausible_child_3) # N: Revealed type is "def () -> builtins.int | builtins.str" +reveal_type(plausible_child_4) # N: Revealed type is "def () -> builtins.int | builtins.str" # Use type aliases in a type alias context in an implausible way @@ -963,8 +963,8 @@ A = List[int, str] | int # E: "list" expects 1 type argument, but 2 given B = int | list[int, str] # E: "list" expects 1 type argument, but 2 given a: A b: B -reveal_type(a) # N: Revealed type is "Union[builtins.list[Any], builtins.int]" -reveal_type(b) # N: Revealed type is "Union[builtins.int, builtins.list[Any]]" +reveal_type(a) # N: Revealed type is "builtins.list[Any] | builtins.int" +reveal_type(b) # N: Revealed type is "builtins.int | builtins.list[Any]" [builtins fixtures/type.pyi] [case testValidTypeAliasValues] @@ -1069,7 +1069,7 @@ from typing_extensions import TypeAliasType TestType = TypeAliasType("TestType", Union[int, str]) x: TestType = 42 y: TestType = 'a' -z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]") +z: TestType = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int | str") reveal_type(TestType) # N: Revealed type is "typing_extensions.TypeAliasType" TestType() # E: "TypeAliasType" not callable @@ -1105,7 +1105,7 @@ reveal_type(t3) # N: Revealed type is "Any" T4 = TypeAliasType("T4") # E: Missing positional argument "value" in call to "TypeAliasType" T5 = TypeAliasType("T5", int, str) # E: Too many positional arguments for "TypeAliasType" \ - # E: Argument 3 to "TypeAliasType" has incompatible type "type[str]"; expected "tuple[Union[TypeVar?, ParamSpec?, TypeVarTuple?], ...]" + # E: Argument 3 to "TypeAliasType" has incompatible type "type[str]"; expected "tuple[TypeVar? | ParamSpec? | TypeVarTuple?, ...]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] diff --git a/test-data/unit/check-type-promotion.test b/test-data/unit/check-type-promotion.test index 1b69174a4545..7f5e14c01c1a 100644 --- a/test-data/unit/check-type-promotion.test +++ b/test-data/unit/check-type-promotion.test @@ -73,7 +73,7 @@ if isinstance(y, float): else: reveal_type(y) # N: Revealed type is "builtins.int" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.float]" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.float" if isinstance(y, int): reveal_type(y) # N: Revealed type is "builtins.int" @@ -86,7 +86,7 @@ else: x: complex = 1 reveal_type(x) # N: Revealed type is "builtins.complex" if isinstance(x, (int, float)): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.float]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.float" else: reveal_type(x) # N: Revealed type is "builtins.complex" @@ -126,8 +126,8 @@ x: Union[float, complex] if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.float, builtins.complex]" -reveal_type(x) # N: Revealed type is "Union[builtins.float, builtins.complex]" + reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" +reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion6] @@ -138,8 +138,8 @@ x: Union[str, complex] if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.complex]" -reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.complex]" + reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex" +reveal_type(x) # N: Revealed type is "builtins.str | builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion7] @@ -150,23 +150,23 @@ x: Union[int, float, complex] if isinstance(x, int): reveal_type(x) # N: Revealed type is "builtins.int" else: - reveal_type(x) # N: Revealed type is "Union[builtins.float, builtins.complex]" + reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.float, builtins.complex]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex" if isinstance(x, float): reveal_type(x) # N: Revealed type is "builtins.float" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.complex]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.float, builtins.complex]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex" if isinstance(x, complex): reveal_type(x) # N: Revealed type is "builtins.complex" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.float]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.float" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.float, builtins.complex]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.float | builtins.complex" [builtins fixtures/primitives.pyi] [case testIntersectionUsingPromotion8] @@ -175,15 +175,15 @@ from typing import Union x: Union[int, float, complex] if isinstance(x, (int, float)): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.float]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.float" else: reveal_type(x) # N: Revealed type is "builtins.complex" if isinstance(x, (int, complex)): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.complex]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.complex" else: reveal_type(x) # N: Revealed type is "builtins.float" if isinstance(x, (float, complex)): - reveal_type(x) # N: Revealed type is "Union[builtins.float, builtins.complex]" + reveal_type(x) # N: Revealed type is "builtins.float | builtins.complex" else: reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/primitives.pyi] diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index e1a70efe9316..29e3a7efbd8c 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -863,12 +863,12 @@ g = G(a=cast(Any, 1)) # Work around #2610 reveal_type(u(d, d)) # N: Revealed type is "TypedDict('__main__.D', {'a': builtins.int, 'b': builtins.int})" reveal_type(u(c, d)) # N: Revealed type is "TypedDict('__main__.C', {'a': builtins.int})" reveal_type(u(d, c)) # N: Revealed type is "TypedDict('__main__.C', {'a': builtins.int})" -reveal_type(u(c, e)) # N: Revealed type is "Union[TypedDict('__main__.E', {'a': builtins.str}), TypedDict('__main__.C', {'a': builtins.int})]" -reveal_type(u(e, c)) # N: Revealed type is "Union[TypedDict('__main__.C', {'a': builtins.int}), TypedDict('__main__.E', {'a': builtins.str})]" -reveal_type(u(c, f)) # N: Revealed type is "Union[TypedDict('__main__.F', {'x': builtins.int}), TypedDict('__main__.C', {'a': builtins.int})]" -reveal_type(u(f, c)) # N: Revealed type is "Union[TypedDict('__main__.C', {'a': builtins.int}), TypedDict('__main__.F', {'x': builtins.int})]" -reveal_type(u(c, g)) # N: Revealed type is "Union[TypedDict('__main__.G', {'a': Any}), TypedDict('__main__.C', {'a': builtins.int})]" -reveal_type(u(g, c)) # N: Revealed type is "Union[TypedDict('__main__.C', {'a': builtins.int}), TypedDict('__main__.G', {'a': Any})]" +reveal_type(u(c, e)) # N: Revealed type is "TypedDict('__main__.E', {'a': builtins.str}) | TypedDict('__main__.C', {'a': builtins.int})" +reveal_type(u(e, c)) # N: Revealed type is "TypedDict('__main__.C', {'a': builtins.int}) | TypedDict('__main__.E', {'a': builtins.str})" +reveal_type(u(c, f)) # N: Revealed type is "TypedDict('__main__.F', {'x': builtins.int}) | TypedDict('__main__.C', {'a': builtins.int})" +reveal_type(u(f, c)) # N: Revealed type is "TypedDict('__main__.C', {'a': builtins.int}) | TypedDict('__main__.F', {'x': builtins.int})" +reveal_type(u(c, g)) # N: Revealed type is "TypedDict('__main__.G', {'a': Any}) | TypedDict('__main__.C', {'a': builtins.int})" +reveal_type(u(g, c)) # N: Revealed type is "TypedDict('__main__.C', {'a': builtins.int}) | TypedDict('__main__.G', {'a': Any})" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -889,9 +889,9 @@ m_s_a: Mapping[str, Any] reveal_type(u(c, m_s_o)) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]" reveal_type(u(m_s_o, c)) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]" -reveal_type(u(c, m_s_s)) # N: Revealed type is "Union[typing.Mapping[builtins.str, builtins.str], TypedDict('__main__.C', {'a': builtins.int, 'b': builtins.int})]" -reveal_type(u(c, m_i_i)) # N: Revealed type is "Union[typing.Mapping[builtins.int, builtins.int], TypedDict('__main__.C', {'a': builtins.int, 'b': builtins.int})]" -reveal_type(u(c, m_s_a)) # N: Revealed type is "Union[typing.Mapping[builtins.str, Any], TypedDict('__main__.C', {'a': builtins.int, 'b': builtins.int})]" +reveal_type(u(c, m_s_s)) # N: Revealed type is "typing.Mapping[builtins.str, builtins.str] | TypedDict('__main__.C', {'a': builtins.int, 'b': builtins.int})" +reveal_type(u(c, m_i_i)) # N: Revealed type is "typing.Mapping[builtins.int, builtins.int] | TypedDict('__main__.C', {'a': builtins.int, 'b': builtins.int})" +reveal_type(u(c, m_s_a)) # N: Revealed type is "typing.Mapping[builtins.str, Any] | TypedDict('__main__.C', {'a': builtins.int, 'b': builtins.int})" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -902,7 +902,7 @@ A = TypedDict('A', {'@type': Literal['a-type'], 'a': str}) B = TypedDict('B', {'@type': Literal['b-type'], 'b': int}) c: Union[A, B] = {'@type': 'a-type', 'a': 'Test'} -reveal_type(c) # N: Revealed type is "Union[TypedDict('__main__.A', {'@type': Literal['a-type'], 'a': builtins.str}), TypedDict('__main__.B', {'@type': Literal['b-type'], 'b': builtins.int})]" +reveal_type(c) # N: Revealed type is "TypedDict('__main__.A', {'@type': Literal['a-type'], 'a': builtins.str}) | TypedDict('__main__.B', {'@type': Literal['b-type'], 'b': builtins.int})" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -923,7 +923,7 @@ A = TypedDict('A', {'@type': Literal['a-type'], 'value': int}) B = TypedDict('B', {'@type': Literal['b-type'], 'value': int}) c: Union[A, B] = {'@type': 'a-type', 'value': 'Test'} # E: Type of TypedDict is ambiguous, none of ("A", "B") matches cleanly \ - # E: Incompatible types in assignment (expression has type "dict[str, str]", variable has type "Union[A, B]") + # E: Incompatible types in assignment (expression has type "dict[str, str]", variable has type "A | B") [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1026,26 +1026,26 @@ x_or_z: Literal['x', 'z'] x_or_y_or_z: Literal['x', 'y', 'z'] # test with literal expression -reveal_type(d.get('x')) # N: Revealed type is "Union[builtins.int, None]" -reveal_type(d.get('y')) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(d.get('x')) # N: Revealed type is "builtins.int | None" +reveal_type(d.get('y')) # N: Revealed type is "builtins.str | None" reveal_type(d.get('z')) # N: Revealed type is "builtins.object" -reveal_type(d.get('x', u)) # N: Revealed type is "Union[builtins.int, __main__.Unrelated]" +reveal_type(d.get('x', u)) # N: Revealed type is "builtins.int | __main__.Unrelated" reveal_type(d.get('x', 1)) # N: Revealed type is "builtins.int" -reveal_type(d.get('y', None)) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(d.get('y', None)) # N: Revealed type is "builtins.str | None" # test with literal type / union of literal types with implicit default -reveal_type(d.get(x)) # N: Revealed type is "Union[builtins.int, None]" -reveal_type(d.get(y)) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(d.get(x)) # N: Revealed type is "builtins.int | None" +reveal_type(d.get(y)) # N: Revealed type is "builtins.str | None" reveal_type(d.get(z)) # N: Revealed type is "builtins.object" -reveal_type(d.get(x_or_y)) # N: Revealed type is "Union[builtins.int, builtins.str, None]" +reveal_type(d.get(x_or_y)) # N: Revealed type is "builtins.int | builtins.str | None" reveal_type(d.get(x_or_z)) # N: Revealed type is "builtins.object" reveal_type(d.get(x_or_y_or_z)) # N: Revealed type is "builtins.object" # test with literal type / union of literal types with explicit default -reveal_type(d.get(x, u)) # N: Revealed type is "Union[builtins.int, __main__.Unrelated]" -reveal_type(d.get(y, u)) # N: Revealed type is "Union[builtins.str, __main__.Unrelated]" +reveal_type(d.get(x, u)) # N: Revealed type is "builtins.int | __main__.Unrelated" +reveal_type(d.get(y, u)) # N: Revealed type is "builtins.str | __main__.Unrelated" reveal_type(d.get(z, u)) # N: Revealed type is "builtins.object" -reveal_type(d.get(x_or_y, u)) # N: Revealed type is "Union[builtins.int, builtins.str, __main__.Unrelated]" +reveal_type(d.get(x_or_y, u)) # N: Revealed type is "builtins.int | builtins.str | __main__.Unrelated" reveal_type(d.get(x_or_z, u)) # N: Revealed type is "builtins.object" reveal_type(d.get(x_or_y_or_z, u)) # N: Revealed type is "builtins.object" [builtins fixtures/dict.pyi] @@ -1076,7 +1076,7 @@ reveal_type(d.get('y', None)) # N: Revealed type is "builtins.str" reveal_type(d.get(x)) # N: Revealed type is "builtins.int" reveal_type(d.get(y)) # N: Revealed type is "builtins.str" reveal_type(d.get(z)) # N: Revealed type is "builtins.object" -reveal_type(d.get(x_or_y)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(d.get(x_or_y)) # N: Revealed type is "builtins.int | builtins.str" reveal_type(d.get(x_or_z)) # N: Revealed type is "builtins.object" reveal_type(d.get(x_or_y_or_z)) # N: Revealed type is "builtins.object" @@ -1084,7 +1084,7 @@ reveal_type(d.get(x_or_y_or_z)) # N: Revealed type is "builtins.object" reveal_type(d.get(x, u)) # N: Revealed type is "builtins.int" reveal_type(d.get(y, u)) # N: Revealed type is "builtins.str" reveal_type(d.get(z, u)) # N: Revealed type is "builtins.object" -reveal_type(d.get(x_or_y, u)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(d.get(x_or_y, u)) # N: Revealed type is "builtins.int | builtins.str" reveal_type(d.get(x_or_z, u)) # N: Revealed type is "builtins.object" reveal_type(d.get(x_or_y_or_z, u)) # N: Revealed type is "builtins.object" @@ -1108,25 +1108,25 @@ x_or_y_or_z: Literal['x', 'y', 'z'] # test with literal expression reveal_type(d.get('x')) # N: Revealed type is "builtins.int" -reveal_type(d.get('y')) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(d.get('y')) # N: Revealed type is "builtins.str | None" reveal_type(d.get('z')) # N: Revealed type is "builtins.object" reveal_type(d.get('x', u)) # N: Revealed type is "builtins.int" reveal_type(d.get('x', 1)) # N: Revealed type is "builtins.int" -reveal_type(d.get('y', None)) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(d.get('y', None)) # N: Revealed type is "builtins.str | None" # test with literal type / union of literal types with implicit default reveal_type(d.get(x)) # N: Revealed type is "builtins.int" -reveal_type(d.get(y)) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(d.get(y)) # N: Revealed type is "builtins.str | None" reveal_type(d.get(z)) # N: Revealed type is "builtins.object" -reveal_type(d.get(x_or_y)) # N: Revealed type is "Union[builtins.int, builtins.str, None]" +reveal_type(d.get(x_or_y)) # N: Revealed type is "builtins.int | builtins.str | None" reveal_type(d.get(x_or_z)) # N: Revealed type is "builtins.object" reveal_type(d.get(x_or_y_or_z)) # N: Revealed type is "builtins.object" # test with literal type / union of literal types with explicit default reveal_type(d.get(x, u)) # N: Revealed type is "builtins.int" -reveal_type(d.get(y, u)) # N: Revealed type is "Union[builtins.str, __main__.Unrelated]" +reveal_type(d.get(y, u)) # N: Revealed type is "builtins.str | __main__.Unrelated" reveal_type(d.get(z, u)) # N: Revealed type is "builtins.object" -reveal_type(d.get(x_or_y, u)) # N: Revealed type is "Union[builtins.int, builtins.str, __main__.Unrelated]" +reveal_type(d.get(x_or_y, u)) # N: Revealed type is "builtins.int | builtins.str | __main__.Unrelated" reveal_type(d.get(x_or_z, u)) # N: Revealed type is "builtins.object" reveal_type(d.get(x_or_y_or_z, u)) # N: Revealed type is "builtins.object" [builtins fixtures/dict.pyi] @@ -1139,9 +1139,9 @@ class A: pass D = TypedDict('D', {'x': List[int], 'y': int}, total=False) d: D reveal_type(d.get('x', [])) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(d.get('x', ['x'])) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" +reveal_type(d.get('x', ['x'])) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" a = [''] -reveal_type(d.get('x', a)) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" +reveal_type(d.get('x', a)) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1158,7 +1158,7 @@ d.get('x', 1, 2) # E: No overload variant of "get" of "Mapping" matches argument # N: Possible overload variants: \ # N: def get(self, k: str) -> object \ # N: def get(self, str, object, /) -> object \ - # N: def [V] get(self, str, Union[int, V], /) -> object + # N: def [V] get(self, str, int | V, /) -> object x = d.get('z') reveal_type(x) # N: Revealed type is "builtins.object" s = '' @@ -1198,8 +1198,8 @@ C = TypedDict('C', {'a': int}, total=True) D = TypedDict('D', {'x': C, 'y': str}, total=False) d: D reveal_type(d.get('x', {})) # N: Revealed type is "TypedDict('__main__.C', {'a'?: builtins.int})" -reveal_type(d.get('x', None)) # N: Revealed type is "Union[TypedDict('__main__.C', {'a': builtins.int}), None]" -reveal_type(d.get('x', {}).get('a')) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(d.get('x', None)) # N: Revealed type is "TypedDict('__main__.C', {'a': builtins.int}) | None" +reveal_type(d.get('x', {}).get('a')) # N: Revealed type is "builtins.int | None" reveal_type(d.get('x', {})['a']) # N: Revealed type is "builtins.int" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1212,7 +1212,7 @@ D = TypedDict('D', {'x': C, 'y': str}, total=True) d: D reveal_type(d.get('x', {})) # N: Revealed type is "TypedDict('__main__.C', {'a'?: builtins.int})" reveal_type(d.get('x', None)) # N: Revealed type is "TypedDict('__main__.C', {'a'?: builtins.int})" -reveal_type(d.get('x', {}).get('a')) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(d.get('x', {}).get('a')) # N: Revealed type is "builtins.int | None" reveal_type(d.get('x', {})['a']) # N: Revealed type is "builtins.int" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1237,8 +1237,8 @@ C = TypedDict('C', {'a': int}, total=False) D = TypedDict('D', {'x': C, 'y': str}, total=False) d: D reveal_type(d.get('x', {})) # N: Revealed type is "TypedDict('__main__.C', {'a'?: builtins.int})" -reveal_type(d.get('x', None)) # N: Revealed type is "Union[TypedDict('__main__.C', {'a'?: builtins.int}), None]" -reveal_type(d.get('x', {}).get('a')) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(d.get('x', None)) # N: Revealed type is "TypedDict('__main__.C', {'a'?: builtins.int}) | None" +reveal_type(d.get('x', {}).get('a')) # N: Revealed type is "builtins.int | None" reveal_type(d.get('x', {})['a']) # N: Revealed type is "builtins.int" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1280,22 +1280,22 @@ def test_chaining(d: Config) -> None: reveal_type( d.get("required_total", {}).get("key_one") ) # N: Revealed type is "builtins.int" reveal_type( d.get("required_total", {}).get("key_two") ) # N: Revealed type is "builtins.str" reveal_type( d.get("required_total", {}).get("bad_key") ) # N: Revealed type is "builtins.object" - reveal_type( d.get("optional_total", {}).get("key_one") ) # N: Revealed type is "Union[builtins.int, None]" - reveal_type( d.get("optional_total", {}).get("key_two") ) # N: Revealed type is "Union[builtins.str, None]" + reveal_type( d.get("optional_total", {}).get("key_one") ) # N: Revealed type is "builtins.int | None" + reveal_type( d.get("optional_total", {}).get("key_two") ) # N: Revealed type is "builtins.str | None" reveal_type( d.get("optional_total", {}).get("bad_key") ) # N: Revealed type is "builtins.object" - reveal_type( d.get("required_maybe", {}).get("key_one") ) # N: Revealed type is "Union[builtins.int, None]" - reveal_type( d.get("required_maybe", {}).get("key_two") ) # N: Revealed type is "Union[builtins.str, None]" + reveal_type( d.get("required_maybe", {}).get("key_one") ) # N: Revealed type is "builtins.int | None" + reveal_type( d.get("required_maybe", {}).get("key_two") ) # N: Revealed type is "builtins.str | None" reveal_type( d.get("required_maybe", {}).get("bad_key") ) # N: Revealed type is "builtins.object" - reveal_type( d.get("optional_maybe", {}).get("key_one") ) # N: Revealed type is "Union[builtins.int, None]" - reveal_type( d.get("optional_maybe", {}).get("key_two") ) # N: Revealed type is "Union[builtins.str, None]" + reveal_type( d.get("optional_maybe", {}).get("key_one") ) # N: Revealed type is "builtins.int | None" + reveal_type( d.get("optional_maybe", {}).get("key_two") ) # N: Revealed type is "builtins.str | None" reveal_type( d.get("optional_maybe", {}).get("bad_key") ) # N: Revealed type is "builtins.object" reveal_type( d.get("required_mixed", {}).get("key_one") ) # N: Revealed type is "builtins.int" - reveal_type( d.get("required_mixed", {}).get("key_two") ) # N: Revealed type is "Union[builtins.str, None]" + reveal_type( d.get("required_mixed", {}).get("key_two") ) # N: Revealed type is "builtins.str | None" reveal_type( d.get("required_mixed", {}).get("bad_key") ) # N: Revealed type is "builtins.object" - reveal_type( d.get("optional_mixed", {}).get("key_one") ) # N: Revealed type is "Union[builtins.int, None]" - reveal_type( d.get("optional_mixed", {}).get("key_two") ) # N: Revealed type is "Union[builtins.str, None]" + reveal_type( d.get("optional_mixed", {}).get("key_one") ) # N: Revealed type is "builtins.int | None" + reveal_type( d.get("optional_mixed", {}).get("key_two") ) # N: Revealed type is "builtins.str | None" reveal_type( d.get("optional_mixed", {}).get("bad_key") ) # N: Revealed type is "builtins.object" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1318,7 +1318,7 @@ A_or_B: TypeAlias = Union[A, B] A_or_B_or_C: TypeAlias = Union[A_or_B, C] def test(d: A_or_B_or_C) -> None: - reveal_type(d.get("key")) # N: Revealed type is "Union[builtins.int, None]" + reveal_type(d.get("key")) # N: Revealed type is "builtins.int | None" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1375,8 +1375,8 @@ D = TypedDict('D', {'x': int, 'y': str}, total=False) d: D reveal_type(d['x']) # N: Revealed type is "builtins.int" reveal_type(d['y']) # N: Revealed type is "builtins.str" -reveal_type(d.get('x')) # N: Revealed type is "Union[builtins.int, None]" -reveal_type(d.get('y')) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(d.get('x')) # N: Revealed type is "builtins.int | None" +reveal_type(d.get('y')) # N: Revealed type is "builtins.str | None" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1851,7 +1851,7 @@ def f1(x: T, y: S) -> Union[T, S]: ... A = TypedDict('A', {'y': int, 'x': str}) a: A -reveal_type(f1(**a)) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(f1(**a)) # N: Revealed type is "builtins.str | builtins.int" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -1963,8 +1963,8 @@ b: B reveal_type(a.pop('x')) # N: Revealed type is "builtins.int" reveal_type(a.pop('y', [])) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(a.pop('x', '')) # N: Revealed type is "Union[builtins.int, Literal['']?]" -reveal_type(a.pop('x', (1, 2))) # N: Revealed type is "Union[builtins.int, tuple[Literal[1]?, Literal[2]?]]" +reveal_type(a.pop('x', '')) # N: Revealed type is "builtins.int | Literal['']?" +reveal_type(a.pop('x', (1, 2))) # N: Revealed type is "builtins.int | tuple[Literal[1]?, Literal[2]?]" a.pop('invalid', '') # E: TypedDict "A" has no key "invalid" b.pop('x') # E: Key "x" of TypedDict "B" cannot be deleted x = '' @@ -2010,12 +2010,12 @@ class TDB(TypedDict): td: Union[TDA, TDB] reveal_type(td.get('a')) # N: Revealed type is "builtins.int" -reveal_type(td.get('b')) # N: Revealed type is "Union[builtins.str, builtins.int]" +reveal_type(td.get('b')) # N: Revealed type is "builtins.str | builtins.int" reveal_type(td.get('c')) # N: Revealed type is "builtins.object" reveal_type(td['a']) # N: Revealed type is "builtins.int" -reveal_type(td['b']) # N: Revealed type is "Union[builtins.str, builtins.int]" -reveal_type(td['c']) # N: Revealed type is "Union[Any, builtins.int]" \ +reveal_type(td['b']) # N: Revealed type is "builtins.str | builtins.int" +reveal_type(td['c']) # N: Revealed type is "Any | builtins.int" \ # E: TypedDict "TDA" has no key "c" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -2035,8 +2035,8 @@ class TDB(TypedDict, total=False): td: Union[TDA, TDB] reveal_type(td.pop('a')) # N: Revealed type is "builtins.int" -reveal_type(td.pop('b')) # N: Revealed type is "Union[builtins.str, builtins.int]" -reveal_type(td.pop('c')) # N: Revealed type is "Union[Any, builtins.int]" \ +reveal_type(td.pop('b')) # N: Revealed type is "builtins.str | builtins.int" +reveal_type(td.pop('c')) # N: Revealed type is "Any | builtins.int" \ # E: TypedDict "TDA" has no key "c" [builtins fixtures/dict.pyi] @@ -2830,9 +2830,9 @@ def func(foo: Union[Foo1, Foo2]) -> str: # ok, but type is incorrect: reveal_type(foo.__getitem__("z")) # N: Revealed type is "builtins.object" - reveal_type(foo["a"]) # N: Revealed type is "Union[builtins.int, Any]" \ + reveal_type(foo["a"]) # N: Revealed type is "builtins.int | Any" \ # E: TypedDict "Foo2" has no key "a" - reveal_type(foo["b"]) # N: Revealed type is "Union[Any, builtins.int]" \ + reveal_type(foo["b"]) # N: Revealed type is "Any | builtins.int" \ # E: TypedDict "Foo1" has no key "b" reveal_type(foo["missing"]) # N: Revealed type is "Any" \ # E: TypedDict "Foo1" has no key "missing" \ @@ -3142,7 +3142,7 @@ def method(message: Response) -> None: ... method({'type': 'a', 'value': True}) # OK method({'type': 'b', 'value': 'abc'}) # OK method({'type': 'a', 'value': 'abc'}) # E: Type of TypedDict is ambiguous, none of ("A", "B") matches cleanly \ - # E: Argument 1 to "method" has incompatible type "dict[str, str]"; expected "Union[A, B]" + # E: Argument 1 to "method" has incompatible type "dict[str, str]"; expected "A | B" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -3161,7 +3161,7 @@ class D(TypedDict, total=False): def foo(data: Union[A, B]) -> None: ... foo({"foo": {"c": "foo"}}) # OK foo({"foo": {"e": "foo"}}) # E: Type of TypedDict is ambiguous, none of ("A", "B") matches cleanly \ - # E: Argument 1 to "foo" has incompatible type "dict[str, dict[str, str]]"; expected "Union[A, B]" + # E: Argument 1 to "foo" has incompatible type "dict[str, dict[str, str]]"; expected "A | B" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -3244,7 +3244,7 @@ C = TypedDict("C", {"bar": int}) a = A({"foo": 1, "bar": 2}) u: Union[B, C] -a.update(u) # E: Argument 1 to "update" of "TypedDict" has incompatible type "Union[B, C]"; expected "Union[TypedDict({'foo': int, 'bar'?: int}), TypedDict({'foo'?: int, 'bar': int})]" +a.update(u) # E: Argument 1 to "update" of "TypedDict" has incompatible type "B | C"; expected "TypedDict({'foo': int, 'bar'?: int}) | TypedDict({'foo'?: int, 'bar': int})" u2: Union[A1, A2] a.update(u2) # OK [builtins fixtures/dict.pyi] @@ -3698,8 +3698,8 @@ class Bar(Foo[T], total=False): other: str b: Bar[int] -reveal_type(b["a"]) # N: Revealed type is "Union[builtins.str, None]" -reveal_type(b["g"]) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(b["a"]) # N: Revealed type is "builtins.str | None" +reveal_type(b["g"]) # N: Revealed type is "builtins.int | None" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -4236,8 +4236,8 @@ class TP(TypedDict): x: TP reveal_type(x) # N: Revealed type is "TypedDict('__main__.TP', {'one'?=: builtins.int, 'two'?=: builtins.str})" -reveal_type(x.get("one")) # N: Revealed type is "Union[builtins.int, None]" -reveal_type(x.get("two")) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(x.get("one")) # N: Revealed type is "builtins.int | None" +reveal_type(x.get("two")) # N: Revealed type is "builtins.str | None" x["one"] = 1 # E: ReadOnly TypedDict key "one" TypedDict is mutated x["two"] = "a" # E: ReadOnly TypedDict key "two" TypedDict is mutated [builtins fixtures/dict.pyi] diff --git a/test-data/unit/check-typeform.test b/test-data/unit/check-typeform.test index 627067903a92..6d59cb008111 100644 --- a/test-data/unit/check-typeform.test +++ b/test-data/unit/check-typeform.test @@ -285,9 +285,9 @@ typx2: TypeForm = no_such_module.NoSuchType # E: Name "no_such_module" is not d # flags: --python-version 3.14 --enable-incomplete-feature=TypeForm from typing_extensions import TypeForm typx_or_int1: TypeForm[int | None] | int = int | None # No error; interpret as TypeForm -typx_or_int2: TypeForm[int | None] | int = str | None # E: Incompatible types in assignment (expression has type "object", variable has type "Union[TypeForm[Optional[int]], int]") +typx_or_int2: TypeForm[int | None] | int = str | None # E: Incompatible types in assignment (expression has type "object", variable has type "TypeForm[int | None] | int") typx_or_int3: TypeForm[int | None] | int = 1 -typx_or_int4: TypeForm[int | None] | int = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[TypeForm[Optional[int]], int]") +typx_or_int4: TypeForm[int | None] | int = object() # E: Incompatible types in assignment (expression has type "object", variable has type "TypeForm[int | None] | int") [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] @@ -297,7 +297,7 @@ from typing_extensions import TypeForm typx_or_str1: TypeForm[int | None] | str = 'int | None' typx_or_str2: TypeForm[int | None] | str = 'str | None' # No error; interpret as str typx_or_str3: TypeForm[int | None] | str = 'hello' -typx_or_str4: TypeForm[int | None] | str = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[TypeForm[Optional[int]], str]") +typx_or_str4: TypeForm[int | None] | str = object() # E: Incompatible types in assignment (expression has type "object", variable has type "TypeForm[int | None] | str") [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] @@ -313,9 +313,9 @@ list_of_typx2: List[TypeForm[int | None] | str] = ['str | None'] # E: TypeForm # flags: --python-version 3.14 --enable-incomplete-feature=TypeForm from typing import List, TypeForm list_of_typx3: List[TypeForm[int | None] | int] = ['int | None'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \ - # E: List item 0 has incompatible type "str"; expected "Union[TypeForm[Optional[int]], int]" + # E: List item 0 has incompatible type "str"; expected "TypeForm[int | None] | int" list_of_typx4: List[TypeForm[str | None] | int] = ['str | None'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \ - # E: List item 0 has incompatible type "str"; expected "Union[TypeForm[Optional[str]], int]" + # E: List item 0 has incompatible type "str"; expected "TypeForm[str | None] | int" [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] @@ -336,9 +336,9 @@ reveal_type(ANY_TF) # N: Revealed type is "TypeForm[Any]" typx1: TypeForm[int | str] = INT_OR_STR_TF typx2: TypeForm[int | str] = INT_TF typx3: TypeForm[int | str] = STR_TF -typx4: TypeForm[int | str] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "TypeForm[Union[int, str]]") +typx4: TypeForm[int | str] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "TypeForm[int | str]") typx5: TypeForm[int | str] = ANY_TF # no error -typx6: TypeForm[int] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[Union[int, str]]", variable has type "TypeForm[int]") +typx6: TypeForm[int] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[int | str]", variable has type "TypeForm[int]") typx7: TypeForm[int] = INT_TF typx8: TypeForm[int] = STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[str]", variable has type "TypeForm[int]") typx9: TypeForm[int] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "TypeForm[int]") @@ -358,7 +358,7 @@ ANY_T: Type = object reveal_type(ANY_T) # N: Revealed type is "type[Any]" typx1: TypeForm[int | str] = INT_T typx2: TypeForm[int | str] = STR_T -typx3: TypeForm[int | str] = OBJECT_T # E: Incompatible types in assignment (expression has type "type[object]", variable has type "TypeForm[Union[int, str]]") +typx3: TypeForm[int | str] = OBJECT_T # E: Incompatible types in assignment (expression has type "type[object]", variable has type "TypeForm[int | str]") typx4: TypeForm[int | str] = ANY_T # no error typx5: TypeForm[int] = INT_T typx6: TypeForm[int] = STR_T # E: Incompatible types in assignment (expression has type "type[str]", variable has type "TypeForm[int]") @@ -378,12 +378,12 @@ STR_TF: TypeForm[str] = str OBJECT_TF: TypeForm[object] = object ANY_TF: TypeForm = object reveal_type(ANY_TF) # N: Revealed type is "TypeForm[Any]" -typ1: Type[int] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[Union[int, str]]", variable has type "type[int]") +typ1: Type[int] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[int | str]", variable has type "type[int]") typ2: Type[int] = INT_TF # E: Incompatible types in assignment (expression has type "TypeForm[int]", variable has type "type[int]") typ3: Type[int] = STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[str]", variable has type "type[int]") typ4: Type[int] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "type[int]") typ5: Type[int] = ANY_TF # E: Incompatible types in assignment (expression has type "TypeForm[Any]", variable has type "type[int]") -typ6: Type[object] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[Union[int, str]]", variable has type "type[object]") +typ6: Type[object] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[int | str]", variable has type "type[object]") typ7: Type[object] = INT_TF # E: Incompatible types in assignment (expression has type "TypeForm[int]", variable has type "type[object]") typ8: Type[object] = STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[str]", variable has type "type[object]") typ9: Type[object] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "type[object]") @@ -586,9 +586,9 @@ reveal_type(f(g)) # N: Revealed type is "type[TypedDict({'b': builtins.str, 'c' # flags: --python-version 3.14 --enable-incomplete-feature=TypeForm from typing_extensions import TypeForm tf1 = TypeForm(int | str) -reveal_type(tf1) # N: Revealed type is "TypeForm[Union[builtins.int, builtins.str]]" +reveal_type(tf1) # N: Revealed type is "TypeForm[builtins.int | builtins.str]" tf2 = TypeForm('int | str') -reveal_type(tf2) # N: Revealed type is "TypeForm[Union[builtins.int, builtins.str]]" +reveal_type(tf2) # N: Revealed type is "TypeForm[builtins.int | builtins.str]" tf3: TypeForm = TypeForm(int | str) reveal_type(tf3) # N: Revealed type is "TypeForm[Any]" tf4: TypeForm = TypeForm(1) # E: Invalid type: try using Literal[1] instead? @@ -620,7 +620,7 @@ from typing_extensions import TypeForm, TypeVar T = TypeVar('T') def as_typeform(typx: TypeForm[T]) -> TypeForm[T]: return typx -reveal_type(as_typeform(int | str)) # N: Revealed type is "TypeForm[Union[builtins.int, builtins.str]]" +reveal_type(as_typeform(int | str)) # N: Revealed type is "TypeForm[builtins.int | builtins.str]" [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] @@ -633,8 +633,8 @@ def as_type(typx: TypeForm[T]) -> Type[T] | None: return typx else: return None -reveal_type(as_type(int | str)) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], None]" -reveal_type(as_type(int)) # N: Revealed type is "Union[type[builtins.int], None]" +reveal_type(as_type(int | str)) # N: Revealed type is "type[builtins.int] | type[builtins.str] | None" +reveal_type(as_type(int)) # N: Revealed type is "type[builtins.int] | None" [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] @@ -647,8 +647,8 @@ def as_instance(typx: TypeForm[T]) -> T | None: return typx() else: return None -reveal_type(as_instance(int | str)) # N: Revealed type is "Union[builtins.int, builtins.str, None]" -reveal_type(as_instance(int)) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(as_instance(int | str)) # N: Revealed type is "builtins.int | builtins.str | None" +reveal_type(as_instance(int)) # N: Revealed type is "builtins.int | None" [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] @@ -678,7 +678,7 @@ count: int | str = 1 if isassignable(count, int): reveal_type(count) # N: Revealed type is "builtins.int" else: - reveal_type(count) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(count) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] diff --git a/test-data/unit/check-typeguard.test b/test-data/unit/check-typeguard.test index b15458d5819a..0c93834fc23d 100644 --- a/test-data/unit/check-typeguard.test +++ b/test-data/unit/check-typeguard.test @@ -113,7 +113,7 @@ def is_foo(a: Union[int, str]) -> TypeGuard[str]: pass def main(a: Union[str, int]) -> None: if is_foo(a): reveal_type(a) # N: Revealed type is "builtins.str" - reveal_type(a) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(a) # N: Revealed type is "builtins.str | builtins.int" [builtins fixtures/tuple.pyi] [case testTypeGuardUnionOut] @@ -122,7 +122,7 @@ from typing_extensions import TypeGuard def is_foo(a: object) -> TypeGuard[Union[int, str]]: pass def main(a: object) -> None: if is_foo(a): - reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testTypeGuardNonzeroFloat] @@ -221,14 +221,14 @@ def is_int(a: object) -> TypeGuard[int]: pass def is_str(a: object) -> TypeGuard[str]: pass def intmain(a: Union[int, str]) -> None: if not is_int(a): - reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" else: reveal_type(a) # N: Revealed type is "builtins.int" def strmain(a: Union[int, str]) -> None: if is_str(a): reveal_type(a) # N: Revealed type is "builtins.str" else: - reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testTypeGuardClassMethod] @@ -286,12 +286,12 @@ def is_int_bool(a: object) -> bool: pass def main(a: List[Optional[int]]) -> None: bb = filter(lambda x: x is not None, a) - reveal_type(bb) # N: Revealed type is "typing.Iterator[Union[builtins.int, None]]" + reveal_type(bb) # N: Revealed type is "typing.Iterator[builtins.int | None]" # Also, if you replace 'bool' with 'Any' in the second overload, bb is Iterator[Any] cc = filter(is_int_typeguard, a) reveal_type(cc) # N: Revealed type is "typing.Iterator[builtins.int]" dd = filter(is_int_bool, a) - reveal_type(dd) # N: Revealed type is "typing.Iterator[Union[builtins.int, None]]" + reveal_type(dd) # N: Revealed type is "typing.Iterator[builtins.int | None]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] @@ -402,7 +402,7 @@ def g(x: object) -> None: ... def test(x: Any) -> None: if not(f(x) or x): return - g(reveal_type(x)) # N: Revealed type is "Union[__main__.A, Any]" + g(reveal_type(x)) # N: Revealed type is "__main__.A | Any" [builtins fixtures/tuple.pyi] [case testTypeGuardNestedRestrictionUnionOther] @@ -418,7 +418,7 @@ def g(x: object) -> None: ... def test(x: object) -> None: if not(f(x) or f2(x)): return - g(reveal_type(x)) # N: Revealed type is "Union[__main__.A, __main__.B]" + g(reveal_type(x)) # N: Revealed type is "__main__.A | __main__.B" [builtins fixtures/tuple.pyi] [case testTypeGuardComprehensionSubtype] @@ -452,7 +452,7 @@ def g(x: object) -> None: ... def test(x: List[object]) -> None: if not(f(x) or isinstance(x, A)): return - g(reveal_type(x)) # N: Revealed type is "Union[builtins.list[builtins.str], __main__.]" + g(reveal_type(x)) # N: Revealed type is "builtins.list[builtins.str] | __main__." [builtins fixtures/tuple.pyi] [case testTypeGuardMultipleCondition-xfail] @@ -859,7 +859,7 @@ def is_b(x: object) -> TypeGuard[B]: ... T = TypeVar("T") def test(x: T) -> T: if isinstance(x, A) or is_b(x): - reveal_type(x.x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x.x) # N: Revealed type is "builtins.int | builtins.str" return x [builtins fixtures/isinstance.pyi] @@ -895,5 +895,5 @@ def main(x: Union[A, B]) -> None: if is_a(x): reveal_type(x) # N: Revealed type is "__main__.A" else: - reveal_type(x) # N: Revealed type is "Union[__main__.A, __main__.B]" + reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" [builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-typeis.test b/test-data/unit/check-typeis.test index 2f54ac5bf5db..ca0f5e5ff1db 100644 --- a/test-data/unit/check-typeis.test +++ b/test-data/unit/check-typeis.test @@ -122,7 +122,7 @@ def main(a: Union[str, int]) -> None: reveal_type(a) # N: Revealed type is "builtins.str" else: reveal_type(a) # N: Revealed type is "builtins.int" - reveal_type(a) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(a) # N: Revealed type is "builtins.str | builtins.int" [builtins fixtures/tuple.pyi] [case testTypeIsUnionOut] @@ -131,7 +131,7 @@ from typing_extensions import TypeIs def is_foo(a: object) -> TypeIs[Union[int, str]]: pass def main(a: object) -> None: if is_foo(a): - reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testTypeIsUnionWithGeneric] @@ -147,28 +147,28 @@ def f1(a: Union[List[int], List[str]]) -> None: reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" else: reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]" - reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" def f2(a: Union[List[int], int]) -> None: if is_int_list(a): reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" else: reveal_type(a) # N: Revealed type is "builtins.int" - reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.int]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.int" def f3(a: Union[List[bool], List[str]]) -> None: if is_int_seq(a): reveal_type(a) # N: Revealed type is "builtins.list[builtins.bool]" else: reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]" - reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.bool], builtins.list[builtins.str]]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.bool] | builtins.list[builtins.str]" def f4(a: Union[List[int], int]) -> None: if is_seq(a): reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" else: reveal_type(a) # N: Revealed type is "builtins.int" - reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.int]" + reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.int" [builtins fixtures/tuple.pyi] [case testTypeIsTupleGeneric] @@ -197,26 +197,26 @@ def test3(t: tuple[A | B]) -> None: if is_tuple_of_B(t): reveal_type(t) # N: Revealed type is "tuple[__main__.B]" else: - reveal_type(t) # N: Revealed type is "tuple[Union[__main__.A, __main__.B]]" + reveal_type(t) # N: Revealed type is "tuple[__main__.A | __main__.B]" def test4(t: tuple[A | B, A | B]) -> None: if is_tuple_of_B(t): reveal_type(t) # N: Revealed type is "tuple[__main__.B, __main__.B]" else: - reveal_type(t) # N: Revealed type is "tuple[Union[__main__.A, __main__.B], Union[__main__.A, __main__.B]]" + reveal_type(t) # N: Revealed type is "tuple[__main__.A | __main__.B, __main__.A | __main__.B]" def test5(t: tuple[A | B, ...]) -> None: if is_tuple_of_B(t): reveal_type(t) # N: Revealed type is "builtins.tuple[__main__.B, ...]" else: - reveal_type(t) # N: Revealed type is "builtins.tuple[Union[__main__.A, __main__.B], ...]" + reveal_type(t) # N: Revealed type is "builtins.tuple[__main__.A | __main__.B, ...]" def test6(t: tuple[B, Unpack[tuple[A | B, ...]], B]) -> None: if is_tuple_of_B(t): # Should this be tuple[B, *tuple[B, ...], B] reveal_type(t) # N: Revealed type is "tuple[__main__.B, Never, __main__.B]" else: - reveal_type(t) # N: Revealed type is "tuple[__main__.B, Unpack[builtins.tuple[Union[__main__.A, __main__.B], ...]], __main__.B]" + reveal_type(t) # N: Revealed type is "tuple[__main__.B, Unpack[builtins.tuple[__main__.A | __main__.B, ...]], __main__.B]" [builtins fixtures/tuple.pyi] [case testTypeIsNonzeroFloat] @@ -368,12 +368,12 @@ def is_int_bool(a: object) -> bool: pass def main(a: List[Optional[int]]) -> None: bb = filter(lambda x: x is not None, a) - reveal_type(bb) # N: Revealed type is "typing.Iterator[Union[builtins.int, None]]" + reveal_type(bb) # N: Revealed type is "typing.Iterator[builtins.int | None]" # Also, if you replace 'bool' with 'Any' in the second overload, bb is Iterator[Any] cc = filter(is_int_typeis, a) reveal_type(cc) # N: Revealed type is "typing.Iterator[builtins.int]" dd = filter(is_int_bool, a) - reveal_type(dd) # N: Revealed type is "typing.Iterator[Union[builtins.int, None]]" + reveal_type(dd) # N: Revealed type is "typing.Iterator[builtins.int | None]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] @@ -426,7 +426,7 @@ def coverage2(obj: Any) -> bool: if not (isfloat(obj) or isstr(obj)): reveal_type(obj) # N: Revealed type is "Any" return True - reveal_type(obj) # N: Revealed type is "Union[builtins.float, builtins.str]" + reveal_type(obj) # N: Revealed type is "builtins.float | builtins.str" return False [builtins fixtures/classmethod.pyi] @@ -488,7 +488,7 @@ def g(x: object) -> None: ... def test(x: Any) -> None: if not(f(x) or x): return - g(reveal_type(x)) # N: Revealed type is "Union[__main__.A, Any]" + g(reveal_type(x)) # N: Revealed type is "__main__.A | Any" [builtins fixtures/tuple.pyi] [case testTypeIsNestedRestrictionUnionOther] @@ -504,7 +504,7 @@ def g(x: object) -> None: ... def test(x: object) -> None: if not(f(x) or f2(x)): return - g(reveal_type(x)) # N: Revealed type is "Union[__main__.A, __main__.B]" + g(reveal_type(x)) # N: Revealed type is "__main__.A | __main__.B" [builtins fixtures/tuple.pyi] [case testTypeIsComprehensionSubtype] @@ -539,7 +539,7 @@ def g(x: object) -> None: ... def test(x: List[Any]) -> None: if not(f(x) or isinstance(x, A)): return - g(reveal_type(x)) # N: Revealed type is "Union[builtins.list[builtins.str], __main__.]" + g(reveal_type(x)) # N: Revealed type is "builtins.list[builtins.str] | __main__." [builtins fixtures/tuple.pyi] [case testTypeIsMultipleCondition] @@ -931,7 +931,7 @@ def main(f: Callable[[], int | Awaitable[int]]) -> None: if is_async_callable(f): reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> typing.Awaitable[Any]" else: - reveal_type(f) # N: Revealed type is "def () -> Union[builtins.int, typing.Awaitable[builtins.int]]" + reveal_type(f) # N: Revealed type is "def () -> builtins.int | typing.Awaitable[builtins.int]" [builtins fixtures/tuple.pyi] [case testTypeIsWithDefer] diff --git a/test-data/unit/check-typevar-defaults.test b/test-data/unit/check-typevar-defaults.test index 60ffd520d1eb..2f9506b71bfb 100644 --- a/test-data/unit/check-typevar-defaults.test +++ b/test-data/unit/check-typevar-defaults.test @@ -274,11 +274,11 @@ def func_a4( a: ClassA4, b: ClassA4[float], ) -> None: - reveal_type(a) # N: Revealed type is "__main__.ClassA4[Union[builtins.int, None]]" + reveal_type(a) # N: Revealed type is "__main__.ClassA4[builtins.int | None]" reveal_type(b) # N: Revealed type is "__main__.ClassA4[builtins.float]" k = ClassA4() - reveal_type(k) # N: Revealed type is "__main__.ClassA4[Union[builtins.int, None]]" + reveal_type(k) # N: Revealed type is "__main__.ClassA4[builtins.int | None]" l = ClassA4[float]() reveal_type(l) # N: Revealed type is "__main__.ClassA4[builtins.float]" @@ -591,11 +591,11 @@ def func_a3( d: TA3[float, float, float], e: TA3[float, float, float, float], # E: Bad number of arguments for type alias, expected between 1 and 3, given 4 ) -> None: - reveal_type(a) # N: Revealed type is "Union[builtins.dict[Any, builtins.int], builtins.list[builtins.str]]" - reveal_type(b) # N: Revealed type is "Union[builtins.dict[builtins.float, builtins.int], builtins.list[builtins.str]]" - reveal_type(c) # N: Revealed type is "Union[builtins.dict[builtins.float, builtins.float], builtins.list[builtins.str]]" - reveal_type(d) # N: Revealed type is "Union[builtins.dict[builtins.float, builtins.float], builtins.list[builtins.float]]" - reveal_type(e) # N: Revealed type is "Union[builtins.dict[Any, builtins.int], builtins.list[builtins.str]]" + reveal_type(a) # N: Revealed type is "builtins.dict[Any, builtins.int] | builtins.list[builtins.str]" + reveal_type(b) # N: Revealed type is "builtins.dict[builtins.float, builtins.int] | builtins.list[builtins.str]" + reveal_type(c) # N: Revealed type is "builtins.dict[builtins.float, builtins.float] | builtins.list[builtins.str]" + reveal_type(d) # N: Revealed type is "builtins.dict[builtins.float, builtins.float] | builtins.list[builtins.float]" + reveal_type(e) # N: Revealed type is "builtins.dict[Any, builtins.int] | builtins.list[builtins.str]" TA4 = Tuple[T1, T4, T2] @@ -777,7 +777,7 @@ class A(Generic[T1, T2]): MyA = A[T1, int] a: MyA = A(None, 10) -reveal_type(a.a) # N: Revealed type is "Union[builtins.int, None]" +reveal_type(a.a) # N: Revealed type is "builtins.int | None" [builtins fixtures/tuple.pyi] [case testTypeVarConstraintsDefaultAliasesTypeAliasType] diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index c60d0aec0835..f646fd18e65f 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -24,7 +24,7 @@ def g(a: Tuple[Unpack[Ts]], b: Tuple[Unpack[Ts]]) -> Tuple[Unpack[Ts]]: reveal_type(g(args, args)) # N: Revealed type is "tuple[builtins.int, builtins.str]" reveal_type(g(args, args2)) # N: Revealed type is "tuple[builtins.int, builtins.str]" -reveal_type(g(args, args3)) # N: Revealed type is "builtins.tuple[Union[builtins.int, builtins.str], ...]" +reveal_type(g(args, args3)) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]" reveal_type(g(any, any)) # N: Revealed type is "builtins.tuple[Any, ...]" [builtins fixtures/tuple.pyi] @@ -471,13 +471,13 @@ def foo2(*args: Unpack[Tuple[str, Unpack[Tuple[int, ...]], bool, bool]]) -> None def foo3(*args: Unpack[Tuple[str, Unpack[Tuple[int, ...]], str, float]]) -> None: reveal_type(args[0]) # N: Revealed type is "builtins.str" - reveal_type(args[1]) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(args[2]) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.float]" + reveal_type(args[1]) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(args[2]) # N: Revealed type is "builtins.int | builtins.str | builtins.float" args[3] # E: Tuple index out of range \ # N: Variadic tuple can have length 3 reveal_type(args[-1]) # N: Revealed type is "builtins.float" reveal_type(args[-2]) # N: Revealed type is "builtins.str" - reveal_type(args[-3]) # N: Revealed type is "Union[builtins.str, builtins.int]" + reveal_type(args[-3]) # N: Revealed type is "builtins.str | builtins.int" args[-4] # E: Tuple index out of range \ # N: Variadic tuple can have length 3 reveal_type(args[::-1]) # N: Revealed type is "tuple[builtins.float, builtins.str, Unpack[builtins.tuple[builtins.int, ...]], builtins.str]" @@ -789,11 +789,11 @@ Ts = TypeVarTuple("Ts") A = Tuple[Unpack[Ts], Optional[A[Unpack[Ts]]]] x: A[int, str] -reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str, Union[..., None]]" +reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str, ... | None]" *_, last = x if last is not None: - reveal_type(last) # N: Revealed type is "tuple[builtins.int, builtins.str, Union[tuple[builtins.int, builtins.str, Union[..., None]], None]]" + reveal_type(last) # N: Revealed type is "tuple[builtins.int, builtins.str, tuple[builtins.int, builtins.str, ... | None] | None]" [builtins fixtures/tuple.pyi] [case testVariadicAliasUpperBoundCheck] @@ -909,7 +909,7 @@ def convert(obj: Any, *to_classes: Unpack[Tuple[Type[T], ...]]) -> Optional[T]: ... x = convert(1, int, float) -reveal_type(x) # N: Revealed type is "Union[builtins.float, None]" +reveal_type(x) # N: Revealed type is "builtins.float | None" [builtins fixtures/tuple.pyi] [case testHomogeneousGenericTupleUnpackInferenceNoCrash2] @@ -988,7 +988,7 @@ from typing_extensions import Unpack def pipeline(*xs: Unpack[Tuple[int, Unpack[Tuple[float, ...]], bool]]) -> None: for x in xs: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.float]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.float" [builtins fixtures/tuple.pyi] [case testFixedUnpackItemInInstanceArguments] @@ -1198,7 +1198,7 @@ Ts = TypeVarTuple("Ts") Alias = Tuple[int, Unpack[Ts], str] A = Union[int, str] -x: List[Alias[int, Unpack[A], str]] # E: "Union[int, str]" cannot be unpacked (must be tuple or TypeVarTuple) +x: List[Alias[int, Unpack[A], str]] # E: "int | str" cannot be unpacked (must be tuple or TypeVarTuple) reveal_type(x) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.int, Unpack[builtins.tuple[Any, ...]], builtins.str, builtins.str]]" y: List[Alias[int, Unpack[Undefined], str]] # E: Name "Undefined" is not defined reveal_type(y) # N: Revealed type is "builtins.list[tuple[builtins.int, Unpack[builtins.tuple[Any, ...]], builtins.str]]" @@ -1715,7 +1715,7 @@ vt: Tuple[int, Unpack[Tuple[float, ...]], int] reveal_type(vt + (1, 2)) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.int, Literal[1]?, Literal[2]?]" reveal_type((1, 2) + vt) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?, builtins.int, Unpack[builtins.tuple[builtins.float, ...]], builtins.int]" -reveal_type(vt + vt) # N: Revealed type is "builtins.tuple[Union[builtins.int, builtins.float], ...]" +reveal_type(vt + vt) # N: Revealed type is "builtins.tuple[builtins.int | builtins.float, ...]" reveal_type(vtf + (1, 2)) # N: Revealed type is "tuple[Unpack[builtins.tuple[builtins.float, ...]], Literal[1]?, Literal[2]?]" reveal_type((1, 2) + vtf) # N: Revealed type is "tuple[Literal[1]?, Literal[2]?, Unpack[builtins.tuple[builtins.float, ...]]]" @@ -2710,7 +2710,7 @@ from typing import TypeVarTuple, Generic, Unpack, Union Ts = TypeVarTuple("Ts") -class MyTuple(tuple[Unpack[Union[int, str]]], Generic[Unpack[Ts]]): # E: "Union[int, str]" cannot be unpacked (must be tuple or TypeVarTuple) +class MyTuple(tuple[Unpack[Union[int, str]]], Generic[Unpack[Ts]]): # E: "int | str" cannot be unpacked (must be tuple or TypeVarTuple) ... x: MyTuple[int, str] diff --git a/test-data/unit/check-typevar-unbound.test b/test-data/unit/check-typevar-unbound.test index 587ae6577328..79d326f9fedf 100644 --- a/test-data/unit/check-typevar-unbound.test +++ b/test-data/unit/check-typevar-unbound.test @@ -80,7 +80,7 @@ class B: ... lookup_table: Mapping[str, Type[Union[A,B]]] def load(lookup_table: Mapping[str, Type[T]], lookup_key: str) -> T: ... -reveal_type(load(lookup_table, "a")) # N: Revealed type is "Union[__main__.A, __main__.B]" +reveal_type(load(lookup_table, "a")) # N: Revealed type is "__main__.A | __main__.B" lookup_table_a: Mapping[str, Type[A]] def load2(lookup_table: Mapping[str, Type[Union[T, int]]], lookup_key: str) -> T: @@ -105,7 +105,7 @@ T2 = TypeVar("T2", bool, str) def foo2(t: Type[T2]) -> None: ... foo2(t1) # Rejected correctly: T2 cannot be Union[bool, str] -foo2(t2) # E: Value of type variable "T2" of "foo2" cannot be "Union[bool, str]" +foo2(t2) # E: Value of type variable "T2" of "foo2" cannot be "bool | str" T3 = TypeVar("T3") def foo3(t: Type[T3]) -> None: ... diff --git a/test-data/unit/check-typevar-values.test b/test-data/unit/check-typevar-values.test index 1be75c0f4706..0e8b3f74f2a7 100644 --- a/test-data/unit/check-typevar-values.test +++ b/test-data/unit/check-typevar-values.test @@ -657,7 +657,7 @@ T = TypeVar("T", bound=Union[Data, Dict[str, str]]) def f(data: T) -> None: - reveal_type(data["x"]) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(data["x"]) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index 35af44c62800..91eac5b7eec8 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -4,32 +4,32 @@ # flags: --python-version 3.10 from __future__ import annotations def f(x: int | str) -> int | str: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" z: int | str = 0 - reveal_type(z) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" return x -reveal_type(f) # N: Revealed type is "def (x: Union[builtins.int, builtins.str]) -> Union[builtins.int, builtins.str]" +reveal_type(f) # N: Revealed type is "def (x: builtins.int | builtins.str) -> builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testUnionOrSyntaxWithThreeBuiltinsTypes] # flags: --python-version 3.10 def f(x: int | str | float) -> int | str | float: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.float]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.float" z: int | str | float = 0 - reveal_type(z) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.float]" + reveal_type(z) # N: Revealed type is "builtins.int | builtins.str | builtins.float" return x -reveal_type(f) # N: Revealed type is "def (x: Union[builtins.int, builtins.str, builtins.float]) -> Union[builtins.int, builtins.str, builtins.float]" +reveal_type(f) # N: Revealed type is "def (x: builtins.int | builtins.str | builtins.float) -> builtins.int | builtins.str | builtins.float" [case testUnionOrSyntaxWithTwoTypes] # flags: --python-version 3.10 class A: pass class B: pass def f(x: A | B) -> A | B: - reveal_type(x) # N: Revealed type is "Union[__main__.A, __main__.B]" + reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" z: A | B = A() - reveal_type(z) # N: Revealed type is "Union[__main__.A, __main__.B]" + reveal_type(z) # N: Revealed type is "__main__.A | __main__.B" return x -reveal_type(f) # N: Revealed type is "def (x: Union[__main__.A, __main__.B]) -> Union[__main__.A, __main__.B]" +reveal_type(f) # N: Revealed type is "def (x: __main__.A | __main__.B) -> __main__.A | __main__.B" [case testUnionOrSyntaxWithThreeTypes] # flags: --python-version 3.10 @@ -37,11 +37,11 @@ class A: pass class B: pass class C: pass def f(x: A | B | C) -> A | B | C: - reveal_type(x) # N: Revealed type is "Union[__main__.A, __main__.B, __main__.C]" + reveal_type(x) # N: Revealed type is "__main__.A | __main__.B | __main__.C" z: A | B | C = A() - reveal_type(z) # N: Revealed type is "Union[__main__.A, __main__.B, __main__.C]" + reveal_type(z) # N: Revealed type is "__main__.A | __main__.B | __main__.C" return x -reveal_type(f) # N: Revealed type is "def (x: Union[__main__.A, __main__.B, __main__.C]) -> Union[__main__.A, __main__.B, __main__.C]" +reveal_type(f) # N: Revealed type is "def (x: __main__.A | __main__.B | __main__.C) -> __main__.A | __main__.B | __main__.C" [case testUnionOrSyntaxWithLiteral] # flags: --python-version 3.10 @@ -64,36 +64,36 @@ z: str | 42 | int # E: Invalid type: try using Literal[42] instead? # flags: --python-version 3.10 from typing import List x: List[int | str] -reveal_type(x) # N: Revealed type is "builtins.list[Union[builtins.int, builtins.str]]" +reveal_type(x) # N: Revealed type is "builtins.list[builtins.int | builtins.str]" [builtins fixtures/list.pyi] [case testUnionOrSyntaxWithQuotedFunctionTypes] from typing import Union def f(x: 'Union[int, str, None]') -> 'Union[int, None]': - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" return 42 -reveal_type(f) # N: Revealed type is "def (x: Union[builtins.int, builtins.str, None]) -> Union[builtins.int, None]" +reveal_type(f) # N: Revealed type is "def (x: builtins.int | builtins.str | None) -> builtins.int | None" def g(x: "int | str | None") -> "int | None": - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, None]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | None" return 42 -reveal_type(g) # N: Revealed type is "def (x: Union[builtins.int, builtins.str, None]) -> Union[builtins.int, None]" +reveal_type(g) # N: Revealed type is "def (x: builtins.int | builtins.str | None) -> builtins.int | None" [case testUnionOrSyntaxWithQuotedVariableTypes] y: "int | str" = 42 -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" [case testUnionOrSyntaxWithTypeAliasWorking] # flags: --python-version 3.10 T = int | str x: T -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" S = list[int] | str | None y: S -reveal_type(y) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.str, None]" +reveal_type(y) # N: Revealed type is "builtins.list[builtins.int] | builtins.str | None" U = str | None z: U -reveal_type(z) # N: Revealed type is "Union[builtins.str, None]" +reveal_type(z) # N: Revealed type is "builtins.str | None" def f(): pass @@ -148,14 +148,14 @@ class C(list[int | None]): def f() -> object: pass -reveal_type(cast(str | None, f())) # N: Revealed type is "Union[builtins.str, None]" -reveal_type(list[str | None]()) # N: Revealed type is "builtins.list[Union[builtins.str, None]]" +reveal_type(cast(str | None, f())) # N: Revealed type is "builtins.str | None" +reveal_type(list[str | None]()) # N: Revealed type is "builtins.list[builtins.str | None]" [builtins fixtures/type.pyi] [case testUnionOrSyntaxRuntimeContextInStubFile] import lib -reveal_type(lib.x) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.str], None]" -reveal_type(lib.y) # N: Revealed type is "builtins.list[Union[builtins.int, None]]" +reveal_type(lib.x) # N: Revealed type is "builtins.int | builtins.list[builtins.str] | None" +reveal_type(lib.y) # N: Revealed type is "builtins.list[builtins.int | None]" [file lib.pyi] A = int | list[str] | None @@ -172,13 +172,13 @@ class C: pass def f(x: int | str | C) -> None: if isinstance(x, int | str): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: reveal_type(x) # N: Revealed type is "__main__.C" def g(x: int | str | tuple[int, str] | C) -> None: if isinstance(x, int | str | tuple): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, tuple[builtins.int, builtins.str]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | tuple[builtins.int, builtins.str]" else: reveal_type(x) # N: Revealed type is "__main__.C" [builtins fixtures/isinstance_python3_10.pyi] @@ -187,7 +187,7 @@ def g(x: int | str | tuple[int, str] | C) -> None: from typing import Union def f(x: Union[int, str, None]) -> None: if isinstance(x, int | str): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: reveal_type(x) # N: Revealed type is "None" [builtins fixtures/isinstance.pyi] @@ -195,7 +195,7 @@ def f(x: Union[int, str, None]) -> None: [case testImplicit604TypeAliasWithCyclicImportInStub] # flags: --python-version 3.10 from was_builtins import foo -reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]" +reveal_type(foo) # N: Revealed type is "builtins.str | was_mmap.mmap" [file was_builtins.pyi] import was_mmap WriteableBuffer = was_mmap.mmap @@ -212,7 +212,7 @@ SimpleAlias = int | str def foo(x: int | str | tuple): if isinstance(x, SimpleAlias): - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" else: reveal_type(x) # N: Revealed type is "builtins.tuple[Any, ...]" @@ -231,7 +231,7 @@ def foo(value: str | bool | None): def bar(value: object): assert isinstance(value, str | None) - reveal_type(value) # N: Revealed type is "Union[builtins.str, None]" + reveal_type(value) # N: Revealed type is "builtins.str | None" [builtins fixtures/type.pyi] diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index a578735ec2c5..b83ffaf52d80 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -64,18 +64,18 @@ z: str if int(): y = w.y -v.y # E: Item "C" of "Union[C, D]" has no attribute "y" \ - # E: Item "D" of "Union[C, D]" has no attribute "y" -u.y # E: Item "C" of "Union[A, C, D]" has no attribute "y" \ - # E: Item "D" of "Union[A, C, D]" has no attribute "y" +v.y # E: Item "C" of "C | D" has no attribute "y" \ + # E: Item "D" of "C | D" has no attribute "y" +u.y # E: Item "C" of "A | C | D" has no attribute "y" \ + # E: Item "D" of "A | C | D" has no attribute "y" if int(): z = w.y # E: Incompatible types in assignment (expression has type "int", variable has type "str") w.y = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int") if int(): - y = x.y # E: Item "C" of "Union[A, C]" has no attribute "y" -zz = x.y # E: Item "C" of "Union[A, C]" has no attribute "y" + y = x.y # E: Item "C" of "A | C" has no attribute "y" +zz = x.y # E: Item "C" of "A | C" has no attribute "y" if int(): - z = zz # E: Incompatible types in assignment (expression has type "Union[int, Any]", variable has type "str") + z = zz # E: Incompatible types in assignment (expression has type "int | Any", variable has type "str") [builtins fixtures/isinstance.pyi] @@ -97,7 +97,7 @@ x.foo() y.foo() i = x.foo() if int(): - i = y.foo() # E: Incompatible types in assignment (expression has type "Union[int, str]", variable has type "int") + i = y.foo() # E: Incompatible types in assignment (expression has type "int | str", variable has type "int") [builtins fixtures/isinstance.pyi] @@ -106,7 +106,7 @@ from typing import Union, List x: Union[List[int], str] x[2] x[2] + 1 # E: Unsupported operand types for + ("str" and "int") \ - # N: Left operand is of type "Union[int, str]" + # N: Left operand is of type "int | str" [builtins fixtures/isinstancelist.pyi] [case testUnionAsOverloadArg] @@ -153,7 +153,7 @@ from typing import Optional def f(x: Optional[int]) -> None: pass f(1) f(None) -f('') # E: Argument 1 to "f" has incompatible type "str"; expected "Optional[int]" +f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int | None" [case testUnionWithNoReturn] from typing import Union, NoReturn @@ -196,15 +196,15 @@ elif foo(): elif foo(): def f(x: Union[int, str, float]) -> None: pass # E: All conditional function variants must have identical signatures \ # N: Original: \ - # N: def f(x: Union[int, str]) -> None \ + # N: def f(x: int | str) -> None \ # N: Redefinition: \ - # N: def f(x: Union[int, str, float]) -> None + # N: def f(x: int | str | float) -> None elif foo(): def f(x: Union[S, T]) -> None: pass elif foo(): def f(x: Union[str]) -> None: pass # E: All conditional function variants must have identical signatures \ # N: Original: \ - # N: def f(x: Union[int, str]) -> None \ + # N: def f(x: int | str) -> None \ # N: Redefinition: \ # N: def f(x: str) -> None else: @@ -217,9 +217,9 @@ if foo(): else: def g(x: Union[int, str]) -> None: pass # E: All conditional function variants must have identical signatures \ # N: Original: \ - # N: def g(x: Union[int, str, bytes]) -> None \ + # N: def g(x: int | str | bytes) -> None \ # N: Redefinition: \ - # N: def g(x: Union[int, str]) -> None + # N: def g(x: int | str) -> None [case testUnionSimplificationSpecialCases] # flags: --no-strict-optional @@ -236,8 +236,8 @@ a = None # type: Any reveal_type(u(C(), None)) # N: Revealed type is "__main__.C" reveal_type(u(None, C())) # N: Revealed type is "__main__.C" -reveal_type(u(C(), a)) # N: Revealed type is "Union[Any, __main__.C]" -reveal_type(u(a, C())) # N: Revealed type is "Union[__main__.C, Any]" +reveal_type(u(C(), a)) # N: Revealed type is "Any | __main__.C" +reveal_type(u(a, C())) # N: Revealed type is "__main__.C | Any" reveal_type(u(C(), C())) # N: Revealed type is "__main__.C" reveal_type(u(a, a)) # N: Revealed type is "Any" @@ -252,8 +252,8 @@ S = TypeVar('S') def u(x: T, y: S) -> Union[S, T]: pass def f(x: T) -> None: - reveal_type(u(C(), x)) # N: Revealed type is "Union[T`-1, __main__.C]" - reveal_type(u(x, C())) # N: Revealed type is "Union[__main__.C, T`-1]" + reveal_type(u(C(), x)) # N: Revealed type is "T`-1 | __main__.C" + reveal_type(u(x, C())) # N: Revealed type is "__main__.C | T`-1" [case testUnionSimplificationSpecialCase3] from typing import Any, TypeVar, Generic, Union @@ -268,7 +268,7 @@ class M(Generic[V]): def f(x: M[C]) -> None: y = x.get(None) - reveal_type(y) # N: Revealed type is "Union[__main__.C, None]" + reveal_type(y) # N: Revealed type is "__main__.C | None" [case testUnionSimplificationSpecialCases2] # flags: --no-strict-optional @@ -291,24 +291,24 @@ reveal_type(u(1, None)) # N: Revealed type is "builtins.int" reveal_type(u(None, 1)) # N: Revealed type is "builtins.int" # Normal instance type and base-class-Any, no simplification -reveal_type(u(C(), 1)) # N: Revealed type is "Union[builtins.int, __main__.C]" -reveal_type(u(1, C())) # N: Revealed type is "Union[__main__.C, builtins.int]" +reveal_type(u(C(), 1)) # N: Revealed type is "builtins.int | __main__.C" +reveal_type(u(1, C())) # N: Revealed type is "__main__.C | builtins.int" # Normal instance type and Any, no simplification -reveal_type(u(1, a)) # N: Revealed type is "Union[Any, builtins.int]" -reveal_type(u(a, 1)) # N: Revealed type is "Union[builtins.int, Any]" +reveal_type(u(1, a)) # N: Revealed type is "Any | builtins.int" +reveal_type(u(a, 1)) # N: Revealed type is "builtins.int | Any" # Any and base-class-Any, no simplification -reveal_type(u(C(), a)) # N: Revealed type is "Union[Any, __main__.C]" -reveal_type(u(a, C())) # N: Revealed type is "Union[__main__.C, Any]" +reveal_type(u(C(), a)) # N: Revealed type is "Any | __main__.C" +reveal_type(u(a, C())) # N: Revealed type is "__main__.C | Any" # Two normal instance types, simplify reveal_type(u(1, object())) # N: Revealed type is "builtins.object" reveal_type(u(object(), 1)) # N: Revealed type is "builtins.object" # Two normal instance types, no simplification -reveal_type(u(1, '')) # N: Revealed type is "Union[builtins.str, builtins.int]" -reveal_type(u('', 1)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(u(1, '')) # N: Revealed type is "builtins.str | builtins.int" +reveal_type(u('', 1)) # N: Revealed type is "builtins.int | builtins.str" [case testUnionSimplificationWithDuplicateItems] from typing import Any, TypeVar, Union @@ -323,17 +323,17 @@ def u(x: T, y: S, z: R) -> Union[R, S, T]: pass a: Any reveal_type(u(1, 1, 1)) # N: Revealed type is "builtins.int" -reveal_type(u(C(), C(), None)) # N: Revealed type is "Union[None, __main__.C]" -reveal_type(u(a, a, 1)) # N: Revealed type is "Union[builtins.int, Any]" -reveal_type(u(a, C(), a)) # N: Revealed type is "Union[Any, __main__.C]" -reveal_type(u('', 1, 1)) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(u(C(), C(), None)) # N: Revealed type is "None | __main__.C" +reveal_type(u(a, a, 1)) # N: Revealed type is "builtins.int | Any" +reveal_type(u(a, C(), a)) # N: Revealed type is "Any | __main__.C" +reveal_type(u('', 1, 1)) # N: Revealed type is "builtins.int | builtins.str" [case testUnionAndBinaryOperation] from typing import Union class A: pass def f(x: Union[int, str, A]): x + object() # E: Unsupported left operand type for + ("A") \ - # N: Left operand is of type "Union[int, str, A]" \ + # N: Left operand is of type "int | str | A" \ # E: Unsupported operand types for + ("int" and "object") \ # E: Unsupported operand types for + ("str" and "object") [builtins fixtures/primitives.pyi] @@ -347,10 +347,10 @@ C = NamedTuple('C', [('x', int)]) def foo(a: Union[A, B, C]): if isinstance(a, (B, C)): - reveal_type(a) # N: Revealed type is "Union[tuple[builtins.int, fallback=__main__.B], tuple[builtins.int, fallback=__main__.C]]" + reveal_type(a) # N: Revealed type is "tuple[builtins.int, fallback=__main__.B] | tuple[builtins.int, fallback=__main__.C]" a.x - a.y # E: Item "B" of "Union[B, C]" has no attribute "y" \ - # E: Item "C" of "Union[B, C]" has no attribute "y" + a.y # E: Item "B" of "B | C" has no attribute "y" \ + # E: Item "C" of "B | C" has no attribute "y" b = a # type: Union[B, C] [builtins fixtures/isinstance.pyi] @@ -360,10 +360,10 @@ T = TypeVar('T') S = TypeVar('S') def u(x: T, y: S) -> Union[T, S]: pass -reveal_type(u(1, 2.3)) # N: Revealed type is "Union[builtins.int, builtins.float]" -reveal_type(u(2.3, 1)) # N: Revealed type is "Union[builtins.float, builtins.int]" -reveal_type(u(False, 2.2)) # N: Revealed type is "Union[builtins.bool, builtins.float]" -reveal_type(u(2.2, False)) # N: Revealed type is "Union[builtins.float, builtins.bool]" +reveal_type(u(1, 2.3)) # N: Revealed type is "builtins.int | builtins.float" +reveal_type(u(2.3, 1)) # N: Revealed type is "builtins.float | builtins.int" +reveal_type(u(False, 2.2)) # N: Revealed type is "builtins.bool | builtins.float" +reveal_type(u(2.2, False)) # N: Revealed type is "builtins.float | builtins.bool" [builtins fixtures/primitives.pyi] [case testSimplifyingUnionWithTypeTypes1] @@ -384,14 +384,14 @@ reveal_type(u(t_a, t_a)) # N: Revealed type is "type[Any]" reveal_type(u(type, type)) # N: Revealed type is "def (x: builtins.object) -> builtins.type" # One type, other non-type -reveal_type(u(t_s, 1)) # N: Revealed type is "Union[builtins.int, type[builtins.str]]" -reveal_type(u(1, t_s)) # N: Revealed type is "Union[type[builtins.str], builtins.int]" -reveal_type(u(type, 1)) # N: Revealed type is "Union[builtins.int, def (x: builtins.object) -> builtins.type]" -reveal_type(u(1, type)) # N: Revealed type is "Union[def (x: builtins.object) -> builtins.type, builtins.int]" -reveal_type(u(t_a, 1)) # N: Revealed type is "Union[builtins.int, type[Any]]" -reveal_type(u(1, t_a)) # N: Revealed type is "Union[type[Any], builtins.int]" -reveal_type(u(t_o, 1)) # N: Revealed type is "Union[builtins.int, type[builtins.object]]" -reveal_type(u(1, t_o)) # N: Revealed type is "Union[type[builtins.object], builtins.int]" +reveal_type(u(t_s, 1)) # N: Revealed type is "builtins.int | type[builtins.str]" +reveal_type(u(1, t_s)) # N: Revealed type is "type[builtins.str] | builtins.int" +reveal_type(u(type, 1)) # N: Revealed type is "builtins.int | def (x: builtins.object) -> builtins.type" +reveal_type(u(1, type)) # N: Revealed type is "def (x: builtins.object) -> builtins.type | builtins.int" +reveal_type(u(t_a, 1)) # N: Revealed type is "builtins.int | type[Any]" +reveal_type(u(1, t_a)) # N: Revealed type is "type[Any] | builtins.int" +reveal_type(u(t_o, 1)) # N: Revealed type is "builtins.int | type[builtins.object]" +reveal_type(u(1, t_o)) # N: Revealed type is "type[builtins.object] | builtins.int" [case testSimplifyingUnionWithTypeTypes2] from typing import TypeVar, Union, Type, Any @@ -414,8 +414,8 @@ reveal_type(u(t_a, object())) # N: Revealed type is "builtins.object" reveal_type(u(object(), t_a)) # N: Revealed type is "builtins.object" # Union between type objects -reveal_type(u(t_o, t_a)) # N: Revealed type is "Union[type[Any], type[builtins.object]]" -reveal_type(u(t_a, t_o)) # N: Revealed type is "Union[type[builtins.object], type[Any]]" +reveal_type(u(t_o, t_a)) # N: Revealed type is "type[Any] | type[builtins.object]" +reveal_type(u(t_a, t_o)) # N: Revealed type is "type[builtins.object] | type[Any]" reveal_type(u(t_s, t_o)) # N: Revealed type is "type[builtins.object]" reveal_type(u(t_o, t_s)) # N: Revealed type is "type[builtins.object]" reveal_type(u(t_o, type)) # N: Revealed type is "type[builtins.object]" @@ -444,8 +444,8 @@ t_a: Type[A] reveal_type(u(M(*a), t_a)) # N: Revealed type is "__main__.M" reveal_type(u(t_a, M(*a))) # N: Revealed type is "__main__.M" -reveal_type(u(M2(*a), t_a)) # N: Revealed type is "Union[type[__main__.A], __main__.M2]" -reveal_type(u(t_a, M2(*a))) # N: Revealed type is "Union[__main__.M2, type[__main__.A]]" +reveal_type(u(M2(*a), t_a)) # N: Revealed type is "type[__main__.A] | __main__.M2" +reveal_type(u(t_a, M2(*a))) # N: Revealed type is "__main__.M2 | type[__main__.A]" [case testSimplifyUnionWithCallable] from typing import TypeVar, Union, Any, Callable @@ -468,11 +468,11 @@ i_C: Callable[[int], C] reveal_type(u(D_C, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" -reveal_type(u(A_C, D_C)) # N: Revealed type is "Union[def (__main__.D) -> __main__.C, def (Any) -> __main__.C]" -reveal_type(u(D_C, A_C)) # N: Revealed type is "Union[def (Any) -> __main__.C, def (__main__.D) -> __main__.C]" +reveal_type(u(A_C, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C | def (Any) -> __main__.C" +reveal_type(u(D_C, A_C)) # N: Revealed type is "def (Any) -> __main__.C | def (__main__.D) -> __main__.C" -reveal_type(u(D_A, D_C)) # N: Revealed type is "Union[def (__main__.D) -> __main__.C, def (__main__.D) -> Any]" -reveal_type(u(D_C, D_A)) # N: Revealed type is "Union[def (__main__.D) -> Any, def (__main__.D) -> __main__.C]" +reveal_type(u(D_A, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C | def (__main__.D) -> Any" +reveal_type(u(D_C, D_A)) # N: Revealed type is "def (__main__.D) -> Any | def (__main__.D) -> __main__.C" reveal_type(u(D_C, C_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" reveal_type(u(C_C, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" @@ -480,7 +480,7 @@ reveal_type(u(C_C, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" reveal_type(u(D_C, D_D)) # N: Revealed type is "def (__main__.D) -> __main__.C" reveal_type(u(D_D, D_C)) # N: Revealed type is "def (__main__.D) -> __main__.C" -reveal_type(u(D_C, i_C)) # N: Revealed type is "Union[def (builtins.int) -> __main__.C, def (__main__.D) -> __main__.C]" +reveal_type(u(D_C, i_C)) # N: Revealed type is "def (builtins.int) -> __main__.C | def (__main__.D) -> __main__.C" [case testUnionOperatorMethodSpecialCase] from typing import Union @@ -494,17 +494,17 @@ class E: [case testUnionSimplificationWithBoolIntAndFloat] from typing import List, Union l = reveal_type([]) # type: List[Union[bool, int, float]] \ - # N: Revealed type is "builtins.list[Union[builtins.int, builtins.float]]" + # N: Revealed type is "builtins.list[builtins.int | builtins.float]" reveal_type(l) \ - # N: Revealed type is "builtins.list[Union[builtins.bool, builtins.int, builtins.float]]" + # N: Revealed type is "builtins.list[builtins.bool | builtins.int | builtins.float]" [builtins fixtures/list.pyi] [case testUnionSimplificationWithBoolIntAndFloat2] from typing import List, Union l = reveal_type([]) # type: List[Union[bool, int, float, str]] \ - # N: Revealed type is "builtins.list[Union[builtins.int, builtins.float, builtins.str]]" + # N: Revealed type is "builtins.list[builtins.int | builtins.float | builtins.str]" reveal_type(l) \ - # N: Revealed type is "builtins.list[Union[builtins.bool, builtins.int, builtins.float, builtins.str]]" + # N: Revealed type is "builtins.list[builtins.bool | builtins.int | builtins.float | builtins.str]" [builtins fixtures/list.pyi] [case testNestedUnionsProcessedCorrectly] @@ -518,7 +518,7 @@ def foo(bar: Union[Union[A, B], C]) -> None: if isinstance(bar, A): reveal_type(bar) # N: Revealed type is "__main__.A" else: - reveal_type(bar) # N: Revealed type is "Union[__main__.B, __main__.C]" + reveal_type(bar) # N: Revealed type is "__main__.B | __main__.C" [builtins fixtures/isinstance.pyi] [out] @@ -528,8 +528,8 @@ x: Union[int, str] a: Any if bool(): x = a - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/bool.pyi] [case testAssignAnyToUnionWithAny] @@ -539,7 +539,7 @@ a: Any if bool(): x = a reveal_type(x) # N: Revealed type is "Any" -reveal_type(x) # N: Revealed type is "Union[builtins.int, Any]" +reveal_type(x) # N: Revealed type is "builtins.int | Any" [builtins fixtures/bool.pyi] [case testUnionMultiassignSingle] @@ -547,11 +547,11 @@ from typing import Union, Tuple, Any a: Union[Tuple[int], Tuple[float]] (a1,) = a -reveal_type(a1) # N: Revealed type is "Union[builtins.int, builtins.float]" +reveal_type(a1) # N: Revealed type is "builtins.int | builtins.float" b: Union[Tuple[int], Tuple[str]] (b1,) = b -reveal_type(b1) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(b1) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testUnionMultiassignDouble] @@ -560,7 +560,7 @@ from typing import Union, Tuple c: Union[Tuple[int, int], Tuple[int, float]] (c1, c2) = c reveal_type(c1) # N: Revealed type is "builtins.int" -reveal_type(c2) # N: Revealed type is "Union[builtins.int, builtins.float]" +reveal_type(c2) # N: Revealed type is "builtins.int | builtins.float" [builtins fixtures/tuple.pyi] [case testUnionMultiassignGeneric] @@ -572,8 +572,8 @@ def pack_two(x: T, y: S) -> Union[Tuple[T, T], Tuple[S, S]]: pass (x, y) = pack_two(1, 'a') -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [case testUnionMultiassignAny] @@ -581,8 +581,8 @@ from typing import Union, Tuple, Any d: Union[Any, Tuple[float, float]] (d1, d2) = d -reveal_type(d1) # N: Revealed type is "Union[Any, builtins.float]" -reveal_type(d2) # N: Revealed type is "Union[Any, builtins.float]" +reveal_type(d1) # N: Revealed type is "Any | builtins.float" +reveal_type(d2) # N: Revealed type is "Any | builtins.float" e: Union[Any, Tuple[float, float], int] (e1, e2) = e # E: "int" object is not iterable @@ -596,7 +596,7 @@ class B(A): pass class C(A): pass a: Union[List[B], List[C]] x, y = a -reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" [builtins fixtures/list.pyi] [case testUnionMultiassignRebind] @@ -608,8 +608,8 @@ class C(A): pass obj: object a: Union[List[B], List[C]] obj, new = a -reveal_type(obj) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(new) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(obj) # N: Revealed type is "__main__.B | __main__.C" +reveal_type(new) # N: Revealed type is "__main__.B | __main__.C" obj = 1 reveal_type(obj) # N: Revealed type is "builtins.int" @@ -627,7 +627,7 @@ b: Union[Tuple[float, int], Tuple[int, int]] b1: object b2: int (b1, b2) = b -reveal_type(b1) # N: Revealed type is "Union[builtins.float, builtins.int]" +reveal_type(b1) # N: Revealed type is "builtins.float | builtins.int" reveal_type(b2) # N: Revealed type is "builtins.int" c: Union[Tuple[int, int], Tuple[int, int]] @@ -641,7 +641,7 @@ d: Union[Tuple[int, int], Tuple[int, float]] d1: object (d1, d2) = d reveal_type(d1) # N: Revealed type is "builtins.int" -reveal_type(d2) # N: Revealed type is "Union[builtins.int, builtins.float]" +reveal_type(d2) # N: Revealed type is "builtins.int | builtins.float" [builtins fixtures/tuple.pyi] [case testUnionMultiassignIndexed] @@ -686,7 +686,7 @@ a2: object reveal_type(a1) # N: Revealed type is "builtins.int" reveal_type(xs) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(a2) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(a2) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/list.pyi] [case testUnpackingUnionOfListsInFunction] @@ -700,8 +700,8 @@ def f(x: bool) -> Union[List[int], List[str]]: def g(x: bool) -> None: a, b = f(x) - reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(b) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(b) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/list.pyi] [case testUnionOfVariableLengthTupleUnpacking] @@ -725,8 +725,8 @@ bad: Union[int, str] x, y = bad # E: "int" object is not iterable \ # E: Unpacking a string is disallowed -reveal_type(x) # N: Revealed type is "Union[Any, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[Any, builtins.str]" +reveal_type(x) # N: Revealed type is "Any | builtins.str" +reveal_type(y) # N: Revealed type is "Any | builtins.str" [builtins fixtures/primitives.pyi] @@ -757,9 +757,9 @@ from typing import Union, Tuple good: Union[Tuple[int, int], Tuple[str, str]] x, y = t = good -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(t) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.str, builtins.str]]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(t) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.str, builtins.str]" [builtins fixtures/tuple.pyi] [out] @@ -768,9 +768,9 @@ from typing import Union, Tuple good: Union[Tuple[int, int], Tuple[str, str]] t = x, y = good -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(t) # N: Revealed type is "Union[tuple[builtins.int, builtins.int], tuple[builtins.str, builtins.str]]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(t) # N: Revealed type is "tuple[builtins.int, builtins.int] | tuple[builtins.str, builtins.str]" [builtins fixtures/tuple.pyi] [out] @@ -779,10 +779,10 @@ from typing import Union, Tuple good: Union[Tuple[int, int], Tuple[str, str]] x, y = a, b = good -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(b) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(b) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [out] @@ -791,9 +791,9 @@ from typing import Union, List good: Union[List[int], List[str]] lst = x, y = good -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(lst) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" [builtins fixtures/list.pyi] [out] @@ -802,10 +802,10 @@ from typing import Union, List good: Union[List[int], List[str]] x, *y, z = lst = good -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" -reveal_type(z) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(lst) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" +reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" [builtins fixtures/list.pyi] [out] @@ -821,13 +821,13 @@ class NTStr(NamedTuple): t1: NTInt reveal_type(t1.__iter__) # N: Revealed type is "def () -> typing.Iterator[builtins.int]" nt: Union[NTInt, NTStr] -reveal_type(nt.__iter__) # N: Revealed type is "Union[def () -> typing.Iterator[builtins.int], def () -> typing.Iterator[builtins.str]]" +reveal_type(nt.__iter__) # N: Revealed type is "def () -> typing.Iterator[builtins.int] | def () -> typing.Iterator[builtins.str]" for nx in nt: - reveal_type(nx) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(nx) # N: Revealed type is "builtins.int | builtins.str" t: Union[Tuple[int, int], Tuple[str, str]] for x in t: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/for.pyi] [out] @@ -836,13 +836,13 @@ from typing import Union, List, Tuple t: Union[List[Tuple[int, int]], List[Tuple[str, str]]] for x, y in t: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" t2: List[Union[Tuple[int, int], Tuple[str, str]]] for x2, y2 in t2: - reveal_type(x2) # N: Revealed type is "Union[builtins.int, builtins.str]" - reveal_type(y2) # N: Revealed type is "Union[builtins.int, builtins.str]" + reveal_type(x2) # N: Revealed type is "builtins.int | builtins.str" + reveal_type(y2) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/for.pyi] [out] @@ -858,12 +858,12 @@ t1: Union[Tuple[A, A], Tuple[B, B]] t2: Union[Tuple[int, int], Tuple[str, str]] x, y = t1 -reveal_type(x) # N: Revealed type is "Union[__main__.A, __main__.B]" -reveal_type(y) # N: Revealed type is "Union[__main__.A, __main__.B]" +reveal_type(x) # N: Revealed type is "__main__.A | __main__.B" +reveal_type(y) # N: Revealed type is "__main__.A | __main__.B" x, y = t2 -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" x, y = object(), object() reveal_type(x) # N: Revealed type is "builtins.object" @@ -876,9 +876,9 @@ from typing import Union, Tuple t: Union[Tuple[int, Tuple[int, int]], Tuple[str, Tuple[str, str]]] x, (y, z) = t -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(z) # N: Revealed type is "Union[builtins.int, builtins.str]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" [builtins fixtures/tuple.pyi] [out] @@ -890,9 +890,9 @@ class B: pass t: Union[Tuple[int, Union[Tuple[int, int], Tuple[A, A]]], Tuple[str, Union[Tuple[str, str], Tuple[B, B]]]] x, (y, z) = t -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, __main__.A, builtins.str, __main__.B]" -reveal_type(z) # N: Revealed type is "Union[builtins.int, __main__.A, builtins.str, __main__.B]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" +reveal_type(z) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" [builtins fixtures/tuple.pyi] [out] @@ -908,9 +908,9 @@ z: object t: Union[Tuple[int, Union[Tuple[int, int], Tuple[A, A]]], Tuple[str, Union[Tuple[str, str], Tuple[B, B]]]] x, (y, z) = t -reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" -reveal_type(y) # N: Revealed type is "Union[builtins.int, __main__.A, builtins.str, __main__.B]" -reveal_type(z) # N: Revealed type is "Union[builtins.int, __main__.A, builtins.str, __main__.B]" +reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" +reveal_type(y) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" +reveal_type(z) # N: Revealed type is "builtins.int | __main__.A | builtins.str | __main__.B" [builtins fixtures/tuple.pyi] [out] @@ -921,7 +921,7 @@ a: Any d: Dict[str, Tuple[List[Tuple[str, str]], str]] x, _ = d.get(a, (None, None)) -for y in x: pass # E: Item "None" of "Optional[list[tuple[str, str]]]" has no attribute "__iter__" (not iterable) +for y in x: pass # E: Item "None" of "list[tuple[str, str]] | None" has no attribute "__iter__" (not iterable) if x: for s, t in x: reveal_type(s) # N: Revealed type is "builtins.str" @@ -936,7 +936,7 @@ x = None d: Dict[str, Tuple[List[Tuple[str, str]], str]] x, _ = d.get(a, (None, None)) -for y in x: pass # E: Item "None" of "Optional[list[tuple[str, str]]]" has no attribute "__iter__" (not iterable) +for y in x: pass # E: Item "None" of "list[tuple[str, str]] | None" has no attribute "__iter__" (not iterable) if x: for s, t in x: reveal_type(s) # N: Revealed type is "builtins.str" @@ -950,7 +950,7 @@ x: object a: Any d: Dict[str, Tuple[List[Tuple[str, str]], str]] x, _ = d.get(a, (None, None)) -reveal_type(x) # N: Revealed type is "Union[builtins.list[tuple[builtins.str, builtins.str]], None]" +reveal_type(x) # N: Revealed type is "builtins.list[tuple[builtins.str, builtins.str]] | None" if x: for y in x: pass @@ -986,7 +986,7 @@ x: Union[ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[int], def takes_int(arg: int) -> None: pass -takes_int(x) # E: Argument 1 to "takes_int" has incompatible type "Union[ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[int], ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[object], ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[float], ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[str], ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[Any], ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[bytes]]"; expected "int" +takes_int(x) # E: Argument 1 to "takes_int" has incompatible type "ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[int] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[object] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[float] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[str] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[Any] | ExtremelyLongTypeNameWhichIsGenericSoWeCanUseItMultipleTimes[bytes]"; expected "int" [case testRecursiveForwardReferenceInUnion] from typing import List, Union @@ -1035,7 +1035,7 @@ class Boop(Enum): def do_thing_with_enums(enums: Union[List[Enum], Enum]) -> None: ... boop: List[Boop] = [] -do_thing_with_enums(boop) # E: Argument 1 to "do_thing_with_enums" has incompatible type "list[Boop]"; expected "Union[list[Enum], Enum]" \ +do_thing_with_enums(boop) # E: Argument 1 to "do_thing_with_enums" has incompatible type "list[Boop]"; expected "list[Enum] | Enum" \ # N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ # N: Consider using "Sequence" instead, which is covariant [builtins fixtures/isinstancelist.pyi] @@ -1071,8 +1071,8 @@ def f(x: T, y: T) -> T: return x x: Union[None, Any] y: Union[int, None] -reveal_type(f(x, y)) # N: Revealed type is "Union[None, Any, builtins.int]" -reveal_type(f(y, x)) # N: Revealed type is "Union[builtins.int, None, Any]" +reveal_type(f(x, y)) # N: Revealed type is "None | Any | builtins.int" +reveal_type(f(y, x)) # N: Revealed type is "builtins.int | None | Any" [case testNestedProtocolUnions] from typing import Union, Iterator, Iterable @@ -1202,8 +1202,8 @@ nc: Union[Container[str], int] 'x' in i 'x' in c 'x' in u -'x' in ni # E: Unsupported right operand type for in ("Union[Iterable[str], int]") -'x' in nc # E: Unsupported right operand type for in ("Union[Container[str], int]") +'x' in ni # E: Unsupported right operand type for in ("Iterable[str] | int") +'x' in nc # E: Unsupported right operand type for in ("Container[str] | int") [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] @@ -1240,7 +1240,7 @@ class B: field_2: Mapped[str] = Mapped('2') mix: Union[Type[A], Type[B]] = A -reveal_type(mix) # N: Revealed type is "Union[type[__main__.A], type[__main__.B]]" +reveal_type(mix) # N: Revealed type is "type[__main__.A] | type[__main__.B]" reveal_type(mix.field_1) # N: Revealed type is "builtins.list[builtins.int]" reveal_type(mix().field_1) # N: Revealed type is "builtins.int" [builtins fixtures/list.pyi] @@ -1292,7 +1292,7 @@ class C10: ... class C11: ... u: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11] -x: int = u # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, <6 more items>]", variable has type "int") +x: int = u # E: Incompatible types in assignment (expression has type "C1 | C2 | C3 | C4 | C5 | <6 more items>", variable has type "int") [case testLargeUnionsLongIfNeeded] from typing import Union @@ -1310,7 +1310,7 @@ class C10: ... x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int] y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str] -x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str]", variable has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int]") \ +x = y # E: Incompatible types in assignment (expression has type "C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | C10 | str", variable has type "C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | C9 | C10 | int") \ # N: Item in the first union not in the second: "str" [case testLargeUnionsNoneShown] @@ -1330,7 +1330,7 @@ class C11: ... x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11] y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, None] -x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, <6 more items>, None]", variable has type "Union[C1, C2, C3, C4, C5, <6 more items>]") \ +x = y # E: Incompatible types in assignment (expression has type "C1 | C2 | C3 | C4 | C5 | <6 more items> | None", variable has type "C1 | C2 | C3 | C4 | C5 | <6 more items>") \ # N: Item in the first union not in the second: "None" [case testTypeAliasWithOldUnionIsInstance] @@ -1341,9 +1341,9 @@ SimpleAlias = Union[int, str] def foo(x: Union[int, str, tuple]): # TODO: fix the typeshed stub for isinstance if isinstance(x, SimpleAlias): # E: Argument 2 to "isinstance" has incompatible type ""; expected "type" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.tuple[Any, ...]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.tuple[Any, ...]" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.tuple[Any, ...]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.tuple[Any, ...]" [builtins fixtures/tuple.pyi] @@ -1355,7 +1355,7 @@ SimpleAlias = Union[int, str] def foo(x: Union[int, str, tuple]): if isinstance(x, SimpleAlias): # E: Parameterized generics cannot be used with class or instance checks \ # E: Argument 2 to "isinstance" has incompatible type ""; expected "type" - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.tuple[Any, ...]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.tuple[Any, ...]" else: - reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str, builtins.tuple[Any, ...]]" + reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.tuple[Any, ...]" [builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index 34d800404903..98c676dbf42b 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -546,8 +546,8 @@ import sys unary_minus = -(sys.platform == 'linux') and str() binary_minus = ((sys.platform == 'linux') - (sys.platform == 'linux')) and str() -reveal_type(unary_minus) # N: Revealed type is "Union[Literal[0], builtins.str]" -reveal_type(binary_minus) # N: Revealed type is "Union[Literal[0], builtins.str]" +reveal_type(unary_minus) # N: Revealed type is "Literal[0] | builtins.str" +reveal_type(binary_minus) # N: Revealed type is "Literal[0] | builtins.str" [builtins fixtures/ops.pyi] [case testMypyFalseValuesInBinaryOps_no_empty] @@ -657,7 +657,7 @@ COMPILE_TIME_FALSE: Literal[True] # type-narrowing: mapped in 'if' only a = COMPILE_TIME_FALSE or indeterminate reveal_type(a) # N: Revealed type is "builtins.str" b = indeterminate or COMPILE_TIME_FALSE -reveal_type(b) # N: Revealed type is "Union[builtins.str, Literal[True]]" +reveal_type(b) # N: Revealed type is "builtins.str | Literal[True]" [typing fixtures/typing-medium.pyi] [case testSemanticAnalysisTrueButTypeNarrowingFalse] @@ -669,7 +669,7 @@ COMPILE_TIME_TRUE: Literal[False] # type narrowed to `else` only a = COMPILE_TIME_TRUE or indeterminate reveal_type(a) # N: Revealed type is "Literal[False]" b = indeterminate or COMPILE_TIME_TRUE -reveal_type(b) # N: Revealed type is "Union[builtins.str, Literal[False]]" +reveal_type(b) # N: Revealed type is "builtins.str | Literal[False]" [typing fixtures/typing-medium.pyi] [case testConditionalAssertWithoutElse] diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 8ad11720ef0a..02702f58b01b 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -94,7 +94,7 @@ a: A b: B c: C -f(a) # E: Argument 1 to "f" has incompatible type "A"; expected "Optional[C]" +f(a) # E: Argument 1 to "f" has incompatible type "A"; expected "C | None" f(c, c) # E: Argument 2 to "f" has incompatible type "C"; expected "A" f(c, a, b, c) # E: Argument 4 to "f" has incompatible type "C"; expected "A" f() @@ -403,10 +403,10 @@ class B: pass a, b = None, None # type: (A, B) f(*()) # E: Too few arguments for "f" -f(a, *[a]) # E: Argument 2 to "f" has incompatible type "*list[A]"; expected "Optional[B]" \ +f(a, *[a]) # E: Argument 2 to "f" has incompatible type "*list[A]"; expected "B | None" \ # E: Argument 2 to "f" has incompatible type "*list[A]"; expected "B" f(a, b, *[a]) # E: Argument 3 to "f" has incompatible type "*list[A]"; expected "B" -f(*(a, a, b)) # E: Argument 1 to "f" has incompatible type "*tuple[A, A, B]"; expected "Optional[B]" +f(*(a, a, b)) # E: Argument 1 to "f" has incompatible type "*tuple[A, A, B]"; expected "B | None" f(*(a,)) f(*(a, b)) f(*(a, b, b, b)) @@ -631,9 +631,9 @@ from typing import TypeVar T = TypeVar('T') def f(*args: T) -> T: ... -reveal_type(f(*(1, None))) # N: Revealed type is "Union[Literal[1]?, None]" -reveal_type(f(1, *(None, 1))) # N: Revealed type is "Union[Literal[1]?, None]" -reveal_type(f(1, *(1, None))) # N: Revealed type is "Union[Literal[1]?, None]" +reveal_type(f(*(1, None))) # N: Revealed type is "Literal[1]? | None" +reveal_type(f(1, *(None, 1))) # N: Revealed type is "Literal[1]? | None" +reveal_type(f(1, *(1, None))) # N: Revealed type is "Literal[1]? | None" [builtins fixtures/tuple.pyi] @@ -705,7 +705,7 @@ h(d) from typing import List, Union def f(numbers: List[Union[int, float]]) -> None: pass a = [1, 2] -f(a) # E: Argument 1 to "f" has incompatible type "list[int]"; expected "list[Union[int, float]]" \ +f(a) # E: Argument 1 to "f" has incompatible type "list[int]"; expected "list[int | float]" \ # N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ # N: Consider using "Sequence" instead, which is covariant x = [1] diff --git a/test-data/unit/fine-grained-suggest.test b/test-data/unit/fine-grained-suggest.test index ce57514276a2..499a0b4a76fd 100644 --- a/test-data/unit/fine-grained-suggest.test +++ b/test-data/unit/fine-grained-suggest.test @@ -57,7 +57,7 @@ foo(1, 2) foo('3', '4') [builtins fixtures/isinstancelist.pyi] [out] -(Union[int, str], object) -> None +(int | str, object) -> None (object, object) -> None == @@ -80,7 +80,7 @@ def untyped(x) -> None: foo(x) [builtins fixtures/isinstancelist.pyi] [out] -(Union[str, int, None], Optional[int]) -> None +(str | int | None, int | None) -> None == [case testSuggestInferFunc2] @@ -115,7 +115,7 @@ from foo import bar bar(None) [out] (Any) -> Any -(Optional[Any]) -> None +(Any | None) -> None == [case testSuggestInferFuncAny2] @@ -272,7 +272,7 @@ def bar() -> None: x.foo(None) [builtins fixtures/isinstancelist.pyi] [out] -(Union[str, int, None], Optional[int]) -> object +(str | int | None, int | None) -> object == [case testSuggestInferMethod2] @@ -298,7 +298,7 @@ def bar() -> None: a: Union[str, int] = x.foo(None) [builtins fixtures/isinstancelist.pyi] [out] -(Union[str, int, None], Optional[int]) -> Union[int, str] +(str | int | None, int | None) -> int | str == [case testSuggestInferMethod3] @@ -318,7 +318,7 @@ def bar() -> None: [builtins fixtures/isinstancelist.pyi] [out] == -(Optional[str]) -> None +(str | None) -> None [case testSuggestBackflow] # suggest: foo.foo @@ -438,7 +438,7 @@ def bar() -> None: [builtins fixtures/isinstancelist.pyi] [out] == -(Union[str, int, None], Optional[int]) -> Union[int, str] +(str | int | None, int | None) -> int | str [case testSuggestInferNestedMethod] # suggest: foo.Foo.Bar.baz @@ -537,8 +537,8 @@ def baz() -> None: foo(None) [builtins fixtures/isinstancelist.pyi] [out] -(Union[str, int, None], Optional[int]) -> object -(Union[str, int, None], Optional[int]) -> None +(str | int | None, int | None) -> object +(str | int | None, int | None) -> None == [case testSuggestInferFuncDecorator1] @@ -1240,15 +1240,15 @@ tuple1(t) (int, int) -> int (int) -> list[int] (list[int]) -> int -(Union[int, str]) -> None +(int | str) -> None (Callable[[int], int]) -> int (Callable[[float], int]) -> int -(Optional[int]) -> None -(Union[None, int, str]) -> None -(Optional[list[int]]) -> int -(Union[set[int], list[int]]) -> None -(Optional[int]) -> None -(Optional[Any]) -> None +(int | None) -> None +(None | int | str) -> None +(list[int] | None) -> int +(set[int] | list[int]) -> None +(int | None) -> None +(Any | None) -> None (dict[int, int]) -> None (Tuple[int, int]) -> None == @@ -1266,5 +1266,5 @@ optional5(None) [builtins fixtures/isinstancelist.pyi] [out] -(Optional[int]) -> None +(int | None) -> None == diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index 4a30c8a3828f..b73ce030027f 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -3528,9 +3528,9 @@ reveal_type(a.n) [out] == == -c.py:4: note: Revealed type is "tuple[Union[tuple[Union[tuple[Union[..., None], builtins.int, fallback=a.N], None], builtins.int, fallback=b.M], None], builtins.int, fallback=a.N]" -c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int") -c.py:7: note: Revealed type is "tuple[Union[tuple[Union[tuple[Union[..., None], builtins.int, fallback=a.N], None], builtins.int, fallback=b.M], None], builtins.int, fallback=a.N]" +c.py:4: note: Revealed type is "tuple[tuple[tuple[... | None, builtins.int, fallback=a.N] | None, builtins.int, fallback=b.M] | None, builtins.int, fallback=a.N]" +c.py:5: error: Incompatible types in assignment (expression has type "N | None", variable has type "int") +c.py:7: note: Revealed type is "tuple[tuple[tuple[... | None, builtins.int, fallback=a.N] | None, builtins.int, fallback=b.M] | None, builtins.int, fallback=a.N]" [case testTupleTypeUpdateNonRecursiveToRecursiveFine] import c @@ -3561,8 +3561,8 @@ def f(x: a.N) -> None: [out] == == -c.py:4: note: Revealed type is "tuple[Union[tuple[Union[..., None], builtins.int, fallback=b.M], None], builtins.int, fallback=a.N]" -c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int") +c.py:4: note: Revealed type is "tuple[tuple[... | None, builtins.int, fallback=b.M] | None, builtins.int, fallback=a.N]" +c.py:5: error: Incompatible types in assignment (expression has type "N | None", variable has type "int") [case testTypeAliasUpdateNonRecursiveToRecursiveFine] import c @@ -3593,8 +3593,8 @@ def f(x: a.N) -> None: [out] == == -c.py:4: note: Revealed type is "tuple[Union[tuple[Union[..., None], builtins.int], None], builtins.int]" -c.py:5: error: Incompatible types in assignment (expression has type "Optional[N]", variable has type "int") +c.py:4: note: Revealed type is "tuple[tuple[... | None, builtins.int] | None, builtins.int]" +c.py:5: error: Incompatible types in assignment (expression has type "N | None", variable has type "int") [case testTypedDictRefresh] import a @@ -4323,9 +4323,9 @@ y = 0 [file a.py.2] y = '' [out] -main:4: error: Need type annotation for "x" (hint: "x: Optional[] = ...") +main:4: error: Need type annotation for "x" (hint: "x: | None = ...") == -main:4: error: Need type annotation for "x" (hint: "x: Optional[] = ...") +main:4: error: Need type annotation for "x" (hint: "x: | None = ...") [case testNonePartialType2] import a @@ -4341,9 +4341,9 @@ y = 0 [file a.py.2] y = '' [out] -main:4: error: Need type annotation for "x" (hint: "x: Optional[] = ...") +main:4: error: Need type annotation for "x" (hint: "x: | None = ...") == -main:4: error: Need type annotation for "x" (hint: "x: Optional[] = ...") +main:4: error: Need type annotation for "x" (hint: "x: | None = ...") [case testNonePartialType3] import a @@ -4355,7 +4355,7 @@ def f() -> None: y = '' [out] == -a.py:1: error: Need type annotation for "y" (hint: "y: Optional[] = ...") +a.py:1: error: Need type annotation for "y" (hint: "y: | None = ...") [case testNonePartialType4] import a @@ -4371,7 +4371,7 @@ def f() -> None: global y y = '' [out] -a.py:1: error: Need type annotation for "y" (hint: "y: Optional[] = ...") +a.py:1: error: Need type annotation for "y" (hint: "y: | None = ...") == [case testSkippedClass1] @@ -4672,7 +4672,7 @@ y: int [out] == == -main:3: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "int") +main:3: error: Incompatible types in assignment (expression has type "str | None", variable has type "int") [case testNoStrictOptionalFunction] # flags: --no-strict-optional @@ -4696,7 +4696,7 @@ def g(x: str) -> None: [out] == == -main:6: error: Argument 1 to "g" has incompatible type "Optional[int]"; expected "str" +main:6: error: Argument 1 to "g" has incompatible type "int | None"; expected "str" [case testNoStrictOptionalMethod] # flags: --no-strict-optional @@ -4724,7 +4724,7 @@ class B: [out] == == -main:7: error: Argument 1 to "g" of "B" has incompatible type "Optional[int]"; expected "str" +main:7: error: Argument 1 to "g" of "B" has incompatible type "int | None"; expected "str" [case testStrictOptionalModule] import a @@ -4739,7 +4739,7 @@ x: Optional[int] y: int [out] == -main:2: error: Incompatible types in assignment (expression has type "Optional[int]", variable has type "int") +main:2: error: Incompatible types in assignment (expression has type "int | None", variable has type "int") [case testStrictOptionalFunction] import a @@ -4757,7 +4757,7 @@ def g(x: int) -> None: pass [out] == -main:5: error: Argument 1 to "g" has incompatible type "Optional[int]"; expected "int" +main:5: error: Argument 1 to "g" has incompatible type "int | None"; expected "int" [case testStrictOptionalMethod] import a @@ -4778,7 +4778,7 @@ class B: pass [out] == -main:6: error: Argument 1 to "g" of "B" has incompatible type "Optional[int]"; expected "int" +main:6: error: Argument 1 to "g" of "B" has incompatible type "int | None"; expected "int" [case testPerFileStrictOptionalModule] import a @@ -4808,7 +4808,7 @@ y: int = x [out] == == -a.py:4: error: Incompatible types in assignment (expression has type "Optional[int]", variable has type "int") +a.py:4: error: Incompatible types in assignment (expression has type "int | None", variable has type "int") [case testPerFileStrictOptionalModuleOnly] import a @@ -4853,7 +4853,7 @@ class Dummy: [out] == == -a.py:3: error: Incompatible types in assignment (expression has type "Optional[int]", variable has type "int") +a.py:3: error: Incompatible types in assignment (expression has type "int | None", variable has type "int") == [case testPerFileStrictOptionalFunction] @@ -4889,7 +4889,7 @@ def g(x: int) -> Optional[int]: return c.h(x) [out] == -b.py:4: error: Argument 1 to "h" has incompatible type "Optional[int]"; expected "int" +b.py:4: error: Argument 1 to "h" has incompatible type "int | None"; expected "int" == [case testPerFileStrictOptionalMethod] @@ -4930,7 +4930,7 @@ class B: return c.C().h(x) [out] == -b.py:5: error: Argument 1 to "h" of "C" has incompatible type "Optional[int]"; expected "int" +b.py:5: error: Argument 1 to "h" of "C" has incompatible type "int | None"; expected "int" == [case testTypeVarValuesFunction] @@ -9810,7 +9810,7 @@ x = 0 # Arbitrary change to trigger reprocessing [builtins fixtures/dict.pyi] [out] == -a.py:5: note: Revealed type is "def (x: builtins.int) -> Union[builtins.str, None]" +a.py:5: note: Revealed type is "def (x: builtins.int) -> builtins.str | None" [case testTypeVarTupleCached] import a @@ -10383,7 +10383,7 @@ reveal_type(x) [builtins fixtures/tuple.pyi] [out] == -a.py:3: note: Revealed type is "Union[def (x: builtins.int) -> builtins.int, def (*x: builtins.int) -> builtins.int]" +a.py:3: note: Revealed type is "def (x: builtins.int) -> builtins.int | def (*x: builtins.int) -> builtins.int" [case testErrorInReAddedModule] # flags: --disallow-untyped-defs --follow-imports=error diff --git a/test-data/unit/merge.test b/test-data/unit/merge.test index 7463571b76b4..79c3efbc304b 100644 --- a/test-data/unit/merge.test +++ b/test-data/unit/merge.test @@ -781,7 +781,7 @@ a: Union[A, int] NameExpr:3: target.A<0> ==> ## target -NameExpr:3: Union[target.A<0>, builtins.int<1>] +NameExpr:3: target.A<0> | builtins.int<1> [case testTypeType_types] import target diff --git a/test-data/unit/semanal-classes.test b/test-data/unit/semanal-classes.test index 7022da01eeaf..c43d7754c75f 100644 --- a/test-data/unit/semanal-classes.test +++ b/test-data/unit/semanal-classes.test @@ -585,7 +585,7 @@ MypyFile:1( TupleType( tuple[builtins.int, builtins.str]) BaseType( - builtins.tuple[Union[builtins.int, builtins.str], ...]) + builtins.tuple[builtins.int | builtins.str, ...]) PassStmt:2())) [case testBaseClassFromIgnoredModule] diff --git a/test-data/unit/semanal-typealiases.test b/test-data/unit/semanal-typealiases.test index e2c1c4863157..e2fdd53dc45c 100644 --- a/test-data/unit/semanal-typealiases.test +++ b/test-data/unit/semanal-typealiases.test @@ -118,12 +118,12 @@ MypyFile:1( ImportFrom:1(typing, [Union]) AssignmentStmt:2( NameExpr(U* [__main__.U]) - TypeAliasExpr(Union[builtins.int, builtins.str])) + TypeAliasExpr(builtins.int | builtins.str)) FuncDef:3( f Args( Var(x)) - def (x: Union[builtins.int, builtins.str]) + def (x: builtins.int | builtins.str) Block:3( PassStmt:3()))) @@ -140,12 +140,12 @@ MypyFile:1( PassStmt:2()) AssignmentStmt:3( NameExpr(U* [__main__.U]) - TypeAliasExpr(Union[builtins.int, __main__.A])) + TypeAliasExpr(builtins.int | __main__.A)) FuncDef:4( f Args( Var(x)) - def (x: Union[builtins.int, __main__.A]) + def (x: builtins.int | __main__.A) Block:4( PassStmt:4()))) @@ -158,12 +158,12 @@ MypyFile:1( Import:1(typing) AssignmentStmt:2( NameExpr(U* [__main__.U]) - TypeAliasExpr(Union[builtins.int, builtins.str])) + TypeAliasExpr(builtins.int | builtins.str)) FuncDef:3( f Args( Var(x)) - def (x: Union[builtins.int, builtins.str]) + def (x: builtins.int | builtins.str) Block:3( PassStmt:3()))) @@ -267,7 +267,7 @@ MypyFile:1( f Args( Var(x)) - def (x: Union[builtins.int, _m.A]) + def (x: builtins.int | _m.A) Block:3( PassStmt:3()))) @@ -287,7 +287,7 @@ MypyFile:1( f Args( Var(x)) - def (x: Union[builtins.int, _m.A]) + def (x: builtins.int | _m.A) Block:3( PassStmt:3()))) @@ -371,14 +371,14 @@ MypyFile:1( ImportFrom:1(typing, [Union]) AssignmentStmt:2( NameExpr(U* [__main__.U]) - TypeAliasExpr(Union[builtins.int, builtins.str])) + TypeAliasExpr(builtins.int | builtins.str)) AssignmentStmt:3( NameExpr(U2* [__main__.U2]) NameExpr(U [__main__.U])) AssignmentStmt:4( NameExpr(x [__main__.x]) IntExpr(1) - Union[builtins.int, builtins.str])) + builtins.int | builtins.str)) [case testTypeAliasOfImportedAlias] from typing import Union @@ -398,7 +398,7 @@ MypyFile:1( AssignmentStmt:4( NameExpr(x [__main__.x]) IntExpr(1) - Union[builtins.int, builtins.str])) + builtins.int | builtins.str)) [case testListTypeDoesNotGenerateAlias] @@ -423,11 +423,11 @@ MypyFile:1( ImportFrom:1(typing, [Union]) AssignmentStmt:2( NameExpr(A* [__main__.A]) - TypeAliasExpr(Union[builtins.int, builtins.str])) + TypeAliasExpr(builtins.int | builtins.str)) AssignmentStmt:3( NameExpr(a [__main__.a]) IntExpr(1) - Union[builtins.int, builtins.str])) + builtins.int | builtins.str)) [case testComplexTypeAlias] from typing import Union, Tuple, Any @@ -439,8 +439,8 @@ MypyFile:1( ImportFrom:1(typing, [Union, Tuple, Any]) AssignmentStmt:2( NameExpr(A* [__main__.A]) - TypeAliasExpr(Union[builtins.int, tuple[builtins.int, Any]])) + TypeAliasExpr(builtins.int | tuple[builtins.int, Any])) AssignmentStmt:3( NameExpr(a [__main__.a]) IntExpr(1) - Union[builtins.int, tuple[builtins.int, Any]])) + builtins.int | tuple[builtins.int, Any])) diff --git a/test-data/unit/semanal-types.test b/test-data/unit/semanal-types.test index a91d334af146..1d917480e154 100644 --- a/test-data/unit/semanal-types.test +++ b/test-data/unit/semanal-types.test @@ -1438,7 +1438,7 @@ MypyFile:1( f Args( Var(x)) - def (x: Union[builtins.int, builtins.str]) + def (x: builtins.int | builtins.str) Block:2( PassStmt:2()))) @@ -1452,7 +1452,7 @@ MypyFile:1( f Args( Var(x)) - def (x: Union[builtins.int, None]) + def (x: builtins.int | None) Block:2( PassStmt:2()))) @@ -1466,7 +1466,7 @@ MypyFile:1( f Args( Var(x)) - def (x: Union[builtins.int, None, builtins.str]) + def (x: builtins.int | None | builtins.str) Block:2( PassStmt:2()))) @@ -1493,7 +1493,7 @@ MypyFile:1( AssignmentStmt:2( NameExpr(x [__main__.x]) IntExpr(1) - Union[builtins.int, None])) + builtins.int | None)) [case testInvalidOptionalType] from typing import Optional