8000 Add support for additional TypedDict methods by JukkaL · Pull Request #6011 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Add support for additional TypedDict methods #6011

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 25 commits into from
Dec 6, 2018
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
Fix anonymous TypedDict types
  • Loading branch information
JukkaL committed Dec 4, 2018
commit 50fc0db28853553bfb69922a75686138823b05d7
14 changes: 4 additions & 10 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ def deserialize(cls, data: JsonDict) -> 'TypedDictType':
Instance.deserialize(data['fallback']))

def is_anonymous(self) -> bool:
return self.fallback.type.fullname() == 'typing.Mapping'
return self.fallback.type.fullname() == 'mypy_extensions._TypedDict'

def as_anonymous(self) -> 'TypedDictType':
if self.is_anonymous():
Expand All @@ -1258,10 +1258,7 @@ def copy_modified(self, *, fallback: Optional[Instance] = None,

def create_anonymous_fallback(self, *, value_type: Type) -> Instance:
anonymous = self.as_anonymous()
return anonymous.fallback.copy_modified(args=[ # i.e. Mapping
anonymous.fallback.args[0], # i.e. str
value_type
])
return anonymous.fallback

def names_are_wider_than(self, other: 'TypedDictType') -> bool:
return len(other.items.keys() - self.items.keys()) == 0
Expand Down Expand Up @@ -1830,13 +1827,10 @@ def item_str(name: str, typ: str) -> str:
s = '{' + ', '.join(item_str(name, typ.accept(self))
for name, typ in t.items.items()) + '}'
prefix = ''
suffix = ''
if t.fallback and t.fallback.type:
if t.fallback.type.fullname() != 'typing.Mapping':
if t.fallback.type.fullname() != 'mypy_extensions._TypedDict':
prefix = repr(t.fallback.type.fullname()) + ', '
else:
suffix = ', fallback={}'.format(t.fallback.accept(self))
return 'TypedDict({}{}{})'.format(prefix, s, suffix)
return 'TypedDict({}{})'.format(prefix, s)

def visit_raw_literal_type(self, t: RawLiteralType) -> str:
return repr(t.value)
Expand Down
24 changes: 12 additions & 12 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ p2 = Point3D(x=1, y=1, z=1)
joined_points = [p1, p2][0]
reveal_type(p1.values()) # E: Revealed type is 'typing.Iterable[builtins.object*]'
reveal_type(p2.values()) # E: Revealed type is 'typing.Iterable[builtins.object*]'
reveal_type(joined_points) # E: Revealed type is 'TypedDict({'x': builtins.int, 'y': builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
reveal_type(joined_points) # E: Revealed type is 'TypedDict({'x': builtins.int, 'y': builtins.int})'
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

Expand All @@ -448,7 +448,7 @@ c2 = CellWithObject(value=2, meta='turtle doves')
joined_cells = [c1, c2]
reveal_type(c1) # E: Revealed type is 'TypedDict('__main__.CellWithInt', {'value': builtins.object, 'meta': builtins.int})'
reveal_type(c2) # E: Revealed type is 'TypedDict('__main__.CellWithObject', {'value': builtins.object, 'meta': builtins.object})'
reveal_type(joined_cells) # E: Revealed type is 'builtins.list[TypedDict({'value': builtins.object}, fallback=typing.Mapping[builtins.str, builtins.object])]'
reveal_type(joined_cells) # E: Revealed type is 'builtins.list[TypedDict({'value': builtins.object})]'
[builtins fixtures/dict.pyi]

[case testJoinOfDisjointTypedDictsIsEmptyTypedDict]
Expand All @@ -460,7 +460,7 @@ d2 = Cell(value='pear tree')
joined_dicts = [d1, d2]
reveal_type(d1) # E: Revealed type is 'TypedDict('__main__.Point', {'x': builtins.int, 'y': builtins.int})'
reveal_type(d2) # E: Revealed type is 'TypedDict('__main__.Cell', {'value': builtins.object})'
reveal_type(joined_dicts) # E: Revealed type is 'builtins.list[TypedDict({}, fallback=typing.Mapping[builtins.str, <nothing>])]'
reveal_type(joined_dicts) # E: Revealed type is 'builtins.list[TypedDict({})]'
[builtins fixtures/dict.pyi]

[case testJoinOfTypedDictWithCompatibleMappingIsMapping]
Expand Down Expand Up @@ -511,7 +511,7 @@ YZ = TypedDict('YZ', {'y': int, 'z': int})
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass
def g(x: XY, y: YZ) -> None: pass
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x': builtins.int, 'y': builtins.int, 'z': builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x': builtins.int, 'y': builtins.int, 'z': builtins.int})'
[builtins fixtures/dict.pyi]

[case testMeetOfTypedDictsWithIncompatibleCommonKeysIsUninhabited]
Expand All @@ -534,7 +534,7 @@ Z = TypedDict('Z', {'z': int})
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass
def g(x: X, y: Z) -> None: pass
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x': builtins.int, 'z': builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x': builtins.int, 'z': builtins.int})'
[builtins fixtures/dict.pyi]

# TODO: It would be more accurate for the meet to be TypedDict instead.
Expand Down Expand Up @@ -583,7 +583,7 @@ YZ = TypedDict('YZ', {'y': int, 'z': int}, total=False)
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass
def g(x: XY, y: YZ) -> None: pass
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x'?: builtins.int, 'y'?: builtins.int, 'z'?: builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x'?: builtins.int, 'y'?: builtins.int, 'z'?: builtins.int})'
[builtins fixtures/dict.pyi]

[case testMeetOfTypedDictsWithNonTotalAndTotal]
Expand All @@ -594,7 +594,7 @@ YZ = TypedDict('YZ', {'y': int, 'z': int})
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass
def g(x: XY, y: YZ) -> None: pass
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x'?: builtins.int, 'y': builtins.int, 'z': builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
reveal_type(f(g)) # E: Revealed type is 'TypedDict({'x'?: builtins.int, 'y': builtins.int, 'z': builtins.int})'
[builtins fixtures/dict.pyi]

[case testMeetOfTypedDictsWithIncompatibleNonTotalAndTotal]
Expand Down Expand Up @@ -1037,15 +1037,15 @@ a: A
b: B
c: C
reveal_type(j(a, b)) \
# E: Revealed type is 'TypedDict({}, fallback=typing.Mapping[builtins.str, <nothing>])'
# E: Revealed type is 'TypedDict({})'
reveal_type(j(b, b)) \
# E: Revealed type is 'TypedDict({'x'?: builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
# E: Revealed type is 'TypedDict({'x'?: builtins.int})'
reveal_type(j(c, c)) \
# E: Revealed type is 'TypedDict({'x'?: builtins.int, 'y'?: builtins.str}, fallback=typing.Mapping[builtins.str, builtins.object])'
# E: Revealed type is 'TypedDict({'x'?: builtins.int, 'y'?: builtins.str})'
reveal_type(j(b, c)) \
# E: Revealed type is 'TypedDict({'x'?: builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
# E: Revealed type is 'TypedDict({'x'?: builtins.int})'
reveal_type(j(c, b)) \
# E: Revealed type is 'TypedDict({'x'?: builtins.int}, fallback=typing.Mapping[builtins.str, builtins.int])'
# E: Revealed type is 'TypedDict({'x'?: builtins.int})'
[builtins fixtures/dict.pyi]

[case testTypedDictClassWithTotalArgument]
Expand Down
0