Skip to content

Commit 8ef8b17

Browse files
committed
Revert "Play with opposite order for funcitn args"
This reverts commit bd2f420.
1 parent bd2f420 commit 8ef8b17

3 files changed

Lines changed: 14 additions & 30 deletions

File tree

mypy/checker.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

test-data/unit/check-redefine2.test

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,28 +1339,3 @@ def process3(items: list[str]) -> None:
13391339
reveal_type(items) # N: Revealed type is "builtins.list[builtins.str]"
13401340
reveal_type(items) # N: Revealed type is "builtins.list[builtins.str]"
13411341
[builtins fixtures/primitives.pyi]
1342-
1343-
[case testNewRedefineHasAttr]
1344-
# flags: --allow-redefinition-new --local-partial-types
1345-
1346-
def test(lst: list[object]) -> None:
1347-
for cls in lst:
1348-
if not hasattr(cls, "module"):
1349-
break
1350-
reveal_type(cls.module) # N: Revealed type is "Any"
1351-
1352-
[case testNewRedefineOldAnySpecialCasing]
1353-
# flags: --allow-redefinition-new --local-partial-types
1354-
from typing import Any
1355-
a: Any
1356-
x = 1
1357-
if bool():
1358-
x = a
1359-
reveal_type(x) # N: Revealed type is "builtins.int"
1360-
1361-
if bool():
1362-
y = a
1363-
else:
1364-
y = 1
1365-
reveal_type(y) # N: Revealed type is "Any"
1366-
[builtins fixtures/isinstancelist.pyi]

test-data/unit/fixtures/isinstancelist.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Ellipsis = ellipsis()
1818

1919
def isinstance(x: object, t: Union[type, Tuple]) -> bool: pass
2020
def issubclass(x: object, t: Union[type, Tuple]) -> bool: pass
21-
def hasattr(x: object, name: str) -> bool: pass
2221

2322
class int:
2423
def __add__(self, x: int) -> int: pass

0 commit comments

Comments
 (0)