Skip to content

Commit 8368356

Browse files
committed
update spec wording and results to make erroring on implicitly open TypedDicts optional
1 parent 59c2a2c commit 8368356

8 files changed

Lines changed: 49 additions & 33 deletions

File tree

‎conformance/results/mypy/callables_kwargs.toml‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ callables_kwargs.py:104: note: "TDProtocol5.__call__" has type "def __call__(sel
2727
callables_kwargs.py:112: error: Overlap between parameter names and ** TypedDict items: "v1" [misc]
2828
callables_kwargs.py:123: error: Unpack item in ** parameter must be a TypedDict [misc]
2929
callables_kwargs.py:145: error: Unexpected keyword argument "closed" for "__init_subclass__" of "TypedDict" [call-arg]
30-
callables_kwargs.py:149: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]
30+
callables_kwargs.py:149: error: Unexpected keyword argument "closed" for "__init_subclass__" of "TypedDict" [call-arg]
31+
callables_kwargs.py:153: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]
32+
callables_kwargs.py:193: error: Name "func10" already defined on line 180 [no-redef]
3133
"""
3234
conformance_automated = "Fail"
3335
errors_diff = """
3436
Line 135: Expected 1 errors
35-
Line 167: Expected 1 errors
36-
Line 185: Expected 1 errors
37-
Line 186: Expected 1 errors
37+
Line 176: Expected 1 errors
38+
Line 194: Expected 1 errors
39+
Line 195: Expected 1 errors
3840
Line 145: Unexpected errors ['callables_kwargs.py:145: error: Unexpected keyword argument "closed" for "__init_subclass__" of "TypedDict" [call-arg]']
39-
Line 149: Unexpected errors ['callables_kwargs.py:149: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]']
41+
Line 149: Unexpected errors ['callables_kwargs.py:149: error: Unexpected keyword argument "closed" for "__init_subclass__" of "TypedDict" [call-arg]']
42+
Line 153: Unexpected errors ['callables_kwargs.py:153: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]']
43+
Line 193: Unexpected errors ['callables_kwargs.py:193: error: Name "func10" already defined on line 180 [no-redef]']
4044
"""

‎conformance/results/pycroscope/callables_kwargs.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Does not check the `extra_items` type of unpacked kwargs against the target call
66
"""
77
conformance_automated = "Fail"
88
errors_diff = """
9-
Line 167: Expected 1 errors
10-
Line 185: Expected 1 errors
11-
Line 186: Expected 1 errors
9+
Line 176: Expected 1 errors
10+
Line 194: Expected 1 errors
11+
Line 195: Expected 1 errors
1212
"""
1313
output = """
1414
./callables_kwargs.py:47:4: In call to callables_kwargs.func1: Missing required argument 'v1' [incompatible_call]

‎conformance/results/pyrefly/callables_kwargs.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Does not check the `extra_items` type of unpacked kwargs against the target call
66
"""
77
conformance_automated = "Fail"
88
errors_diff = """
9-
Line 167: Expected 1 errors
10-
Line 185: Expected 1 errors
11-
Line 186: Expected 1 errors
9+
Line 176: Expected 1 errors
10+
Line 194: Expected 1 errors
11+
Line 195: Expected 1 errors
1212
"""
1313
output = """
1414
ERROR callables_kwargs.py:47:10-12: Missing argument `v1` in function `func1` [missing-argument]

‎conformance/results/pyright/callables_kwargs.toml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ callables_kwargs.py:123:21 - error: Expected TypedDict type argument for Unpack
3434
callables_kwargs.py:135:19 - error: Type "(*, v1: int, v3: str, v2: str = "") -> None" is not assignable to declared type "TDProtocol6"
3535
  Type "(*, v1: int, v3: str, v2: str = "") -> None" is not assignable to type "(**kwargs: **TD2) -> None"
3636
    Parameter "**kwargs" has no corresponding parameter (reportAssignmentType)
37-
callables_kwargs.py:186:29 - error: Argument of type "int" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs"
37+
callables_kwargs.py:180:5 - error: Function declaration "func10" is obscured by a declaration of the same name (reportRedeclaration)
38+
callables_kwargs.py:195:29 - error: Argument of type "int" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs"
3839
  "int" is not assignable to "str" (reportArgumentType)
3940
"""
4041
conformance_automated = "Fail"
4142
errors_diff = """
42-
Line 167: Expected 1 errors
43-
Line 185: Expected 1 errors
43+
Line 176: Expected 1 errors
44+
Line 194: Expected 1 errors
45+
Line 180: Unexpected errors ['callables_kwargs.py:180:5 - error: Function declaration "func10" is obscured by a declaration of the same name (reportRedeclaration)']
4446
"""

‎conformance/results/ty/callables_kwargs.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Does not reject passing unpacked kwargs typed with an open TypedDict to a callab
44
"""
55
conformance_automated = "Fail"
66
errors_diff = """
7-
Line 167: Expected 1 errors
7+
Line 176: Expected 1 errors
88
"""
99
output = """
1010
callables_kwargs.py:47:5: error[missing-argument] No arguments provided for required parameters `v1`, `v3` of function `func1`
@@ -22,6 +22,6 @@ callables_kwargs.py:104:19: error[invalid-assignment] Object of type `def func1(
2222
callables_kwargs.py:112:20: error[invalid-type-form] Parameter `v1` overlaps with unpacked TypedDict key in `**kwargs` annotation
2323
callables_kwargs.py:123:21: error[invalid-type-form] Unpacked value for `**kwargs` must be a TypedDict, not `T@func6`
2424
callables_kwargs.py:135:19: error[invalid-assignment] Object of type `def func7(*, v1: int, v3: str, v2: str = "") -> None` is not assignable to `TDProtocol6`
25-
callables_kwargs.py:185:16: error[unknown-argument] Unpacked argument may contain keyword arguments that do not match any known parameter of function `takes_name`
26-
callables_kwargs.py:186:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Expected `str`, found `int`
25+
callables_kwargs.py:194:16: error[unknown-argument] Unpacked argument may contain keyword arguments that do not match any known parameter of function `takes_name`
26+
callables_kwargs.py:195:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Expected `str`, found `int`
2727
"""

‎conformance/results/zuban/callables_kwargs.toml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ Does not reject passing unpacked kwargs typed with a TypedDict that has `extra_i
55
"""
66
conformance_automated = "Fail"
77
errors_diff = """
8-
Line 167: Expected 1 errors
9-
Line 185: Expected 1 errors
8+
Line 176: Expected 1 errors
9+
Line 194: Expected 1 errors
10+
Line 193: Unexpected errors ['callables_kwargs.py:193: error: Name "func10" already defined on line 180 [no-redef]']
1011
"""
1112
output = """
1213
callables_kwargs.py:47: error: Missing named argument "v1" for "func1" [call-arg]
@@ -29,5 +30,6 @@ callables_kwargs.py:104: error: Incompatible types in assignment (expression has
2930
callables_kwargs.py:112: error: Overlap between parameter names and ** TypedDict items: "v1" [misc]
3031
callables_kwargs.py:123: error: Unpack item in ** parameter must be a TypedDict [misc]
3132
callables_kwargs.py:135: error: Incompatible types in assignment (expression has type "def func7(*, v1: int, v3: str, v2: str = ...) -> None", variable has type "TDProtocol6") [assignment]
32-
callables_kwargs.py:186: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TD5"; expected "str" [arg-type]
33+
callables_kwargs.py:193: error: Name "func10" already defined on line 180 [no-redef]
34+
callables_kwargs.py:195: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TD6"; expected "str" [arg-type]
3335
"""

‎conformance/tests/callables_kwargs.py‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,15 @@ class TD3(TypedDict):
142142
name: str
143143

144144

145-
class TD4(TypedDict, closed=True):
145+
class TD4(TypedDict, closed=False):
146146
name: str
147147

148148

149-
class TD5(TypedDict, extra_items=int):
149+
class TD5(TypedDict, closed=True):
150+
name: str
151+
152+
153+
class TD6(TypedDict, extra_items=int):
150154
name: str
151155

152156

@@ -158,17 +162,22 @@ def takes_name_kwargs(name: str, **kwargs) -> None:
158162
...
159163

160164

161-
# > ``kwargs`` hinted with an unpacked, non-closed ``TypedDict`` can only
162-
# > be passed to another function if the function to which unpacked kwargs are being
163-
# > passed to has ``**kwargs`` in its signature as well, because then additional
164-
# > keywords would not cause errors at runtime during function invocation.
165+
# > It is only safe to pass ``kwargs`` hinted with an unpacked, non-closed ``TypedDict``
166+
# > to another function if that function has ``**kwargs`` in its signature as well. In this situation,
167+
# > type checkers should error if the ``TypedDict`` is explicitly open (``closed=False``) or has ``extra_items``.
168+
# > Additionally, type checkers may error if the ``TypedDict`` is implicitly open.
165169

166170
def func8(**kwargs: Unpack[TD3]) -> None:
167-
takes_name(**kwargs) # E: a subtype may contain unknown keys
171+
takes_name(**kwargs) # E?: a subtype may contain unknown keys
168172
takes_name_kwargs(**kwargs)
169173

170174

171175
def func9(**kwargs: Unpack[TD4]) -> None:
176+
takes_name(**kwargs) # E: a subtype may contain unknown keys
177+
takes_name_kwargs(**kwargs)
178+
179+
180+
def func10(**kwargs: Unpack[TD5]) -> None:
172181
takes_name(**kwargs)
173182
takes_name_kwargs(**kwargs)
174183

@@ -181,7 +190,7 @@ def takes_name_int_kwargs(name: str, **kwargs: int) -> None:
181190
...
182191

183192

184-
def func10(**kwargs: Unpack[TD5]) -> None:
193+
def func10(**kwargs: Unpack[TD6]) -> None:
185194
takes_name(**kwargs) # E: extra items may be present
186195
takes_name_str_kwargs(**kwargs) # E: extra items type is not compatible
187196
takes_name_int_kwargs(**kwargs)

‎docs/spec/callables.rst‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,10 @@ it needs completely ignoring any additional values.
413413
The calls to ``bar`` and ``spam`` will fail because an unexpected keyword
414414
argument will be passed to the ``takes_name`` function.
415415

416-
Therefore, ``kwargs`` hinted with an unpacked, non-closed ``TypedDict`` can only be passed
417-
to another function if the function to which unpacked kwargs are being passed
418-
to has ``**kwargs`` in its signature as well, because then additional keywords
419-
would not cause errors at runtime during function invocation. Otherwise, the
420-
type checker should generate an error.
416+
Therefore, it is only safe to pass ``kwargs`` hinted with an unpacked, non-closed ``TypedDict``
417+
to another function if that function has ``**kwargs`` in its signature as well. In this situation,
418+
type checkers should error if the ``TypedDict`` is explicitly open (``closed=False``) or has ``extra_items``.
419+
Additionally, type checkers may error if the ``TypedDict`` is implicitly open.
421420

422421
In cases similar to the ``bar`` function above the problem could be worked
423422
around by marking ``Animal`` with ``closed=True``, or by explicitly dereferencing desired

0 commit comments

Comments
 (0)