diff --git a/CHANGELOG.md b/CHANGELOG.md index 68d1bae2a..27ab5b21a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Basedmypy Changelog ## [Unreleased] +### Added +- `float` and `complex` literals +- narrow types to literal values +- infer literal in generics ## [2.9.0] ### Added diff --git a/mypy/binder.py b/mypy/binder.py index 28462e8d0..59247cc91 100644 --- a/mypy/binder.py +++ b/mypy/binder.py @@ -5,7 +5,6 @@ from typing import DefaultDict, Generator, Iterator, List, NamedTuple, Optional, Tuple, Union from typing_extensions import TypeAlias as _TypeAlias -from mypy.erasetype import remove_instance_last_known_values from mypy.join import join_simple from mypy.literals import Key, literal, literal_hash, subkeys from mypy.nodes import Expression, IndexExpr, MemberExpr, NameExpr, RefExpr, TypeInfo, Var @@ -331,7 +330,8 @@ def assign_type( ) -> None: # We should erase last known value in binder, because if we are using it, # it means that the target is not final, and therefore can't hold a literal. - type = remove_instance_last_known_values(type) + # HUUHHH????? + # type = remove_instance_last_known_values(type) if self.type_assignments is not None: # We are in a multiassign from union, defer the actual binding, diff --git a/mypy/checker.py b/mypy/checker.py index 0fdb34335..62211f6e5 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3589,19 +3589,30 @@ def check_assignment( ): lvalue.node.type = remove_instance_last_known_values(lvalue_type) + elif lvalue.node and lvalue.node.is_inferred and rvalue_type: + # for literal values + # Don't use type binder for definitions of special forms, like named tuples. + if not (isinstance(lvalue, NameExpr) and lvalue.is_special_form): + self.binder.assign_type(lvalue, rvalue_type, lvalue_type, False) + elif index_lvalue: self.check_indexed_assignment(index_lvalue, rvalue, lvalue) if inferred: type_context = self.get_variable_type_context(inferred) rvalue_type = self.expr_checker.accept(rvalue, type_context=type_context) + original_rvalue_type = rvalue_type if not ( inferred.is_final or inferred.is_index_var or (isinstance(lvalue, NameExpr) and lvalue.name == "__match_args__") ): rvalue_type = remove_instance_last_known_values(rvalue_type) - self.infer_variable_type(inferred, lvalue, rvalue_type, rvalue) + if self.infer_variable_type(inferred, lvalue, rvalue_type, rvalue) or self.binder.type_assignments: + # we don't always want to assign the type here as it might be something like partial + self.binder.assign_type( + lvalue, original_rvalue_type, original_rvalue_type, False + ) self.check_assignment_to_slots(lvalue) # (type, operator) tuples for augmented assignments supported with partial types @@ -4553,12 +4564,13 @@ def is_definition(self, s: Lvalue) -> bool: def infer_variable_type( self, name: Var, lvalue: Lvalue, init_type: Type, context: Context - ) -> None: + ) -> bool: """Infer the type of initialized variables from initializer type.""" + valid = True if isinstance(init_type, DeletedType): self.msg.deleted_as_rvalue(init_type, context) elif ( - not is_valid_inferred_type(init_type, is_lvalue_final=name.is_final) + not (valid := is_valid_inferred_type(init_type, is_lvalue_final=name.is_final)) and not self.no_partial_types ): # We cannot use the type of the initialization expression for full type @@ -4585,6 +4597,7 @@ def infer_variable_type( init_type = strip_type(init_type) self.set_inferred_type(name, lvalue, init_type) + return valid def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool: init_type = get_proper_type(init_type) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index e447ea0e4..905b78089 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -210,7 +210,8 @@ # Type of callback user for checking individual function arguments. See # check_args() below for details. ArgChecker: _TypeAlias = Callable[ - [Type, Type, ArgKind, Type, int, int, CallableType, Optional[Type], Context, Context], None + [Type, Type, ArgKind, Type, int, int, CallableType, Optional[Type], Context, Context, bool], + None, ] # Maximum nesting level for math union in overloads, setting this to large values @@ -2175,6 +2176,13 @@ def infer_function_type_arguments( Return a derived callable type that has the arguments applied. """ if self.chk.in_checked_function(): + if isinstance(callee_type.ret_type, TypeVarType): + # if the return type is constant, infer as literal + rvalue_type = [ + remove_instance_last_known_values(arg) if isinstance(arg, Instance) else arg + for arg in args + ] + # Disable type errors during type inference. There may be errors # due to partial available context information at this time, but # these errors can be safely ignored as the arguments will be @@ -2581,6 +2589,8 @@ def check_argument_types( context: Context, check_arg: ArgChecker | None = None, object_type: Type | None = None, + *, + type_function=False, ) -> None: """Check argument types against a callable type. @@ -2712,6 +2722,7 @@ def check_argument_types( object_type, args[actual], context, + type_function, ) def check_arg( @@ -2726,12 +2737,16 @@ def check_arg( object_type: Type | None, context: Context, outer_context: Context, + type_function=False, ) -> None: """Check the type of a single argument in a call.""" caller_type = get_proper_type(caller_type) original_caller_type = get_proper_type(original_caller_type) callee_type = get_proper_type(callee_type) - + if type_function: + # TODO: make this work at all + if not isinstance(caller_type, Instance) or not caller_type.last_known_value: + caller_type = self.named_type("builtins.object") if isinstance(caller_type, DeletedType): self.msg.deleted_as_rvalue(caller_type, context) # Only non-abstract non-protocol class can be given where Type[...] is expected... @@ -3348,6 +3363,7 @@ def check_arg( object_type: Type | None, context: Context, outer_context: Context, + type_function: bool, ) -> None: if not arg_approximate_similarity(caller_type, callee_type): # No match -- exit early since none of the remaining work can change @@ -3580,10 +3596,14 @@ def visit_bytes_expr(self, e: BytesExpr) -> Type: def visit_float_expr(self, e: FloatExpr) -> Type: """Type check a float literal (trivial).""" + if mypy.options._based: + return self.infer_literal_expr_type(e.value, "builtins.float") return self.named_type("builtins.float") def visit_complex_expr(self, e: ComplexExpr) -> Type: """Type check a complex literal.""" + if mypy.options._based: + return self.infer_literal_expr_type(e.value, "builtins.complex") return self.named_type("builtins.complex") def visit_ellipsis(self, e: EllipsisExpr) -> Type: diff --git a/mypy/expandtype.py b/mypy/expandtype.py index ff9d845ec..46d97dae2 100644 --- a/mypy/expandtype.py +++ b/mypy/expandtype.py @@ -1,5 +1,6 @@ from __future__ import annotations +from contextlib import contextmanager from typing import Final, Iterable, Mapping, Sequence, TypeVar, cast, overload from mypy.nodes import ARG_STAR, FakeInfo, Var @@ -185,6 +186,16 @@ def __init__(self, variables: Mapping[TypeVarId, Type]) -> None: super().__init__() self.variables = variables self.recursive_tvar_guard: dict[TypeVarId, Type | None] = {} + self._erase_literals = False + + @contextmanager + def erase_literals(self): + _erase_literals = self._erase_literals + self._erase_literals = True + try: + yield + finally: + self._erase_literals = _erase_literals def visit_unbound_type(self, t: UnboundType) -> Type: return t @@ -211,7 +222,8 @@ def visit_erased_type(self, t: ErasedType) -> Type: return t def visit_instance(self, t: Instance) -> Type: - args = self.expand_types_with_unpack(list(t.args)) + with self.erase_literals(): + args = self.expand_types_with_unpack(list(t.args)) if isinstance(t.type, FakeInfo): # The type checker expands function definitions and bodies @@ -238,7 +250,7 @@ def visit_type_var(self, t: TypeVarType) -> Type: if t.id.is_self(): t = t.copy_modified(upper_bound=t.upper_bound.accept(self)) repl = self.variables.get(t.id, t) - if isinstance(repl, ProperType) and isinstance(repl, Instance): + if self._erase_literals and isinstance(repl, ProperType) and isinstance(repl, Instance): # TODO: do we really need to do this? # If I try to remove this special-casing ~40 tests fail on reveal_type(). return repl.copy_modified(last_known_value=None) @@ -410,17 +422,18 @@ def visit_callable_type(self, t: CallableType) -> CallableType: var_arg = t.var_arg() needs_normalization = False - if var_arg is not None and isinstance(var_arg.typ, UnpackType): - needs_normalization = True - arg_types = self.interpolate_args_for_unpack(t, var_arg.typ) - else: - arg_types = self.expand_types(t.arg_types) - expanded = t.copy_modified( - arg_types=arg_types, - ret_type=t.ret_type.accept(self), - type_guard=t.type_guard and cast(TypeGuardType, t.type_guard.accept(self)), - type_is=(t.type_is.accept(self) if t.type_is is not None else None), - ) + with self.erase_literals(): + if var_arg is not None and isinstance(var_arg.typ, UnpackType): + needs_normalization = True + arg_types = self.interpolate_args_for_unpack(t, var_arg.typ) + else: + arg_types = self.expand_types(t.arg_types) + expanded = t.copy_modified( + arg_types=arg_types, + ret_type=t.ret_type.accept(self), + type_guard=t.type_guard and cast(TypeGuardType, t.type_guard.accept(self)), + type_is=(t.type_is.accept(self) if t.type_is is not None else None), + ) if needs_normalization: return expanded.with_normalized_var_args() return expanded @@ -467,7 +480,9 @@ def visit_typeddict_type(self, t: TypedDictType) -> Type: return cached fallback = t.fallback.accept(self) assert isinstance(fallback, ProperType) and isinstance(fallback, Instance) - result = t.copy_modified(item_types=self.expand_types(t.items.values()), fallback=fallback) + with self.erase_literals(): + # TODO: we don't want to erase literals for `ReadOnly` keys + result = t.copy_modified(item_types=self.expand_types(t.items.values()), fallback=fallback) self.set_cached(t, result) return result diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 2e2cd7248..afff56f65 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -2129,7 +2129,8 @@ def numeric_type(self, value: object, n: AST) -> Type: # Other kinds of numbers (floats, complex) are not valid parameters for # RawExpressionType so we just pass in 'None' for now. We'll report the # appropriate error at a later stage. - numeric_value = None + # based: they are valid + numeric_value = value type_name = f"builtins.{type(value).__name__}" return RawExpressionType( numeric_value, type_name, line=self.line, column=getattr(n, "col_offset", -1) diff --git a/mypy/join.py b/mypy/join.py index c32f6a477..b6413c604 100644 --- a/mypy/join.py +++ b/mypy/join.py @@ -232,7 +232,7 @@ def join_simple(declaration: Type | None, s: Type, t: Type) -> ProperType: if declaration is None or is_subtype(value, declaration): return value - return declaration + return value def trivial_join(s: Type, t: Type) -> Type: diff --git a/mypy/meet.py b/mypy/meet.py index 08af963ea..68766b737 100644 --- a/mypy/meet.py +++ b/mypy/meet.py @@ -197,9 +197,12 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type: # Special case: 'int' can't be narrowed down to a native int type such as # i64, since they have different runtime representations. return original_declared + if isinstance(narrowed, Instance) and narrowed.last_known_value: + return narrowed return meet_types(original_declared, original_narrowed) elif isinstance(declared, (TupleType, TypeType, LiteralType)): - return meet_types(original_declared, original_narrowed) + # this way around to preserve the last know of the items in the tuple + return meet_types(original_narrowed, original_declared, intersect=True) elif isinstance(declared, TypedDictType) and isinstance(narrowed, Instance): # Special case useful for selecting TypedDicts from unions using isinstance(x, dict). if narrowed.type.fullname == "builtins.dict" and all( diff --git a/mypy/test/testtypes.py b/mypy/test/testtypes.py index ca6d460a5..ff6f8b891 100644 --- a/mypy/test/testtypes.py +++ b/mypy/test/testtypes.py @@ -694,8 +694,14 @@ def test_simplified_intersection(self): [fx.b, IntersectionType([fx.c, IntersectionType([fx.d])])], IntersectionType([fx.b, fx.c, fx.d]), ) + self.assert_simplified_intersection([fx.bool_type, fx.lit_true], fx.lit_true) + + # special case: it's not currently symmetric when there are last known values + narrowed = fx.bool_type.copy_modified(last_known_value=fx.lit_true) + assert_equal(make_simplified_intersection([narrowed, fx.bool_type]), narrowed) def assert_simplified_intersection(self, original: list[Type], intersection: Type) -> None: + __tracebackhide__ = True assert_equal(make_simplified_intersection(original), intersection) assert_equal(make_simplified_intersection(list(reversed(original))), intersection) diff --git a/mypy/typeanal.py b/mypy/typeanal.py index 9d6d0ec4d..78c29ca00 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -527,7 +527,6 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool) return res elif isinstance(node, TypeInfo): return self.analyze_type_with_type_info(node, t.args, t, t.empty_tuple_index) - elif node.fullname in TYPE_ALIAS_NAMES: return AnyType(TypeOfAny.special_form) # Concatenate is an operator, no need for a proper type @@ -1432,7 +1431,11 @@ def visit_raw_expression_type(self, t: RawExpressionType) -> Type: if self.report_invalid_types: msg = None - if t.base_type_name in ("builtins.int", "builtins.bool"): + if ( + t.base_type_name in ("builtins.int", "builtins.bool") + or mypy.options._based + and t.base_type_name in ("builtins.float", "builtins.complex") + ): if not self.options.bare_literals: # The only time it makes sense to use an int or bool is inside of # a literal type. @@ -1453,7 +1456,12 @@ def visit_raw_expression_type(self, t: RawExpressionType) -> Type: self.fail(msg, t, code=codes.VALID_TYPE) if t.note is not None: self.note(t.note, t, code=codes.VALID_TYPE) - if t.base_type_name in ("builtins.int", "builtins.bool"): + if t.base_type_name in ( + "builtins.int", + "builtins.bool", + "builtins.float", + "builtins.complex", + ): v = t.literal_value assert v is not None result = LiteralType( diff --git a/mypy/typeops.py b/mypy/typeops.py index afd8334d3..e058695f8 100644 --- a/mypy/typeops.py +++ b/mypy/typeops.py @@ -724,6 +724,8 @@ def _remove_redundant_intersection_items(items: list[Type], keep_erased: bool) - if inner_i in removed: continue proper_inner = get_proper_type(items[inner_i]) + # hacky: we check this one first, because it's more likely that the value on the left + # has a last known value/metadata/extra args if is_proper_subtype( proper_outer, proper_inner, keep_erased_types=keep_erased, ignore_promotions=True ): diff --git a/mypy/types.py b/mypy/types.py index 6036065c4..43249d487 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -75,7 +75,7 @@ # # Note: Float values are only used internally. They are not accepted within # Literal[...]. -LiteralValue: _TypeAlias = Union[int, str, bool, float] +LiteralValue: _TypeAlias = Union[int, str, bool, float, complex] # If we only import type_visitor in the middle of the file, mypy @@ -3137,14 +3137,18 @@ def value_repr(self) -> str: def serialize(self) -> JsonDict | str: return { ".class": "LiteralType", - "value": self.value, + "value": self.value if not isinstance(self.value, complex) else str(self.value), "fallback": self.fallback.serialize(), } @classmethod def deserialize(cls, data: JsonDict) -> LiteralType: assert data[".class"] == "LiteralType" - return LiteralType(value=data["value"], fallback=Instance.deserialize(data["fallback"])) + fallback = Instance.deserialize(data["fallback"]) + value = data["value"] + if fallback.type_ref == "builtins.complex": + value = complex(value) + return LiteralType(value=value, fallback=fallback) def is_singleton_type(self) -> bool: return self.is_enum_literal() or isinstance(self.value, bool) diff --git a/mypy/typeshed/stdlib/operator.pyi b/mypy/typeshed/stdlib/operator.pyi index b73e037f3..9c862af2a 100644 --- a/mypy/typeshed/stdlib/operator.pyi +++ b/mypy/typeshed/stdlib/operator.pyi @@ -183,16 +183,16 @@ if sys.version_info >= (3, 11): @final class attrgetter(Generic[_T_co]): @overload - def __new__(cls, attr: str, /) -> attrgetter[Any]: ... + def __new__(cls, attr: str, /) -> attrgetter[object]: ... @overload - def __new__(cls, attr: str, attr2: str, /) -> attrgetter[tuple[Any, Any]]: ... + def __new__(cls, attr: str, attr2: str, /) -> attrgetter[tuple[object, object]]: ... @overload - def __new__(cls, attr: str, attr2: str, attr3: str, /) -> attrgetter[tuple[Any, Any, Any]]: ... + def __new__(cls, attr: str, attr2: str, attr3: str, /) -> attrgetter[tuple[object, object, object]]: ... @overload - def __new__(cls, attr: str, attr2: str, attr3: str, attr4: str, /) -> attrgetter[tuple[Any, Any, Any, Any]]: ... + def __new__(cls, attr: str, attr2: str, attr3: str, attr4: str, /) -> attrgetter[tuple[object, object, object, object]]: ... @overload - def __new__(cls, attr: str, /, *attrs: str) -> attrgetter[tuple[Any, ...]]: ... - def __call__(self, obj: Any, /) -> _T_co: ... + def __new__(cls, attr: str, /, *attrs: str) -> attrgetter[tuple[object, ...]]: ... + def __call__(self, obj: object, /) -> _T_co: ... @final class itemgetter(Generic[_T_co]): diff --git a/test-data/unit/check-based-assignment.test b/test-data/unit/check-based-assignment.test new file mode 100644 index 000000000..abb49780d --- /dev/null +++ b/test-data/unit/check-based-assignment.test @@ -0,0 +1,17 @@ +[case testNarrowToLiteral] +a = "a" +reveal_type(a) # N: Revealed type is "'a'" (narrowed from "str") +a = "b" +reveal_type(a) # N: Revealed type is "'b'" (narrowed from "str") +b = a +reveal_type(b) # N: Revealed type is "'b'" (narrowed from "str") +b = "c" +reveal_type(b) # N: Revealed type is "'c'" (narrowed from "str") + + +[case testNarrowToLiteralGeneric] +from helper import T +def f(t: T) -> T: ... + +a = f(1) +reveal_type(a) # N: Revealed type is "1" (narrowed from "int") diff --git a/test-data/unit/check-based-bare-literals.test b/test-data/unit/check-based-bare-literals.test index bd61c0cb5..bae36c682 100644 --- a/test-data/unit/check-based-bare-literals.test +++ b/test-data/unit/check-based-bare-literals.test @@ -18,14 +18,22 @@ reveal_type(c) # N: Revealed type is "False" c2: False | str reveal_type(c2) # N: Revealed type is "False | str" -d: 1.1 # E: Invalid type: float literals cannot be used as a type [valid-type] -d2: 1.1 | str # E: Invalid type: float literals cannot be used as a type [valid-type] - -e: 1j # E: Invalid type: complex literals cannot be used as a type [valid-type] -e2: 1j | str # E: Invalid type: complex literals cannot be used as a type [valid-type] +d: 1.1 +reveal_type(d) # N: Revealed type is "1.1" +d2: 1.1 | str +reveal_type(d2) # N: Revealed type is "1.1 | str" [builtins fixtures/tuple.pyi] +[case testBareLiteralLiteralsComplex] +from __future__ import annotations +e: 1j +reveal_type(e) # N: Revealed type is "1j" +e2: 1j | str +reveal_type(e2) # N: Revealed type is "1j | str" +[builtins fixtures/dict.pyi] + + [case testNoBareLiteralLiterals] # flags: --python-version 3.10 @@ -38,11 +46,12 @@ b2: True | str # E: "True" is a bare literal and shouldn't be used in a type op c1: False c2: False | str # E: "False" is a bare literal and shouldn't be used in a type operation without "__future__.annotations" [valid-type] -d1: 1.1 # E: Invalid type: float literals cannot be used as a type [valid-type] -d2: 1.1 | str # E: Invalid type: float literals cannot be used as a type [valid-type] +d1: 1.1 +d2: 1.1 | str # E: "1.1" is a bare literal and shouldn't be used in a type operation without "__future__.annotations" [valid-type] -e1: 1j # E: Invalid type: complex literals cannot be used as a type [valid-type] -e2: 1j | str # E: Invalid type: complex literals cannot be used as a type [valid-type] +e1: 1j +e2: 1j | str # E: "1j" is a bare literal and shouldn't be used in a type operation without "__future__.annotations" [valid-type] +[builtins fixtures/complex.pyi] [case testBareLiteralEnum] @@ -109,3 +118,8 @@ import a from typing import TypeVar T = TypeVar("T", 1, 2) a: 1 | 2 + + +[case testBareLiteralComplex-writescache] +a: 1j +[builtins fixtures/complex.pyi] diff --git a/test-data/unit/check-based-inference.test b/test-data/unit/check-based-inference.test new file mode 100644 index 000000000..087ceb089 --- /dev/null +++ b/test-data/unit/check-based-inference.test @@ -0,0 +1,36 @@ +[case testInferLiteralGeneric] +from helper import T +def f(t: T) -> T: ... + +reveal_type(f(1)) # N: Revealed type is "1" + + +[case testDontInferLiteralGenericAssignment] +from helper import T +def f(t: T) -> T: ... + +a = f(1) +reveal_type(a) # N: Revealed type is "1" (narrowed from "int") + + +[case testDontInferLiteralGenericClass] +from helper import T +from typing import Generic + +class A(Generic[T]): + def __init__(self, t: T): ... + +def f(t: T) -> A[T]: ... + +reveal_type(f(1)) # N: Revealed type is "__main__.A[int]" +a = f(1) +reveal_type(a) # N: Revealed type is "__main__.A[int]" + + +[case testNarrowTuple] +from helper import T +def f(t: T) -> (T,): + return (t,) +a = f(1) +reveal_type(a) # N: Revealed type is "(1,)" (narrowed from "(int,)") +[builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-based-misc.test b/test-data/unit/check-based-misc.test index 6f5216b68..3d1b96833 100644 --- a/test-data/unit/check-based-misc.test +++ b/test-data/unit/check-based-misc.test @@ -25,7 +25,7 @@ main:3:5:3:31: error: Unused "type: ignore" comment [unused-ignore] [case testNarrowOnInitialAssignment] a: object = 1 -reveal_type(a) # N: Revealed type is "int" (narrowed from "object") +reveal_type(a) # N: Revealed type is "1" (narrowed from "object") [case testNarrowWithAny] @@ -35,7 +35,7 @@ from typing import Any, Union a: Union[int, Any] if bool(): a = 1 - reveal_type(a) # N: Revealed type is "int" (narrowed from "int | Any") + reveal_type(a) # N: Revealed type is "1" (narrowed from "int | Any") b: Any = 1 reveal_type(b) # N: Revealed type is "Any" c: Any @@ -199,7 +199,7 @@ a = False b: object if a: b = 1 -reveal_type(b) # N: Revealed type is "int" (narrowed from "object") +reveal_type(b) # N: Revealed type is "1" (narrowed from "object") [case testRedundantExprOnWhileTrue] diff --git a/test-data/unit/check-based-tuple-literal.test b/test-data/unit/check-based-tuple-literal.test index a54679cb2..804e335bc 100644 --- a/test-data/unit/check-based-tuple-literal.test +++ b/test-data/unit/check-based-tuple-literal.test @@ -2,9 +2,9 @@ from __future__ import annotations a: (1, str) = (1, "") -reveal_type(a) # N: Revealed type is "(1, str)" +reveal_type(a) # N: Revealed type is "(1, '')" (narrowed from "(1, str)") b: (str,) = ("sus",) -reveal_type(b) # N: Revealed type is "(str,)" +reveal_type(b) # N: Revealed type is "('sus',)" (narrowed from "(str,)") c: () = (1,) # E: Incompatible types in assignment (expression has type "(int,)", variable has type "()") [assignment] reveal_type(c) # N: Revealed type is "()" diff --git a/test-data/unit/check-based-type-render.test b/test-data/unit/check-based-type-render.test index 5ffb19cc5..915edcab5 100644 --- a/test-data/unit/check-based-type-render.test +++ b/test-data/unit/check-based-type-render.test @@ -97,7 +97,7 @@ a = 1 # E: Incompatible types in assignment (expression has type "int", variabl [case testNarrowedFrom] a: object a = 1 -reveal_type(a) # N: Revealed type is "int" (narrowed from "object") +reveal_type(a) # N: Revealed type is "1" (narrowed from "object") [case testNarrowedFromClass] diff --git a/test-data/unit/check-based-typevar.test b/test-data/unit/check-based-typevar.test index fe7073c4d..3b1e16268 100644 --- a/test-data/unit/check-based-typevar.test +++ b/test-data/unit/check-based-typevar.test @@ -51,3 +51,43 @@ class A(Generic[T]): ... a = A[Any]() # E: Expression type contains "Any" (has type "A[Any]") [no-any-expr] [builtins fixtures/tuple.pyi] + + +[case testInferLiteralGeneric] +from helper import T +from typing import List + +def f0(t: T) -> T: + return t + + +reveal_type(f0(17)) # N: Revealed type is "17" + + +def f1(t: T) -> (List[T],): + return ([t],) + + +reveal_type(f1(17)) # N: Revealed type is "(list[int],)" + + +def f2(t: T) -> (T,): + return (t,) + + +reveal_type(f2(17)) # N: Revealed type is "(17,)" + + +def f3(t: T) -> List[T]: + return [t] + + +reveal_type(f3(17)) # N: Revealed type is "list[int]" + + +def f4(t: T) -> (T, List[T]): + return t, [t] + +# maybe this should be: (int, list[int]) +reveal_type(f4(17)) # N: Revealed type is "(17, list[int])" +[builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 761152131..47d0dd5cf 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -1636,7 +1636,7 @@ a.f = a.f a.f.x # E: "int" has no attribute "x" a.f = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") a.f = 1 -reveal_type(a.f) # N: Revealed type is "builtins.int" +reveal_type(a.f) # N: Revealed type is "Literal[1]?" [builtins fixtures/property.pyi] [case testPropertyWithDeleterButNoSetter] @@ -7515,7 +7515,7 @@ def access_before_declaration(self) -> None: obj.value x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" x = x + 1 class Foo: @@ -7527,7 +7527,7 @@ def access_after_declaration(self) -> None: obj.value x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" x = x + 1 [case testIsSubClassNarrowDownTypesOfTypeVariables] diff --git a/test-data/unit/check-columns.test b/test-data/unit/check-columns.test index 2baf9b6f9..0bd48243d 100644 --- a/test-data/unit/check-columns.test +++ b/test-data/unit/check-columns.test @@ -282,7 +282,7 @@ if int(): [case testColumnRedundantCast] # flags: --warn-redundant-casts from typing import cast -y = 1 +y: int x = cast(int, y) # E:5: Redundant cast to "int" [case testColumnTypeSignatureHasTooFewArguments] diff --git a/test-data/unit/check-custom-plugin.test b/test-data/unit/check-custom-plugin.test index 0e8052369..5b2991071 100644 --- a/test-data/unit/check-custom-plugin.test +++ b/test-data/unit/check-custom-plugin.test @@ -901,8 +901,8 @@ plugins=/test-data/unit/plugins/descriptor.py def dynamic_signature(arg1: str) -> str: ... a: int = 1 -reveal_type(dynamic_signature(a)) # N: Revealed type is "builtins.int" -b: bytes = b'foo' +reveal_type(dynamic_signature(a)) # N: Revealed type is "Literal[1]?" +b: bytes reveal_type(dynamic_signature(b)) # N: Revealed type is "builtins.bytes" [file mypy.ini] \[mypy] diff --git a/test-data/unit/check-dataclass-transform.test b/test-data/unit/check-dataclass-transform.test index 51b2e1862..a6843cbef 100644 --- a/test-data/unit/check-dataclass-transform.test +++ b/test-data/unit/check-dataclass-transform.test @@ -830,11 +830,11 @@ class Bar(Base, frozen=False): foo = Foo(0, 1) foo.foo = 5 # E: Property "foo" defined in "Foo" is read-only foo.base = 6 -reveal_type(foo.base) # N: Revealed type is "builtins.int" +reveal_type(foo.base) # N: Revealed type is "Literal[6]?" bar = Bar(0, 1) bar.bar = 5 bar.base = 6 -reveal_type(bar.base) # N: Revealed type is "builtins.int" +reveal_type(bar.base) # N: Revealed type is "Literal[6]?" [typing fixtures/typing-full.pyi] [builtins fixtures/dataclasses.pyi] diff --git a/test-data/unit/check-enum.test b/test-data/unit/check-enum.test index e801ce2a4..95909b93c 100644 --- a/test-data/unit/check-enum.test +++ b/test-data/unit/check-enum.test @@ -1363,9 +1363,12 @@ class Foo(Enum): A = 1 B = 2 -a = Foo.A +a: Foo 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]?]" +a = Foo.A +reveal_type(a.value) # N: Revealed type is "Literal[1]?" +reveal_type(a._value_) # N: Revealed type is "Literal[1]?" [builtins fixtures/enum.pyi] [case testNewSetsUnexpectedValueType] diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index cee8ee5da..1ac81760e 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -1133,7 +1133,7 @@ def unsafe_func(x: object) -> Union[int, str]: [case testUnimportedRevealType] # flags: --enable-error-code=unimported-reveal -x = 1 +x: int reveal_type(x) [out] main:3: error: Name "reveal_type" is not defined [unimported-reveal] @@ -1143,7 +1143,7 @@ main:3: note: Revealed type is "builtins.int" [case testUnimportedRevealTypePy311] # flags: --enable-error-code=unimported-reveal --python-version=3.11 -x = 1 +x: int reveal_type(x) [out] main:3: error: Name "reveal_type" is not defined [unimported-reveal] @@ -1166,14 +1166,14 @@ main:4: note: 'reveal_type' always outputs 'Any' in unchecked functions [case testUnimportedRevealTypeImportedTypingExtensions] # flags: --enable-error-code=unimported-reveal from typing_extensions import reveal_type -x = 1 +x: int reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/isinstancelist.pyi] [case testUnimportedRevealTypeImportedTyping311] # flags: --enable-error-code=unimported-reveal --python-version=3.11 from typing import reveal_type -x = 1 +x: int reveal_type(x) # N: Revealed type is "builtins.int" [builtins fixtures/isinstancelist.pyi] [typing fixtures/typing-full.pyi] diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index baac06425..14537fd36 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -987,7 +987,7 @@ main:4: error: "A" not callable [case testAssertType] from typing import assert_type, Any from typing_extensions import Literal -a: int = 1 +a: int returned = assert_type(a, int) reveal_type(returned) # N: Revealed type is "builtins.int" assert_type(a, str) # E: Expression is of type "int", not "str" @@ -1002,7 +1002,7 @@ from typing import assert_type, TypeVar, Generic from typing_extensions import Literal T = TypeVar("T") def f(x: T) -> T: return x -assert_type(f(1), int) +assert_type(f(1), Literal[1]) class Gen(Generic[T]): def __new__(cls, obj: T) -> Gen[T]: ... assert_type(Gen(1), Gen[int]) @@ -1027,7 +1027,7 @@ main:5: note: "assert_type" expects everything to be "Any" in unchecked function from typing import assert_type from typing_extensions import Literal def f(): - x = 42 + x = int() assert_type(x, Literal[42]) [out] main:6: error: Expression is of type "int", not "Literal[42]" @@ -1839,14 +1839,14 @@ def f(): x = 42 reveal_type(x) [out] -main:4: note: Revealed type is "builtins.int" +main:4: note: Revealed type is "Literal[42]?" [case testRevealTypedDef] def f() -> None: x = 42 reveal_type(x) [out] -main:3: note: Revealed type is "builtins.int" +main:3: note: Revealed type is "Literal[42]?" [case testLambdaTypedContext] def f() -> None: diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index f1c35ee40..ff79f2533 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -1671,8 +1671,8 @@ strict = false [case testStrictAndStrictEquality] -- maybe a little useless because theres no strict but w/e # flags: --strict-equality -x = 0 -y = '' +x: int +y: str if x == y: # E: Non-overlapping equality check (left operand type: "int", right operand type: "str") int() [builtins fixtures/ops.pyi] @@ -2212,7 +2212,7 @@ bad_return_type('no args taken!') def f(x): pass -y = 1 +y: int f(reveal_type(y)) # E: Call to untyped function "f" in typed context \ # N: Revealed type is "builtins.int" diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test index c15265345..43ab11de5 100644 --- a/test-data/unit/check-functions.test +++ b/test-data/unit/check-functions.test @@ -1575,7 +1575,7 @@ f = g if g(): def f(): pass f() -f(1) # E: Too many arguments +f(1) # E: Too many arguments for "g" [case testRedefineFunctionDefinedAsVariableInitializedToNone] def g(): pass @@ -2403,7 +2403,7 @@ def g(__x: T) -> T: pass f = g reveal_type(f) # N: Revealed type is "def [T] (T`-1) -> T`-1" i = f(3) -reveal_type(i) # N: Revealed type is "builtins.int" +reveal_type(i) # N: Revealed type is "Literal[3]?" [case testFunctionReturningGenericFunction] from typing import Callable, TypeVar @@ -2414,7 +2414,7 @@ reveal_type(deco) # N: Revealed type is "def () -> def [T] (T`-1) -> T`-1" f = deco() reveal_type(f) # N: Revealed type is "def [T] (T`1) -> T`1" i = f(3) -reveal_type(i) # N: Revealed type is "builtins.int" +reveal_type(i) # N: Revealed type is "Literal[3]?" [case testFunctionReturningGenericFunctionPartialBinding] from typing import Callable, TypeVar @@ -2427,7 +2427,7 @@ reveal_type(deco) # N: Revealed type is "def [U] (x: U`-1) -> def [T] (T`-2, U` f = deco("foo") reveal_type(f) # N: Revealed type is "def [T] (T`1, builtins.str) -> T`1" i = f(3, "eggs") -reveal_type(i) # N: Revealed type is "builtins.int" +reveal_type(i) # N: Revealed type is "Literal[3]?" [case testFunctionReturningGenericFunctionTwoLevelBinding] from typing import Callable, TypeVar @@ -2438,9 +2438,9 @@ def deco() -> Callable[[T], Callable[[T, R], R]]: pass f = deco() reveal_type(f) # N: Revealed type is "def [T] (T`2) -> def [R] (T`2, R`1) -> R`1" g = f(3) -reveal_type(g) # N: Revealed type is "def [R] (builtins.int, R`3) -> R`3" +reveal_type(g) # N: Revealed type is "def [R] (builtins.int, R`5) -> R`5" s = g(4, "foo") -reveal_type(s) # N: Revealed type is "builtins.str" +reveal_type(s) # N: Revealed type is "Literal['foo']?" [case testGenericFunctionReturnAsDecorator] from typing import Callable, TypeVar @@ -2584,7 +2584,7 @@ main:1: error: Unsupported operand types for + ("int" and "str") f = lambda: 5 reveal_type(f) [out] -main:2: note: Revealed type is "def () -> builtins.int" +main:2: note: Revealed type is "def () -> Literal[5]?" [case testRevealLocalsFunction] a = 1.0 diff --git a/test-data/unit/check-functools.test b/test-data/unit/check-functools.test index c331029dd..85de33938 100644 --- a/test-data/unit/check-functools.test +++ b/test-data/unit/check-functools.test @@ -248,8 +248,8 @@ reveal_type(p2("a")) # N: Revealed type is "builtins.str" def bar(a: T, b: U) -> U: ... p3 = functools.partial(bar, 1) -reveal_type(p3(2)) # N: Revealed type is "builtins.int" -reveal_type(p3("a")) # N: Revealed type is "builtins.str" +reveal_type(p3(2)) # N: Revealed type is "Literal[2]?" +reveal_type(p3("a")) # N: Revealed type is "Literal['a']?" [builtins fixtures/dict.pyi] [case testFunctoolsPartialCallable] diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 043428bb4..443fa7f8f 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -833,13 +833,13 @@ def f1(x: T) -> SameTP[T]: a, b, c = f1(1) # E: Need more than 2 values to unpack (3 expected) x, y = f1(1) -reveal_type(x) # N: Revealed type is "builtins.int" +reveal_type(x) # N: Revealed type is "Literal[1]?" def f2(x: IntTP[T]) -> IntTP[T]: return x f2((1, 2, 3)) # E: Argument 1 to "f2" has incompatible type "Tuple[int, int, int]"; expected "Tuple[int, Never]" -reveal_type(f2((1, 'x'))) # N: Revealed type is "Tuple[builtins.int, builtins.str]" +reveal_type(f2((1, 'x'))) # N: Revealed type is "Tuple[builtins.int, Literal['x']?]" [builtins fixtures/for.pyi] @@ -2750,7 +2750,7 @@ T= TypeVar("T") def func(var: T) -> T: return var -reveal_type(func(1)) # N: Revealed type is "builtins.int" +reveal_type(func(1)) # N: Revealed type is "Literal[1]?" [builtins fixtures/tuple.pyi] @@ -2935,8 +2935,8 @@ def mix(fs: List["FunctionType[[S], T]"]) -> "FunctionType[[S], List[T]]": def id(__x: U) -> U: ... fs = [id, id, id] -reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`11) -> builtins.list[S`11]" -reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`13) -> builtins.list[S`13]" +reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`13) -> builtins.list[S`13]" +reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`15) -> builtins.list[S`15]" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericCurry] @@ -3131,8 +3131,8 @@ def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ... def id(x: U) -> U: ... def either(x: U, y: U) -> U: ... def pair(x: U, y: V) -> Tuple[U, V]: ... -reveal_type(dec(id)) # N: Revealed type is "def [T] (x: T`3) -> builtins.list[T`3]" -reveal_type(dec(either)) # N: Revealed type is "def [T] (x: T`5, y: T`5) -> builtins.list[T`5]" +reveal_type(dec(id)) # N: Revealed type is "def [T] (x: T`4) -> builtins.list[T`4]" +reveal_type(dec(either)) # N: Revealed type is "def [T] (x: T`6, y: T`6) -> builtins.list[T`6]" reveal_type(dec(pair)) # N: Revealed type is "def [U, V] (x: U`-1, y: V`-2) -> builtins.list[Tuple[U`-1, V`-2]]" [builtins fixtures/list.pyi] @@ -3149,8 +3149,8 @@ V = TypeVar('V') def dec(f: Callable[P, List[T]]) -> Callable[P, T]: ... def id(x: U) -> U: ... def either(x: U, y: U) -> U: ... -reveal_type(dec(id)) # N: Revealed type is "def [T] (x: builtins.list[T`3]) -> T`3" -reveal_type(dec(either)) # N: Revealed type is "def [T] (x: builtins.list[T`5], y: builtins.list[T`5]) -> T`5" +reveal_type(dec(id)) # N: Revealed type is "def [T] (x: builtins.list[T`4]) -> T`4" +reveal_type(dec(either)) # N: Revealed type is "def [T] (x: builtins.list[T`6], y: builtins.list[T`6]) -> T`6" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericParamSpecPopOff] @@ -3168,9 +3168,9 @@ def dec(f: Callable[Concatenate[T, P], S]) -> Callable[P, Callable[[T], S]]: ... def id(x: U) -> U: ... def either(x: U, y: U) -> U: ... def pair(x: U, y: V) -> Tuple[U, V]: ... -reveal_type(dec(id)) # N: Revealed type is "def () -> def [T] (T`2) -> T`2" -reveal_type(dec(either)) # N: Revealed type is "def [T] (y: T`5) -> def (T`5) -> T`5" -reveal_type(dec(pair)) # N: Revealed type is "def [V] (y: V`-2) -> def [T] (T`8) -> Tuple[T`8, V`-2]" +reveal_type(dec(id)) # N: Revealed type is "def () -> def [T] (T`3) -> T`3" +reveal_type(dec(either)) # N: Revealed type is "def [T] (y: T`6) -> def (T`6) -> T`6" +reveal_type(dec(pair)) # N: Revealed type is "def [V] (y: V`-2) -> def [T] (T`9) -> Tuple[T`9, V`-2]" reveal_type(dec(dec)) # N: Revealed type is "def () -> def [T, P, S] (def (T`-1, *P.args, **P.kwargs) -> S`-3) -> def (*P.args, **P.kwargs) -> def (T`-1) -> S`-3" [builtins fixtures/list.pyi] @@ -3189,11 +3189,11 @@ def dec(f: Callable[P, Callable[[T], S]]) -> Callable[Concatenate[T, P], S]: ... def id() -> Callable[[U], U]: ... def either(x: U) -> Callable[[U], U]: ... def pair(x: U) -> Callable[[V], Tuple[V, U]]: ... -reveal_type(dec(id)) # N: Revealed type is "def [T] (T`3) -> T`3" -reveal_type(dec(either)) # N: Revealed type is "def [T] (T`6, x: T`6) -> T`6" -reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`9, x: U`-1) -> Tuple[T`9, U`-1]" +reveal_type(dec(id)) # N: Revealed type is "def [T] (T`4) -> T`4" +reveal_type(dec(either)) # N: Revealed type is "def [T] (T`7, x: T`7) -> T`7" +reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`10, x: U`-1) -> Tuple[T`10, U`-1]" # This is counter-intuitive but looks correct, dec matches itself only if P can be empty -reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`13, f: def () -> def (T`13) -> S`14) -> S`14" +reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`14, f: def () -> def (T`14) -> S`15) -> S`15" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericParamSpecVsParamSpec] @@ -3210,7 +3210,7 @@ class Bar(Generic[P, T]): ... def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ... def f(*args: Q.args, **kwargs: Q.kwargs) -> Foo[Q]: ... -reveal_type(dec(f)) # N: Revealed type is "def [P] (*P.args, **P.kwargs) -> builtins.list[__main__.Foo[P`2]]" +reveal_type(dec(f)) # N: Revealed type is "def [P] (*P.args, **P.kwargs) -> builtins.list[__main__.Foo[P`3]]" g: Callable[Concatenate[int, Q], Foo[Q]] reveal_type(dec(g)) # N: Revealed type is "def [Q] (builtins.int, *Q.args, **Q.kwargs) -> builtins.list[__main__.Foo[Q`-1]]" h: Callable[Concatenate[T, Q], Bar[Q, T]] @@ -3271,8 +3271,8 @@ def transform( def dec(f: Callable[W, U]) -> Callable[W, U]: ... def dec2(f: Callable[Concatenate[str, W], U]) -> Callable[Concatenate[bytes, W], U]: ... -reveal_type(transform(dec)) # N: Revealed type is "def [P, T] (def (builtins.int, *P.args, **P.kwargs) -> T`3) -> def (builtins.int, *P.args, **P.kwargs) -> T`3" -reveal_type(transform(dec2)) # N: Revealed type is "def [W, T] (def (builtins.int, builtins.str, *W.args, **W.kwargs) -> T`7) -> def (builtins.int, builtins.bytes, *W.args, **W.kwargs) -> T`7" +reveal_type(transform(dec)) # N: Revealed type is "def [P, T] (def (builtins.int, *P.args, **P.kwargs) -> T`4) -> def (builtins.int, *P.args, **P.kwargs) -> T`4" +reveal_type(transform(dec2)) # N: Revealed type is "def [W, T] (def (builtins.int, builtins.str, *W.args, **W.kwargs) -> T`8) -> def (builtins.int, builtins.bytes, *W.args, **W.kwargs) -> T`8" [builtins fixtures/tuple.pyi] [case testNoAccidentalVariableClashInNestedGeneric] @@ -3326,8 +3326,8 @@ def id(x: U) -> U: ... def either(x: U, y: U) -> U: ... def pair(x: U, y: V) -> Tuple[U, V]: ... -reveal_type(dec(id)) # N: Revealed type is "def [T] (T`3) -> builtins.list[T`3]" -reveal_type(dec(either)) # N: Revealed type is "def [T] (T`5, T`5) -> builtins.list[T`5]" +reveal_type(dec(id)) # N: Revealed type is "def [T] (T`4) -> builtins.list[T`4]" +reveal_type(dec(either)) # N: Revealed type is "def [T] (T`6, T`6) -> builtins.list[T`6]" reveal_type(dec(pair)) # N: Revealed type is "def [U, V] (U`-1, V`-2) -> builtins.list[Tuple[U`-1, V`-2]]" [builtins fixtures/tuple.pyi] @@ -3345,8 +3345,8 @@ V = TypeVar("V") def id(x: U) -> U: ... def either(x: U, y: U) -> U: ... -reveal_type(dec(id)) # N: Revealed type is "def [T] (builtins.list[T`3]) -> T`3" -reveal_type(dec(either)) # N: Revealed type is "def [T] (builtins.list[T`5], builtins.list[T`5]) -> T`5" +reveal_type(dec(id)) # N: Revealed type is "def [T] (builtins.list[T`4]) -> T`4" +reveal_type(dec(either)) # N: Revealed type is "def [T] (builtins.list[T`6], builtins.list[T`6]) -> T`6" [builtins fixtures/tuple.pyi] [case testInferenceAgainstGenericVariadicPopOff] @@ -3365,9 +3365,9 @@ def id(x: U) -> U: ... def either(x: U, y: U) -> U: ... def pair(x: U, y: V) -> Tuple[U, V]: ... -reveal_type(dec(id)) # N: Revealed type is "def () -> def [T] (T`2) -> T`2" -reveal_type(dec(either)) # N: Revealed type is "def [T] (T`5) -> def (T`5) -> T`5" -reveal_type(dec(pair)) # N: Revealed type is "def [V] (V`-2) -> def [T] (T`8) -> Tuple[T`8, V`-2]" +reveal_type(dec(id)) # N: Revealed type is "def () -> def [T] (T`3) -> T`3" +reveal_type(dec(either)) # N: Revealed type is "def [T] (T`6) -> def (T`6) -> T`6" +reveal_type(dec(pair)) # N: Revealed type is "def [V] (V`-2) -> def [T] (T`9) -> Tuple[T`9, V`-2]" reveal_type(dec(dec)) # N: Revealed type is "def () -> def [T, Ts, S] (def (T`-1, *Unpack[Ts`-2]) -> S`-3) -> def (*Unpack[Ts`-2]) -> def (T`-1) -> S`-3" [builtins fixtures/list.pyi] @@ -3387,11 +3387,11 @@ def id() -> Callable[[U], U]: ... def either(x: U) -> Callable[[U], U]: ... def pair(x: U) -> Callable[[V], Tuple[V, U]]: ... -reveal_type(dec(id)) # N: Revealed type is "def [T] (T`3) -> T`3" -reveal_type(dec(either)) # N: Revealed type is "def [T] (T`6, T`6) -> T`6" -reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`9, U`-1) -> Tuple[T`9, U`-1]" +reveal_type(dec(id)) # N: Revealed type is "def [T] (T`4) -> T`4" +reveal_type(dec(either)) # N: Revealed type is "def [T] (T`7, T`7) -> T`7" +reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`10, U`-1) -> Tuple[T`10, U`-1]" # This is counter-intuitive but looks correct, dec matches itself only if Ts is empty -reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`13, def () -> def (T`13) -> S`14) -> S`14" +reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`14, def () -> def (T`14) -> S`15) -> S`15" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericVariadicVsVariadic] @@ -3409,9 +3409,9 @@ class Bar(Generic[Unpack[Ts], T]): ... def dec(f: Callable[[Unpack[Ts]], T]) -> Callable[[Unpack[Ts]], List[T]]: ... def f(*args: Unpack[Us]) -> Foo[Unpack[Us]]: ... -reveal_type(dec(f)) # N: Revealed type is "def [Ts] (*Unpack[Ts`2]) -> builtins.list[__main__.Foo[Unpack[Ts`2]]]" +reveal_type(dec(f)) # N: Revealed type is "def [Ts] (*Unpack[Ts`3]) -> builtins.list[__main__.Foo[Unpack[Ts`3]]]" g: Callable[[Unpack[Us]], Foo[Unpack[Us]]] -reveal_type(dec(g)) # N: Revealed type is "def [Ts] (*Unpack[Ts`4]) -> builtins.list[__main__.Foo[Unpack[Ts`4]]]" +reveal_type(dec(g)) # N: Revealed type is "def [Ts] (*Unpack[Ts`5]) -> builtins.list[__main__.Foo[Unpack[Ts`5]]]" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericVariadicVsVariadicConcatenate] @@ -3480,7 +3480,7 @@ def fake_partial(fun: Callable[[A1, A2], R], arg: A1) -> Callable[[A2], R]: ... f_pid = fake_partial(apply, id) reveal_type(f_pid) # N: Revealed type is "def [A2] (A2`2) -> A2`2" -reveal_type(f_pid(1)) # N: Revealed type is "builtins.int" +reveal_type(f_pid(1)) # N: Revealed type is "Literal[1]?" [case testInvalidTypeVarParametersConcrete] from typing import Callable, Generic, ParamSpec, Protocol, TypeVar, overload diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index f3cf83e6e..eee259558 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -1176,7 +1176,7 @@ reveal_type(foo) [rechecked m, n] [stale] [out1] -tmp/n.py:2: note: Revealed type is "builtins.str" +tmp/n.py:2: note: Revealed type is "Literal['hello']?" tmp/m.py:3: error: Argument 1 to "accept_int" has incompatible type "str"; expected "int" [out2] tmp/n.py:2: note: Revealed type is "builtins.float" diff --git a/test-data/unit/check-inference-context.test b/test-data/unit/check-inference-context.test index a16b61817..5b2f333bf 100644 --- a/test-data/unit/check-inference-context.test +++ b/test-data/unit/check-inference-context.test @@ -920,7 +920,7 @@ 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 "Union[Literal[1]?, 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]]" [builtins fixtures/list.pyi] @@ -931,7 +931,7 @@ 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 "Union[Literal[1]?, 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]]" [builtins fixtures/list.pyi] @@ -946,7 +946,7 @@ 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 "Union[builtins.list[builtins.int], Literal['']?]" 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]" diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 74813ed6e..f3512b16f 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -138,7 +138,7 @@ def f() -> None: import typing def f() -> None: a = g - a(B()) # E: Argument 1 has incompatible type "B"; expected "A" + a(B()) # E: Argument 1 to "g" has incompatible type "B"; expected "A" a(A()) def g(a: 'A') -> None: pass @@ -151,7 +151,7 @@ class B: pass import typing def f() -> None: a = A - a(A()) # E: Too many arguments + a(A()) # E: Too many arguments for "A" a() t = a # type: type @@ -343,7 +343,7 @@ for var2 in [g, h, i, j, k, l]: reveal_type(var2) # N: Revealed type is "Union[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" T = TypeVar("T", bound=Type[Foo]) @@ -1254,19 +1254,19 @@ for a in ("hourly", "daily"): reveal_type(x[a]) # N: Revealed type is "builtins.int" reveal_type(a.upper()) # N: Revealed type is "builtins.str" c = a - reveal_type(c) # N: Revealed type is "builtins.str" + reveal_type(c) # N: Revealed type is "Union[Literal['hourly']?, Literal['daily']?]" a = "monthly" - reveal_type(a) # N: Revealed type is "builtins.str" + reveal_type(a) # N: Revealed type is "Literal['monthly']?" a = "yearly" - reveal_type(a) # N: Revealed type is "builtins.str" + reveal_type(a) # N: Revealed type is "Literal['yearly']?" a = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") - reveal_type(a) # N: Revealed type is "builtins.str" + reveal_type(a) # N: Revealed type is "Literal['yearly']?" d = a - reveal_type(d) # N: Revealed type is "builtins.str" + reveal_type(d) # N: Revealed type is "Literal['yearly']?" b: str for b in ("hourly", "daily"): - reveal_type(b) # N: Revealed type is "builtins.str" + reveal_type(b) # N: Revealed type is "Union[Literal['hourly']?, Literal['daily']?]" reveal_type(b.upper()) # N: Revealed type is "builtins.str" [builtins fixtures/for.pyi] @@ -2538,7 +2538,7 @@ def f(x: T) -> Tuple[T, T]: ... x = None (x, x) = f('') -reveal_type(x) # N: Revealed type is "builtins.str" +reveal_type(x) # N: Revealed type is "Literal['']?" [builtins fixtures/tuple.pyi] [out] @@ -2552,8 +2552,8 @@ def make_tuple(elem: T) -> Tuple[T]: def main() -> None: ((a, b),) = make_tuple((1, 2)) - reveal_type(a) # N: Revealed type is "builtins.int" - reveal_type(b) # N: Revealed type is "builtins.int" + reveal_type(a) # N: Revealed type is "Literal[1]?" + reveal_type(b) # N: Revealed type is "Literal[2]?" [builtins fixtures/tuple.pyi] [out] @@ -3358,7 +3358,7 @@ def f(x: Callable[[], T]) -> T: return x() reveal_type(f(lambda: None)) # N: Revealed type is "None" -reveal_type(f(lambda: 1)) # N: Revealed type is "builtins.int" +reveal_type(f(lambda: 1)) # N: Revealed type is "Literal[1]?" def g() -> None: pass @@ -3373,7 +3373,7 @@ def f(x: Callable[[], T]) -> T: return x() reveal_type(f(lambda: None)) # N: Revealed type is "None" -reveal_type(f(lambda: 1)) # N: Revealed type is "builtins.int" +reveal_type(f(lambda: 1)) # N: Revealed type is "Literal[1]?" def g() -> None: pass @@ -3830,7 +3830,7 @@ def dec2(f: Callable[[S], List[T]]) -> Callable[[S], T]: ... 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(42)) # N: Revealed type is "Union[Literal[42]?, builtins.str]" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericCallbackProtoMultiple] @@ -3851,7 +3851,7 @@ def Negate(count: int, /, metric: Metric[float]) -> float: ... def Combine(count: int, m1: Metric[T], m2: Metric[T], /, *more: Metric[T]) -> T: ... reveal_type(Negate) # N: Revealed type is "def (metric: __main__.Metric[builtins.float]) -> builtins.float" -reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`5, def () -> T`5, *more: def () -> T`5) -> T`5" +reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`6, def () -> T`6, *more: def () -> T`6) -> T`6" def m1() -> float: ... def m2() -> float: ... diff --git a/test-data/unit/check-isinstance.test b/test-data/unit/check-isinstance.test index cffdddf39..0f163e14d 100644 --- a/test-data/unit/check-isinstance.test +++ b/test-data/unit/check-isinstance.test @@ -815,8 +815,10 @@ 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 "Union[int & str, int, str]" x = 'a' + + [builtins fixtures/isinstancelist.pyi] [case testModifyLoop2] @@ -837,7 +839,9 @@ 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 "Union[int, int & str, str]" + + [builtins fixtures/isinstancelist.pyi] [case testModifyLoop3] @@ -856,7 +860,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 "Union[int & str, int, str]" x = 1 for y in [1]: x + 1 @@ -865,7 +869,9 @@ 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 "Union[int & str, int, str]" + + [builtins fixtures/isinstancelist.pyi] [case testModifyLoopWhile4] @@ -889,15 +895,17 @@ 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 "Union[int, int & str, 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 "Union[int, int & str, str]" x = 'a' x + 'a' + + [builtins fixtures/isinstancelist.pyi] [case testModifyLoopFor4] @@ -921,15 +929,17 @@ 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 "Union[int, int & str, 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 "Union[int, int & str, str]" x = 'a' x + 'a' + + [builtins fixtures/isinstancelist.pyi] [case testModifyNestedLoop] @@ -950,7 +960,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 "Union[int & str, int, str]" x = 1 while bool(): while bool(): @@ -961,7 +971,9 @@ 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 "Union[int & str, int, str]" + + [builtins fixtures/isinstancelist.pyi] [case testModifyLoopLong] @@ -1019,8 +1031,8 @@ while isinstance(x, int): continue x = 'a' else: - reveal_type(x) # N: Revealed type is "builtins.str" -reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['a']?" +reveal_type(x) # N: Revealed type is "Literal['a']?" [builtins fixtures/isinstance.pyi] [case testWhileExitCondition2] @@ -1031,8 +1043,8 @@ while isinstance(x, int): break 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 "Literal['a']?" +reveal_type(x) # N: Revealed type is "Union[builtins.int, Literal['a']?]" [builtins fixtures/isinstance.pyi] [case testWhileLinkedList] @@ -1887,10 +1899,10 @@ isinstance() # E: Missing positional arguments "x", "t" in call to "isinstance" x: object if isinstance(): # E: Missing positional arguments "x", "t" in call to "isinstance" x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" if isinstance(x): # E: Missing positional argument "t" in call to "isinstance" x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" [builtins fixtures/isinstancelist.pyi] [case testIsSubclassTooFewArgs] @@ -1907,12 +1919,13 @@ 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 "Union[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" x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" + [builtins fixtures/isinstancelist.pyi] [case testIsinstanceNarrowAny] @@ -2803,6 +2816,8 @@ x: A if hasattr(x, "x"): if isinstance(x, B): reveal_type(x.x) # N: Revealed type is "Any" + # this is due to a limitation in `_remove_redundant_intersection_items` + # reveal_type(x.x) if hasattr(x, "x") and hasattr(x, "y"): reveal_type(x.x) # N: Revealed type is "Any" @@ -2816,6 +2831,7 @@ if hasattr(x, "x"): if hasattr(x, "x") or hasattr(x, "y"): x.x # E: "A" has no attribute "x" x.y # E: "A" has no attribute "y" + [builtins fixtures/isinstance.pyi] [case testHasAttrPreciseType] diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index 5c09b5134..844e48551 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -595,6 +595,16 @@ d2: d2t # E: Variable "__main__.d2t" is not valid as a type \ # N: See https://kotlinisland.github.io/basedmypy/common_issues.html#variables-vs-type-aliases [builtins fixtures/complex_tuple.pyi] [out] +main:4: error: Invalid type: float literals cannot be used as a type +main:6: error: Invalid type: complex literals cannot be used as a type +main:8: error: Parameter 1 of Literal[...] cannot be of type "float" +main:10: error: Parameter 1 of Literal[...] cannot be of type "complex" +main:14: note: Revealed type is "Any" +main:15: error: Variable "__main__.b2t" is not valid as a type +main:15: note: See https://kotlinisland.github.io/basedmypy/common_issues.html#variables-vs-type-aliases +main:17: note: Revealed type is "Any" +main:18: error: Variable "__main__.d2t" is not valid as a type +main:18: note: See https://kotlinisland.github.io/basedmypy/common_issues.html#variables-vs-type-aliases [case testLiteralDisallowComplexExpressions] from typing_extensions import Literal @@ -1001,14 +1011,14 @@ none2 = None none3: None = None reveal_type(int1) # N: Revealed type is "Literal[1]" -reveal_type(int2) # N: Revealed type is "builtins.int" -reveal_type(int3) # N: Revealed type is "builtins.int" +reveal_type(int2) # N: Revealed type is "Literal[1]?" +reveal_type(int3) # N: Revealed type is "Literal[1]?" reveal_type(str1) # N: Revealed type is "Literal['foo']" -reveal_type(str2) # N: Revealed type is "builtins.str" -reveal_type(str3) # N: Revealed type is "builtins.str" +reveal_type(str2) # N: Revealed type is "Literal['foo']?" +reveal_type(str3) # N: Revealed type is "Literal['foo']?" reveal_type(bool1) # N: Revealed type is "Literal[True]" -reveal_type(bool2) # N: Revealed type is "builtins.bool" -reveal_type(bool3) # N: Revealed type is "builtins.bool" +reveal_type(bool2) # N: Revealed type is "Literal[True]?" +reveal_type(bool3) # N: Revealed type is "Literal[True]?" reveal_type(none1) # N: Revealed type is "None" reveal_type(none2) # N: Revealed type is "None" reveal_type(none3) # N: Revealed type is "None" @@ -1029,14 +1039,14 @@ b = "foo" c = True d = None -w = a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[1]") -x = b # E: Incompatible types in assignment (expression has type "str", variable has type "Literal['foo']") -y = c # E: Incompatible types in assignment (expression has type "bool", variable has type "Literal[True]") +w = a +x = b +y = c 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 +combined = b +combined = c combined = d # Also ok, for similar reasons. e: Literal[1] = 1 @@ -1345,7 +1355,7 @@ def f1(x: T, y: str) -> Union[T, str]: ... def f1(x, y): pass a: Literal[1] -reveal_type(f1(1, 1)) # N: Revealed type is "builtins.int" +reveal_type(f1(1, 1)) # N: Revealed type is "Literal[1]?" reveal_type(f1(a, 1)) # N: Revealed type is "Literal[1]" @overload @@ -1354,7 +1364,7 @@ def f2(x: T, y: Literal[3]) -> T: ... def f2(x: T, y: str) -> Union[T]: ... def f2(x, y): pass -reveal_type(f2(1, 3)) # N: Revealed type is "builtins.int" +reveal_type(f2(1, 3)) # N: Revealed type is "Literal[1]?" reveal_type(f2(a, 3)) # N: Revealed type is "Literal[1]" @overload @@ -1363,7 +1373,7 @@ def f3(x: Literal[3]) -> Literal[3]: ... def f3(x: T) -> T: ... def f3(x): pass -reveal_type(f3(1)) # N: Revealed type is "builtins.int" +reveal_type(f3(1)) # N: Revealed type is "Literal[1]?" reveal_type(f3(a)) # N: Revealed type is "Literal[1]" @overload @@ -1373,7 +1383,7 @@ def f4(x: T) -> T: ... def f4(x): pass b: Literal['foo'] -reveal_type(f4(1)) # N: Revealed type is "builtins.int" +reveal_type(f4(1)) # N: Revealed type is "Literal[1]?" reveal_type(f4(a)) # N: Revealed type is "Literal[1]" reveal_type(f4("foo")) # N: Revealed type is "builtins.str" @@ -1551,7 +1561,7 @@ def expects_literal(x: Literal[3]) -> None: pass def expects_int(x: int) -> None: pass a: Literal[3] -reveal_type(foo(3)) # N: Revealed type is "builtins.int" +reveal_type(foo(3)) # N: Revealed type is "Literal[3]?" reveal_type(foo(a)) # N: Revealed type is "Literal[3]" expects_literal(3) @@ -1670,9 +1680,9 @@ reveal_type(func1(b)) # E: Value of type variable "TLiteral" of "func1" cannot reveal_type(func1(c)) # E: Value of type variable "TLiteral" of "func1" cannot be "int" \ # N: Revealed type is "builtins.int" -reveal_type(func2(3)) # N: Revealed type is "builtins.int" +reveal_type(func2(3)) # N: Revealed type is "Literal[3]?" reveal_type(func2(a)) # N: Revealed type is "Literal[3]" -reveal_type(func2(4)) # N: Revealed type is "builtins.int" +reveal_type(func2(4)) # N: Revealed type is "Literal[4]?" reveal_type(func2(b)) # N: Revealed type is "Literal[4]" reveal_type(func2(c)) # N: Revealed type is "builtins.int" [builtins fixtures/tuple.pyi] @@ -2464,7 +2474,7 @@ c = a def expect_3(x: Literal[3]) -> None: pass expect_3(a) expect_3(b) -expect_3(c) # E: Argument 1 to "expect_3" has incompatible type "int"; expected "Literal[3]" +expect_3(c) [builtins fixtures/tuple.pyi] [out] @@ -2682,12 +2692,12 @@ expects_test1_foo(Test2.FOO) # E: Argument 1 to "expects_test1_foo" has incompa expects_test2_foo(Test1.FOO) # E: Argument 1 to "expects_test2_foo" has incompatible type "Literal[Test1.FOO]"; expected "Literal[Test2.FOO]" # Make sure enums follow the same semantics as 'x = 1' vs 'x: Final = 1' -var1 = Test1.FOO +var1: Test1 final1: Final = Test1.FOO expects_test1_foo(var1) # E: Argument 1 to "expects_test1_foo" has incompatible type "Test1"; expected "Literal[Test1.FOO]" expects_test1_foo(final1) -var2 = Test2.FOO +var2: Test2 final2: Final = Test2.FOO expects_test2_foo(var2) # E: Argument 1 to "expects_test2_foo" has incompatible type "Test2"; expected "Literal[Test2.FOO]" expects_test2_foo(final2) @@ -2728,19 +2738,19 @@ reveal_type(Test5.FOO.name) # N: Revealed type is "Literal['FOO']?" [builtins fixtures/tuple.pyi] [out] -[case testLiteralBinderLastValueErased] -# mypy: strict-equality - -from typing_extensions import Literal - -def takes_three(x: Literal[3]) -> None: ... -x: object -x = 3 - -takes_three(x) # E: Argument 1 to "takes_three" has incompatible type "int"; expected "Literal[3]" -if x == 2: # OK - ... -[builtins fixtures/bool.pyi] +--[case testLiteralBinderLastValueErased] +-- # mypy: strict-equality +-- +-- from typing_extensions import Literal +-- +-- def takes_three(x: Literal[3]) -> None: ... +-- x: object +-- x = 3 +-- +-- takes_three(x) # E: Argument 1 to "takes_three" has incompatible type "int"; expected "Literal[3]" +-- if x == 2: # OK +-- ... +-- [builtins fixtures/bool.pyi] [case testLiteralBinderLastValueErasedPartialTypes] # mypy: strict-equality diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 7fbdfbf4d..759ff47ea 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -1990,7 +1990,7 @@ x = 42 [case testModuleAliasToQualifiedImport] import package.module alias = package.module -reveal_type(alias.whatever('/')) # N: Revealed type is "builtins.str" +reveal_type(alias.whatever('/')) # N: Revealed type is "Literal['/']?" [file package/__init__.py] [file package/module.py] @@ -2003,7 +2003,7 @@ def whatever(x: T) -> T: pass import mod import othermod alias = mod.submod -reveal_type(alias.whatever('/')) # N: Revealed type is "builtins.str" +reveal_type(alias.whatever('/')) # N: Revealed type is "Literal['/']?" if int(): alias = othermod # E: Cannot assign multiple modules to name "alias" without explicit "types.ModuleType" annotation [file mod.py] diff --git a/test-data/unit/check-newsemanal.test b/test-data/unit/check-newsemanal.test index 5d283d50b..8c93833d7 100644 --- a/test-data/unit/check-newsemanal.test +++ b/test-data/unit/check-newsemanal.test @@ -284,7 +284,7 @@ import a MYPY = False if MYPY: from b import x as y -x = 0 +x: int def y(): pass # E: Name "y" already defined on line 4 reveal_type(y) # N: Revealed type is "builtins.int" @@ -311,7 +311,7 @@ import a [file a.py] from b import x as y -x = 0 +x: int def y(): pass # E: Name "y" already defined on line 2 reveal_type(y) # N: Revealed type is "builtins.int" diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index 91feff357..caa937f2f 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -99,7 +99,7 @@ x: 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 "Union[builtins.str, Literal[1]?]" z1 = 'a' or x reveal_type(z1) # N: Revealed type is "Union[builtins.str, None]" z2 = int() or x @@ -109,9 +109,9 @@ reveal_type(z2) # N: Revealed type is "Union[builtins.int, builtins.str, None]" from typing import Optional x: 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 "Union[Literal[''], None, Literal['b']?]" 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 "Union[Literal[''], None, Literal[1]?]" z1 = 'b' and x reveal_type(z1) # N: Revealed type is "Union[builtins.str, None]" z2 = int() and x @@ -119,7 +119,7 @@ reveal_type(z2) # N: Revealed type is "Union[Literal[0], builtins.str, None]" [case testLambdaReturningNone] f = lambda: None -x = f() # E: Function does not return a value (it only ever returns None) +x = f() # E: does not return a value (it only ever returns None) reveal_type(x) # N: Revealed type is "None" [case testNoneArgumentType] @@ -609,8 +609,8 @@ reveal_type(u(None, C())) # N: Revealed type is "Union[__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(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 "Union[None, Literal[1]?]" +reveal_type(u(None, 1)) # N: Revealed type is "Union[Literal[1]?, None]" [case testOptionalAndAnyBaseClass] from typing import Any, Optional @@ -738,7 +738,7 @@ def g(x: Optional[int]) -> int: if x is None: reveal_type(x) # N: Revealed type is "None" x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" # Since we've assigned to x, the special case None behavior shouldn't happen x = f() reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" @@ -794,11 +794,11 @@ from typing import Any, Optional def f1(b: bool) -> Optional[int]: if b: z = 10 - reveal_type(z) # N: Revealed type is "builtins.int" + reveal_type(z) # N: Revealed type is "Literal[10]?" 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 "Union[Literal[10]?, None]" return z def f2(b: bool) -> int: @@ -914,7 +914,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 "Union[Literal[50]?, None]" def f17(b: bool, c: bool, d: bool) -> None: if b: @@ -933,7 +933,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 "Union[Literal[5]?, None]" def f19(b: bool, c: bool, d: bool) -> None: if b: @@ -942,7 +942,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 "Union[Literal[6]?, None]" def f20(b: bool) -> None: if b: @@ -1020,13 +1020,13 @@ from typing import Any, Optional def f1(b: bool) -> Optional[int]: if b: z = 10 - reveal_type(z) # N: Revealed type is "builtins.int" + reveal_type(z) # N: Revealed type is "Literal[10]?" else: # Force the node to get deferred between the two assignments 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 "Union[Literal[10]?, None]" return z class Defer: diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index e0926a081..bd3bf2566 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -4989,6 +4989,13 @@ def attr(default: T = ..., blah: int = ...) -> T: ... @overload def attr(default: Any = ...) -> int: ... [out] +main:4: note: Revealed type is "Literal[1]?" +main:5: note: Revealed type is "builtins.int" +main:7: note: Revealed type is "Any" +main:8: error: No overload variant of "attr" matches argument types "str", "int" +main:8: note: Possible overload variants: +main:8: note: def [T: int] attr(default: T = ..., blah: int = ...) -> T +main:8: note: def attr(default: Any = ...) -> int [case testAnyIsOKAsFallbackInOverloads] import stub diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index 2ff96b977..a79339a04 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -903,8 +903,8 @@ class A: def func(self, action: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R: ... -reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`6, *_P.args, **_P.kwargs) -> _R`6" -reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`10, *_P.args, **_P.kwargs) -> _R`10" +reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`7, *_P.args, **_P.kwargs) -> _R`7" +reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`11, *_P.args, **_P.kwargs) -> _R`11" def f(x: int) -> int: ... @@ -935,8 +935,8 @@ class A: def func(self, action: Job[_P, None]) -> Job[_P, None]: ... -reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`4, None]) -> __main__.Job[_P`4, None]" -reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`6, None]) -> __main__.Job[_P`6, None]" +reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`5, None]) -> __main__.Job[_P`5, None]" +reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`7, None]) -> __main__.Job[_P`7, None]" reveal_type(A().func(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]" def f(x: int, y: int) -> None: ... @@ -1098,7 +1098,7 @@ j = Job(generic_f) reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`-1]]" jf = j.into_callable() -reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`4)" +reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`5)" reveal_type(jf(1)) # N: Revealed type is "None" [builtins fixtures/paramspec.pyi] @@ -1117,11 +1117,11 @@ class Job(Generic[_P, _T]): def generic_f(x: _T) -> _T: ... j = Job(generic_f) -reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`3], _T`3]" +reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`4], _T`4]" jf = j.into_callable() -reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`4) -> _T`4" -reveal_type(jf(1)) # N: Revealed type is "builtins.int" +reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`5) -> _T`5" +reveal_type(jf(1)) # N: Revealed type is "Literal[1]?" [builtins fixtures/paramspec.pyi] [case testStackedConcatenateIsIllegal] @@ -1632,13 +1632,13 @@ U = TypeVar("U") def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ... def test(x: U) -> U: ... reveal_type(dec) # N: Revealed type is "def [P, T] (f: def (*P.args, **P.kwargs) -> T`-2) -> def (*P.args, **P.kwargs) -> builtins.list[T`-2]" -reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`3) -> builtins.list[T`3]" +reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`4) -> builtins.list[T`4]" class A: ... TA = TypeVar("TA", bound=A) def test_with_bound(x: TA) -> TA: ... -reveal_type(dec(test_with_bound)) # N: Revealed type is "def [T <: __main__.A] (x: T`5) -> builtins.list[T`5]" +reveal_type(dec(test_with_bound)) # N: Revealed type is "def [T <: __main__.A] (x: T`6) -> builtins.list[T`6]" dec(test_with_bound)(0) # E: Value of type variable "T" of function cannot be "int" dec(test_with_bound)(A()) # OK [builtins fixtures/paramspec.pyi] @@ -1672,7 +1672,7 @@ T = TypeVar("T") def identity(x: T) -> T: return x -reveal_type(call(identity, 2)) # N: Revealed type is "builtins.int" +reveal_type(call(identity, 2)) # N: Revealed type is "Literal[2]?" y: int = call(identity, 2) [builtins fixtures/paramspec.pyi] @@ -2180,7 +2180,7 @@ def dec2(f: Callable[P, List[T]]) -> Callable[P, T]: ... 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(42)) # N: Revealed type is "Union[Literal[42]?, builtins.str]" [builtins fixtures/paramspec.pyi] [case testParamSpecPreciseKindsUsedIfPossible] diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index ad0d3dced..e4a5d0319 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -1200,7 +1200,7 @@ match m: reveal_type(a) # N: Revealed type is "builtins.bool" a = 3 -reveal_type(a) # N: Revealed type is "builtins.int" +reveal_type(a) # N: Revealed type is "Literal[3]?" [case testMatchCapturePatternPreexistingIncompatible] a: str @@ -1291,7 +1291,7 @@ m: str match m: case a if a := "test": - reveal_type(a) # N: Revealed type is "builtins.str" + reveal_type(a) # N: Revealed type is "Literal['test']?" [case testMatchNarrowingPatternGuard] m: object @@ -1513,7 +1513,7 @@ def f(m: Medal) -> None: case _: assert_never(m) - reveal_type(always_assigned) # N: Revealed type is "builtins.int" + reveal_type(always_assigned) # N: Revealed type is "Literal[1]?" [builtins fixtures/bool.pyi] [case testMatchLiteralPatternEnumNegativeNarrowing] diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index 8be22c952..6873123eb 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -28,7 +28,7 @@ def f(x: MyGen[int]): def f[T](x: T) -> T: return reveal_type(x) # N: Revealed type is "T`-1" -reveal_type(f(1)) # N: Revealed type is "builtins.int" +reveal_type(f(1)) # N: Revealed type is "Literal[1]?" async def g[T](x: T) -> T: return reveal_type(x) # N: Revealed type is "T`-1" @@ -37,6 +37,8 @@ reveal_type(g(1)) # E: Value of type "Coroutine[Any, Any, int]" must be used \ # N: Are you missing an await? \ # N: Revealed type is "typing.Coroutine[Any, Any, builtins.int]" + + [case testPEP695TypeVarBasic] from typing import Callable type Alias1[T: int] = list[T] @@ -79,7 +81,7 @@ reveal_type(ba2) # N: Revealed type is "def (*Any) -> builtins.str" # mypy: enable-incomplete-feature=NewGenericSyntax def f[T](x: T) -> T: return x -reveal_type(f(1)) # N: Revealed type is "builtins.int" +reveal_type(f(1)) # N: Revealed type is "Literal[1]?" [case testPEP695GenericFunctionSyntax] def ident[TV](x: TV) -> TV: @@ -87,8 +89,8 @@ def ident[TV](x: TV) -> TV: y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "TV") return x -reveal_type(ident(1)) # N: Revealed type is "builtins.int" -reveal_type(ident('x')) # N: Revealed type is "builtins.str" +reveal_type(ident(1)) # N: Revealed type is "Literal[1]?" +reveal_type(ident('x')) # N: Revealed type is "Literal['x']?" a: TV # E: Name "TV" is not defined @@ -96,7 +98,7 @@ def tup[T, S](x: T, y: S) -> tuple[T, S]: reveal_type((x, y)) # N: Revealed type is "Tuple[T`-1, S`-2]" return (x, y) -reveal_type(tup(1, 'x')) # N: Revealed type is "Tuple[builtins.int, builtins.str]" +reveal_type(tup(1, 'x')) # N: Revealed type is "Tuple[Literal[1]?, Literal['x']?]" [builtins fixtures/tuple.pyi] [case testPEP695GenericClassSyntax] @@ -125,7 +127,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 "Union[builtins.str, Literal[1]?]" [case testPEP695InferVarianceSimpleFromMethod] class Invariant[T]: @@ -979,8 +981,8 @@ def g[T: int, S: (str, None)](x: T, y: S) -> T | S: return x [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:2: note: Revealed type is "Literal[1]?" +tmp/a.py:3: note: Revealed type is "Union[Literal[1]?, 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" tmp/a.py:5: note: "S" of "g" is a constrained type variable, it is not generic @@ -1082,23 +1084,23 @@ class C: reveal_type(y) # N: Revealed type is "T`-1" return cast(T, y) -reveal_type(C().m(1)) # N: Revealed type is "builtins.int" +reveal_type(C().m(1)) # N: Revealed type is "Literal[1]?" [case testPEP695ScopingBasics] T = 1 def f[T](x: T) -> T: T = 'a' - reveal_type(T) # N: Revealed type is "builtins.str" + reveal_type(T) # N: Revealed type is "Literal['a']?" return x -reveal_type(T) # N: Revealed type is "builtins.int" +reveal_type(T) # N: Revealed type is "Literal[1]?" class C[T]: T = 1.2 reveal_type(T) # N: Revealed type is "builtins.float" -reveal_type(T) # N: Revealed type is "builtins.int" +reveal_type(T) # N: Revealed type is "Literal[1]?" [case testPEP695ClassScoping] class C: @@ -1113,7 +1115,7 @@ C().m(1, C.D()) # E: Value of type variable "T" of "m" of "C" cannot be "int" [case testPEP695NestedGenericFunction] def f[T](x: T) -> T: reveal_type(f(x)) # N: Revealed type is "T`-1" - reveal_type(f(1)) # N: Revealed type is "builtins.int" + reveal_type(f(1)) # N: Revealed type is "Literal[1]?" def ff(x: T) -> T: y: T = x @@ -1124,12 +1126,12 @@ def f[T](x: T) -> T: def g[S](a: S) -> S: ff(a) # E: Argument 1 to "ff" has incompatible type "S"; expected "T" return a - reveal_type(g(1)) # N: Revealed type is "builtins.int" + reveal_type(g(1)) # N: Revealed type is "Literal[1]?" reveal_type(g(x)) # N: Revealed type is "T`-1" def h[S](a: S) -> S: return a - reveal_type(h(1)) # N: Revealed type is "builtins.int" + reveal_type(h(1)) # N: Revealed type is "Literal[1]?" reveal_type(h(x)) # N: Revealed type is "T`-1" return x @@ -1140,7 +1142,7 @@ def f() -> None: nonlocal T # E: nonlocal binding not allowed for type parameter "T" T = 'x' # E: "T" is a type variable and only valid in type context return x - reveal_type(T) # N: Revealed type is "builtins.int" + reveal_type(T) # N: Revealed type is "Literal[1]?" def g() -> None: a = 1 @@ -1398,8 +1400,8 @@ class C: class D(C): pass -reveal_type(C.m(1)) # N: Revealed type is "Tuple[__main__.C, builtins.int]" -reveal_type(D.m(1)) # N: Revealed type is "Tuple[__main__.D, builtins.int]" +reveal_type(C.m(1)) # N: Revealed type is "Tuple[__main__.C, Literal[1]?]" +reveal_type(D.m(1)) # N: Revealed type is "Tuple[__main__.D, Literal[1]?]" class E[T]: def m(self) -> Self: @@ -1412,9 +1414,9 @@ class F[T](E[T]): pass reveal_type(E[int]().m()) # N: Revealed type is "__main__.E[builtins.int]" -reveal_type(E[int]().mm(b'x')) # N: Revealed type is "Tuple[__main__.E[builtins.int], builtins.bytes]" +reveal_type(E[int]().mm(b'x')) # N: Revealed type is "Tuple[__main__.E[builtins.int], Literal[b'x']?]" reveal_type(F[str]().m()) # N: Revealed type is "__main__.F[builtins.str]" -reveal_type(F[str]().mm(b'x')) # N: Revealed type is "Tuple[__main__.F[builtins.str], builtins.bytes]" +reveal_type(F[str]().mm(b'x')) # N: Revealed type is "Tuple[__main__.F[builtins.str], Literal[b'x']?]" [builtins fixtures/tuple.pyi] [case testPEP695CallAlias] @@ -1586,7 +1588,7 @@ type C = int # E: Name "C" already defined on line 1 A = 0 type A = str # E: Name "A" already defined on line 4 -reveal_type(A) # N: Revealed type is "builtins.int" +reveal_type(A) # N: Revealed type is "Literal[0]?" [case testPEP695RedefineAsTypeAlias2] from m import D diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index f0ce96c96..20e3c4e4b 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -206,22 +206,23 @@ h(1) i() i(1) f(arg=0) # E: Unexpected keyword argument "arg" for "f" -g(arg=0) # E: Unexpected keyword argument "arg" +g(arg=0) # E: Unexpected keyword argument "arg" for h(arg=0) # E: Unexpected keyword argument "arg" for "h" -i(arg=0) # E: Unexpected keyword argument "arg" +i(arg=0) # E: Unexpected keyword argument "arg" for [case testWalrus] from typing import NamedTuple, Optional, List from typing_extensions import Final if a := 2: - reveal_type(a) # N: Revealed type is "builtins.int" + reveal_type(a) # N: Revealed type is "Literal[2]?" while b := "x": - reveal_type(b) # N: Revealed type is "builtins.str" + reveal_type(b) # N: Revealed type is "Literal['x']?" l = [y2 := 1, y2 + 2, y2 + 3] -reveal_type(y2) # N: Revealed type is "builtins.int" +reveal_type(y2) # N: Revealed type is "Literal[1]?" +# TODO: get rid of the `| 3` reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]" filtered_data = [y3 for x in l if (y3 := a) is not None] @@ -230,12 +231,12 @@ reveal_type(y3) # N: Revealed type is "builtins.int" d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2} reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" -reveal_type(a2) # N: Revealed type is "builtins.int" +reveal_type(a2) # N: Revealed type is "Literal[1]?" d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2} reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" -reveal_type(prefix) # N: Revealed type is "builtins.str" -reveal_type(start_val) # N: Revealed type is "builtins.int" +reveal_type(prefix) # N: Revealed type is "Literal['key_']?" +reveal_type(start_val) # N: Revealed type is "Literal[1]?" filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2} reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" @@ -243,18 +244,18 @@ reveal_type(new_v) # N: Revealed type is "builtins.int" def f(x: int = (c := 4)) -> int: if a := 2: - reveal_type(a) # N: Revealed type is "builtins.int" + reveal_type(a) # N: Revealed type is "Literal[2]?" while b := "x": - reveal_type(b) # N: Revealed type is "builtins.str" + reveal_type(b) # N: Revealed type is "Literal['x']?" x = (y := 1) + (z := 2) reveal_type(x) # N: Revealed type is "builtins.int" - reveal_type(y) # N: Revealed type is "builtins.int" - reveal_type(z) # N: Revealed type is "builtins.int" + reveal_type(y) # N: Revealed type is "Literal[1]?" + reveal_type(z) # N: Revealed type is "Literal[2]?" l = [y2 := 1, y2 + 2, y2 + 3] - reveal_type(y2) # N: Revealed type is "builtins.int" + reveal_type(y2) # N: Revealed type is "Literal[1]?" reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]" filtered_data = [y3 for x in l if (y3 := a) is not None] @@ -263,12 +264,12 @@ def f(x: int = (c := 4)) -> int: d = {'a': (a2 := 1), 'b': a2 + 1, 'c': a2 + 2} reveal_type(d) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" - reveal_type(a2) # N: Revealed type is "builtins.int" + reveal_type(a2) # N: Revealed type is "Literal[1]?" d2 = {(prefix := 'key_') + 'a': (start_val := 1), prefix + 'b': start_val + 1, prefix + 'c': start_val + 2} reveal_type(d2) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" - reveal_type(prefix) # N: Revealed type is "builtins.str" - reveal_type(start_val) # N: Revealed type is "builtins.int" + reveal_type(prefix) # N: Revealed type is "Literal['key_']?" + reveal_type(start_val) # N: Revealed type is "Literal[1]?" filtered_dict = {k: new_v for k, v in [('a', 1), ('b', 2), ('c', 3)] if (new_v := v + 1) == 2} reveal_type(filtered_dict) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" @@ -276,16 +277,16 @@ def f(x: int = (c := 4)) -> int: # https://www.python.org/dev/peps/pep-0572/#exceptional-cases (y4 := 3) - reveal_type(y4) # N: Revealed type is "builtins.int" + reveal_type(y4) # N: Revealed type is "Literal[3]?" y5 = (y6 := 3) - reveal_type(y5) # N: Revealed type is "builtins.int" - reveal_type(y6) # N: Revealed type is "builtins.int" + reveal_type(y5) # N: Revealed type is "Literal[3]?" + reveal_type(y6) # N: Revealed type is "Literal[3]?" f(x=(y7 := 3)) - reveal_type(y7) # N: Revealed type is "builtins.int" + reveal_type(y7) # N: Revealed type is "Literal[3]?" - reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is "builtins.int" + reveal_type((lambda: (y8 := 3) and y8)()) # N: Revealed type is "Literal[3]?" y8 # E: Name "y8" is not defined y7 = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int") @@ -302,8 +303,8 @@ def f(x: int = (c := 4)) -> int: # N: See https://kotlinisland.github.io/basedmypy/common_issues.html#variables-vs-type-aliases if (reveal_type(y9 := 3) and # N: Revealed type is "Literal[3]?" - reveal_type(y9)): # N: Revealed type is "builtins.int" - reveal_type(y9) # N: Revealed type is "builtins.int" + reveal_type(y9)): # N: Revealed type is "Literal[3]?" + reveal_type(y9) # N: Revealed type is "Literal[3]?" return (y10 := 3) + y10 @@ -320,22 +321,22 @@ def check_binder(x: Optional[int], y: Optional[int], z: Optional[int], a: Option reveal_type(x) # N: Revealed type is "Union[builtins.int, None]" (x := 1) - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" if x or (y := 1): reveal_type(y) # N: Revealed type is "Union[builtins.int, None]" if x and (y := 1): - reveal_type(y) # N: Revealed type is "builtins.int" + reveal_type(y) # N: Revealed type is "Literal[1]?" if (a := 1) and x: - reveal_type(a) # N: Revealed type is "builtins.int" + reveal_type(a) # N: Revealed type is "Literal[1]?" if (b := 1) or x: - reveal_type(b) # N: Revealed type is "builtins.int" + reveal_type(b) # N: Revealed type is "Literal[1]?" if z := 1: - reveal_type(z) # N: Revealed type is "builtins.int" + reveal_type(z) # N: Revealed type is "Literal[1]?" def check_partial() -> None: x = None @@ -362,16 +363,16 @@ def check_narrow(x: Optional[int], s: List[int]) -> None: class AssignmentExpressionsClass: x = (y := 1) + (z := 2) - reveal_type(z) # N: Revealed type is "builtins.int" + reveal_type(z) # N: Revealed type is "Literal[2]?" l = [x2 := 1, 2, 3] - reveal_type(x2) # N: Revealed type is "builtins.int" + reveal_type(x2) # N: Revealed type is "Literal[1]?" def __init__(self) -> None: reveal_type(self.z) # N: Revealed type is "builtins.int" l = [z2 := 1, z2 + 2, z2 + 3] - reveal_type(z2) # N: Revealed type is "builtins.int" + reveal_type(z2) # N: Revealed type is "Literal[1]?" reveal_type(l) # N: Revealed type is "builtins.list[builtins.int]" filtered_data = [z3 for x in l if (z3 := 1) is not None] @@ -379,8 +380,12 @@ class AssignmentExpressionsClass: reveal_type(z3) # N: Revealed type is "builtins.int" # Assignment expressions from inside the class should not escape the class scope. -reveal_type(x2) # E: Name "x2" is not defined # N: Revealed type is "Any" -reveal_type(z2) # E: Name "z2" is not defined # N: Revealed type is "Any" +reveal_type(x2) # E: Name "x2" is not defined \ + # N: Revealed type is "Any" +reveal_type(z2) # E: Name "z2" is not defined \ + # N: Revealed type is "Any" + + [builtins fixtures/isinstancelist.pyi] diff --git a/test-data/unit/check-redefine.test b/test-data/unit/check-redefine.test index 4a6ea7385..f16fb1a0e 100644 --- a/test-data/unit/check-redefine.test +++ b/test-data/unit/check-redefine.test @@ -9,27 +9,27 @@ # flags: --allow-redefinition def f() -> None: x = 0 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[0]?" x = '' - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" [case testCannotConditionallyRedefineLocalWithDifferentType] # flags: --allow-redefinition def f() -> None: y = 0 - reveal_type(y) # N: Revealed type is "builtins.int" + reveal_type(y) # N: Revealed type is "Literal[0]?" if int(): - y = '' \ - # E: Incompatible types in assignment (expression has type "str", variable has type "int") - reveal_type(y) # N: Revealed type is "builtins.int" - reveal_type(y) # N: Revealed type is "builtins.int" + y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") + reveal_type(y) # N: Revealed type is "Literal[0]?" + reveal_type(y) # N: Revealed type is "Literal[0]?" + [case testRedefineFunctionArg] # flags: --allow-redefinition def f(x: int) -> None: g(x) x = '' - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" def g(x: int) -> None: if int(): x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") @@ -45,9 +45,9 @@ def f() -> None: def g() -> None: x: int x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" x = '' - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" [case testRedefineLocalUsingOldValue] # flags: --allow-redefinition @@ -60,7 +60,7 @@ def f(x: int) -> None: reveal_type(x) # N: Revealed type is "Union[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 "Union[Literal[1]?, builtins.str]" def g(x: T) -> Union[T, str]: pass @@ -69,12 +69,10 @@ def g(x: T) -> Union[T, str]: pass from typing import Iterable def f(a: Iterable[int], b: Iterable[str]) -> None: for x in a: - x = '' \ - # E: Incompatible types in assignment (expression has type "str", variable has type "int") + x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" for x in b: - x = 1 \ - # E: Incompatible types in assignment (expression has type "int", variable has type "str") + x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") reveal_type(x) # N: Revealed type is "builtins.str" def g(a: Iterable[int]) -> None: @@ -83,9 +81,11 @@ def g(a: Iterable[int]) -> None: def h(a: Iterable[int]) -> None: x = '' - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" for x in a: pass + + [case testCannotRedefineLocalWithinTry] # flags: --allow-redefinition def f() -> None: @@ -112,7 +112,7 @@ def f() -> None: x g() # Might raise an exception, but we ignore this x = '' - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" y = 0 y y = '' @@ -177,15 +177,15 @@ def f() -> None: # flags: --allow-redefinition def f() -> None: x, x = 1, '' - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" x = object() reveal_type(x) # N: Revealed type is "builtins.object" def g() -> None: x = 1 if 1: - x, x = '', 1 \ - # E: Incompatible types in assignment (expression has type "str", variable has type "int") + x, x = '', 1 # E: Incompatible types in assignment (expression has type "str", variable has type "int") + [case testRedefineUnderscore] # flags: --allow-redefinition @@ -297,18 +297,18 @@ def f() -> None: # flags: --allow-redefinition def f() -> None: x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" x: object reveal_type(x) # N: Revealed type is "builtins.object" def g() -> None: x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" x: object reveal_type(x) # N: Revealed type is "builtins.object" def h() -> None: x: int x = 1 - reveal_type(x) # N: Revealed type is "builtins.int" + reveal_type(x) # N: Revealed type is "Literal[1]?" x: object x: object = '' # E: Name "x" already defined on line 16 def farg(x: int) -> None: @@ -325,10 +325,10 @@ def f() -> None: x = '' reveal_type(x) # N: Revealed type is "builtins.object" x = '' - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" if int(): - x = 2 \ - # E: Incompatible types in assignment (expression has type "int", variable has type "str") + x = 2 # E: Incompatible types in assignment (expression has type "int", variable has type "str") + [case testCannotRedefineSelf] @@ -340,7 +340,7 @@ class A: reveal_type(self.x) # N: Revealed type is "builtins.int" self = f() self.y: str = '' - reveal_type(self.y) # N: Revealed type is "builtins.str" + reveal_type(self.y) # N: Revealed type is "Literal['']?" def f() -> A: return A() @@ -361,9 +361,9 @@ reveal_type(x) x = '' reveal_type(x) [out] -tmp/m.py:2: note: Revealed type is "builtins.int" +tmp/m.py:2: note: Revealed type is "Literal[0]?" tmp/m.py:4: note: Revealed type is "builtins.object" -tmp/m.py:6: note: Revealed type is "builtins.str" +tmp/m.py:6: note: Revealed type is "Literal['']?" main:3: note: Revealed type is "builtins.str" [case testRedefineGlobalForIndex] @@ -398,13 +398,13 @@ reveal_type(a) # N: Revealed type is "typing.Iterable[builtins.int]" [case testRedefineGlobalWithSeparateDeclaration] # flags: --allow-redefinition x = '' -reveal_type(x) # N: Revealed type is "builtins.str" +reveal_type(x) # N: Revealed type is "Literal['']?" x: int x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" x: object x = 1 -reveal_type(x) # N: Revealed type is "builtins.int" +reveal_type(x) # N: Revealed type is "Literal[1]?" if int(): x = object() @@ -414,7 +414,7 @@ from typing import Iterable, TypeVar, Union T = TypeVar('T') def f(x: T) -> Iterable[Union[T, str]]: pass x = 0 -reveal_type(x) # N: Revealed type is "builtins.int" +reveal_type(x) # N: Revealed type is "Literal[0]?" for x in f(x): pass reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" @@ -458,13 +458,13 @@ def g() -> None: [case testRedefineAsException] # flags: --allow-redefinition e = 1 -reveal_type(e) # N: Revealed type is "builtins.int" +reveal_type(e) # N: Revealed type is "Literal[1]?" try: pass except Exception as e: reveal_type(e) # N: Revealed type is "builtins.Exception" e = '' -reveal_type(e) # N: Revealed type is "builtins.str" +reveal_type(e) # N: Revealed type is "Literal['']?" [builtins fixtures/exception.pyi] [case testRedefineUsingWithStatement] @@ -529,7 +529,7 @@ def f(arg: str) -> str: from a import g _ = g -_('a') # E: Argument 1 has incompatible type "str"; expected "int" +_('a') # E: Argument 1 to "g" has incompatible type "str"; expected "int" reveal_type(_(1)) # N: Revealed type is "builtins.int" [file a.py] diff --git a/test-data/unit/check-selftype.test b/test-data/unit/check-selftype.test index b77e773e9..2568b8d76 100644 --- a/test-data/unit/check-selftype.test +++ b/test-data/unit/check-selftype.test @@ -1121,7 +1121,7 @@ S = TypeVar('S') class C(Generic[T]): def limited(self: C[str], arg: S) -> S: ... -reveal_type(C[str]().limited(0)) # N: Revealed type is "builtins.int" +reveal_type(C[str]().limited(0)) # N: Revealed type is "Literal[0]?" [case testSelfTypeMultipleTypeVars] from typing import Generic, TypeVar, Tuple @@ -1647,8 +1647,8 @@ class D(C[int]): ... c: C[int] d: D -reveal_type(c.meth("test")) # N: Revealed type is "Tuple[__main__.C[builtins.int], builtins.str, builtins.int]" -reveal_type(d.meth("test")) # N: Revealed type is "Tuple[__main__.D, builtins.str, builtins.int]" +reveal_type(c.meth("test")) # N: Revealed type is "Tuple[__main__.C[builtins.int], Literal['test']?, builtins.int]" +reveal_type(d.meth("test")) # N: Revealed type is "Tuple[__main__.D, Literal['test']?, builtins.int]" [builtins fixtures/tuple.pyi] [case testTypingSelfRecursiveInit] @@ -1799,7 +1799,7 @@ class C: def foo(self, x: S) -> Tuple[Self, S]: ... reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C, S] (self: Self`1, x: S`2) -> Tuple[Self`1, S`2]" -reveal_type(C().foo(42)) # N: Revealed type is "Tuple[__main__.C, builtins.int]" +reveal_type(C().foo(42)) # N: Revealed type is "Tuple[__main__.C, Literal[42]?]" [builtins fixtures/tuple.pyi] [case testTypingSelfTypeVarClashAttr] @@ -1813,7 +1813,7 @@ class C: foo: Callable[[S, Self], Tuple[Self, S]] reveal_type(C().foo) # N: Revealed type is "def [S] (S`1, __main__.C) -> Tuple[__main__.C, S`1]" -reveal_type(C().foo(42, C())) # N: Revealed type is "Tuple[__main__.C, builtins.int]" +reveal_type(C().foo(42, C())) # N: Revealed type is "Tuple[__main__.C, Literal[42]?]" class This: ... [builtins fixtures/tuple.pyi] @@ -2083,7 +2083,7 @@ class Partial2: p: Partial reveal_type(p()) # N: Revealed type is "Never" p2: Partial2 -reveal_type(p2(42)) # N: Revealed type is "builtins.int" +reveal_type(p2(42)) # N: Revealed type is "Literal[42]?" [case testAccessingSelfClassVarInClassMethod] from typing import Self, ClassVar, Type, TypeVar diff --git a/test-data/unit/check-serialize.test b/test-data/unit/check-serialize.test index 81da94c05..b31341dac 100644 --- a/test-data/unit/check-serialize.test +++ b/test-data/unit/check-serialize.test @@ -81,8 +81,8 @@ T = TypeVar('T') def f(x: T) -> T: return x [out2] -tmp/a.py:2: note: Revealed type is "builtins.int" -tmp/a.py:3: note: Revealed type is "builtins.str" +tmp/a.py:2: note: Revealed type is "Literal[1]?" +tmp/a.py:3: note: Revealed type is "Literal['']?" [case testSerializeFunctionReturningGenericFunction] import a @@ -100,7 +100,7 @@ T = TypeVar('T') def f() -> Callable[[T], T]: pass [out2] tmp/a.py:2: note: Revealed type is "def () -> def [T] (T`-1) -> T`-1" -tmp/a.py:3: note: Revealed type is "builtins.str" +tmp/a.py:3: note: Revealed type is "Literal['']?" [case testSerializeArgumentKinds] import a diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index 106e4fb70..85f170329 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -2115,11 +2115,11 @@ class B(): pass main:8: error: Incompatible types in assignment (expression has type "A", variable has type "B") [case testAugmentedAssignmentIntFloat] -weight0 = 65.5 +weight0: float reveal_type(weight0) # N: Revealed type is "builtins.float" if int(): weight0 = 65 - reveal_type(weight0) # N: Revealed type is "builtins.int" + reveal_type(weight0) # N: Revealed type is "Literal[65]?" weight0 *= 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "float") weight0 *= 0.5 reveal_type(weight0) # N: Revealed type is "builtins.float" @@ -2131,10 +2131,10 @@ if int(): [case testAugmentedAssignmentIntFloatMember] class A: def __init__(self) -> None: - self.weight0 = 65.5 + self.weight0: float reveal_type(self.weight0) # N: Revealed type is "builtins.float" self.weight0 = 65 - reveal_type(self.weight0) # N: Revealed type is "builtins.int" + reveal_type(self.weight0) # N: Revealed type is "Literal[65]?" self.weight0 *= 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "float") self.weight0 *= 0.5 reveal_type(self.weight0) # N: Revealed type is "builtins.float" diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index e3607775b..08e834d79 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -317,7 +317,7 @@ a: A b: B x: Tuple[A, B, C] y: Tuple[A, C, E] -n = 0 +n: int if int(): a = t1[1] # E: Incompatible types in assignment (expression has type "B", variable has type "A") @@ -1424,8 +1424,8 @@ f(0) # E: Argument 1 to "f" has incompatible type "int"; expected "Tuple[()]" [case testNonliteralTupleIndex] t = (0, "") -x = 0 -y = "" +x: int +y: str reveal_type(t[x]) # N: Revealed type is "Union[builtins.int, builtins.str]" t[y] # E: No overload variant of "__getitem__" of "tuple" matches argument type "str" \ # N: Possible overload variants: \ @@ -1436,7 +1436,7 @@ t[y] # E: No overload variant of "__getitem__" of "tuple" matches argument type [case testNonliteralTupleSlice] t = (0, "") -x = 0 +x: int y = "" reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[Union[builtins.int, builtins.str], ...]" t[y:] # E: Slice index must be an integer, SupportsIndex or None @@ -1617,14 +1617,14 @@ points = (1, "test") # type: Tuple[int, str] x, y, z = *points, 0 reveal_type(x) # N: Revealed type is "builtins.int" reveal_type(y) # N: Revealed type is "builtins.str" -reveal_type(z) # N: Revealed type is "builtins.int" +reveal_type(z) # N: Revealed type is "Literal[0]?" points2 = [1,2] x2, y2, z2= *points2, "test" reveal_type(x2) # N: Revealed type is "builtins.int" reveal_type(y2) # N: Revealed type is "builtins.int" -reveal_type(z2) # N: Revealed type is "builtins.str" +reveal_type(z2) # N: Revealed type is "Literal['test']?" x3, x4, y3, y4, z3 = *points, *points2, "test" @@ -1632,7 +1632,7 @@ reveal_type(x3) # N: Revealed type is "builtins.int" reveal_type(x4) # N: Revealed type is "builtins.str" reveal_type(y3) # N: Revealed type is "builtins.int" reveal_type(y4) # N: Revealed type is "builtins.int" -reveal_type(z3) # N: Revealed type is "builtins.str" +reveal_type(z3) # N: Revealed type is "Literal['test']?" x5, x6, y5, y6, z4 = *points2, *points2, "test" @@ -1640,7 +1640,7 @@ reveal_type(x5) # N: Revealed type is "builtins.int" reveal_type(x6) # N: Revealed type is "builtins.int" reveal_type(y5) # N: Revealed type is "builtins.int" reveal_type(y6) # N: Revealed type is "builtins.int" -reveal_type(z4) # N: Revealed type is "builtins.str" +reveal_type(z4) # N: Revealed type is "Literal['test']?" points3 = ["test1", "test2"] x7, x8, y7, y8 = *points2, *points3 # E: Contiguous iterable with same type expected diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index baea0e9ac..34bd18093 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -53,7 +53,7 @@ p = Point(42, 1337) # E: Expected keyword arguments, {...}, or dict(...) in Typ [case testCannotCreateTypedDictInstanceNonLiteralItemName] from mypy_extensions import TypedDict Point = TypedDict('Point', {'x': int, 'y': int}) -x = 'x' +x: str p = Point({x: 42, 'y': 1337}) # E: Expected TypedDict key to be string literal [builtins fixtures/dict.pyi] @@ -1649,10 +1649,10 @@ reveal_type(a.setdefault('x', 1)) # N: Revealed type is "builtins.int" reveal_type(a.setdefault('y', [])) # N: Revealed type is "builtins.list[builtins.int]" a.setdefault('y', '') # E: Argument 2 to "setdefault" of "TypedDict" has incompatible type "str"; expected "List[int]" \ # E: Argument 2 to "setdefault" of "TypedDict" has incompatible type "str"; expected "List[int]" -x = '' +x: str a.setdefault(x, 1) # E: Expected TypedDict key to be string literal alias = a.setdefault -alias(x, 1) # E: Argument 1 has incompatible type "str"; expected "Never" +alias(x, 1) # E: Argument 1 to "setdefault" of "TypedDict" has incompatible type "str"; expected "Never" a.update({}) a.update({'x': 1}) @@ -1681,11 +1681,11 @@ 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]?]]" a.pop('invalid', '') # E: TypedDict "A" has no key "invalid" b.pop('x') # E: Key "x" of TypedDict "B" cannot be deleted -x = '' +x: str b.pop(x) # E: Expected TypedDict key to be string literal pop = b.pop -pop('x') # E: Argument 1 has incompatible type "str"; expected "Never" -pop('invalid') # E: Argument 1 has incompatible type "str"; expected "Never" +pop('x') # E: Argument 1 to "pop" of "TypedDict" has incompatible type "str"; expected "Never" +pop('invalid') # E: Argument 1 to "pop" of "TypedDict" has incompatible type "str"; expected "Never" [builtins fixtures/dict.pyi] [case testTypedDictDel] @@ -1700,7 +1700,7 @@ b: B del a['x'] del a['invalid'] # E: TypedDict "A" has no key "invalid" del b['x'] # E: Key "x" of TypedDict "B" cannot be deleted -s = '' +s: str del a[s] # E: Expected TypedDict key to be string literal del b[s] # E: Expected TypedDict key to be string literal alias = b.__delitem__ diff --git a/test-data/unit/check-typeis.test b/test-data/unit/check-typeis.test index 894700640..0c05881a1 100644 --- a/test-data/unit/check-typeis.test +++ b/test-data/unit/check-typeis.test @@ -605,12 +605,12 @@ def func1(name: object): if guard(name, name): reveal_type(name) # N: Revealed type is "builtins.object" if guard(name, 1): - reveal_type(name) # N: Revealed type is "builtins.int" + reveal_type(name) # N: Revealed type is "Literal[1]?" def func2(name: int): reveal_type(name) # N: Revealed type is "builtins.int" if guard(name, True): - reveal_type(name) # N: Revealed type is "builtins.bool" + reveal_type(name) # N: Revealed type is "Literal[True]?" [builtins fixtures/tuple.pyi] [case testTypeIsWithGenericInstance] diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 25829f89d..88a67fe4d 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -1598,9 +1598,9 @@ def foo(arg: Tuple[int, Unpack[Ts], str]) -> None: reveal_type(y1) # N: Revealed type is "builtins.list[builtins.object]" reveal_type(z1) # N: Revealed type is "builtins.str" x2, *y2, z2 = 1, *arg, 2 - reveal_type(x2) # N: Revealed type is "builtins.int" + reveal_type(x2) # N: Revealed type is "Literal[1]?" reveal_type(y2) # N: Revealed type is "builtins.list[builtins.object]" - reveal_type(z2) # N: Revealed type is "builtins.int" + reveal_type(z2) # N: Revealed type is "Literal[2]?" x3, *y3 = *arg, 42 reveal_type(x3) # N: Revealed type is "builtins.int" reveal_type(y3) # N: Revealed type is "builtins.list[builtins.object]" @@ -1608,11 +1608,11 @@ def foo(arg: Tuple[int, Unpack[Ts], str]) -> None: reveal_type(y4) # N: Revealed type is "builtins.list[builtins.object]" reveal_type(z4) # N: Revealed type is "builtins.str" x5, xx5, *y5, z5, zz5 = 1, *arg, 2 - reveal_type(x5) # N: Revealed type is "builtins.int" + reveal_type(x5) # N: Revealed type is "Literal[1]?" reveal_type(xx5) # N: Revealed type is "builtins.int" reveal_type(y5) # N: Revealed type is "builtins.list[builtins.object]" reveal_type(z5) # N: Revealed type is "builtins.str" - reveal_type(zz5) # N: Revealed type is "builtins.int" + reveal_type(zz5) # N: Revealed type is "Literal[2]?" [builtins fixtures/tuple.pyi] [case testRepackingVariadicTuplesHomogeneous] @@ -1625,9 +1625,9 @@ def foo(arg: Tuple[int, Unpack[Tuple[float, ...]], str]) -> None: reveal_type(y1) # N: Revealed type is "builtins.list[builtins.float]" reveal_type(z1) # N: Revealed type is "builtins.str" x2, *y2, z2 = 1, *arg, 2 - reveal_type(x2) # N: Revealed type is "builtins.int" + reveal_type(x2) # N: Revealed type is "Literal[1]?" reveal_type(y2) # N: Revealed type is "builtins.list[builtins.object]" - reveal_type(z2) # N: Revealed type is "builtins.int" + reveal_type(z2) # N: Revealed type is "Literal[2]?" x3, *y3 = *arg, 42 reveal_type(x3) # N: Revealed type is "builtins.int" reveal_type(y3) # N: Revealed type is "builtins.list[builtins.object]" @@ -1635,11 +1635,11 @@ def foo(arg: Tuple[int, Unpack[Tuple[float, ...]], str]) -> None: reveal_type(y4) # N: Revealed type is "builtins.list[builtins.float]" reveal_type(z4) # N: Revealed type is "builtins.str" x5, xx5, *y5, z5, zz5 = 1, *arg, 2 - reveal_type(x5) # N: Revealed type is "builtins.int" + reveal_type(x5) # N: Revealed type is "Literal[1]?" reveal_type(xx5) # N: Revealed type is "builtins.int" reveal_type(y5) # N: Revealed type is "builtins.list[builtins.float]" reveal_type(z5) # N: Revealed type is "builtins.str" - reveal_type(zz5) # N: Revealed type is "builtins.int" + reveal_type(zz5) # N: Revealed type is "Literal[2]?" [builtins fixtures/tuple.pyi] [case testPackingVariadicTuplesTypeVar] @@ -2461,7 +2461,7 @@ Ts = TypeVarTuple("Ts") @cm def test(x: T, *args: Unpack[Ts]) -> Tuple[T, Unpack[Ts]]: ... -reveal_type(test) # N: Revealed type is "def [T, Ts] (builtins.list[T`2], *args: Unpack[Ts`-2]) -> __main__.CM[Tuple[T`2, Unpack[Ts`-2]]]" +reveal_type(test) # N: Revealed type is "def [T, Ts] (builtins.list[T`3], *args: Unpack[Ts`-2]) -> __main__.CM[Tuple[T`3, Unpack[Ts`-2]]]" [builtins fixtures/tuple.pyi] [case testMixingTypeVarTupleAndParamSpec] diff --git a/test-data/unit/check-typevar-values.test b/test-data/unit/check-typevar-values.test index 912361f4a..21500115e 100644 --- a/test-data/unit/check-typevar-values.test +++ b/test-data/unit/check-typevar-values.test @@ -368,7 +368,7 @@ X = TypeVar('X', int, str) class C(Generic[X]): def f(self, x: X) -> None: self.x = 1 - reveal_type(self.x) # N: Revealed type is "builtins.int" + reveal_type(self.x) # N: Revealed type is "Literal[1]?" ci: C[int] cs: C[str] reveal_type(ci.x) # N: Revealed type is "builtins.int" diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index 117dc4fdf..66ed7e0c9 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -287,16 +287,16 @@ reveal_type(u(C(), None)) # N: Revealed type is "__main__.C" reveal_type(u(None, C())) # N: Revealed type is "__main__.C" # Normal instance type and None, simplify -reveal_type(u(1, None)) # N: Revealed type is "builtins.int" -reveal_type(u(None, 1)) # N: Revealed type is "builtins.int" +reveal_type(u(1, None)) # N: Revealed type is "Literal[1]?" +reveal_type(u(None, 1)) # N: Revealed type is "Literal[1]?" # 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 "Union[Literal[1]?, __main__.C]" +reveal_type(u(1, C())) # N: Revealed type is "Union[__main__.C, Literal[1]?]" # 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 "Union[Any, Literal[1]?]" +reveal_type(u(a, 1)) # N: Revealed type is "Union[Literal[1]?, Any]" # Any and base-class-Any, no simplification reveal_type(u(C(), a)) # N: Revealed type is "Union[Any, __main__.C]" @@ -307,8 +307,8 @@ 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 "Union[Literal['']?, Literal[1]?]" +reveal_type(u('', 1)) # N: Revealed type is "Union[Literal[1]?, Literal['']?]" [case testUnionSimplificationWithDuplicateItems] from typing import Any, TypeVar, Union @@ -322,11 +322,11 @@ 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(1, 1, 1)) # N: Revealed type is "Literal[1]?" 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, a, 1)) # N: Revealed type is "Union[Literal[1]?, 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('', 1, 1)) # N: Revealed type is "Union[Literal[1]?, Literal['']?]" [case testUnionAndBinaryOperation] from typing import Union @@ -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 "Union[Literal[1]?, builtins.float]" +reveal_type(u(2.3, 1)) # N: Revealed type is "Union[builtins.float, Literal[1]?]" +reveal_type(u(False, 2.2)) # N: Revealed type is "Union[Literal[False]?, builtins.float]" +reveal_type(u(2.2, False)) # N: Revealed type is "Union[builtins.float, Literal[False]?]" [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 "Union[Literal[1]?, Type[builtins.str]]" +reveal_type(u(1, t_s)) # N: Revealed type is "Union[Type[builtins.str], Literal[1]?]" +reveal_type(u(type, 1)) # N: Revealed type is "Union[Literal[1]?, def (x: builtins.object) -> builtins.type]" +reveal_type(u(1, type)) # N: Revealed type is "Union[def (x: builtins.object) -> builtins.type, Literal[1]?]" +reveal_type(u(t_a, 1)) # N: Revealed type is "Union[Literal[1]?, Type[Any]]" +reveal_type(u(1, t_a)) # N: Revealed type is "Union[Type[Any], Literal[1]?]" +reveal_type(u(t_o, 1)) # N: Revealed type is "Union[Literal[1]?, Type[builtins.object]]" +reveal_type(u(1, t_o)) # N: Revealed type is "Union[Type[builtins.object], Literal[1]?]" [case testSimplifyingUnionWithTypeTypes2] from typing import TypeVar, Union, Type, Any @@ -573,8 +573,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 "Union[Literal[1]?, builtins.int & Literal['a']?, Literal['a']?]" +reveal_type(y) # N: Revealed type is "Union[Literal[1]?, builtins.int & Literal['a']?, Literal['a']?]" [builtins fixtures/tuple.pyi] [case testUnionMultiassignAny] @@ -613,7 +613,7 @@ reveal_type(obj) # N: Revealed type is "Union[__main__.B, __main__.C]" reveal_type(new) # N: Revealed type is "Union[__main__.B, __main__.C]" obj = 1 -reveal_type(obj) # N: Revealed type is "builtins.int" +reveal_type(obj) # N: Revealed type is "Literal[1]?" [builtins fixtures/list.pyi] [case testUnionMultiassignAlreadyDeclared] diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index 80c7605cb..9a7e256fb 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -345,7 +345,7 @@ def foo() -> None: x = '' else: x = 0 - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" [builtins fixtures/ops.pyi] [out] @@ -357,7 +357,7 @@ class C: x = '' else: x = 0 - reveal_type(x) # N: Revealed type is "builtins.str" + reveal_type(x) # N: Revealed type is "Literal['']?" [builtins fixtures/ops.pyi] [out] @@ -428,7 +428,7 @@ if sys.version_info == (3, 11): x = "foo" else: x = 3 -reveal_type(x) # N: Revealed type is "builtins.str" +reveal_type(x) # N: Revealed type is "Literal['foo']?" [builtins fixtures/ops.pyi] [out] @@ -439,7 +439,7 @@ if sys.version_info == (3, 6): x = "foo" else: x = 3 -reveal_type(x) # N: Revealed type is "builtins.int" +reveal_type(x) # N: Revealed type is "Literal[3]?" [builtins fixtures/ops.pyi] [out] @@ -450,7 +450,7 @@ if sys.platform == 'linux': x = "foo" else: x = 3 -reveal_type(x) # N: Revealed type is "builtins.str" +reveal_type(x) # N: Revealed type is "Literal['foo']?" [builtins fixtures/ops.pyi] [out] @@ -461,7 +461,7 @@ if sys.platform == 'linux': x = "foo" else: x = 3 -reveal_type(x) # N: Revealed type is "builtins.int" +reveal_type(x) # N: Revealed type is "Literal[3]?" [builtins fixtures/ops.pyi] [out] @@ -472,7 +472,7 @@ if sys.platform.startswith('win'): x = "foo" else: x = 3 -reveal_type(x) # N: Revealed type is "builtins.str" +reveal_type(x) # N: Revealed type is "Literal['foo']?" [builtins fixtures/ops.pyi] [out] @@ -510,12 +510,12 @@ if PY2 and sys.platform == 'linux': x = 'foo' else: x = 3 -reveal_type(x) # N: Revealed type is "builtins.int" +reveal_type(x) # N: Revealed type is "Literal[3]?" if sys.platform == 'linux' and PY2: y = 'foo' else: y = 3 -reveal_type(y) # N: Revealed type is "builtins.int" +reveal_type(y) # N: Revealed type is "Literal[3]?" [builtins fixtures/ops.pyi] [case testShortCircuitOrWithConditionalAssignment] @@ -528,12 +528,12 @@ if PY2 or sys.platform == 'linux': x = 'foo' else: x = 3 -reveal_type(x) # N: Revealed type is "builtins.str" +reveal_type(x) # N: Revealed type is "Literal['foo']?" if sys.platform == 'linux' or PY2: y = 'foo' else: y = 3 -reveal_type(y) # N: Revealed type is "builtins.str" +reveal_type(y) # N: Revealed type is "Literal['foo']?" [builtins fixtures/ops.pyi] [case testShortCircuitNoEvaluation] diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 2f5b4f72b..0392f9b3b 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -953,7 +953,7 @@ class Person(TypedDict, Generic[T]): value: T def foo(**kwargs: Unpack[Person[T]]) -> T: ... -reveal_type(foo(name="test", value=42)) # N: Revealed type is "builtins.int" +reveal_type(foo(name="test", value=42)) # N: Revealed type is "Literal[42]?" [builtins fixtures/dict.pyi] [case testUnpackKwargsInference] diff --git a/test-data/unit/check-warnings.test b/test-data/unit/check-warnings.test index 45d24ce9c..cfc713e18 100644 --- a/test-data/unit/check-warnings.test +++ b/test-data/unit/check-warnings.test @@ -6,7 +6,7 @@ [case testRedundantCast] # flags: --warn-redundant-casts from typing import cast -a = 1 +a: int b = cast(str, a) c = cast(int, a) [out] diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index bd077d407..a254169d4 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -466,7 +466,7 @@ int_pow.py:7: note: Revealed type is "builtins.float" int_pow.py:8: note: Revealed type is "builtins.float" int_pow.py:9: note: Revealed type is "Any" int_pow.py:10: note: Revealed type is "builtins.int" -int_pow.py:11: note: Revealed type is "Any" +int_pow.py:11: note: Revealed type is "builtins.int" == Return code: 0 [case testDisallowAnyGenericsBuiltinCollectionsPre39] diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index 4e275ba90..42aa1663b 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -10170,7 +10170,7 @@ class C(B): ... [out] == == -main.py:2: note: Revealed type is "builtins.int" +main.py:2: note: Revealed type is "Literal[42]?" [case testBoundGenericMethodParamSpecFine] import main diff --git a/test-data/unit/merge.test b/test-data/unit/merge.test index 29c1ea717..6e676ec78 100644 --- a/test-data/unit/merge.test +++ b/test-data/unit/merge.test @@ -456,14 +456,14 @@ NameExpr:4: target.A<0> IntExpr:5: Literal[1]?<1> MemberExpr:5: builtins.int<1> NameExpr:5: target.A<0> -MemberExpr:6: builtins.int<1> +MemberExpr:6: Literal[1]?<1> NameExpr:6: target.A<0> ==> ## target IntExpr:3: Literal[1]?<1> MemberExpr:3: builtins.int<1> NameExpr:3: target.A<0> -MemberExpr:4: builtins.int<1> +MemberExpr:4: Literal[1]?<1> NameExpr:4: target.A<0> CallExpr:5: target.A<0> MemberExpr:5: target.A<0> diff --git a/test-data/unit/pep561.test b/test-data/unit/pep561.test index cbe2c2a83..ca37e9819 100644 --- a/test-data/unit/pep561.test +++ b/test-data/unit/pep561.test @@ -116,8 +116,8 @@ dne("abc") [out] testTypedPkgNamespaceImportAs.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.a.dne" testTypedPkgNamespaceImportAs.py:4: note: See https://kotlinisland.github.io/basedmypy/running_mypy.html#missing-imports -testTypedPkgNamespaceImportAs.py:10: error: Argument 1 has incompatible type "bool"; expected "str" -testTypedPkgNamespaceImportAs.py:11: error: Argument 1 has incompatible type "int"; expected "bool" +testTypedPkgNamespaceImportAs.py:10: error: Argument 1 to "af" has incompatible type "bool"; expected "str" +testTypedPkgNamespaceImportAs.py:11: error: Argument 1 to "bf" has incompatible type "int"; expected "bool" [case testTypedPkgNamespaceRegImport] # pkgs: typedpkg, typedpkg_ns_a @@ -136,8 +136,8 @@ dne("abc") [out] testTypedPkgNamespaceRegImport.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.a.dne" testTypedPkgNamespaceRegImport.py:4: note: See https://kotlinisland.github.io/basedmypy/running_mypy.html#missing-imports -testTypedPkgNamespaceRegImport.py:10: error: Argument 1 has incompatible type "bool"; expected "str" -testTypedPkgNamespaceRegImport.py:11: error: Argument 1 has incompatible type "int"; expected "bool" +testTypedPkgNamespaceRegImport.py:10: error: Argument 1 to "af" has incompatible type "bool"; expected "str" +testTypedPkgNamespaceRegImport.py:11: error: Argument 1 to "bf" has incompatible type "int"; expected "bool" -- This is really testing the test framework to make sure incremental works [case testPep561TestIncremental] diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 9f3ad94f5..38adf952d 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -249,7 +249,7 @@ _program.py:4: error: "TextIOWrapper[_WrappedBuffer]" has no attribute "foobar" reveal_type(open('x')) reveal_type(open('x', 'r')) reveal_type(open('x', 'rb')) -mode = 'rb' +mode = str() reveal_type(open('x', mode)) [out] _program.py:1: note: Revealed type is "_io.TextIOWrapper[_io._WrappedBuffer]" @@ -260,7 +260,7 @@ _program.py:5: note: Revealed type is "typing.IO[Any]" [case testOpenReturnTypeInferenceSpecialCases] reveal_type(open(mode='rb', file='x')) reveal_type(open(file='x', mode='rb')) -mode = 'rb' +mode = str() reveal_type(open(mode=mode, file='r')) [out] _testOpenReturnTypeInferenceSpecialCases.py:1: note: Revealed type is "_io.BufferedReader" @@ -273,7 +273,7 @@ p = Path("x") reveal_type(p.open()) reveal_type(p.open('r')) reveal_type(p.open('rb')) -mode = 'rb' +mode = str() reveal_type(p.open(mode)) [out] _program.py:3: note: Revealed type is "_io.TextIOWrapper[_io._WrappedBuffer]" @@ -286,7 +286,7 @@ from pathlib import Path p = Path("x") reveal_type(p.open(mode='r', errors='replace')) reveal_type(p.open(errors='replace', mode='r')) -mode = 'r' +mode = str() reveal_type(p.open(mode=mode, errors='replace')) [out] _program.py:3: note: Revealed type is "_io.TextIOWrapper[_io._WrappedBuffer]" @@ -1166,8 +1166,8 @@ for a, b in x.items(): reveal_type(a) reveal_type(b) [out] -_testNoCrashOnGenericUnionUnpacking.py:6: note: Revealed type is "builtins.str" -_testNoCrashOnGenericUnionUnpacking.py:7: note: Revealed type is "builtins.str" +_testNoCrashOnGenericUnionUnpacking.py:6: note: Revealed type is "Union[builtins.str, Literal[1]?]" +_testNoCrashOnGenericUnionUnpacking.py:7: note: Revealed type is "Union[builtins.str, Literal[2]?]" _testNoCrashOnGenericUnionUnpacking.py:10: note: Revealed type is "Union[builtins.str, builtins.int]" _testNoCrashOnGenericUnionUnpacking.py:11: note: Revealed type is "Union[builtins.str, builtins.int]" _testNoCrashOnGenericUnionUnpacking.py:17: note: Revealed type is "Union[builtins.int, builtins.str]" diff --git a/test-data/unit/typexport-basic.test b/test-data/unit/typexport-basic.test index bcaa71469..32ebcb806 100644 --- a/test-data/unit/typexport-basic.test +++ b/test-data/unit/typexport-basic.test @@ -188,8 +188,8 @@ a or a not a [builtins fixtures/bool.pyi] [out] -OpExpr(4) : builtins.int -OpExpr(5) : builtins.int +OpExpr(4) : Union[Literal[0], Literal[1]?] +OpExpr(5) : Union[Literal[1]?, Literal[0]] UnaryExpr(6) : builtins.bool [case testBooleanOpsOnBools]