Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Report errors for explicit Any in TypedDict declaration
  • Loading branch information
ilinum committed Jun 21, 2017
commit e3cd6ebf17bab68caf28998d3011e686cea48f03
5 changes: 5 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,6 +2383,11 @@ def parse_typeddict_args(self, call: CallExpr,
"TypedDict() expects a dictionary literal as the second argument", call)
dictexpr = args[1]
items, types, ok = self.parse_typeddict_fields_with_types(dictexpr.items, call)
if ('explicit' in self.options.disallow_any and
not self.is_typeshed_stub_file and
any(has_explicit_any(t) for t in types)):
self.msg.explicit_any("in TypedDict declaration", call)

return items, types, ok

def parse_typeddict_fields_with_types(self, dict_items: List[Tuple[Expression, Expression]],
Expand Down
4 changes: 4 additions & 0 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,10 @@ def __init__(self) -> None:
def visit_any(self, t: AnyType) -> bool:
return t.explicit

def visit_typeddict_type(self, t: TypedDictType) -> bool:
# typedict is checked during TypedDict declaration
return False


def has_any_from_unimported_type(t: Type) -> bool:
"""Return true if this type is Any because an import was not followed.
Expand Down
35 changes: 35 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,38 @@ Bar = NewType('Bar', List[Any])

[out]
m.py:5: error: Explicit "Any" is not allowed in a NewType declaration

[case testDisallowAnyExplicitTypedDictSimple]
# cmd: mypy m.py

[file mypy.ini]
[[mypy]
[[mypy-m*]
disallow_any = explicit

[file m.py]
from mypy_extensions import TypedDict
from typing import Any

M = TypedDict('M', {'x': str, 'y': Any}) # error
M(x='x', y=2) # no error
def f(m: M) -> None: pass # no error
[out]
m.py:4: error: Explicit "Any" is not allowed in a in TypedDict declaration

[case testDisallowAnyExplicitTypedDictGeneric]
# cmd: mypy m.py

[file mypy.ini]
[[mypy]
[[mypy-m*]
disallow_any = explicit

[file m.py]
from mypy_extensions import TypedDict
from typing import Any, List

M = TypedDict('M', {'x': str, 'y': List[Any]}) # error
N = TypedDict('N', {'x': str, 'y': List}) # no error
[out]
m.py:4: error: Explicit "Any" is not allowed in a in TypedDict declaration