From 4c9c0abfd4d86fa6e57004934f817a4f70ad37c5 Mon Sep 17 00:00:00 2001 From: Shantanu Jain Date: Wed, 13 Nov 2024 19:07:15 -0500 Subject: [PATCH 1/2] narrow on initial assignment --- mypy/checker.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 47b08b683e36..3b3cd81c4285 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3017,7 +3017,9 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None: # as X | Y. if not (s.is_alias_def and self.is_stub): with self.enter_final_context(s.is_final_def): - self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax) + self.check_assignment( + s.lvalues[-1], s.rvalue, infer_lvalue_type=True, new_syntax=s.new_syntax + ) if s.is_alias_def: self.check_type_alias_rvalue(s) @@ -3045,7 +3047,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None: rvalue = self.temp_node(self.lookup_type(s.rvalue), s) for lv in s.lvalues[:-1]: with self.enter_final_context(s.is_final_def): - self.check_assignment(lv, rvalue, s.type is None) + self.check_assignment(lv, rvalue, infer_lvalue_type=True) self.check_final(s) if ( From 503a96858f6ee775e12443a6a706933d0896504b Mon Sep 17 00:00:00 2001 From: Shantanu Jain Date: Sat, 18 Jan 2025 16:20:22 -0800 Subject: [PATCH 2/2] fixes --- mypy/plugins/attrs.py | 1 - mypy/semanal.py | 2 +- mypy/types.py | 9 ++------- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py index 0c29d992c22e..d434fb388943 100644 --- a/mypy/plugins/attrs.py +++ b/mypy/plugins/attrs.py @@ -764,7 +764,6 @@ def _parse_converter( converter_info.init_type = AnyType(TypeOfAny.from_error) return converter_info - converter_type = get_proper_type(converter_type) if isinstance(converter_type, CallableType) and converter_type.arg_types: converter_info.init_type = converter_type.arg_types[0] if not is_attr_converters_optional: diff --git a/mypy/semanal.py b/mypy/semanal.py index 034d8fb28b42..0713a7f68428 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -6765,7 +6765,7 @@ def _get_node_for_class_scoped_import( # mypyc is absolutely convinced that `symbol_node` narrows to a Var in the following, # when it can also be a FuncBase. Once fixed, `f` in the following can be removed. # See also https://github.com/mypyc/mypyc/issues/892 - f: Callable[[object], Any] = lambda x: x + f = cast(Callable[[object], Any], lambda x: x) if isinstance(f(symbol_node), (Decorator, FuncBase, Var)): # For imports in class scope, we construct a new node to represent the symbol and # set its `info` attribute to `self.type`. diff --git a/mypy/types.py b/mypy/types.py index f3745695889f..82a9d87cabfa 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -2179,7 +2179,8 @@ def with_normalized_var_args(self) -> Self: types_suffix = self.arg_types[var_arg_index + 1 :] kinds_suffix = self.arg_kinds[var_arg_index + 1 :] names_suffix = self.arg_names[var_arg_index + 1 :] - no_name: str | None = None # to silence mypy + names_middle: list[str | None] + no_name = cast("str | None", None) # Now we have something non-trivial to do. if unpack_index is None: @@ -3725,12 +3726,6 @@ def find_unpack_in_list(items: Sequence[Type]) -> int | None: unpack_index: int | None = None for i, item in enumerate(items): if isinstance(item, UnpackType): - # We cannot fail here, so we must check this in an earlier - # semanal phase. - # Funky code here avoids mypyc narrowing the type of unpack_index. - old_index = unpack_index - assert old_index is None - # Don't return so that we can also sanity check there is only one. unpack_index = i return unpack_index