@@ -4706,8 +4706,15 @@ def infer_rvalue_with_fallback_context(
47064706 # There are two cases where we want to try re-inferring r.h.s. in a fallback
47074707 # type context. First case is when redefinitions are allowed, and we got
47084708 # invalid type when using the preferred (empty) type context.
4709- redefinition_fallback = inferred is not None and not is_valid_inferred_type (
4710- rvalue_type , self .options
4709+ redefinition_fallback = (
4710+ inferred is not None
4711+ and not inferred .is_argument
4712+ and not is_valid_inferred_type (rvalue_type , self .options )
4713+ )
4714+ # For function arguments the preference order is opposite, and we use errors
4715+ # during type-checking as the fallback trigger.
4716+ argument_redefinition_fallback = (
4717+ inferred is not None and inferred .is_argument and local_errors .has_new_errors ()
47114718 )
47124719 # Try re-inferring r.h.s. in empty context for union with explicit annotation,
47134720 # and use it results in a narrower type. This helps with various practical
@@ -4719,7 +4726,7 @@ def infer_rvalue_with_fallback_context(
47194726 )
47204727
47214728 # Skip literal types, as they have special logic (for better errors).
4722- try_fallback = redefinition_fallback or union_fallback
4729+ try_fallback = redefinition_fallback or union_fallback or argument_redefinition_fallback
47234730 if try_fallback and not is_literal_type_like (rvalue_type ):
47244731 with (
47254732 self .msg .filter_errors (save_filtered_errors = True ) as alt_local_errors ,
@@ -4734,6 +4741,7 @@ def infer_rvalue_with_fallback_context(
47344741 and (
47354742 # For redefinition fallback we are fine getting not a subtype.
47364743 redefinition_fallback
4744+ or argument_redefinition_fallback
47374745 # Skip Any type, since it is special cased in binder.
47384746 or not isinstance (get_proper_type (alt_rvalue_type ), AnyType )
47394747 and is_proper_subtype (alt_rvalue_type , rvalue_type )
@@ -4782,7 +4790,9 @@ def check_simple_assignment(
47824790 rvalue , type_context = lvalue_type , always_allow_any = always_allow_any
47834791 )
47844792 else :
4785- if inferred is not None :
4793+ # Prefer full type context for function arguments as this reduces
4794+ # false positives, see issue #19918 for discussion.
4795+ if inferred is not None and not inferred .is_argument :
47864796 preferred = None
47874797 fallback = lvalue_type
47884798 else :
0 commit comments