Skip to content

Allow TypedDict field names starting with underscore #2839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 0 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,9 +1100,6 @@ def check_typeddict_classdef(self, defn: ClassDef,
fields.append(name)
types.append(AnyType() if stmt.type is None else self.anal_type(stmt.type))
# ...despite possible minor failures that allow further analyzis.
if name.startswith('_'):
self.fail('TypedDict field name cannot start with an underscore: {}'
.format(name), stmt)
if stmt.type is None or hasattr(stmt, 'new_syntax') and not stmt.new_syntax:
self.fail(TPDICT_CLASS_ERROR, stmt)
elif not isinstance(stmt.rvalue, TempNode):
Expand Down Expand Up @@ -2118,10 +2115,6 @@ 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)
underscore = [item for item in items if item.startswith('_')]
if underscore:
self.fail("TypedDict() item names cannot start with an underscore: "
+ ', '.join(underscore), call)
return items, types, ok

def parse_typeddict_fields_with_types(self, dict_items: List[Tuple[Expression, Expression]],
Expand Down
9 changes: 7 additions & 2 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,18 @@ p = Point(x=42, y=1337, z='whatever')
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, z=builtins.str, _fallback=typing.Mapping[builtins.str, builtins.object])'
[builtins fixtures/dict.pyi]

[case testCannotCreateTypedDictWithClassUnderscores]
[case testCanCreateTypedDictTypeWithUnderscoreItemName]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int, '_fallback': object})
[builtins fixtures/dict.pyi]

[case testCanCreateTypedDictWithClassUnderscores]
# flags: --python-version 3.6
from mypy_extensions import TypedDict

class Point(TypedDict):
x: int
_y: int # E: TypedDict field name cannot start with an underscore: _y
_y: int

p: Point
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, _y=builtins.int, _fallback=__main__.Point)'
Expand Down
5 changes: 0 additions & 5 deletions test-data/unit/semanal-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x'}) # E: TypedDict() expects a dictionary literal as the second argument
[builtins fixtures/dict.pyi]

[case testCannotCreateTypedDictTypeWithUnderscoreItemName]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int, '_fallback': object}) # E: TypedDict() item names cannot start with an underscore: _fallback
[builtins fixtures/dict.pyi]

-- NOTE: The following code works at runtime but is not yet supported by mypy.
-- Keyword arguments may potentially be supported in the future.
[case testCannotCreateTypedDictTypeWithNonpositionalArgs]
Expand Down