File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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"
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ Ellipsis = ellipsis()
1818
1919def isinstance (x : object , t : Union [type , Tuple ]) -> bool : pass
2020def issubclass (x : object , t : Union [type , Tuple ]) -> bool : pass
21+ def hasattr (x : object , name : str ) -> bool : pass
2122
2223class int :
2324 def __add__ (self , x : int ) -> int : pass
You can’t perform that action at this time.
0 commit comments