Skip to content

Commit d72d98d

Browse files
committed
Add test case for another edge case
1 parent 8ef8b17 commit d72d98d

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

mypy/checker.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,7 +4720,7 @@ def infer_rvalue_with_fallback_context(
47204720
# and use it results in a narrower type. This helps with various practical
47214721
# examples, see e.g. testOptionalTypeNarrowedByGenericCall.
47224722
union_fallback = (
4723-
inferred is None
4723+
preferred_context is not None
47244724
and isinstance(get_proper_type(lvalue_type), UnionType)
47254725
and binder_version == self.binder.version
47264726
)
@@ -4739,9 +4739,8 @@ def infer_rvalue_with_fallback_context(
47394739
not alt_local_errors.has_new_errors()
47404740
and is_valid_inferred_type(alt_rvalue_type, self.options)
47414741
and (
4742-
# For redefinition fallback we are fine getting not a subtype.
4743-
redefinition_fallback
4744-
or argument_redefinition_fallback
4742+
# For redefinition fallbacks we are fine getting not a subtype.
4743+
inferred is not None
47454744
# Skip Any type, since it is special cased in binder.
47464745
or not isinstance(get_proper_type(alt_rvalue_type), AnyType)
47474746
and is_proper_subtype(alt_rvalue_type, rvalue_type)

test-data/unit/check-redefine2.test

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,3 +1339,42 @@ 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+
[builtins fixtures/isinstancelist.pyi]
1352+
1353+
[case testNewRedefineOldAnySpecialCasing]
1354+
# flags: --allow-redefinition-new --local-partial-types
1355+
from typing import Any
1356+
1357+
def test() -> None:
1358+
a: Any
1359+
x = 1
1360+
if bool():
1361+
x = a
1362+
reveal_type(x) # N: Revealed type is "builtins.int"
1363+
1364+
if bool():
1365+
y = a
1366+
else:
1367+
y = 1
1368+
reveal_type(y) # N: Revealed type is "Any"
1369+
1370+
[case testNewRedefineUnionArgumentFallback]
1371+
# flags: --allow-redefinition-new --local-partial-types
1372+
from typing import Optional, TypeVar
1373+
1374+
T = TypeVar("T")
1375+
async def gen(x: T) -> T: ...
1376+
1377+
async def test(x: Optional[str]) -> None:
1378+
if x is None:
1379+
x = await gen("foo")
1380+
reveal_type(x) # N: Revealed type is "builtins.str"

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)