Skip to content

Commit 925b8dd

Browse files
committed
support conditional fields on TypedDict and NamedTuple
1 parent 322fb10 commit 925b8dd

15 files changed

Lines changed: 182 additions & 110 deletions
Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
conformant = "Partial"
22
notes = """
33
Does not reject override of named tuple attribute in child class.
4+
Does not support version-conditional fields.
45
"""
56
output = """
6-
namedtuples_define_class.py:32: error: Tuple index out of range [misc]
77
namedtuples_define_class.py:33: error: Tuple index out of range [misc]
8-
namedtuples_define_class.py:44: error: Missing positional argument "y" in call to "Point" [call-arg]
8+
namedtuples_define_class.py:34: error: Tuple index out of range [misc]
99
namedtuples_define_class.py:45: error: Missing positional argument "y" in call to "Point" [call-arg]
10-
namedtuples_define_class.py:46: error: Argument 2 to "Point" has incompatible type "str"; expected "int" [arg-type]
11-
namedtuples_define_class.py:47: error: Argument "units" to "Point" has incompatible type "int"; expected "str" [arg-type]
12-
namedtuples_define_class.py:48: error: Too many arguments for "Point" [call-arg]
13-
namedtuples_define_class.py:49: error: Unexpected keyword argument "other" for "Point" [call-arg]
14-
namedtuples_define_class.py:59: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" [misc]
15-
namedtuples_define_class.py:65: error: Missing positional argument "units" in call to "Point2" [call-arg]
16-
namedtuples_define_class.py:67: error: Too many values to unpack (2 expected, 3 provided) [misc]
17-
namedtuples_define_class.py:76: error: NamedTuple field name cannot start with an underscore: _y [misc]
18-
namedtuples_define_class.py:86: error: Non-default NamedTuple fields cannot follow default fields [misc]
19-
namedtuples_define_class.py:125: error: Argument 2 to "Property" has incompatible type "float"; expected "str" [arg-type]
20-
namedtuples_define_class.py:132: error: NamedTuple should be a single base [misc]
10+
namedtuples_define_class.py:46: error: Missing positional argument "y" in call to "Point" [call-arg]
11+
namedtuples_define_class.py:47: error: Argument 2 to "Point" has incompatible type "str"; expected "int" [arg-type]
12+
namedtuples_define_class.py:48: error: Argument "units" to "Point" has incompatible type "int"; expected "str" [arg-type]
13+
namedtuples_define_class.py:49: error: Too many arguments for "Point" [call-arg]
14+
namedtuples_define_class.py:50: error: Unexpected keyword argument "other" for "Point" [call-arg]
15+
namedtuples_define_class.py:60: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" [misc]
16+
namedtuples_define_class.py:66: error: Missing positional argument "units" in call to "Point2" [call-arg]
17+
namedtuples_define_class.py:68: error: Too many values to unpack (2 expected, 3 provided) [misc]
18+
namedtuples_define_class.py:77: error: NamedTuple field name cannot start with an underscore: _y [misc]
19+
namedtuples_define_class.py:87: error: Non-default NamedTuple fields cannot follow default fields [misc]
20+
namedtuples_define_class.py:114: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" [misc]
21+
namedtuples_define_class.py:118: error: Too many arguments for "ConditionalField" [call-arg]
22+
namedtuples_define_class.py:137: error: Argument 2 to "Property" has incompatible type "float"; expected "str" [arg-type]
23+
namedtuples_define_class.py:144: error: NamedTuple should be a single base [misc]
2124
"""
2225
conformance_automated = "Fail"
2326
errors_diff = """
24-
Line 69: Expected 1 errors
25-
Line 106: Expected 1 errors
26-
Line 59: Unexpected errors ['namedtuples_define_class.py:59: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" [misc]']
27-
Line 65: Unexpected errors ['namedtuples_define_class.py:65: error: Missing positional argument "units" in call to "Point2" [call-arg]']
28-
Line 67: Unexpected errors ['namedtuples_define_class.py:67: error: Too many values to unpack (2 expected, 3 provided) [misc]']
27+
Line 70: Expected 1 errors
28+
Line 107: Expected 1 errors
29+
Line 60: Unexpected errors ['namedtuples_define_class.py:60: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" [misc]']
30+
Line 66: Unexpected errors ['namedtuples_define_class.py:66: error: Missing positional argument "units" in call to "Point2" [call-arg]']
31+
Line 68: Unexpected errors ['namedtuples_define_class.py:68: error: Too many values to unpack (2 expected, 3 provided) [misc]']
32+
Line 114: Unexpected errors ['namedtuples_define_class.py:114: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" [misc]']
33+
Line 118: Unexpected errors ['namedtuples_define_class.py:118: error: Too many arguments for "ConditionalField" [call-arg]']
2934
"""
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
conformant = "Pass"
1+
conformant = "Partial"
2+
notes = """
3+
Does not support version-conditional items in TypedDict definitions.
4+
"""
25
output = """
3-
typeddicts_class_syntax.py:29: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
4-
typeddicts_class_syntax.py:33: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
5-
typeddicts_class_syntax.py:38: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
6-
typeddicts_class_syntax.py:44: error: Unexpected keyword argument "metaclass" for "__init_subclass__" of "TypedDict" [call-arg]
7-
typeddicts_class_syntax.py:49: error: Unexpected keyword argument "other" for "__init_subclass__" of "TypedDict" [call-arg]
6+
typeddicts_class_syntax.py:30: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
7+
typeddicts_class_syntax.py:34: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
8+
typeddicts_class_syntax.py:39: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
9+
typeddicts_class_syntax.py:45: error: Unexpected keyword argument "metaclass" for "__init_subclass__" of "TypedDict" [call-arg]
10+
typeddicts_class_syntax.py:50: error: Unexpected keyword argument "other" for "__init_subclass__" of "TypedDict" [call-arg]
11+
typeddicts_class_syntax.py:58: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
12+
typeddicts_class_syntax.py:62: error: Extra key "y" for TypedDict "ConditionalField" [typeddict-unknown-key]
813
"""
9-
conformance_automated = "Pass"
14+
conformance_automated = "Fail"
1015
errors_diff = """
16+
Line 58: Unexpected errors ['typeddicts_class_syntax.py:58: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]']
17+
Line 62: Unexpected errors ['typeddicts_class_syntax.py:62: error: Extra key "y" for TypedDict "ConditionalField" [typeddict-unknown-key]']
1118
"""

conformance/results/pyrefly/namedtuples_define_class.toml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ conformance_automated = "Pass"
33
errors_diff = """
44
"""
55
output = """
6-
ERROR namedtuples_define_class.py:32:10-11: Index 3 out of range for tuple with 3 elements [bad-index]
7-
ERROR namedtuples_define_class.py:33:10-12: Index -4 out of range for tuple with 3 elements [bad-index]
8-
ERROR namedtuples_define_class.py:44:11-14: Missing argument `y` in function `Point.__new__` [missing-argument]
9-
ERROR namedtuples_define_class.py:45:11-16: Missing argument `y` in function `Point.__new__` [missing-argument]
10-
ERROR namedtuples_define_class.py:46:15-17: Argument `Literal['']` is not assignable to parameter `y` with type `int` in function `Point.__new__` [bad-argument-type]
11-
ERROR namedtuples_define_class.py:47:24-25: Argument `Literal[3]` is not assignable to parameter `units` with type `str` in function `Point.__new__` [bad-argument-type]
12-
ERROR namedtuples_define_class.py:48:22-24: Expected 3 positional arguments, got 4 in function `Point.__new__` [bad-argument-count]
13-
ERROR namedtuples_define_class.py:49:23-28: Unexpected keyword argument `other` in function `Point.__new__` [unexpected-keyword]
14-
ERROR namedtuples_define_class.py:69:20-22: Expected 2 positional arguments, got 3 in function `Point2.__new__` [bad-argument-count]
15-
ERROR namedtuples_define_class.py:76:5-7: NamedTuple field name may not start with an underscore: `_y` [bad-class-definition]
16-
ERROR namedtuples_define_class.py:86:5-13: NamedTuple field 'latitude' without a default may not follow NamedTuple field with a default [bad-class-definition]
17-
ERROR namedtuples_define_class.py:106:5-6: Cannot override named tuple element `x` [bad-override]
18-
ERROR namedtuples_define_class.py:125:19-22: Argument `float` is not assignable to parameter `value` with type `str` in function `Property.__new__` [bad-argument-type]
19-
ERROR namedtuples_define_class.py:132:7-11: Named tuples do not support multiple inheritance [invalid-inheritance]
6+
ERROR namedtuples_define_class.py:33:10-11: Index 3 out of range for tuple with 3 elements [bad-index]
7+
ERROR namedtuples_define_class.py:34:10-12: Index -4 out of range for tuple with 3 elements [bad-index]
8+
ERROR namedtuples_define_class.py:45:11-14: Missing argument `y` in function `Point.__new__` [missing-argument]
9+
ERROR namedtuples_define_class.py:46:11-16: Missing argument `y` in function `Point.__new__` [missing-argument]
10+
ERROR namedtuples_define_class.py:47:15-17: Argument `Literal['']` is not assignable to parameter `y` with type `int` in function `Point.__new__` [bad-argument-type]
11+
ERROR namedtuples_define_class.py:48:24-25: Argument `Literal[3]` is not assignable to parameter `units` with type `str` in function `Point.__new__` [bad-argument-type]
12+
ERROR namedtuples_define_class.py:49:22-24: Expected 3 positional arguments, got 4 in function `Point.__new__` [bad-argument-count]
13+
ERROR namedtuples_define_class.py:50:23-28: Unexpected keyword argument `other` in function `Point.__new__` [unexpected-keyword]
14+
ERROR namedtuples_define_class.py:70:20-22: Expected 2 positional arguments, got 3 in function `Point2.__new__` [bad-argument-count]
15+
ERROR namedtuples_define_class.py:77:5-7: NamedTuple field name may not start with an underscore: `_y` [bad-class-definition]
16+
ERROR namedtuples_define_class.py:87:5-13: NamedTuple field 'latitude' without a default may not follow NamedTuple field with a default [bad-class-definition]
17+
ERROR namedtuples_define_class.py:107:5-6: Cannot override named tuple element `x` [bad-override]
18+
ERROR namedtuples_define_class.py:137:19-22: Argument `float` is not assignable to parameter `value` with type `str` in function `Property.__new__` [bad-argument-type]
19+
ERROR namedtuples_define_class.py:144:7-11: Named tuples do not support multiple inheritance [invalid-inheritance]
2020
"""

conformance/results/pyrefly/typeddicts_class_syntax.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ conformance_automated = "Pass"
33
errors_diff = """
44
"""
55
output = """
6-
ERROR typeddicts_class_syntax.py:29:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
7-
ERROR typeddicts_class_syntax.py:34:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
8-
ERROR typeddicts_class_syntax.py:39:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
9-
ERROR typeddicts_class_syntax.py:44:7-20: Metaclass of `BadTypedDict2` has type `type[Any]` that is not a simple class type [invalid-inheritance]
10-
ERROR typeddicts_class_syntax.py:49:7-20: TypedDict does not support keyword argument `other` [bad-typed-dict]
6+
ERROR typeddicts_class_syntax.py:30:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
7+
ERROR typeddicts_class_syntax.py:35:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
8+
ERROR typeddicts_class_syntax.py:40:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
9+
ERROR typeddicts_class_syntax.py:45:7-20: Metaclass of `BadTypedDict2` has type `type[Any]` that is not a simple class type [invalid-inheritance]
10+
ERROR typeddicts_class_syntax.py:50:7-20: TypedDict does not support keyword argument `other` [bad-typed-dict]
1111
"""

conformance/results/pyright/namedtuples_define_class.toml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
conformant = "Pass"
22
output = """
3-
namedtuples_define_class.py:32:7 - error: Index 3 is out of range for type Point (reportGeneralTypeIssues)
4-
namedtuples_define_class.py:33:7 - error: Index -4 is out of range for type Point (reportGeneralTypeIssues)
5-
namedtuples_define_class.py:44:6 - error: Argument missing for parameter "y" (reportCallIssue)
3+
namedtuples_define_class.py:33:7 - error: Index 3 is out of range for type Point (reportGeneralTypeIssues)
4+
namedtuples_define_class.py:34:7 - error: Index -4 is out of range for type Point (reportGeneralTypeIssues)
65
namedtuples_define_class.py:45:6 - error: Argument missing for parameter "y" (reportCallIssue)
7-
namedtuples_define_class.py:46:15 - error: Argument of type "Literal['']" cannot be assigned to parameter "y" of type "int" in function "__new__"
6+
namedtuples_define_class.py:46:6 - error: Argument missing for parameter "y" (reportCallIssue)
7+
namedtuples_define_class.py:47:15 - error: Argument of type "Literal['']" cannot be assigned to parameter "y" of type "int" in function "__new__"
88
  "Literal['']" is not assignable to "int" (reportArgumentType)
9-
namedtuples_define_class.py:47:24 - error: Argument of type "Literal[3]" cannot be assigned to parameter "units" of type "str" in function "__new__"
9+
namedtuples_define_class.py:48:24 - error: Argument of type "Literal[3]" cannot be assigned to parameter "units" of type "str" in function "__new__"
1010
  "Literal[3]" is not assignable to "str" (reportArgumentType)
11-
namedtuples_define_class.py:48:22 - error: Expected 3 positional arguments (reportCallIssue)
12-
namedtuples_define_class.py:49:23 - error: No parameter named "other" (reportCallIssue)
13-
namedtuples_define_class.py:69:20 - error: Expected 2 positional arguments (reportCallIssue)
14-
namedtuples_define_class.py:76:5 - error: Named tuple field names cannot start with an underscore (reportGeneralTypeIssues)
15-
namedtuples_define_class.py:86:5 - error: Fields without default values cannot appear after fields with default values (reportGeneralTypeIssues)
16-
namedtuples_define_class.py:106:5 - error: Cannot override "x" because parent class "Point" is a named tuple (reportIncompatibleVariableOverride)
17-
namedtuples_define_class.py:125:19 - error: Argument of type "float" cannot be assigned to parameter "value" of type "str" in function "__new__"
11+
namedtuples_define_class.py:49:22 - error: Expected 3 positional arguments (reportCallIssue)
12+
namedtuples_define_class.py:50:23 - error: No parameter named "other" (reportCallIssue)
13+
namedtuples_define_class.py:70:20 - error: Expected 2 positional arguments (reportCallIssue)
14+
namedtuples_define_class.py:77:5 - error: Named tuple field names cannot start with an underscore (reportGeneralTypeIssues)
15+
namedtuples_define_class.py:87:5 - error: Fields without default values cannot appear after fields with default values (reportGeneralTypeIssues)
16+
namedtuples_define_class.py:107:5 - error: Cannot override "x" because parent class "Point" is a named tuple (reportIncompatibleVariableOverride)
17+
namedtuples_define_class.py:137:19 - error: Argument of type "float" cannot be assigned to parameter "value" of type "str" in function "__new__"
1818
  "float" is not assignable to "str" (reportArgumentType)
19-
namedtuples_define_class.py:132:7 - error: Multiple inheritance with NamedTuple is not supported (reportGeneralTypeIssues)
19+
namedtuples_define_class.py:144:7 - error: Multiple inheritance with NamedTuple is not supported (reportGeneralTypeIssues)
2020
"""
2121
conformance_automated = "Pass"
2222
errors_diff = """
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
conformant = "Pass"
1+
conformant = "Partial"
2+
notes = """
3+
Does not support version-conditional items in TypedDict definitions.
4+
"""
25
output = """
3-
typeddicts_class_syntax.py:29:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
4-
typeddicts_class_syntax.py:33:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
5-
typeddicts_class_syntax.py:38:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
6-
typeddicts_class_syntax.py:44:32 - error: TypedDict does not support __init_subclass__ parameter "metaclass" (reportGeneralTypeIssues)
7-
typeddicts_class_syntax.py:49:32 - error: TypedDict does not support __init_subclass__ parameter "other" (reportGeneralTypeIssues)
6+
typeddicts_class_syntax.py:30:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
7+
typeddicts_class_syntax.py:34:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
8+
typeddicts_class_syntax.py:39:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
9+
typeddicts_class_syntax.py:45:32 - error: TypedDict does not support __init_subclass__ parameter "metaclass" (reportGeneralTypeIssues)
10+
typeddicts_class_syntax.py:50:32 - error: TypedDict does not support __init_subclass__ parameter "other" (reportGeneralTypeIssues)
11+
typeddicts_class_syntax.py:58:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
812
"""
9-
conformance_automated = "Pass"
13+
conformance_automated = "Fail"
1014
errors_diff = """
15+
Line 58: Unexpected errors ['typeddicts_class_syntax.py:58:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)']
1116
"""

conformance/results/results.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -936,11 +936,11 @@ <h3>Python Type System Conformance Test Results</h3>
936936
<th class="column col2 conformant">Pass</th>
937937
</tr>
938938
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typeddicts_class_syntax</th>
939+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not support version-conditional items in TypedDict definitions.</p></span></div></th>
940+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not support version-conditional items in TypedDict definitions.</p></span></div></th>
941+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not support version-conditional items in TypedDict definitions.</p></span></div></th>
939942
<th class="column col2 conformant">Pass</th>
940-
<th class="column col2 conformant">Pass</th>
941-
<th class="column col2 conformant">Pass</th>
942-
<th class="column col2 conformant">Pass</th>
943-
<th class="column col2 conformant">Pass</th>
943+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Errors on version-conditional items in TypedDict definitions.</p></span></div></th>
944944
</tr>
945945
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typeddicts_extra_items</th>
946946
<th class="column col2 not-conformant"><div class="hover-text">Unsupported<span class="tooltip-text" id="bottom"><p>Not supported.</p></span></div></th>
@@ -1054,9 +1054,9 @@ <h3>Python Type System Conformance Test Results</h3>
10541054
<a class="test_group" href="https://typing.readthedocs.io/en/latest/spec/namedtuples.html">Named tuples</a>
10551055
</th></tr>
10561056
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;namedtuples_define_class</th>
1057-
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not reject override of named tuple attribute in child class.</p></span></div></th>
1058-
<th class="column col2 conformant">Pass</th>
1057+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not reject override of named tuple attribute in child class.</p><p>Does not support version-conditional fields.</p></span></div></th>
10591058
<th class="column col2 conformant">Pass</th>
1059+
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not support version-conditional fields in NamedTuple definitions.</p></span></div></th>
10601060
<th class="column col2 conformant">Pass</th>
10611061
<th class="column col2 conformant">Pass</th>
10621062
</tr>

0 commit comments

Comments
 (0)