diff --git a/mypy/checker.py b/mypy/checker.py index b1c15d20c329..a92b43ebebf9 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3710,6 +3710,10 @@ def try_infer_partial_generic_type_from_assignment( return rvalue_type = self.expr_checker.accept(rvalue) rvalue_type = get_proper_type(rvalue_type) + if var not in partial_types: + # Evaluating rvalue may have already resolved this partial type + # (e.g. a walrus assignment to the same variable inside rvalue). + return if isinstance(rvalue_type, Instance): if rvalue_type.type == typ.type and is_valid_inferred_type( rvalue_type, self.options diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index d2ede55840cf..a83b1a7d312f 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -455,6 +455,15 @@ def check_partial_list() -> None: reveal_type(z) # N: Revealed type is "builtins.list[builtins.int]" [builtins fixtures/list.pyi] +[case testWalrusPartialTypeShadowedInComprehension] +# https://github.com/python/mypy/issues/20792 +errors = [] +errors = [ + i for i in [1, 2, 3] if (errors := [1]) +] +reveal_type(errors) # N: Revealed type is "builtins.list[builtins.int]" +[builtins fixtures/list.pyi] + [case testWalrusAssignmentAndConditionScopeForLiteral] # flags: --warn-unreachable