Skip to content

Commit af86235

Browse files
typeddict: disallow nesting Required in itself (#2374)
Fixes #2372
1 parent d06c5dd commit af86235

8 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎conformance/results/mypy/typeddicts_required.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ conformant = "Pass"
22
output = """
33
typeddicts_required.py:12: error: Required[] can be only used in a TypedDict definition [valid-type]
44
typeddicts_required.py:16: error: NotRequired[] can be only used in a TypedDict definition [valid-type]
5-
typeddicts_required.py:59: error: "Required[]" type cannot be nested [valid-type]
6-
typeddicts_required.py:60: error: "NotRequired[]" type cannot be nested [valid-type]
5+
typeddicts_required.py:60: error: "Required[]" type cannot be nested [valid-type]
6+
typeddicts_required.py:61: error: "NotRequired[]" type cannot be nested [valid-type]
77
"""
88
conformance_automated = "Pass"
99
errors_diff = """

‎conformance/results/pycroscope/typeddicts_required.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ errors_diff = """
44
output = """
55
./typeddicts_required.py:12:7: Unexpected Required annotation [invalid_qualifier]
66
./typeddicts_required.py:16:7: Unexpected NotRequired annotation [invalid_qualifier]
7-
./typeddicts_required.py:59:16: Required[] cannot be nested [invalid_qualifier]
8-
./typeddicts_required.py:60:7: Required[] and NotRequired[] cannot be nested [invalid_qualifier]
7+
./typeddicts_required.py:60:16: Required[] cannot be nested [invalid_qualifier]
8+
./typeddicts_required.py:61:7: Required[] and NotRequired[] cannot be nested [invalid_qualifier]
99
"""

‎conformance/results/pyrefly/typeddicts_required.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ output = """
66
ERROR typeddicts_required.py:12:5-6: `Required` may only be used for TypedDict members [invalid-annotation]
77
ERROR typeddicts_required.py:16:8-19: `NotRequired` is only allowed inside a class body [invalid-annotation]
88
ERROR typeddicts_required.py:16:8-24: `NotRequired` is not allowed in this context [invalid-annotation]
9-
ERROR typeddicts_required.py:59:8-31: Duplicate qualifier `Required` [invalid-annotation]
10-
ERROR typeddicts_required.py:60:8-34: Cannot combine `Required` and `NotRequired` for a TypedDict field [invalid-annotation]
9+
ERROR typeddicts_required.py:60:8-31: Duplicate qualifier `Required` [invalid-annotation]
10+
ERROR typeddicts_required.py:61:8-34: Cannot combine `Required` and `NotRequired` for a TypedDict field [invalid-annotation]
1111
"""

‎conformance/results/pyright/typeddicts_required.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ conformant = "Pass"
22
output = """
33
typeddicts_required.py:12:8 - error: "Required" is not allowed in this context (reportInvalidTypeForm)
44
typeddicts_required.py:16:8 - error: "NotRequired" is not allowed in this context (reportInvalidTypeForm)
5-
typeddicts_required.py:59:8 - error: "Required" is not allowed in this context (reportInvalidTypeForm)
65
typeddicts_required.py:60:8 - error: "Required" is not allowed in this context (reportInvalidTypeForm)
6+
typeddicts_required.py:61:8 - error: "Required" is not allowed in this context (reportInvalidTypeForm)
77
"""
88
conformance_automated = "Pass"
99
errors_diff = """

‎conformance/results/ty/typeddicts_required.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ errors_diff = """
44
output = """
55
typeddicts_required.py:12:8: error[invalid-type-form] `Required` is only allowed in TypedDict fields
66
typeddicts_required.py:16:8: error[invalid-type-form] Type qualifier `typing.NotRequired` is not allowed in parameter annotations
7-
typeddicts_required.py:59:8: error[invalid-type-form] `typing.Required` cannot be nested inside `Required` or `NotRequired`
87
typeddicts_required.py:60:8: error[invalid-type-form] `typing.Required` cannot be nested inside `Required` or `NotRequired`
8+
typeddicts_required.py:61:8: error[invalid-type-form] `typing.Required` cannot be nested inside `Required` or `NotRequired`
99
"""

‎conformance/results/zuban/typeddicts_required.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ errors_diff = """
44
output = """
55
typeddicts_required.py:12: error: Required[] can be only used in a TypedDict definition [valid-type]
66
typeddicts_required.py:16: error: NotRequired[] can be only used in a TypedDict definition [valid-type]
7-
typeddicts_required.py:59: error: "Required[]" type cannot be nested [misc]
8-
typeddicts_required.py:60: error: "NotRequired[]" type cannot be nested [misc]
7+
typeddicts_required.py:60: error: "Required[]" type cannot be nested [misc]
8+
typeddicts_required.py:61: error: "NotRequired[]" type cannot be nested [misc]
99
"""

‎conformance/tests/typeddicts_required.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ class TD5(TypedDict, total=True):
5656

5757

5858
class TD6(TypedDict):
59+
# > It is also an error to nest a qualifier within itself (e.g., ``Required[Required[T]]``).
5960
a: Required[Required[int]] # E: Nesting not allowed
6061
b: Required[NotRequired[int]] # E: Nesting not allowed
6162

6263

6364
class TD7(TypedDict):
64-
# > Required[] and NotRequired[] can be used with Annotated[], in any nesting order.
65+
# > These type qualifiers may be nested arbitrarily or wrapped in ``Annotated[]``.
6566
x: Annotated[Required[int], ""]
6667
y: Required[Annotated[int, ""]]
6768
z: Annotated[Required[Annotated[int, ""]], ""]

‎docs/spec/typeddict.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ expression contains a :term:`type expression`, optionally qualified with one of
143143
:term:`type qualifiers <type qualifier>` ``Required``, ``NotRequired``, or ``ReadOnly``.
144144
These type qualifiers may be nested arbitrarily or wrapped in ``Annotated[]``. It is
145145
an error to use both ``Required`` and ``NotRequired`` in the same item definition.
146+
It is also an error to nest a qualifier within itself (e.g., ``Required[Required[T]]``).
146147
An item is :term:`read-only` if and only if the ``ReadOnly`` qualifier is used.
147148

148149
To determine whether an item is :term:`required` or :term:`non-required`, the following

0 commit comments

Comments
 (0)