Skip to content

Commit e0281be

Browse files
authored
Infer type from "x.extend(y)" where y has type Any (etc.) (#8169)
Previously we inferred a type from an argument like `List[Any]` but not from plain `Any`, which was inconsistent.
1 parent 0932261 commit e0281be

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

mypy/checkexpr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ def try_infer_partial_value_type_from_call(
651651
for item_type in arg_type.args):
652652
return self.chk.named_generic_type(typename,
653653
list(arg_type.args))
654+
elif isinstance(arg_type, AnyType):
655+
return self.chk.named_type(typename)
656+
654657
return None
655658

656659
def apply_function_plugin(self,

test-data/unit/check-inference.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,21 @@ class C:
17231723
self.a = 1
17241724
reveal_type(C().a) # N: Revealed type is 'Union[builtins.int, None]'
17251725

1726+
[case testInferListTypeFromEmptyListAndAny]
1727+
def f():
1728+
return []
1729+
1730+
def g() -> None:
1731+
x = []
1732+
if bool():
1733+
x = f()
1734+
reveal_type(x) # N: Revealed type is 'builtins.list[Any]'
1735+
1736+
y = []
1737+
y.extend(f())
1738+
reveal_type(y) # N: Revealed type is 'builtins.list[Any]'
1739+
[builtins fixtures/list.pyi]
1740+
17261741

17271742
-- Inferring types of variables first initialized to None (partial types)
17281743
-- ----------------------------------------------------------------------

0 commit comments

Comments
 (0)