Skip to content

Commit 1423a54

Browse files
committed
When positional args passed to dict(), analyze them. Fix #1357. (#1359)
1 parent 748c4bb commit 1423a54

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

mypy/semanal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,9 @@ def translate_dict_call(self, call: CallExpr) -> Optional[DictExpr]:
18111811
if not call.args:
18121812
return None
18131813
if not all(kind == ARG_NAMED for kind in call.arg_kinds):
1814+
# Must still accept those args.
1815+
for a in call.args:
1816+
a.accept(self)
18141817
return None
18151818
expr = DictExpr([(StrExpr(key), value)
18161819
for key, value in zip(call.arg_names, call.args)])

mypy/test/data/check-expressions.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,3 +1376,9 @@ d4 = dict(a=1, b=1)
13761376
d4.xyz # E: Dict[str, int] has no attribute "xyz"
13771377
d5 = dict(a=1, b='') # type: Dict[str, Any]
13781378
[builtins fixtures/dict.py]
1379+
1380+
[case testDictWithoutKeywordArgs]
1381+
dict(undefined)
1382+
[builtins fixtures/dict.py]
1383+
[out]
1384+
main:1: error: Name 'undefined' is not defined

mypy/test/data/fixtures/dict.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Builtins stub used in dictionary-related test cases.
22

3-
from typing import TypeVar, Generic, Iterable, Iterator
3+
from typing import TypeVar, Generic, Iterable, Iterator, Any
44

55
T = TypeVar('T')
66
S = TypeVar('S')
@@ -11,6 +11,7 @@ def __init__(self) -> None: pass
1111
class type: pass
1212

1313
class dict(Generic[T, S]):
14+
def __init__(self, arg: Any = None) -> None: pass
1415
def __setitem__(self, k: T, v: S) -> None: pass
1516
def update(self, a: 'dict[T, S]') -> None: pass
1617
class int: pass # for convenience

0 commit comments

Comments
 (0)