diff --git a/conformance/results/mypy/callables_kwargs.toml b/conformance/results/mypy/callables_kwargs.toml index d34b72732..6358cd601 100644 --- a/conformance/results/mypy/callables_kwargs.toml +++ b/conformance/results/mypy/callables_kwargs.toml @@ -1,29 +1,96 @@ conformant = "Partial" notes = """ Allows callable without kwargs to be assigned to callable with unpacked kwargs. +Does not support the `extra_items` TypedDict class argument. +Does not reject unpacking an open TypedDict in a call to a callable that has no `**kwargs`. +Does not reject unpacking an open TypedDict in a call to a callable whose `**kwargs` is typed with a closed TypedDict. +Does not check the type of an unpacked TypedDict's extra items against the callee's `**kwargs` annotation. +Does not check the type of an unpacked TypedDict's extra items against the callee's named keyword parameters. +Does not reject unpacking a TypedDict that sets `extra_items` in a call to a callable that has no `**kwargs`. +Does not reject unpacking a TypedDict that sets `extra_items` in a call to a callable whose `**kwargs` is typed with a closed TypedDict. +When `Base` is open and `Child` adds a non-required `int` key, incorrectly allows a callable taking `**kwargs: Unpack[Child]` to stand in for one taking `**kwargs: Unpack[Base]` and rejects the reverse assignment. +Incorrectly rejects assigning a callable whose `**kwargs` is an unpacked open TypedDict to an equivalent callable with `**kwargs: object`. +Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's `**kwargs: Unpack[...]` TypedDict. +Incorrectly rejects unpacking a TypedDict with an extra key when calling a function with `**kwargs` typed with an open TypedDict. """ output = """ -callables_kwargs.py:46: error: Missing named argument "v1" for "func1" [call-arg] -callables_kwargs.py:46: error: Missing named argument "v3" for "func1" [call-arg] -callables_kwargs.py:51: error: Unexpected keyword argument "v4" for "func1" [call-arg] -callables_kwargs.py:52: error: Too many positional arguments for "func1" [call-arg] -callables_kwargs.py:58: error: Argument 1 to "func1" has incompatible type "**dict[str, str]"; expected "int" [arg-type] -callables_kwargs.py:61: error: Argument 1 to "func1" has incompatible type "**dict[str, object]"; expected "int" [arg-type] -callables_kwargs.py:61: error: Argument 1 to "func1" has incompatible type "**dict[str, object]"; expected "str" [arg-type] -callables_kwargs.py:63: error: "func1" gets multiple values for keyword argument "v1" [misc] -callables_kwargs.py:64: error: "func2" gets multiple values for keyword argument "v3" [misc] -callables_kwargs.py:64: error: Argument 1 to "func2" has incompatible type "int"; expected "str" [arg-type] -callables_kwargs.py:65: error: "func2" gets multiple values for keyword argument "v1" [misc] -callables_kwargs.py:101: error: Incompatible types in assignment (expression has type "def func1(**kwargs: **TD2) -> None", variable has type "TDProtocol3") [assignment] -callables_kwargs.py:101: note: "TDProtocol3.__call__" has type "def __call__(self, *, v1: int, v2: int, v3: str) -> None" -callables_kwargs.py:102: error: Incompatible types in assignment (expression has type "def func1(**kwargs: **TD2) -> None", variable has type "TDProtocol4") [assignment] -callables_kwargs.py:102: note: "TDProtocol4.__call__" has type "def __call__(self, *, v1: int) -> None" -callables_kwargs.py:103: error: Incompatible types in assignment (expression has type "def func1(**kwargs: **TD2) -> None", variable has type "TDProtocol5") [assignment] -callables_kwargs.py:103: note: "TDProtocol5.__call__" has type "def __call__(self, v1: int, v3: str) -> None" -callables_kwargs.py:111: error: Overlap between parameter names and ** TypedDict items: "v1" [misc] -callables_kwargs.py:122: error: Unpack item in ** parameter must be a TypedDict [misc] +callables_kwargs.py:27: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg] +callables_kwargs.py:55: error: Missing named argument "v1" for "func1" [call-arg] +callables_kwargs.py:55: error: Missing named argument "v3" for "func1" [call-arg] +callables_kwargs.py:60: error: Unexpected keyword argument "v4" for "func1" [call-arg] +callables_kwargs.py:61: error: Too many positional arguments for "func1" [call-arg] +callables_kwargs.py:67: error: Argument 1 to "func1" has incompatible type "**dict[str, str]"; expected "int" [arg-type] +callables_kwargs.py:70: error: Argument 1 to "func1" has incompatible type "**dict[str, object]"; expected "int" [arg-type] +callables_kwargs.py:70: error: Argument 1 to "func1" has incompatible type "**dict[str, object]"; expected "str" [arg-type] +callables_kwargs.py:72: error: "func1" gets multiple values for keyword argument "v1" [misc] +callables_kwargs.py:73: error: "func2" gets multiple values for keyword argument "v3" [misc] +callables_kwargs.py:73: error: Argument 1 to "func2" has incompatible type "int"; expected "str" [arg-type] +callables_kwargs.py:74: error: "func2" gets multiple values for keyword argument "v1" [misc] +callables_kwargs.py:121: error: Incompatible types in assignment (expression has type "def func1(**kwargs: **TD2) -> None", variable has type "TDProtocol3") [assignment] +callables_kwargs.py:121: note: "TDProtocol3.__call__" has type "def __call__(self, *, v1: int, v2: int, v3: str) -> None" +callables_kwargs.py:122: error: Incompatible types in assignment (expression has type "def func1(**kwargs: **TD2) -> None", variable has type "TDProtocol4") [assignment] +callables_kwargs.py:122: note: "TDProtocol4.__call__" has type "def __call__(self, *, v1: int) -> None" +callables_kwargs.py:123: error: Incompatible types in assignment (expression has type "def func1(**kwargs: **TD2) -> None", variable has type "TDProtocol5") [assignment] +callables_kwargs.py:123: note: "TDProtocol5.__call__" has type "def __call__(self, v1: int, v3: str) -> None" +callables_kwargs.py:131: error: Overlap between parameter names and ** TypedDict items: "v1" [misc] +callables_kwargs.py:142: error: Unpack item in ** parameter must be a TypedDict [misc] +callables_kwargs.py:206: error: Incompatible types in assignment (expression has type "def func8(**kwargs: **TDBase) -> None", variable has type "TDProtocol10") [assignment] +callables_kwargs.py:206: note: "TDProtocol10.__call__" has type "def __call__(self, **kwargs: **TDChild) -> None" +callables_kwargs.py:216: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg] +callables_kwargs.py:265: error: Incompatible types in assignment (expression has type "def func8(**kwargs: **TDBase) -> None", variable has type "TDProtocol11") [assignment] +callables_kwargs.py:265: note: "TDProtocol11.__call__" has type "def __call__(self, *, x: int, **kwargs: object) -> None" +callables_kwargs.py:269: error: Incompatible types in assignment (expression has type "def func13(**kwargs: **TDExtraInt) -> None", variable has type "TDProtocol15") [assignment] +callables_kwargs.py:269: note: "TDProtocol15.__call__" has type "def __call__(self, *, x: int, **kwargs: int) -> None" +callables_kwargs.py:299: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg] +callables_kwargs.py:303: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg] +callables_kwargs.py:307: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg] +callables_kwargs.py:394: error: Unexpected keyword argument "year" for "takes_open" [call-arg] +callables_kwargs.py:395: error: Unexpected keyword argument "year" for "takes_closed" [call-arg] +callables_kwargs.py:396: error: Unexpected keyword argument "year" for "takes_extra_items_int" [call-arg] +callables_kwargs.py:397: error: Unexpected keyword argument "year" for "takes_extra_items_int" [call-arg] +callables_kwargs.py:401: error: Extra argument "y" from **args for "func8" [misc] +callables_kwargs.py:448: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg] """ conformance_automated = "Fail" errors_diff = """ -Line 134: Expected 1 errors +Line 150: Expected 1 errors +Line 170: Expected 1 errors +Line 205: Expected 1 errors +Line 354: Expected 1 errors +Line 356: Expected 1 errors +Line 357: Expected 1 errors +Line 359: Expected 1 errors +Line 360: Expected 1 errors +Line 361: Expected 1 errors +Line 366: Expected 1 errors +Line 367: Expected 1 errors +Line 373: Expected 1 errors +Line 375: Expected 1 errors +Line 376: Expected 1 errors +Line 378: Expected 1 errors +Line 379: Expected 1 errors +Line 380: Expected 1 errors +Line 385: Expected 1 errors +Line 386: Expected 1 errors +Line 416: Expected 1 errors +Line 417: Expected 1 errors +Line 419: Expected 1 errors +Line 420: Expected 1 errors +Line 422: Expected 1 errors +Line 423: Expected 1 errors +Line 424: Expected 1 errors +Line 425: Expected 1 errors +Line 444: Expected 1 errors +Line 445: Expected 1 errors +Line 27: Unexpected errors ['callables_kwargs.py:27: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]'] +Line 206: Unexpected errors ['callables_kwargs.py:206: error: Incompatible types in assignment (expression has type "def func8(**kwargs: **TDBase) -> None", variable has type "TDProtocol10") [assignment]'] +Line 216: Unexpected errors ['callables_kwargs.py:216: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]'] +Line 265: Unexpected errors ['callables_kwargs.py:265: error: Incompatible types in assignment (expression has type "def func8(**kwargs: **TDBase) -> None", variable has type "TDProtocol11") [assignment]'] +Line 269: Unexpected errors ['callables_kwargs.py:269: error: Incompatible types in assignment (expression has type "def func13(**kwargs: **TDExtraInt) -> None", variable has type "TDProtocol15") [assignment]'] +Line 299: Unexpected errors ['callables_kwargs.py:299: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]'] +Line 303: Unexpected errors ['callables_kwargs.py:303: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]'] +Line 307: Unexpected errors ['callables_kwargs.py:307: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]'] +Line 396: Unexpected errors ['callables_kwargs.py:396: error: Unexpected keyword argument "year" for "takes_extra_items_int" [call-arg]'] +Line 401: Unexpected errors ['callables_kwargs.py:401: error: Extra argument "y" from **args for "func8" [misc]'] +Line 448: Unexpected errors ['callables_kwargs.py:448: error: Unexpected keyword argument "extra_items" for "__init_subclass__" of "TypedDict" [call-arg]'] """ diff --git a/conformance/results/pycroscope/callables_kwargs.toml b/conformance/results/pycroscope/callables_kwargs.toml index 492f041f5..2ea91d346 100644 --- a/conformance/results/pycroscope/callables_kwargs.toml +++ b/conformance/results/pycroscope/callables_kwargs.toml @@ -1,19 +1,58 @@ -conformance_automated = "Pass" +conformant = "Partial" +notes = """ +Does not reject unpacking an open TypedDict in a call to a callable that has no `**kwargs`. +Does not reject unpacking an open TypedDict in a call to a callable whose `**kwargs` is typed with a closed TypedDict. +Incorrectly rejects unpacking a TypedDict with an extra key when calling a function with `**kwargs` typed with an open TypedDict. +""" +conformance_automated = "Fail" errors_diff = """ +Line 354: Expected 1 errors +Line 357: Expected 1 errors +Line 360: Expected 1 errors +Line 361: Expected 1 errors +Line 373: Expected 1 errors +Line 376: Expected 1 errors +Line 379: Expected 1 errors +Line 380: Expected 1 errors +Line 401: Unexpected errors ["./callables_kwargs.py:401:4: In call to callables_kwargs.func8: Got an unexpected keyword argument 'y' [incompatible_call]"] """ output = """ -./callables_kwargs.py:46:4: In call to callables_kwargs.func1: Missing required argument 'v1' [incompatible_call] -./callables_kwargs.py:51:4: In call to callables_kwargs.func1: Got an unexpected keyword argument 'v4' [incompatible_call] -./callables_kwargs.py:52:4: In call to callables_kwargs.func1: Missing required argument 'v1' [incompatible_call] -./callables_kwargs.py:58:4: Incompatible argument type for v1: expected int but got str [incompatible_argument] -./callables_kwargs.py:61:4: In call to callables_kwargs.func1: Got an unexpected keyword argument 'v4' [incompatible_call] -./callables_kwargs.py:63:4: Multiple values provided for argument 'v1' [incompatible_call] -./callables_kwargs.py:64:4: In call to callables_kwargs.func2: Parameter 'v3' provided as both a positional and a keyword argument [incompatible_call] -./callables_kwargs.py:65:4: Multiple values provided for argument 'v1' [incompatible_call] -./callables_kwargs.py:101:0: Incompatible assignment: expected callables_kwargs.TDProtocol3, got function 'callables_kwargs.func1' [incompatible_assignment] -./callables_kwargs.py:102:0: Incompatible assignment: expected callables_kwargs.TDProtocol4, got function 'callables_kwargs.func1' [incompatible_assignment] -./callables_kwargs.py:103:0: Incompatible assignment: expected callables_kwargs.TDProtocol5, got function 'callables_kwargs.func1' [incompatible_assignment] -./callables_kwargs.py:111:21: Parameter v1 overlaps with TypedDict key in **kwargs [invalid_annotation] -./callables_kwargs.py:122:12: Expected TypedDict type inside Unpack[] for **kwargs [invalid_annotation] -./callables_kwargs.py:134:0: Incompatible assignment: expected callables_kwargs.TDProtocol6, got function 'callables_kwargs.func7' [incompatible_assignment] +./callables_kwargs.py:55:4: In call to callables_kwargs.func1: Missing required argument 'v1' [incompatible_call] +./callables_kwargs.py:60:4: In call to callables_kwargs.func1: Got an unexpected keyword argument 'v4' [incompatible_call] +./callables_kwargs.py:61:4: In call to callables_kwargs.func1: Missing required argument 'v1' [incompatible_call] +./callables_kwargs.py:67:4: Incompatible argument type for v1: expected int but got str [incompatible_argument] +./callables_kwargs.py:70:4: In call to callables_kwargs.func1: Got an unexpected keyword argument 'v4' [incompatible_call] +./callables_kwargs.py:72:4: Multiple values provided for argument 'v1' [incompatible_call] +./callables_kwargs.py:73:4: In call to callables_kwargs.func2: Parameter 'v3' provided as both a positional and a keyword argument [incompatible_call] +./callables_kwargs.py:74:4: Multiple values provided for argument 'v1' [incompatible_call] +./callables_kwargs.py:121:0: Incompatible assignment: expected callables_kwargs.TDProtocol3, got function 'callables_kwargs.func1' [incompatible_assignment] +./callables_kwargs.py:122:0: Incompatible assignment: expected callables_kwargs.TDProtocol4, got function 'callables_kwargs.func1' [incompatible_assignment] +./callables_kwargs.py:123:0: Incompatible assignment: expected callables_kwargs.TDProtocol5, got function 'callables_kwargs.func1' [incompatible_assignment] +./callables_kwargs.py:131:21: Parameter v1 overlaps with TypedDict key in **kwargs [invalid_annotation] +./callables_kwargs.py:142:12: Expected TypedDict type inside Unpack[] for **kwargs [invalid_annotation] +./callables_kwargs.py:150:0: Incompatible assignment: expected callables_kwargs.TDProtocol6, got function 'callables_kwargs.func7' [incompatible_assignment] +./callables_kwargs.py:170:4: Value of method incompatible with base class callables_kwargs.BaseUnpacked [incompatible_override] +./callables_kwargs.py:205:0: Incompatible assignment: expected callables_kwargs.TDProtocol9, got function 'callables_kwargs.func9' [incompatible_assignment] +./callables_kwargs.py:356:4: Incompatible argument type for kwargs: expected dict[str, str] but got [incompatible_argument] +./callables_kwargs.py:359:4: Incompatible argument type for kwargs: expected dict[str, str] but got [incompatible_argument] +./callables_kwargs.py:366:4: Incompatible argument type for kwargs: expected dict[str, int] but got [incompatible_argument] +./callables_kwargs.py:367:4: Incompatible argument type for kwargs: expected dict[str, int] but got [incompatible_argument] +./callables_kwargs.py:375:4: Incompatible argument type for kwargs: expected dict[str, str] but got [incompatible_argument] +./callables_kwargs.py:378:4: Incompatible argument type for kwargs: expected dict[str, str] but got [incompatible_argument] +./callables_kwargs.py:385:4: Incompatible argument type for kwargs: expected dict[str, int] but got [incompatible_argument] +./callables_kwargs.py:386:4: Incompatible argument type for kwargs: expected dict[str, int] but got [incompatible_argument] +./callables_kwargs.py:394:4: In call to callables_kwargs.takes_open: Got an unexpected keyword argument 'year' [incompatible_call] +./callables_kwargs.py:395:4: In call to callables_kwargs.takes_closed: Got an unexpected keyword argument 'year' [incompatible_call] +./callables_kwargs.py:397:4: Incompatible argument type for kwargs: expected dict[str, int] but got [incompatible_argument] +./callables_kwargs.py:401:4: In call to callables_kwargs.func8: Got an unexpected keyword argument 'y' [incompatible_call] +./callables_kwargs.py:416:4: In call to callables_kwargs.takes_name: **kwargs provided but not used [incompatible_call] +./callables_kwargs.py:417:4: Incompatible argument type for kwargs: expected dict[str, str] but got [incompatible_argument] +./callables_kwargs.py:419:4: In call to callables_kwargs.takes_name: **kwargs provided but not used [incompatible_call] +./callables_kwargs.py:420:4: Incompatible argument type for kwargs: expected dict[str, str] but got [incompatible_argument] +./callables_kwargs.py:422:4: In call to callables_kwargs.takes_closed: **kwargs provided but not used [incompatible_call] +./callables_kwargs.py:423:4: In call to callables_kwargs.takes_closed: **kwargs provided but not used [incompatible_call] +./callables_kwargs.py:424:4: Incompatible argument type for label: expected str but got int [incompatible_argument] +./callables_kwargs.py:425:4: Incompatible argument type for label: expected str but got int [incompatible_argument] +./callables_kwargs.py:444:4: Incompatible argument type for y: expected int but got object [incompatible_argument] +./callables_kwargs.py:445:4: Incompatible argument type for y: expected int but got object [incompatible_argument] """ diff --git a/conformance/results/pyrefly/callables_kwargs.toml b/conformance/results/pyrefly/callables_kwargs.toml index d37554ce3..c19540c99 100644 --- a/conformance/results/pyrefly/callables_kwargs.toml +++ b/conformance/results/pyrefly/callables_kwargs.toml @@ -1,23 +1,57 @@ -conformant = "Pass" -conformance_automated = "Pass" +conformant = "Partial" +notes = """ +Incorrectly allows a callable taking `**kwargs: Unpack[Child]` to stand in for one taking `**kwargs: Unpack[Base]` when `Base` is open and `Child` adds a non-required `int` key. +""" +conformance_automated = "Fail" errors_diff = """ +Line 205: Expected 1 errors """ output = """ -ERROR callables_kwargs.py:46:10-12: Missing argument `v1` in function `func1` [missing-argument] -ERROR callables_kwargs.py:46:10-12: Missing argument `v3` in function `func1` [missing-argument] -ERROR callables_kwargs.py:51:32-34: Unexpected keyword argument `v4` in function `func1` [unexpected-keyword] -ERROR callables_kwargs.py:52:11-12: Expected argument `v1` to be passed by name in function `func1` [unexpected-positional-argument] -ERROR callables_kwargs.py:52:11-12: Expected 0 positional arguments, got 3 in function `func1` [bad-argument-count] -ERROR callables_kwargs.py:52:14-16: Expected argument `v3` to be passed by name in function `func1` [unexpected-positional-argument] -ERROR callables_kwargs.py:58:11-20: Unpacked keyword argument `str` is not assignable to parameter `v1` with type `int` in function `func1` [bad-argument-type] -ERROR callables_kwargs.py:63:17-22: Multiple values for argument `v1` in function `func1` [bad-keyword-argument] -ERROR callables_kwargs.py:64:11-12: Argument `Literal[1]` is not assignable to parameter `v3` with type `str` in function `func2` [bad-argument-type] -ERROR callables_kwargs.py:64:14-19: Multiple values for argument `v3` in function `func2` [bad-keyword-argument] -ERROR callables_kwargs.py:65:17-22: Multiple values for argument `v1` in function `func2` [bad-keyword-argument] -ERROR callables_kwargs.py:101:19-24: `(**kwargs: Unpack[TD2]) -> None` is not assignable to `TDProtocol3` [bad-assignment] -ERROR callables_kwargs.py:102:19-24: `(**kwargs: Unpack[TD2]) -> None` is not assignable to `TDProtocol4` [bad-assignment] -ERROR callables_kwargs.py:103:19-24: `(**kwargs: Unpack[TD2]) -> None` is not assignable to `TDProtocol5` [bad-assignment] -ERROR callables_kwargs.py:111:20-41: TypedDict key 'v1' in **kwargs overlaps with parameter 'v1' [bad-function-definition] -ERROR callables_kwargs.py:122:21-30: `Unpack` in **kwargs annotation must be used only with a `TypedDict` [invalid-annotation] -ERROR callables_kwargs.py:134:19-24: `(*, v1: int, v3: str, v2: str = '') -> None` is not assignable to `TDProtocol6` [bad-assignment] +ERROR callables_kwargs.py:55:10-12: Missing argument `v1` in function `func1` [missing-argument] +ERROR callables_kwargs.py:55:10-12: Missing argument `v3` in function `func1` [missing-argument] +ERROR callables_kwargs.py:60:32-34: Unexpected keyword argument `v4` in function `func1` [unexpected-keyword] +ERROR callables_kwargs.py:61:11-12: Expected argument `v1` to be passed by name in function `func1` [unexpected-positional-argument] +ERROR callables_kwargs.py:61:11-12: Expected 0 positional arguments, got 3 in function `func1` [bad-argument-count] +ERROR callables_kwargs.py:61:14-16: Expected argument `v3` to be passed by name in function `func1` [unexpected-positional-argument] +ERROR callables_kwargs.py:67:11-20: Unpacked keyword argument `str` is not assignable to parameter `v1` with type `int` in function `func1` [bad-argument-type] +ERROR callables_kwargs.py:72:17-22: Multiple values for argument `v1` in function `func1` [bad-keyword-argument] +ERROR callables_kwargs.py:73:11-12: Argument `Literal[1]` is not assignable to parameter `v3` with type `str` in function `func2` [bad-argument-type] +ERROR callables_kwargs.py:73:14-19: Multiple values for argument `v3` in function `func2` [bad-keyword-argument] +ERROR callables_kwargs.py:74:17-22: Multiple values for argument `v1` in function `func2` [bad-keyword-argument] +ERROR callables_kwargs.py:121:19-24: `(**kwargs: Unpack[TD2]) -> None` is not assignable to `TDProtocol3` [bad-assignment] +ERROR callables_kwargs.py:122:19-24: `(**kwargs: Unpack[TD2]) -> None` is not assignable to `TDProtocol4` [bad-assignment] +ERROR callables_kwargs.py:123:19-24: `(**kwargs: Unpack[TD2]) -> None` is not assignable to `TDProtocol5` [bad-assignment] +ERROR callables_kwargs.py:131:20-41: TypedDict key 'v1' in **kwargs overlaps with parameter 'v1' [bad-function-definition] +ERROR callables_kwargs.py:142:21-30: `Unpack` in **kwargs annotation must be used only with a `TypedDict` [invalid-annotation] +ERROR callables_kwargs.py:150:19-24: `(*, v1: int, v3: str, v2: str = '') -> None` is not assignable to `TDProtocol6` [bad-assignment] +ERROR callables_kwargs.py:170:9-15: Class member `SubNoKwargs.method` overrides parent class `BaseUnpacked` in an inconsistent manner [bad-override] +ERROR callables_kwargs.py:354:16-23: `TDOpenImplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_name` [open-unpacking] +ERROR callables_kwargs.py:356:27-34: Extra items of type `object` are not assignable to parameter `kwargs` with type `str` in function `takes_name_str_kwargs` [open-unpacking] +ERROR callables_kwargs.py:357:16-24: `TDOpenImplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_name` [open-unpacking] +ERROR callables_kwargs.py:359:27-35: Extra items of type `object` are not assignable to parameter `kwargs` with type `str` in function `takes_name_str_kwargs` [open-unpacking] +ERROR callables_kwargs.py:360:18-25: `TDOpenImplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_closed` [open-unpacking] +ERROR callables_kwargs.py:361:18-26: `TDOpenImplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_closed` [open-unpacking] +ERROR callables_kwargs.py:366:27-34: Extra items of type `object` are not assignable to parameter `kwargs` with type `int` in function `takes_extra_items_int` [open-unpacking] +ERROR callables_kwargs.py:367:27-35: Extra items of type `object` are not assignable to parameter `kwargs` with type `int` in function `takes_extra_items_int` [open-unpacking] +ERROR callables_kwargs.py:373:16-23: `TDOpenExplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_name` [open-unpacking] +ERROR callables_kwargs.py:375:27-34: Extra items of type `object` are not assignable to parameter `kwargs` with type `str` in function `takes_name_str_kwargs` [open-unpacking] +ERROR callables_kwargs.py:376:16-24: `TDOpenExplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_name` [open-unpacking] +ERROR callables_kwargs.py:378:27-35: Extra items of type `object` are not assignable to parameter `kwargs` with type `str` in function `takes_name_str_kwargs` [open-unpacking] +ERROR callables_kwargs.py:379:18-25: `TDOpenExplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_closed` [open-unpacking] +ERROR callables_kwargs.py:380:18-26: `TDOpenExplicit` may contain extra items of type `object`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_closed` [open-unpacking] +ERROR callables_kwargs.py:385:27-34: Extra items of type `object` are not assignable to parameter `kwargs` with type `int` in function `takes_extra_items_int` [open-unpacking] +ERROR callables_kwargs.py:386:27-35: Extra items of type `object` are not assignable to parameter `kwargs` with type `int` in function `takes_extra_items_int` [open-unpacking] +ERROR callables_kwargs.py:394:25-29: Unexpected keyword argument `year` in function `takes_open` [unexpected-keyword] +ERROR callables_kwargs.py:395:27-31: Unexpected keyword argument `year` in function `takes_closed` [unexpected-keyword] +ERROR callables_kwargs.py:397:41-43: Keyword argument `year` with type `Literal['']` is not assignable to parameter `**kwargs` with type `int` in function `takes_extra_items_int` [bad-argument-type] +ERROR callables_kwargs.py:416:16-23: `TDExtraItems` may contain extra items of type `int`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_name` [unexpected-keyword] +ERROR callables_kwargs.py:417:27-34: Extra items of type `int` are not assignable to parameter `kwargs` with type `str` in function `takes_name_str_kwargs` [bad-argument-type] +ERROR callables_kwargs.py:419:16-24: `TDExtraItems` may contain extra items of type `int`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_name` [unexpected-keyword] +ERROR callables_kwargs.py:420:27-35: Extra items of type `int` are not assignable to parameter `kwargs` with type `str` in function `takes_name_str_kwargs` [bad-argument-type] +ERROR callables_kwargs.py:422:18-25: `TDExtraItems` may contain extra items of type `int`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_closed` [unexpected-keyword] +ERROR callables_kwargs.py:423:18-26: `TDExtraItems` may contain extra items of type `int`, which cannot be unpacked into a callable that accepts no extra keyword arguments in function `takes_closed` [unexpected-keyword] +ERROR callables_kwargs.py:424:37-44: Extra items of type `int` are not assignable to parameter `label` with type `str` in function `takes_optional_label_int_kwargs` [bad-argument-type] +ERROR callables_kwargs.py:425:37-45: Extra items of type `int` are not assignable to parameter `label` with type `str` in function `takes_optional_label_int_kwargs` [bad-argument-type] +ERROR callables_kwargs.py:444:11-18: Extra items of type `object` are not assignable to parameter `y` with type `int` in function `func9` [open-unpacking] +ERROR callables_kwargs.py:445:11-19: Extra items of type `object` are not assignable to parameter `y` with type `int` in function `func9` [open-unpacking] """ diff --git a/conformance/results/pyright/callables_kwargs.toml b/conformance/results/pyright/callables_kwargs.toml index b04d6a66a..5f2f1a9f0 100644 --- a/conformance/results/pyright/callables_kwargs.toml +++ b/conformance/results/pyright/callables_kwargs.toml @@ -1,36 +1,104 @@ -conformant = "Pass" +conformant = "Partial" +notes = """ +Does not reject unpacking an open TypedDict in a call to a callable that has no `**kwargs`. +Does not reject unpacking an open TypedDict in a call to a callable whose `**kwargs` is typed with a closed TypedDict. +Does not reject unpacking a TypedDict that sets `extra_items` in a call to a callable that has no `**kwargs`. +Does not reject unpacking a TypedDict that sets `extra_items` in a call to a callable whose `**kwargs` is typed with a closed TypedDict. +Does not check the type of an unpacked TypedDict's extra items against the callee's named keyword parameters. +Incorrectly rejects assigning a callable taking `**kwargs: Unpack[Base]` to one taking `**kwargs: Unpack[Child]` when `Base` is open and `Child` adds a non-required `int` key. +Incorrectly rejects assigning a callable whose `**kwargs` is an unpacked open TypedDict to an equivalent callable with `**kwargs: object`. +Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's `**kwargs: Unpack[...]` TypedDict. +""" output = """ -callables_kwargs.py:28:5 - error: Could not access item in TypedDict +callables_kwargs.py:37:5 - error: Could not access item in TypedDict   "v2" is not a required key in "TD2", so access may result in runtime exception (reportTypedDictNotRequiredAccess) -callables_kwargs.py:46:5 - error: Arguments missing for parameters "v1", "v3" (reportCallIssue) -callables_kwargs.py:51:32 - error: No parameter named "v4" (reportCallIssue) -callables_kwargs.py:52:11 - error: Expected 0 positional arguments (reportCallIssue) -callables_kwargs.py:58:13 - error: Argument of type "str" cannot be assigned to parameter "v1" of type "int" in function "func1" +callables_kwargs.py:55:5 - error: Arguments missing for parameters "v1", "v3" (reportCallIssue) +callables_kwargs.py:60:32 - error: No parameter named "v4" (reportCallIssue) +callables_kwargs.py:61:11 - error: Expected 0 positional arguments (reportCallIssue) +callables_kwargs.py:67:13 - error: Argument of type "str" cannot be assigned to parameter "v1" of type "int" in function "func1"   "str" is not assignable to "int" (reportArgumentType) -callables_kwargs.py:63:19 - error: Unable to match unpacked TypedDict argument to parameters +callables_kwargs.py:72:19 - error: Unable to match unpacked TypedDict argument to parameters   Parameter "v1" is already assigned (reportCallIssue) -callables_kwargs.py:64:16 - error: Unable to match unpacked TypedDict argument to parameters +callables_kwargs.py:73:16 - error: Unable to match unpacked TypedDict argument to parameters   Parameter "v3" is already assigned (reportCallIssue) -callables_kwargs.py:65:19 - error: Unable to match unpacked TypedDict argument to parameters +callables_kwargs.py:74:19 - error: Unable to match unpacked TypedDict argument to parameters   Parameter "v1" is already assigned (reportCallIssue) -callables_kwargs.py:101:19 - error: Type "(**kwargs: **TD2) -> None" is not assignable to declared type "TDProtocol3" +callables_kwargs.py:121:19 - error: Type "(**kwargs: **TD2) -> None" is not assignable to declared type "TDProtocol3"   Type "(**kwargs: **TD2) -> None" is not assignable to type "(*, v1: int, v2: int, v3: str) -> None"     Keyword parameter "v2" of type "int" is incompatible with type "str"       "int" is not assignable to "str" (reportAssignmentType) -callables_kwargs.py:102:19 - error: Type "(**kwargs: **TD2) -> None" is not assignable to declared type "TDProtocol4" +callables_kwargs.py:122:19 - error: Type "(**kwargs: **TD2) -> None" is not assignable to declared type "TDProtocol4"   Type "(**kwargs: **TD2) -> None" is not assignable to type "(*, v1: int) -> None"     Extra parameter "v3" (reportAssignmentType) -callables_kwargs.py:103:19 - error: Type "(**kwargs: **TD2) -> None" is not assignable to declared type "TDProtocol5" +callables_kwargs.py:123:19 - error: Type "(**kwargs: **TD2) -> None" is not assignable to declared type "TDProtocol5"   Type "(**kwargs: **TD2) -> None" is not assignable to type "(v1: int, v3: str) -> None"     Function accepts too few positional parameters; expected 2 but received 0       Parameter "v1" is missing default argument       Parameter "v3" is missing default argument (reportAssignmentType) -callables_kwargs.py:111:30 - error: Typed dictionary overlaps with keyword parameter: v1 (reportGeneralTypeIssues) -callables_kwargs.py:122:21 - error: Expected TypedDict type argument for Unpack (reportGeneralTypeIssues) -callables_kwargs.py:134:19 - error: Type "(*, v1: int, v3: str, v2: str = "") -> None" is not assignable to declared type "TDProtocol6" +callables_kwargs.py:131:30 - error: Typed dictionary overlaps with keyword parameter: v1 (reportGeneralTypeIssues) +callables_kwargs.py:142:21 - error: Expected TypedDict type argument for Unpack (reportGeneralTypeIssues) +callables_kwargs.py:150:19 - error: Type "(*, v1: int, v3: str, v2: str = "") -> None" is not assignable to declared type "TDProtocol6"   Type "(*, v1: int, v3: str, v2: str = "") -> None" is not assignable to type "(**kwargs: **TD2) -> None"     Parameter "**kwargs" has no corresponding parameter (reportAssignmentType) +callables_kwargs.py:170:9 - error: Method "method" overrides class "BaseUnpacked" in an incompatible manner +  Parameter "**kwargs" has no corresponding parameter (reportIncompatibleMethodOverride) +callables_kwargs.py:205:20 - error: Type "(**kwargs: **TDChild) -> None" is not assignable to declared type "TDProtocol9" +  Type "(**kwargs: **TDChild) -> None" is not assignable to type "(**kwargs: **TDBase) -> None" +    Parameter 1: type "*TDBase" is incompatible with type "int" +      "*TDBase" is not assignable to "int" +    Parameter 1: type "*TDBase" is incompatible with type "*TDChild" +      "y" is missing from "*TDBase" (reportAssignmentType) +callables_kwargs.py:206:21 - error: Type "(**kwargs: **TDBase) -> None" is not assignable to declared type "TDProtocol10" +  Type "(**kwargs: **TDBase) -> None" is not assignable to type "(**kwargs: **TDChild) -> None" +    Parameter 1: type "int" is incompatible with type "*TDBase" +      "int" is not assignable to "*TDBase" (reportAssignmentType) +callables_kwargs.py:265:21 - error: Type "(**kwargs: **TDBase) -> None" is not assignable to declared type "TDProtocol11" +  Type "(**kwargs: **TDBase) -> None" is not assignable to type "(*, x: int, **kwargs: object) -> None" +    Parameter 3: type "object" is incompatible with type "*TDBase" +      "object" is not assignable to "*TDBase" (reportAssignmentType) +callables_kwargs.py:356:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs" +  "object" is not assignable to "str" (reportArgumentType) +callables_kwargs.py:359:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs" +  "object" is not assignable to "str" (reportArgumentType) +callables_kwargs.py:366:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "int" in function "takes_extra_items_int" +  "object" is not assignable to "int" (reportArgumentType) +callables_kwargs.py:367:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "int" in function "takes_extra_items_int" +  "object" is not assignable to "int" (reportArgumentType) +callables_kwargs.py:375:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs" +  "object" is not assignable to "str" (reportArgumentType) +callables_kwargs.py:378:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs" +  "object" is not assignable to "str" (reportArgumentType) +callables_kwargs.py:385:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "int" in function "takes_extra_items_int" +  "object" is not assignable to "int" (reportArgumentType) +callables_kwargs.py:386:29 - error: Argument of type "object" cannot be assigned to parameter "kwargs" of type "int" in function "takes_extra_items_int" +  "object" is not assignable to "int" (reportArgumentType) +callables_kwargs.py:394:25 - error: No parameter named "year" (reportCallIssue) +callables_kwargs.py:395:27 - error: No parameter named "year" (reportCallIssue) +callables_kwargs.py:397:41 - error: Argument of type "Literal['']" cannot be assigned to parameter "year" of type "int" in function "takes_extra_items_int" +  "Literal['']" is not assignable to "int" (reportArgumentType) +callables_kwargs.py:417:29 - error: Argument of type "int" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs" +  "int" is not assignable to "str" (reportArgumentType) +callables_kwargs.py:420:29 - error: Argument of type "int" cannot be assigned to parameter "kwargs" of type "str" in function "takes_name_str_kwargs" +  "int" is not assignable to "str" (reportArgumentType) """ -conformance_automated = "Pass" +conformance_automated = "Fail" errors_diff = """ +Line 354: Expected 1 errors +Line 357: Expected 1 errors +Line 360: Expected 1 errors +Line 361: Expected 1 errors +Line 373: Expected 1 errors +Line 376: Expected 1 errors +Line 379: Expected 1 errors +Line 380: Expected 1 errors +Line 416: Expected 1 errors +Line 419: Expected 1 errors +Line 422: Expected 1 errors +Line 423: Expected 1 errors +Line 424: Expected 1 errors +Line 425: Expected 1 errors +Line 444: Expected 1 errors +Line 445: Expected 1 errors +Line 206: Unexpected errors ['callables_kwargs.py:206:21 - error: Type "(**kwargs: **TDBase) -> None" is not assignable to declared type "TDProtocol10"'] +Line 265: Unexpected errors ['callables_kwargs.py:265:21 - error: Type "(**kwargs: **TDBase) -> None" is not assignable to declared type "TDProtocol11"'] """ diff --git a/conformance/results/results.html b/conformance/results/results.html index e4c61d5d3..e28bacadc 100644 --- a/conformance/results/results.html +++ b/conformance/results/results.html @@ -1430,13 +1430,64 @@

Python Type System Conformance Test Results

Partial
  • Allows callable without kwargs to be assigned to callable with unpacked kwargs.
  • +
  • Does not support the extra_items TypedDict class argument.
  • +
  • Does not reject unpacking an open TypedDict in a call to a callable that has no **kwargs.
  • +
  • Does not reject unpacking an open TypedDict in a call to a callable whose **kwargs is typed with a closed TypedDict.
  • +
  • Does not check the type of an unpacked TypedDict's extra items against the callee's **kwargs annotation.
  • +
  • Does not check the type of an unpacked TypedDict's extra items against the callee's named keyword parameters.
  • +
  • Does not reject unpacking a TypedDict that sets extra_items in a call to a callable that has no **kwargs.
  • +
  • Does not reject unpacking a TypedDict that sets extra_items in a call to a callable whose **kwargs is typed with a closed TypedDict.
  • +
  • When Base is open and Child adds a non-required int key, incorrectly allows a callable taking **kwargs: Unpack[Child] to stand in for one taking **kwargs: Unpack[Base] and rejects the reverse assignment.
  • +
  • Incorrectly rejects assigning a callable whose **kwargs is an unpacked open TypedDict to an equivalent callable with **kwargs: object.
  • +
  • Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's **kwargs: Unpack[...] TypedDict.
  • +
  • Incorrectly rejects unpacking a TypedDict with an extra key when calling a function with **kwargs typed with an open TypedDict.
  • +
+ + + Partial +
    +
  • Does not reject unpacking an open TypedDict in a call to a callable that has no **kwargs.
  • +
  • Does not reject unpacking an open TypedDict in a call to a callable whose **kwargs is typed with a closed TypedDict.
  • +
  • Incorrectly rejects unpacking a TypedDict with an extra key when calling a function with **kwargs typed with an open TypedDict.
  • +
+ + + Partial +
    +
  • Incorrectly allows a callable taking **kwargs: Unpack[Child] to stand in for one taking **kwargs: Unpack[Base] when Base is open and Child adds a non-required int key.
  • +
+ + + Partial +
    +
  • Does not reject unpacking an open TypedDict in a call to a callable that has no **kwargs.
  • +
  • Does not reject unpacking an open TypedDict in a call to a callable whose **kwargs is typed with a closed TypedDict.
  • +
  • Does not reject unpacking a TypedDict that sets extra_items in a call to a callable that has no **kwargs.
  • +
  • Does not reject unpacking a TypedDict that sets extra_items in a call to a callable whose **kwargs is typed with a closed TypedDict.
  • +
  • Does not check the type of an unpacked TypedDict's extra items against the callee's named keyword parameters.
  • +
  • Incorrectly rejects assigning a callable taking **kwargs: Unpack[Base] to one taking **kwargs: Unpack[Child] when Base is open and Child adds a non-required int key.
  • +
  • Incorrectly rejects assigning a callable whose **kwargs is an unpacked open TypedDict to an equivalent callable with **kwargs: object.
  • +
  • Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's **kwargs: Unpack[...] TypedDict.
  • +
+ + + Partial +
    +
  • Does not reject unpacking an open TypedDict in a call to a callable that has no **kwargs.
  • +
  • Does not reject unpacking an open TypedDict in a call to a callable whose **kwargs is typed with a closed TypedDict.
  • +
  • Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's **kwargs: Unpack[...] TypedDict.
  • +
+ + + Partial +
    +
  • Does not reject unpacking an open TypedDict in a call to a callable that has no **kwargs.
  • +
  • Does not reject unpacking an open TypedDict in a call to a callable whose **kwargs is typed with a closed TypedDict.
  • +
  • Does not reject unpacking an open TypedDict in a call to a callable whose **kwargs is typed with a TypedDict whose extra_items type is narrower than object.
  • +
  • Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's **kwargs: Unpack[...] TypedDict.
  • +
  • Incorrectly rejects unpacking a TypedDict with an extra key when calling a function with **kwargs typed with an open TypedDict.
- Pass - Pass - Pass - Pass - Pass callables_protocol @@ -1459,11 +1510,11 @@

Python Type System Conformance Test Results

3 / 4 • 75.0% - 4 / 4 • 100.0% - 4 / 4 • 100.0% - 4 / 4 • 100.0% - 4 / 4 • 100.0% - 4 / 4 • 100.0% + 3.5 / 4 • 87.5% + 3.5 / 4 • 87.5% + 3.5 / 4 • 87.5% + 3.5 / 4 • 87.5% + 3.5 / 4 • 87.5% @@ -2487,11 +2538,11 @@

Python Type System Conformance Test Results

110.5 / 146 • 75.7% - 139 / 146 • 95.2% - 146 / 146 • 100.0% - 140.5 / 146 • 96.2% - 142 / 146 • 97.3% - 146 / 146 • 100.0% + 138.5 / 146 • 94.9% + 145.5 / 146 • 99.7% + 140 / 146 • 95.9% + 141.5 / 146 • 96.9% + 145.5 / 146 • 99.7% diff --git a/conformance/results/ty/callables_kwargs.toml b/conformance/results/ty/callables_kwargs.toml index 3318d6bb6..1fdc13ef7 100644 --- a/conformance/results/ty/callables_kwargs.toml +++ b/conformance/results/ty/callables_kwargs.toml @@ -1,20 +1,56 @@ -conformance_automated = "Pass" +conformant = "Partial" +conformance_automated = "Fail" +notes = """ +Does not reject unpacking an open TypedDict in a call to a callable that has no `**kwargs`. +Does not reject unpacking an open TypedDict in a call to a callable whose `**kwargs` is typed with a closed TypedDict. +Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's `**kwargs: Unpack[...]` TypedDict. +""" errors_diff = """ +Line 354: Expected 1 errors +Line 357: Expected 1 errors +Line 360: Expected 1 errors +Line 361: Expected 1 errors +Line 373: Expected 1 errors +Line 376: Expected 1 errors +Line 379: Expected 1 errors +Line 380: Expected 1 errors +Line 444: Expected 1 errors +Line 445: Expected 1 errors """ output = """ -callables_kwargs.py:46:5: error[missing-argument] No arguments provided for required parameters `v1`, `v3` of function `func1` -callables_kwargs.py:52:5: error[missing-argument] No arguments provided for required parameters `v1`, `v3` of function `func1` -callables_kwargs.py:52:11: error[too-many-positional-arguments] Too many positional arguments to function `func1`: expected 0, got 3 -callables_kwargs.py:58:11: error[invalid-argument-type] Argument to function `func1` is incorrect: Expected `int`, found `str` -callables_kwargs.py:61:11: error[invalid-argument-type] Argument to function `func1` is incorrect: Expected `str`, found `int | str` -callables_kwargs.py:63:17: error[parameter-already-assigned] Multiple values provided for parameter `v1` of function `func1` -callables_kwargs.py:64:11: error[invalid-argument-type] Argument to function `func2` is incorrect: Expected `str`, found `Literal[1]` -callables_kwargs.py:64:14: error[parameter-already-assigned] Multiple values provided for parameter `v3` of function `func2` -callables_kwargs.py:65:17: error[parameter-already-assigned] Multiple values provided for parameter `v1` of function `func2` -callables_kwargs.py:101:19: error[invalid-assignment] Object of type `def func1(*, v1: int, v2: str = ..., v3: str, **kwargs: object) -> None` is not assignable to `TDProtocol3` -callables_kwargs.py:102:19: error[invalid-assignment] Object of type `def func1(*, v1: int, v2: str = ..., v3: str, **kwargs: object) -> None` is not assignable to `TDProtocol4` -callables_kwargs.py:103:19: error[invalid-assignment] Object of type `def func1(*, v1: int, v2: str = ..., v3: str, **kwargs: object) -> None` is not assignable to `TDProtocol5` -callables_kwargs.py:111:20: error[invalid-type-form] Parameter `v1` overlaps with unpacked TypedDict key in `**kwargs` annotation -callables_kwargs.py:122:21: error[invalid-type-form] Unpacked value for `**kwargs` must be a TypedDict, not `T@func6` -callables_kwargs.py:134:19: error[invalid-assignment] Object of type `def func7(*, v1: int, v3: str, v2: str = "") -> None` is not assignable to `TDProtocol6` +callables_kwargs.py:55:5: error[missing-argument] No arguments provided for required parameters `v1`, `v3` of function `func1` +callables_kwargs.py:61:5: error[missing-argument] No arguments provided for required parameters `v1`, `v3` of function `func1` +callables_kwargs.py:61:11: error[too-many-positional-arguments] Too many positional arguments to function `func1`: expected 0, got 3 +callables_kwargs.py:67:11: error[invalid-argument-type] Argument to function `func1` is incorrect: Expected `int`, found `str` +callables_kwargs.py:70:11: error[invalid-argument-type] Argument to function `func1` is incorrect: Expected `str`, found `int | str` +callables_kwargs.py:72:17: error[parameter-already-assigned] Multiple values provided for parameter `v1` of function `func1` +callables_kwargs.py:73:11: error[invalid-argument-type] Argument to function `func2` is incorrect: Expected `str`, found `Literal[1]` +callables_kwargs.py:73:14: error[parameter-already-assigned] Multiple values provided for parameter `v3` of function `func2` +callables_kwargs.py:74:17: error[parameter-already-assigned] Multiple values provided for parameter `v1` of function `func2` +callables_kwargs.py:121:19: error[invalid-assignment] Object of type `def func1(*, v1: int, v2: str = ..., v3: str, **kwargs: object) -> None` is not assignable to `TDProtocol3` +callables_kwargs.py:122:19: error[invalid-assignment] Object of type `def func1(*, v1: int, v2: str = ..., v3: str, **kwargs: object) -> None` is not assignable to `TDProtocol4` +callables_kwargs.py:123:19: error[invalid-assignment] Object of type `def func1(*, v1: int, v2: str = ..., v3: str, **kwargs: object) -> None` is not assignable to `TDProtocol5` +callables_kwargs.py:131:20: error[invalid-type-form] Parameter `v1` overlaps with unpacked TypedDict key in `**kwargs` annotation +callables_kwargs.py:142:21: error[invalid-type-form] Unpacked value for `**kwargs` must be a TypedDict, not `T@func6` +callables_kwargs.py:150:19: error[invalid-assignment] Object of type `def func7(*, v1: int, v3: str, v2: str = "") -> None` is not assignable to `TDProtocol6` +callables_kwargs.py:170:9: error[incompatible-method-override] Invalid override of method `method`: Definition is incompatible with `BaseUnpacked.method` +callables_kwargs.py:205:20: error[invalid-assignment] Object of type `def func9(*, x: int, y: int = ..., **kwargs: object) -> None` is not assignable to `TDProtocol9` +callables_kwargs.py:356:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `str` +callables_kwargs.py:359:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `str` +callables_kwargs.py:366:27: error[invalid-argument-type] Argument to function `takes_extra_items_int` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `int` +callables_kwargs.py:367:27: error[invalid-argument-type] Argument to function `takes_extra_items_int` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `int` +callables_kwargs.py:375:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `str` +callables_kwargs.py:378:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `str` +callables_kwargs.py:385:27: error[invalid-argument-type] Argument to function `takes_extra_items_int` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `int` +callables_kwargs.py:386:27: error[invalid-argument-type] Argument to function `takes_extra_items_int` is incorrect: Possible extra items in unpacked open `TypedDict` have type `object`, expected `int` +callables_kwargs.py:395:27: error[unknown-argument] Argument `year` does not match any known parameter of function `takes_closed` +callables_kwargs.py:397:36: error[invalid-argument-type] Argument to function `takes_extra_items_int` is incorrect: Expected `int`, found `Literal[""]` +callables_kwargs.py:416:16: error[unknown-argument] Unpacked argument may contain keyword arguments that do not match any known parameter of function `takes_name` +callables_kwargs.py:417:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Expected `str`, found `int` +callables_kwargs.py:419:16: error[unknown-argument] Unpacked argument may contain keyword arguments that do not match any known parameter of function `takes_name` +callables_kwargs.py:420:27: error[invalid-argument-type] Argument to function `takes_name_str_kwargs` is incorrect: Expected `str`, found `int` +callables_kwargs.py:422:18: error[unknown-argument] Unpacked argument may contain keyword arguments that do not match any known parameter of function `takes_closed` +callables_kwargs.py:423:18: error[unknown-argument] Unpacked argument may contain keyword arguments that do not match any known parameter of function `takes_closed` +callables_kwargs.py:424:37: error[invalid-argument-type] Argument to function `takes_optional_label_int_kwargs` is incorrect: Expected `str`, found `int` +callables_kwargs.py:425:37: error[invalid-argument-type] Argument to function `takes_optional_label_int_kwargs` is incorrect: Expected `str`, found `int` """ diff --git a/conformance/results/zuban/callables_kwargs.toml b/conformance/results/zuban/callables_kwargs.toml index 678f5d318..55674cef0 100644 --- a/conformance/results/zuban/callables_kwargs.toml +++ b/conformance/results/zuban/callables_kwargs.toml @@ -1,25 +1,70 @@ -conformance_automated = "Pass" +conformant = "Partial" +notes = """ +Does not reject unpacking an open TypedDict in a call to a callable that has no `**kwargs`. +Does not reject unpacking an open TypedDict in a call to a callable whose `**kwargs` is typed with a closed TypedDict. +Does not reject unpacking an open TypedDict in a call to a callable whose `**kwargs` is typed with a TypedDict whose `extra_items` type is narrower than `object`. +Does not check an unpacked open TypedDict's extra items against non-required keys of the callee's `**kwargs: Unpack[...]` TypedDict. +Incorrectly rejects unpacking a TypedDict with an extra key when calling a function with `**kwargs` typed with an open TypedDict. +""" +conformance_automated = "Fail" errors_diff = """ +Line 354: Expected 1 errors +Line 357: Expected 1 errors +Line 360: Expected 1 errors +Line 361: Expected 1 errors +Line 366: Expected 1 errors +Line 367: Expected 1 errors +Line 373: Expected 1 errors +Line 376: Expected 1 errors +Line 379: Expected 1 errors +Line 380: Expected 1 errors +Line 385: Expected 1 errors +Line 386: Expected 1 errors +Line 444: Expected 1 errors +Line 445: Expected 1 errors +Line 401: Unexpected errors ['callables_kwargs.py:401: error: Extra argument "y" from **args for "func8" [call-arg]'] """ output = """ -callables_kwargs.py:46: error: Missing named argument "v1" for "func1" [call-arg] -callables_kwargs.py:46: error: Missing named argument "v3" for "func1" [call-arg] -callables_kwargs.py:51: error: Unexpected keyword argument "v4" for "func1" [call-arg] -callables_kwargs.py:52: error: Too many positional arguments for "func1" [call-arg] -callables_kwargs.py:52: error: Missing named argument "v1" for "func1" [call-arg] -callables_kwargs.py:52: error: Missing named argument "v3" for "func1" [call-arg] -callables_kwargs.py:58: error: Argument 1 to "func1" has incompatible type "**dict[str, str]"; expected "int" [arg-type] -callables_kwargs.py:61: error: Argument 1 to "func1" has incompatible type "**dict[str, int | str]"; expected "int" [arg-type] -callables_kwargs.py:61: error: Argument 1 to "func1" has incompatible type "**dict[str, int | str]"; expected "str" [arg-type] -callables_kwargs.py:61: error: Argument 1 to "func1" has incompatible type "**dict[str, int | str]"; expected "str" [arg-type] -callables_kwargs.py:63: error: "func1" gets multiple values for keyword argument "v1" [call-arg] -callables_kwargs.py:64: error: Argument 1 to "func2" has incompatible type "int"; expected "str" [arg-type] -callables_kwargs.py:64: error: "func2" gets multiple values for keyword argument "v3" [call-arg] -callables_kwargs.py:65: error: "func2" gets multiple values for keyword argument "v1" [call-arg] -callables_kwargs.py:101: error: Incompatible types in assignment (expression has type "def func1(**kwargs: Unpack[TD2]) -> None", variable has type "TDProtocol3") [assignment] -callables_kwargs.py:102: error: Incompatible types in assignment (expression has type "def func1(**kwargs: Unpack[TD2]) -> None", variable has type "TDProtocol4") [assignment] -callables_kwargs.py:103: error: Incompatible types in assignment (expression has type "def func1(**kwargs: Unpack[TD2]) -> None", variable has type "TDProtocol5") [assignment] -callables_kwargs.py:111: error: Overlap between parameter names and ** TypedDict items: "v1" [misc] -callables_kwargs.py:122: error: Unpack item in ** parameter must be a TypedDict [misc] -callables_kwargs.py:134: error: Incompatible types in assignment (expression has type "def func7(*, v1: int, v3: str, v2: str = ...) -> None", variable has type "TDProtocol6") [assignment] +callables_kwargs.py:55: error: Missing named argument "v1" for "func1" [call-arg] +callables_kwargs.py:55: error: Missing named argument "v3" for "func1" [call-arg] +callables_kwargs.py:60: error: Unexpected keyword argument "v4" for "func1" [call-arg] +callables_kwargs.py:61: error: Too many positional arguments for "func1" [call-arg] +callables_kwargs.py:61: error: Missing named argument "v1" for "func1" [call-arg] +callables_kwargs.py:61: error: Missing named argument "v3" for "func1" [call-arg] +callables_kwargs.py:67: error: Argument 1 to "func1" has incompatible type "**dict[str, str]"; expected "int" [arg-type] +callables_kwargs.py:70: error: Argument 1 to "func1" has incompatible type "**dict[str, int | str]"; expected "int" [arg-type] +callables_kwargs.py:70: error: Argument 1 to "func1" has incompatible type "**dict[str, int | str]"; expected "str" [arg-type] +callables_kwargs.py:70: error: Argument 1 to "func1" has incompatible type "**dict[str, int | str]"; expected "str" [arg-type] +callables_kwargs.py:72: error: "func1" gets multiple values for keyword argument "v1" [call-arg] +callables_kwargs.py:73: error: Argument 1 to "func2" has incompatible type "int"; expected "str" [arg-type] +callables_kwargs.py:73: error: "func2" gets multiple values for keyword argument "v3" [call-arg] +callables_kwargs.py:74: error: "func2" gets multiple values for keyword argument "v1" [call-arg] +callables_kwargs.py:121: error: Incompatible types in assignment (expression has type "def func1(**kwargs: Unpack[TD2]) -> None", variable has type "TDProtocol3") [assignment] +callables_kwargs.py:122: error: Incompatible types in assignment (expression has type "def func1(**kwargs: Unpack[TD2]) -> None", variable has type "TDProtocol4") [assignment] +callables_kwargs.py:123: error: Incompatible types in assignment (expression has type "def func1(**kwargs: Unpack[TD2]) -> None", variable has type "TDProtocol5") [assignment] +callables_kwargs.py:131: error: Overlap between parameter names and ** TypedDict items: "v1" [misc] +callables_kwargs.py:142: error: Unpack item in ** parameter must be a TypedDict [misc] +callables_kwargs.py:150: error: Incompatible types in assignment (expression has type "def func7(*, v1: int, v3: str, v2: str = ...) -> None", variable has type "TDProtocol6") [assignment] +callables_kwargs.py:170: error: Signature of "method" incompatible with supertype "BaseUnpacked" [override] +callables_kwargs.py:170: note: Superclass: +callables_kwargs.py:170: note: def method(self, **kwargs: Unpack[TDPerson]) -> None +callables_kwargs.py:170: note: Subclass: +callables_kwargs.py:170: note: def method(self, *, name: str, age: int) -> None +callables_kwargs.py:205: error: Incompatible types in assignment (expression has type "def func9(**kwargs: Unpack[TDChild]) -> None", variable has type "TDProtocol9") [assignment] +callables_kwargs.py:356: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TDOpenImplicit"; expected "str" [arg-type] +callables_kwargs.py:359: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TDOpenImplicit"; expected "str" [arg-type] +callables_kwargs.py:375: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TDOpenExplicit"; expected "str" [arg-type] +callables_kwargs.py:378: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TDOpenExplicit"; expected "str" [arg-type] +callables_kwargs.py:394: error: Unexpected keyword argument "year" for "takes_open" [call-arg] +callables_kwargs.py:395: error: Unexpected keyword argument "year" for "takes_closed" [call-arg] +callables_kwargs.py:397: error: Argument "year" to "takes_extra_items_int" has incompatible type "str"; expected "int" [arg-type] +callables_kwargs.py:401: error: Extra argument "y" from **args for "func8" [call-arg] +callables_kwargs.py:416: error: Too many arguments for "takes_name" [call-arg] +callables_kwargs.py:417: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TDExtraItems"; expected "str" [arg-type] +callables_kwargs.py:419: error: Too many arguments for "takes_name" [call-arg] +callables_kwargs.py:420: error: Argument 1 to "takes_name_str_kwargs" has incompatible type "**TDExtraItems"; expected "str" [arg-type] +callables_kwargs.py:422: error: Argument 1 to "takes_closed" has incompatible type "**TDExtraItems"; expected "Never" [arg-type] +callables_kwargs.py:423: error: Argument 1 to "takes_closed" has incompatible type "**TDExtraItems"; expected "Never" [arg-type] +callables_kwargs.py:424: error: Argument 1 to "takes_optional_label_int_kwargs" has incompatible type "**TDExtraItems"; expected "str" [arg-type] +callables_kwargs.py:425: error: Argument 1 to "takes_optional_label_int_kwargs" has incompatible type "**TDExtraItems"; expected "str" [arg-type] """ diff --git a/conformance/tests/callables_kwargs.py b/conformance/tests/callables_kwargs.py index a4c431dbd..4de5ba28a 100644 --- a/conformance/tests/callables_kwargs.py +++ b/conformance/tests/callables_kwargs.py @@ -7,7 +7,8 @@ # This sample tests the handling of Unpack[TypedDict] when used with # a **kwargs parameter in a function signature. -from typing import Protocol, TypeVar, TypedDict, NotRequired, Required, Unpack, assert_type +from typing import Protocol, TypeVar, Never, NotRequired, Required, Unpack, assert_type +from typing_extensions import TypedDict # for the `closed` and `extra_items` arguments class TD1(TypedDict): @@ -19,6 +20,14 @@ class TD2(TD1): v3: Required[str] +class TD3(TD1, closed=True): + v3: Required[str] + + +class TD4(TD1, extra_items=Never): + v3: Required[str] + + def func1(**kwargs: Unpack[TD2]) -> None: v1 = kwargs["v1"] assert_type(v1, int) @@ -94,7 +103,18 @@ class TDProtocol6(Protocol): def __call__(self, **kwargs: Unpack[TD2]) -> None: ... -# Specification: https://typing.readthedocs.io/en/latest/spec/callables.html#assignment + +class TDProtocol7(Protocol): + def __call__(self, **kwargs: Unpack[TD3]) -> None: + ... + + +class TDProtocol8(Protocol): + def __call__(self, **kwargs: Unpack[TD4]) -> None: + ... + + +# Specification: https://typing.readthedocs.io/en/latest/spec/callables.html#desugar-unpack-signature v1: TDProtocol1 = func1 # OK v2: TDProtocol2 = func1 # OK @@ -122,13 +142,316 @@ def func5(v1: int, **kwargs: Unpack[TD2]) -> None: # E: parameter v1 overlaps w def func6(**kwargs: Unpack[T]) -> None: # E: unpacked value must be a TypedDict, not a TypeVar bound to TypedDict. ... -# > The situation where the destination callable contains **kwargs: Unpack[TypedDict] and -# > the source callable doesn’t contain **kwargs should be disallowed. This is because, -# > we cannot be sure that additional keyword arguments are not being passed in when an instance of a subclass -# > had been assigned to a variable with a base class type and then unpacked in the destination callable invocation def func7(*, v1: int, v3: str, v2: str = "") -> None: ... v7: TDProtocol6 = func7 # E: source does not have kwargs +v8: TDProtocol7 = func7 # OK, unpacked TypedDict has closed=True +v9: TDProtocol8 = func7 # OK, unpacked TypedDict has extra_items=Never + + +# An override without **kwargs is not compatible with a base method +# whose **kwargs is an unpacked open TypedDict. + + +class TDPerson(TypedDict): + name: str + age: int + + +class BaseUnpacked: + def method(self, **kwargs: Unpack[TDPerson]) -> None: + ... + + +class SubNoKwargs(BaseUnpacked): + def method(self, *, name: str, age: int) -> None: # E: base accepts **kwargs, override does not + ... + + +# An open TypedDict used to annotate **kwargs is treated as having extra items +# of type `object`, so a callable that accepts additional non-required keys +# cannot be assigned to one that only accepts the base TypedDict's keys. + + +class TDBase(TypedDict): + x: int + + +class TDChild(TDBase, total=False): + y: int + + +class TDProtocol9(Protocol): + def __call__(self, **kwargs: Unpack[TDBase]) -> None: + ... + + +class TDProtocol10(Protocol): + def __call__(self, **kwargs: Unpack[TDChild]) -> None: + ... + + +def func8(**kwargs: Unpack[TDBase]) -> None: + ... + + +def func9(**kwargs: Unpack[TDChild]) -> None: + ... + + +v10: TDProtocol9 = func9 # E: `y` may be passed as an extra item of type `object` +v11: TDProtocol10 = func8 # OK + + +# An Unpack annotation is equivalent to its desugared signature. + + +class TDClosedInt(TypedDict, closed=True): + x: int + + +class TDExtraInt(TypedDict, extra_items=int): + x: int + + +class TDProtocol11(Protocol): + def __call__(self, *, x: int, **kwargs: object) -> None: + ... + + +class TDProtocol12(Protocol): + def __call__(self, **kwargs: Unpack[TDClosedInt]) -> None: + ... + + +class TDProtocol13(Protocol): + def __call__(self, *, x: int) -> None: + ... + + +class TDProtocol14(Protocol): + def __call__(self, **kwargs: Unpack[TDExtraInt]) -> None: + ... + + +class TDProtocol15(Protocol): + def __call__(self, *, x: int, **kwargs: int) -> None: + ... + + +def func10(*, x: int, **kwargs: object) -> None: + ... + + +def func11(**kwargs: Unpack[TDClosedInt]) -> None: + ... + + +def func12(*, x: int) -> None: + ... + + +def func13(**kwargs: Unpack[TDExtraInt]) -> None: + ... + + +def func14(*, x: int, **kwargs: int) -> None: + ... + + +v12: TDProtocol11 = func8 # OK +v13: TDProtocol9 = func10 # OK +v14: TDProtocol13 = func11 # OK +v15: TDProtocol12 = func12 # OK +v16: TDProtocol15 = func13 # OK +v17: TDProtocol14 = func14 # OK + + +class TDClosedObject(TypedDict, closed=True): + x: object + + +def func15(**kwargs: Unpack[TDClosedObject]) -> None: + ... + + +v18: TDProtocol12 = func15 # OK + + +# Specification: https://typing.readthedocs.io/en/latest/spec/callables.html#unpacking-a-typeddict-as-keyword-argument-types + + +class TDOpenImplicit(TypedDict): + name: str + + +class TDOpenExplicit(TypedDict, closed=False): + name: str + + +class TDClosed(TypedDict, closed=True): + name: str + + +class TDExtraItems(TypedDict, extra_items=int): + name: str + + +class TDExtraItemsNever(TypedDict, extra_items=Never): + name: str + + +class TDExtraItemsObject(TypedDict, extra_items=object): + name: str + + +def takes_name(name: str) -> None: + ... + + +def takes_name_kwargs(name: str, **kwargs) -> None: + ... + + +def takes_name_object_kwargs(name: str, **kwargs: object) -> None: + ... + + +def takes_name_str_kwargs(name: str, **kwargs: str) -> None: + ... + + +def takes_name_int_kwargs(name: str, **kwargs: int) -> None: + ... + + +def takes_optional_label_int_kwargs(name: str, *, label: str = "", **kwargs: int) -> None: + ... + + +def takes_open(**kwargs: Unpack[TDOpenImplicit]) -> None: + ... + + +def takes_closed(**kwargs: Unpack[TDClosed]) -> None: + ... + + +# Equivalent to `def takes_extra_items_object(*, name: str, **kwargs: object)`. +def takes_extra_items_object(**kwargs: Unpack[TDExtraItemsObject]) -> None: + ... + + +# Equivalent to `def takes_extra_items_int(*, name: str, **kwargs: int)`. +def takes_extra_items_int(**kwargs: Unpack[TDExtraItems]) -> None: + ... + + +def open_implicit(value: TDOpenImplicit, **kwargs: Unpack[TDOpenImplicit]) -> None: + takes_name(**value) # E: a subtype may contain unknown keys + takes_name_kwargs(**value) + takes_name_str_kwargs(**value) # E: extra items type is not compatible + takes_name(**kwargs) # E: a subtype may contain unknown keys + takes_name_kwargs(**kwargs) + takes_name_str_kwargs(**kwargs) # E: extra items type is not compatible + takes_closed(**value) # E: a subtype may contain unknown keys + takes_closed(**kwargs) # E: a subtype may contain unknown keys + takes_name_object_kwargs(**value) + takes_name_object_kwargs(**kwargs) + takes_extra_items_object(**value) + takes_extra_items_object(**kwargs) + takes_extra_items_int(**value) # E: extra items type is not compatible + takes_extra_items_int(**kwargs) # E: extra items type is not compatible + takes_open(**value) + takes_open(**kwargs) + + +def open_explicit(value:TDOpenExplicit, **kwargs: Unpack[TDOpenExplicit]) -> None: + takes_name(**value) # E: a subtype may contain unknown keys + takes_name_kwargs(**value) + takes_name_str_kwargs(**value) # E: extra items type is not compatible + takes_name(**kwargs) # E: a subtype may contain unknown keys + takes_name_kwargs(**kwargs) + takes_name_str_kwargs(**kwargs) # E: extra items type is not compatible + takes_closed(**value) # E: a subtype may contain unknown keys + takes_closed(**kwargs) # E: a subtype may contain unknown keys + takes_name_object_kwargs(**value) + takes_name_object_kwargs(**kwargs) + takes_extra_items_object(**value) + takes_extra_items_object(**kwargs) + takes_extra_items_int(**value) # E: extra items type is not compatible + takes_extra_items_int(**kwargs) # E: extra items type is not compatible + takes_open(**value) + takes_open(**kwargs) + + +def explicit_extra_keyword() -> None: + # > If the ``TypedDict`` used for annotating ``**kwargs`` is :term:`open`, type checkers may emit + # > an error if an unrecognized argument is provided explicitly + takes_open(name="", year=2007) # E?: unrecognized keyword argument + takes_closed(name="", year=2007) # E: unrecognized keyword argument + takes_extra_items_int(name="", year=2007) # OK + takes_extra_items_int(name="", year="") # E: extra keyword argument must be `int` + + +def forward(child: TDChild) -> None: + func8(**child) # OK + + +def kwargs_closed(value: TDClosed, **kwargs: Unpack[TDClosed]) -> None: + takes_name(**value) + takes_name_kwargs(**value) + takes_name_str_kwargs(**value) + takes_name(**kwargs) + takes_name_kwargs(**kwargs) + takes_name_str_kwargs(**kwargs) + takes_closed(**value) + takes_closed(**kwargs) + + +def kwargs_extra_items(value: TDExtraItems, **kwargs: Unpack[TDExtraItems]) -> None: + takes_name(**value) # E: extra items may be present + takes_name_str_kwargs(**value) # E: extra items type is not compatible + takes_name_int_kwargs(**value) + takes_name(**kwargs) # E: extra items may be present + takes_name_str_kwargs(**kwargs) # E: extra items type is not compatible + takes_name_int_kwargs(**kwargs) + takes_closed(**value) # E: extra items may be present + takes_closed(**kwargs) # E: extra items may be present + takes_optional_label_int_kwargs(**value) # E: extra items type is not compatible w/ `label` param + takes_optional_label_int_kwargs(**kwargs) # E: extra items type is not compatible w/ `label` param + takes_extra_items_object(**value) + takes_extra_items_object(**kwargs) + takes_extra_items_int(**value) + takes_extra_items_int(**kwargs) + + +def kwargs_extra_items_never(value: TDExtraItemsNever, **kwargs: Unpack[TDExtraItemsNever]) -> None: + takes_name(**value) + takes_name_kwargs(**value) + takes_name_str_kwargs(**value) + takes_name(**kwargs) + takes_name_kwargs(**kwargs) + takes_name_str_kwargs(**kwargs) + takes_closed(**value) + takes_closed(**kwargs) + + +def unpack_into_unpack(value: TDBase, **kwargs: Unpack[TDBase]) -> None: + func9(**value) # E: an extra item of type `object` may be passed as `y` + func9(**kwargs) # E: an extra item of type `object` may be passed as `y` + + +class TDNotRequiredExtraStr(TypedDict, extra_items=str): + x: NotRequired[int] + + +def takes_optional_x_str_kwargs(*, x: int = 0, **kwargs: str) -> None: + ... + + +def declared_key_not_extra(value: TDNotRequiredExtraStr) -> None: + takes_optional_x_str_kwargs(**value) # OK: extra items cannot have `x` diff --git a/conformance/tests/pyrefly.toml b/conformance/tests/pyrefly.toml index c9c44e591..a9b656bd7 100644 --- a/conformance/tests/pyrefly.toml +++ b/conformance/tests/pyrefly.toml @@ -1 +1,2 @@ -# Empty configuration file to make sure pyrefly treats this directory as the project root. +[errors] +open-unpacking = "error" \ No newline at end of file diff --git a/docs/spec/callables.rst b/docs/spec/callables.rst index f23220400..ba679043a 100644 --- a/docs/spec/callables.rst +++ b/docs/spec/callables.rst @@ -121,7 +121,7 @@ This second usage is described in this section. The following example:: from typing import TypedDict, Unpack - class Movie(TypedDict): + class Movie(TypedDict, closed=True): name: str year: int @@ -142,25 +142,55 @@ function body as a ``TypedDict``:: def foo(**kwargs: Unpack[Movie]) -> None: assert_type(kwargs, Movie) # OK! +.. _desugar-unpack-signature: -Function calls with standard dictionaries -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Desugaring ``Unpack`` in function signatures +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Passing a dictionary of type ``dict[str, object]`` as a ``**kwargs`` argument -to a function that has ``**kwargs`` annotated with ``Unpack`` must generate a -type checker error. On the other hand, the behavior for functions using -standard, untyped dictionaries can depend on the type checker. For example:: +For checks on a function signature, ``**kwargs`` that are annotated with ``Unpack`` can be desugared +into regular keyword parameters using the following rules. - def func(**kwargs: Unpack[Movie]) -> None: ... +1. All :term:`required` items are turned into required keyword-only parameters of the same type. +2. All :term:`non-required` items are turned into optional keyword-only parameters of the same type. +3. An explicit :term:`extra items` is turned into ``**kwargs`` of the same type. +4. If the ``TypedDict`` is :term:`open`, add ``**kwargs: object``. - movie: dict[str, object] = {"name": "Life of Brian", "year": 1979} - func(**movie) # WRONG! Movie is of type dict[str, object] +For example:: - typed_movie: Movie = {"name": "The Meaning of Life", "year": 1983} - func(**typed_movie) # OK! + class MovieOpen(TypedDict): + name: str + year: NotRequired[int] - another_movie = {"name": "Life of Brian", "year": 1979} - func(**another_movie) # Depends on the type checker. + class MovieClosed(TypedDict, closed=True): + name: str + year: NotRequired[int] + + class MovieExtra(TypedDict, extra_items=int): + name: str + year: NotRequired[int] + + def takes_open(**kwargs: Unpack[MovieOpen]) -> None: ... + def takes_extra(**kwargs: Unpack[MovieExtra]) -> None: ... + def takes_closed(**kwargs: Unpack[MovieClosed]) -> None: ... + + # These desugar to: + def takes_open(*, name: str, year: int = ..., **kwargs: object) -> None: ... + def takes_extra(*, name: str, year: int = ..., **kwargs: int) -> None: ... + def takes_closed(*, name: str, year: int = ...) -> None: ... + +After desugaring, the normal callable rules should be applied, with one exception: +if the ``TypedDict`` used for annotating ``**kwargs`` is :term:`open`, type checkers may emit +an error if an unrecognized argument is provided explicitly:: + + takes_open(name="No Country for Old Men", rating=5) # Unrecognized item, maybe error + takes_extra(name="No Country for Old Men", rating=5) # OK + takes_closed(name="No Country for Old Men", rating=5) # Unrecognized item, error + + class RatedMovie(MovieOpen): + rating: int + + movie: MovieOpen = RatedMovie(name="No Country for Old Men", rating=5) + takes_open(**movie) # OK: "rating" is supplied via unpacking Keyword collisions ^^^^^^^^^^^^^^^^^^ @@ -180,15 +210,6 @@ generated. For example:: # so **kwargs can contain # a "name" keyword. -Required and non-required items -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Items in a TypedDict may be either :term:`required` or :term:`non-required`. -When using a ``TypedDict`` to type ``**kwargs`` all of the required and -non-required keys should correspond to required and non-required function -keyword parameters. Therefore, if a required key is not provided by the -caller, then an error must be reported by type checkers. - Read-only items ^^^^^^^^^^^^^^^ @@ -212,220 +233,39 @@ However, it *will* prevent the item from being modified in the body of the funct fn: Function = impl # Accepted by type checker: function signatures are identical -Extra items -^^^^^^^^^^^ - -If the TypedDict used for annotating ``**kwargs`` is defined to allow -:term:`extra items`, arbitrary additional keyword arguments of the right -type may be passed to the function:: - - class MovieNoExtra(TypedDict): - name: str - - class MovieExtra(TypedDict, extra_items=int): - name: str - - def f(**kwargs: Unpack[MovieNoExtra]) -> None: ... - def g(**kwargs: Unpack[MovieExtra]) -> None: ... - - # Should be equivalent to: - def f(*, name: str) -> None: ... - def g(*, name: str, **kwargs: int) -> None: ... - - f(name="No Country for Old Men", year=2007) # Not OK. Unrecognized item - g(name="No Country for Old Men", year=2007) # OK - -Assignment -^^^^^^^^^^ - -Assignments of a function typed with ``**kwargs: Unpack[Movie]`` and another -callable type should pass type checking only for the scenarios described below. - -Source and destination contain ``**kwargs`` -""""""""""""""""""""""""""""""""""""""""""" - -Both destination and source functions have a ``**kwargs: Unpack[TypedDict]`` -parameter and the destination function's ``TypedDict`` is :term:`assignable` to -the source function's ``TypedDict`` and the rest of the parameters are -assignable:: - - class Animal(TypedDict): - name: str - - class Dog(Animal): - breed: str - - def accept_animal(**kwargs: Unpack[Animal]): ... - def accept_dog(**kwargs: Unpack[Dog]): ... - - accept_dog = accept_animal # OK! Expression of type Dog can be - # assigned to a variable of type Animal. - - accept_animal = accept_dog # WRONG! Expression of type Animal - # cannot be assigned to a variable of type Dog. - -.. _PEP 692 assignment dest no kwargs: - -Source contains ``**kwargs`` and destination doesn't -"""""""""""""""""""""""""""""""""""""""""""""""""""" - -The destination callable doesn't contain ``**kwargs``, the source callable -contains ``**kwargs: Unpack[TypedDict]`` and the destination function's keyword -arguments are :term:`assignable` to the corresponding keys in source function's -``TypedDict``. Moreover, not required keys should correspond to optional -function arguments, whereas required keys should correspond to required -function arguments. Again, the rest of the parameters have to be assignable. -Continuing the previous example:: - - class Example(TypedDict): - animal: Animal - string: str - number: NotRequired[int] - - def src(**kwargs: Unpack[Example]): ... - def dest(*, animal: Dog, string: str, number: int = ...): ... +Unpacking a ``TypedDict`` as keyword argument types +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - dest = src # OK! +A ``TypedDict`` that is unpacked in a function call may be converted into normal keyword arguments using the following rules: -It is worth pointing out that the destination function's parameters that are to -be assignable to the keys and values from the ``TypedDict`` must be keyword -only:: +1. All :term:`required` items are turned into keyword arguments of the same name & type. +2. All :term:`non-required` items are turned into keyword arguments of the same name & type, which may be absent. +3. An explicit :term:`extra items` is turned into an arbitrary number of keyword arguments of the same type. +4. An :term:`open` ``TypedDict`` provides an arbitrary number of keyword arguments of type ``object``. - def dest(dog: Dog, string: str, number: int = ...): ... +If the ``TypedDict`` can contain extra items, the callee's desugared signature must accept arbitrary keyword names. - dog: Dog = {"name": "Daisy", "breed": "labrador"} +Type checkers should ensure the extra arguments are compatible with the type of the callee's ``**kwargs``, +as well as any keyword parameters an extra item could bind to. - dest(dog, "some string") # OK! - - dest = src # Type checker error! - dest(dog, "some string") # The same call fails at - # runtime now because 'src' expects - # keyword arguments. - -The reverse situation where the destination callable contains -``**kwargs: Unpack[TypedDict]`` and the source callable doesn't contain -``**kwargs`` should be disallowed. This is because, we cannot be sure that -additional keyword arguments are not being passed in when an instance of a -subclass had been assigned to a variable with a base class type and then -unpacked in the destination callable invocation:: - - def dest(**kwargs: Unpack[Animal]): ... - def src(name: str): ... - - dog: Dog = {"name": "Daisy", "breed": "Labrador"} - animal: Animal = dog - - dest = src # WRONG! - dest(**animal) # Fails at runtime. - -A similar situation can happen even without inheritance as :term:`assignability -` between ``TypedDict``\s is :term:`structural`. - -Source contains untyped ``**kwargs`` -"""""""""""""""""""""""""""""""""""" - -The destination callable contains ``**kwargs: Unpack[TypedDict]`` and the -source callable contains untyped ``**kwargs``:: - - def src(**kwargs): ... - def dest(**kwargs: Unpack[Movie]): ... - - dest = src # OK! - -Source contains traditionally typed ``**kwargs: T`` -""""""""""""""""""""""""""""""""""""""""""""""""""" - -The destination callable contains ``**kwargs: Unpack[TypedDict]``, the source -callable contains traditionally typed ``**kwargs: T`` and each of the -destination function ``TypedDict``'s fields is :term:`assignable` to a variable -of type ``T``:: - - class Vehicle: - ... - - class Car(Vehicle): - ... - - class Motorcycle(Vehicle): - ... - - class Vehicles(TypedDict): - car: Car - moto: Motorcycle - - def dest(**kwargs: Unpack[Vehicles]): ... - def src(**kwargs: Vehicle): ... - - dest = src # OK! - -On the other hand, if the destination callable contains either untyped or -traditionally typed ``**kwargs: T`` and the source callable is typed using -``**kwargs: Unpack[TypedDict]`` then an error should be generated, because -traditionally typed ``**kwargs`` aren't checked for keyword names. - -To summarize, function parameters should behave contravariantly and function -return types should behave covariantly. - -Passing kwargs inside a function to another function -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -:ref:`A previous point ` -mentions the problem of possibly passing additional keyword arguments by -assigning a subclass instance to a variable that has a base class type. Let's -consider the following example:: - - class Animal(TypedDict): - name: str - - class Dog(Animal): - breed: str - - def takes_name(name: str): ... - - dog: Dog = {"name": "Daisy", "breed": "Labrador"} - animal: Animal = dog - - def foo(**kwargs: Unpack[Animal]): - print(kwargs["name"].capitalize()) - - def bar(**kwargs: Unpack[Animal]): - takes_name(**kwargs) - - def baz(animal: Animal): - takes_name(**animal) - - def spam(**kwargs: Unpack[Animal]): - baz(kwargs) - - foo(**animal) # OK! foo only expects and uses keywords of 'Animal'. - - bar(**animal) # WRONG! This will fail at runtime because 'breed' keyword - # will be passed to 'takes_name' as well. - - spam(**animal) # WRONG! Again, 'breed' keyword will be eventually passed - # to 'takes_name'. +Function calls with standard dictionaries +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -In the example above, the call to ``foo`` will not cause any issues at -runtime. Even though ``foo`` expects ``kwargs`` of type ``Animal`` it doesn't -matter if it receives additional arguments because it only reads and uses what -it needs completely ignoring any additional values. +Passing a dictionary of type ``dict[str, object]`` as a ``**kwargs`` argument +to a function that has ``**kwargs`` annotated with ``Unpack`` must generate a +type checker error. On the other hand, the behavior for functions using +standard, untyped dictionaries can depend on the type checker. For example:: -The calls to ``bar`` and ``spam`` will fail because an unexpected keyword -argument will be passed to the ``takes_name`` function. + def func(**kwargs: Unpack[Movie]) -> None: ... -Therefore, ``kwargs`` hinted with an unpacked ``TypedDict`` can only be passed -to another function if the function to which unpacked kwargs are being passed -to has ``**kwargs`` in its signature as well, because then additional keywords -would not cause errors at runtime during function invocation. Otherwise, the -type checker should generate an error. + movie: dict[str, object] = {"name": "Life of Brian", "year": 1979} + func(**movie) # WRONG! Movie is of type dict[str, object] -In cases similar to the ``bar`` function above the problem could be worked -around by explicitly dereferencing desired fields and using them as arguments -to perform the function call:: + typed_movie: Movie = {"name": "The Meaning of Life", "year": 1983} + func(**typed_movie) # OK! - def bar(**kwargs: Unpack[Animal]): - name = kwargs["name"] - takes_name(name) + another_movie = {"name": "Life of Brian", "year": 1979} + func(**another_movie) # Depends on the type checker. Using ``Unpack`` with types other than ``TypedDict`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^