Skip to content

Commit dd16ba3

Browse files
authored
TypedDict key (#10352)
1 parent 8e79a69 commit dd16ba3

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

mypy/messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,11 +2121,11 @@ def make_inferred_type_note(context: Context,
21212121

21222122

21232123
def format_key_list(keys: List[str], *, short: bool = False) -> str:
2124-
reprs = [repr(key) for key in keys]
2124+
formatted_keys = ['"{}"'.format(key) for key in keys]
21252125
td = '' if short else 'TypedDict '
21262126
if len(keys) == 0:
21272127
return 'no {}keys'.format(td)
21282128
elif len(keys) == 1:
2129-
return '{}key {}'.format(td, reprs[0])
2129+
return '{}key {}'.format(td, formatted_keys[0])
21302130
else:
2131-
return '{}keys ({})'.format(td, ', '.join(reprs))
2131+
return '{}keys ({})'.format(td, ', '.join(formatted_keys))

test-data/unit/check-errorcodes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ class E(TypedDict):
454454
y: int
455455

456456
a: D = {'x': ''} # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int") [typeddict-item]
457-
b: D = {'y': ''} # E: Extra key 'y' for TypedDict "D" [typeddict-item]
457+
b: D = {'y': ''} # E: Extra key "y" for TypedDict "D" [typeddict-item]
458458
c = D(x=0) if int() else E(x=0, y=0)
459-
c = {} # E: Expected TypedDict key 'x' but found no keys [typeddict-item]
459+
c = {} # E: Expected TypedDict key "x" but found no keys [typeddict-item]
460460
[builtins fixtures/dict.pyi]
461461

462462
[case testErrorCodeCannotDetermineType]

test-data/unit/check-typeddict.test

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ p = Point({x: 42, 'y': 1337}) # E: Expected TypedDict key to be string literal
6060
[case testCannotCreateTypedDictInstanceWithExtraItems]
6161
from mypy_extensions import TypedDict
6262
Point = TypedDict('Point', {'x': int, 'y': int})
63-
p = Point(x=42, y=1337, z=666) # E: Extra key 'z' for TypedDict "Point"
63+
p = Point(x=42, y=1337, z=666) # E: Extra key "z" for TypedDict "Point"
6464
[builtins fixtures/dict.pyi]
6565

6666
[case testCannotCreateTypedDictInstanceWithMissingItems]
6767
from mypy_extensions import TypedDict
6868
Point = TypedDict('Point', {'x': int, 'y': int})
69-
p = Point(x=42) # E: Missing key 'y' for TypedDict "Point"
69+
p = Point(x=42) # E: Missing key "y" for TypedDict "Point"
7070
[builtins fixtures/dict.pyi]
7171

7272
[case testCannotCreateTypedDictInstanceWithIncompatibleItemType]
@@ -149,7 +149,7 @@ def foo(x):
149149
# type: (Movie) -> None
150150
pass
151151

152-
foo({}) # E: Missing keys ('name', 'year') for TypedDict "Movie"
152+
foo({}) # E: Missing keys ("name", "year") for TypedDict "Movie"
153153
foo({'name': 'lol', 'year': 2009, 'based_on': 0}) # E: Incompatible types (expression has type "int", TypedDict item "based_on" has type "str")
154154

155155
[builtins fixtures/dict.pyi]
@@ -871,15 +871,15 @@ Point = TypedDict('Point', {'x': int, 'y': int})
871871
def f(p: Point) -> None:
872872
if int():
873873
p = {'x': 2, 'y': 3}
874-
p = {'x': 2} # E: Missing key 'y' for TypedDict "Point"
874+
p = {'x': 2} # E: Missing key "y" for TypedDict "Point"
875875
p = dict(x=2, y=3)
876876

877877
f({'x': 1, 'y': 3})
878878
f({'x': 1, 'y': 'z'}) # E: Incompatible types (expression has type "str", TypedDict item "y" has type "int")
879879

880880
f(dict(x=1, y=3))
881-
f(dict(x=1, y=3, z=4)) # E: Extra key 'z' for TypedDict "Point"
882-
f(dict(x=1, y=3, z=4, a=5)) # E: Extra keys ('z', 'a') for TypedDict "Point"
881+
f(dict(x=1, y=3, z=4)) # E: Extra key "z" for TypedDict "Point"
882+
f(dict(x=1, y=3, z=4, a=5)) # E: Extra keys ("z", "a") for TypedDict "Point"
883883

884884
[builtins fixtures/dict.pyi]
885885

@@ -888,15 +888,15 @@ from mypy_extensions import TypedDict
888888

889889
Point = TypedDict('Point', {'x': int, 'y': int})
890890

891-
p1a: Point = {'x': 'hi'} # E: Missing key 'y' for TypedDict "Point"
892-
p1b: Point = {} # E: Missing keys ('x', 'y') for TypedDict "Point"
891+
p1a: Point = {'x': 'hi'} # E: Missing key "y" for TypedDict "Point"
892+
p1b: Point = {} # E: Missing keys ("x", "y") for TypedDict "Point"
893893

894894
p2: Point
895-
p2 = dict(x='bye') # E: Missing key 'y' for TypedDict "Point"
895+
p2 = dict(x='bye') # E: Missing key "y" for TypedDict "Point"
896896

897897
p3 = Point(x=1, y=2)
898898
if int():
899-
p3 = {'x': 'hi'} # E: Missing key 'y' for TypedDict "Point"
899+
p3 = {'x': 'hi'} # E: Missing key "y" for TypedDict "Point"
900900

901901
p4: Point = {'x': 1, 'y': 2}
902902

@@ -911,7 +911,7 @@ T = TypeVar('T')
911911
def join(x: T, y: T) -> T: return x
912912
ab = join(A(x=1, y=1), B(x=1, y=''))
913913
if int():
914-
ab = {'x': 1, 'z': 1} # E: Expected TypedDict key 'x' but found keys ('x', 'z')
914+
ab = {'x': 1, 'z': 1} # E: Expected TypedDict key "x" but found keys ("x", "z")
915915
[builtins fixtures/dict.pyi]
916916

917917
[case testCannotCreateAnonymousTypedDictInstanceUsingDictLiteralWithMissingItems]
@@ -923,7 +923,7 @@ T = TypeVar('T')
923923
def join(x: T, y: T) -> T: return x
924924
ab = join(A(x=1, y=1, z=1), B(x=1, y=1, z=''))
925925
if int():
926-
ab = {} # E: Expected TypedDict keys ('x', 'y') but found no keys
926+
ab = {} # E: Expected TypedDict keys ("x", "y") but found no keys
927927
[builtins fixtures/dict.pyi]
928928

929929

@@ -1044,7 +1044,7 @@ f({})
10441044
f({'x': 1})
10451045
f({'y': ''})
10461046
f({'x': 1, 'y': ''})
1047-
f({'x': 1, 'z': ''}) # E: Extra key 'z' for TypedDict "D"
1047+
f({'x': 1, 'z': ''}) # E: Extra key "z" for TypedDict "D"
10481048
f({'x': ''}) # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int")
10491049
[builtins fixtures/dict.pyi]
10501050

@@ -1056,7 +1056,7 @@ reveal_type(D()) # N: Revealed type is "TypedDict('__main__.D', {'x'?: builtins.
10561056
reveal_type(D(x=1)) # N: Revealed type is "TypedDict('__main__.D', {'x'?: builtins.int, 'y'?: builtins.str})"
10571057
f(D(y=''))
10581058
f(D(x=1, y=''))
1059-
f(D(x=1, z='')) # E: Extra key 'z' for TypedDict "D"
1059+
f(D(x=1, z='')) # E: Extra key "z" for TypedDict "D"
10601060
f(D(x='')) # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int")
10611061
[builtins fixtures/dict.pyi]
10621062

@@ -1614,9 +1614,9 @@ a.update({'x': 1})
16141614
a.update({'x': ''}) # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int")
16151615
a.update({'x': 1, 'y': []})
16161616
a.update({'x': 1, 'y': [1]})
1617-
a.update({'z': 1}) # E: Unexpected TypedDict key 'z'
1618-
a.update({'z': 1, 'zz': 1}) # E: Unexpected TypedDict keys ('z', 'zz')
1619-
a.update({'z': 1, 'x': 1}) # E: Expected TypedDict key 'x' but found keys ('z', 'x')
1617+
a.update({'z': 1}) # E: Unexpected TypedDict key "z"
1618+
a.update({'z': 1, 'zz': 1}) # E: Unexpected TypedDict keys ("z", "zz")
1619+
a.update({'z': 1, 'x': 1}) # E: Expected TypedDict key "x" but found keys ("z", "x")
16201620
d = {'x': 1}
16211621
a.update(d) # E: Argument 1 to "update" of "TypedDict" has incompatible type "Dict[str, int]"; expected "TypedDict({'x'?: int, 'y'?: List[int]})"
16221622
[builtins fixtures/dict.pyi]
@@ -1977,7 +1977,7 @@ v = {union: 2} # E: Expected TypedDict key to be string literal
19771977
num2: Literal['num']
19781978
v = {num2: 2}
19791979
bad2: Literal['bad']
1980-
v = {bad2: 2} # E: Extra key 'bad' for TypedDict "Value"
1980+
v = {bad2: 2} # E: Extra key "bad" for TypedDict "Value"
19811981

19821982
[builtins fixtures/dict.pyi]
19831983
[typing fixtures/typing-typeddict.pyi]
@@ -2107,5 +2107,5 @@ d[True] # E: TypedDict key must be a string literal; expected one of ('foo')
21072107
from mypy_extensions import TypedDict
21082108

21092109
Foo = TypedDict('Foo', {'camelCaseKey': str})
2110-
value: Foo = {} # E: Missing key 'camelCaseKey' for TypedDict "Foo"
2110+
value: Foo = {} # E: Missing key "camelCaseKey" for TypedDict "Foo"
21112111
[builtins fixtures/dict.pyi]

test-data/unit/pythoneval.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ _testTypedDictMappingMethods.py:10: note: Revealed type is "typing.ItemsView[bui
11201120
_testTypedDictMappingMethods.py:11: note: Revealed type is "typing.ValuesView[builtins.object]"
11211121
_testTypedDictMappingMethods.py:12: note: Revealed type is "TypedDict('_testTypedDictMappingMethods.Cell', {'value': builtins.int})"
11221122
_testTypedDictMappingMethods.py:13: note: Revealed type is "builtins.int"
1123-
_testTypedDictMappingMethods.py:15: error: Unexpected TypedDict key 'invalid'
1123+
_testTypedDictMappingMethods.py:15: error: Unexpected TypedDict key "invalid"
11241124
_testTypedDictMappingMethods.py:16: error: Key "value" of TypedDict "Cell" cannot be deleted
11251125
_testTypedDictMappingMethods.py:21: note: Revealed type is "builtins.int"
11261126

0 commit comments

Comments
 (0)