Skip to content

Commit bd2f420

Browse files
committed
Play with opposite order for funcitn args
1 parent 8372d52 commit bd2f420

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

mypy/checker.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4706,15 +4706,8 @@ 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 = (
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()
4709+
redefinition_fallback = inferred is not None and not is_valid_inferred_type(
4710+
rvalue_type, self.options
47184711
)
47194712
# Try re-inferring r.h.s. in empty context for union with explicit annotation,
47204713
# and use it results in a narrower type. This helps with various practical
@@ -4726,7 +4719,7 @@ def infer_rvalue_with_fallback_context(
47264719
)
47274720

47284721
# Skip literal types, as they have special logic (for better errors).
4729-
try_fallback = redefinition_fallback or union_fallback or argument_redefinition_fallback
4722+
try_fallback = redefinition_fallback or union_fallback
47304723
if try_fallback and not is_literal_type_like(rvalue_type):
47314724
with (
47324725
self.msg.filter_errors(save_filtered_errors=True) as alt_local_errors,
@@ -4741,7 +4734,6 @@ def infer_rvalue_with_fallback_context(
47414734
and (
47424735
# For redefinition fallback we are fine getting not a subtype.
47434736
redefinition_fallback
4744-
or argument_redefinition_fallback
47454737
# Skip Any type, since it is special cased in binder.
47464738
or not isinstance(get_proper_type(alt_rvalue_type), AnyType)
47474739
and is_proper_subtype(alt_rvalue_type, rvalue_type)
@@ -4790,9 +4782,7 @@ def check_simple_assignment(
47904782
rvalue, type_context=lvalue_type, always_allow_any=always_allow_any
47914783
)
47924784
else:
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:
4785+
if inferred is not None:
47964786
preferred = None
47974787
fallback = lvalue_type
47984788
else:

test-data/unit/check-redefine2.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,3 +1339,28 @@ 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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
2122

2223
class int:
2324
def __add__(self, x: int) -> int: pass

0 commit comments

Comments
 (0)