Skip to content

Commit a2e6356

Browse files
authored
Update typeddicts_class_syntax.py test to use a custom metaclass. (#2327)
The TypedDict chapter of the spec says: > It is also invalid to specify a custom metaclass. It looks like most type checkers interpret this to mean that the appearance of `metaclass=...` is illegal in a TypedDict definition. Pyrefly interprets this to mean that a non-`type` metaclass is illegal, so a redundant `metaclass=type` is fine. IMO either interpretation is defensible, but the conformance test currently uses `metaclass=type`, which only accommodates the first. This PR changes the test to use a non-`type` metaclass, which is unambiguously an error. This doesn't actually change pyrefly's conformance results because pyrefly has a bug that causes it to emit a different, nonsensical error on the `metaclass=type` line, which was mistakenly taken as pyrefly passing the test. I'm in the process of fixing that bug, which is how I discovered that pyrefly behaves slightly differently from the other checkers here.
1 parent 60df123 commit a2e6356

8 files changed

Lines changed: 37 additions & 33 deletions

‎conformance/results/basilisk/typeddicts_class_syntax.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ output = """
66
typeddicts_class_syntax.py:30:9: error: Method `method1` is not allowed in TypedDict class `BadTypedDict1` [typeddicts_class_syntax]
77
typeddicts_class_syntax.py:35:9: error: Method `method2` is not allowed in TypedDict class `BadTypedDict1` [typeddicts_class_syntax]
88
typeddicts_class_syntax.py:40:9: error: Method `method3` is not allowed in TypedDict class `BadTypedDict1` [typeddicts_class_syntax]
9-
typeddicts_class_syntax.py:45:7: error: TypedDict class `BadTypedDict2` uses unrecognised keyword `metaclass` [typeddicts_class_syntax_2]
10-
typeddicts_class_syntax.py:50:7: error: TypedDict class `BadTypedDict3` uses unrecognised keyword `other` [typeddicts_class_syntax_2]
11-
typeddicts_class_syntax.py:65:1: error: Unrecognized item `z` for `ConditionalField`: extra items are not allowed [typeddicts_extra_items]
9+
typeddicts_class_syntax.py:49:7: error: TypedDict class `BadTypedDict2` uses unrecognised keyword `metaclass` [typeddicts_class_syntax_2]
10+
typeddicts_class_syntax.py:54:7: error: TypedDict class `BadTypedDict3` uses unrecognised keyword `other` [typeddicts_class_syntax_2]
11+
typeddicts_class_syntax.py:69:1: error: Unrecognized item `z` for `ConditionalField`: extra items are not allowed [typeddicts_extra_items]
1212
"""

‎conformance/results/mypy/typeddicts_class_syntax.toml‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ output = """
66
typeddicts_class_syntax.py:30: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
77
typeddicts_class_syntax.py:34: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
88
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:60: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
13-
typeddicts_class_syntax.py:64: error: Extra key "y" for TypedDict "ConditionalField" [typeddict-unknown-key]
14-
typeddicts_class_syntax.py:65: error: Extra keys ("y", "z") for TypedDict "ConditionalField" [typeddict-unknown-key]
9+
typeddicts_class_syntax.py:49: error: Unexpected keyword argument "metaclass" for "__init_subclass__" of "TypedDict" [call-arg]
10+
typeddicts_class_syntax.py:54: error: Unexpected keyword argument "other" for "__init_subclass__" of "TypedDict" [call-arg]
11+
typeddicts_class_syntax.py:62: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
12+
typeddicts_class_syntax.py:64: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
13+
typeddicts_class_syntax.py:68: error: Extra key "y" for TypedDict "ConditionalField" [typeddict-unknown-key]
14+
typeddicts_class_syntax.py:69: error: Extra keys ("y", "z") for TypedDict "ConditionalField" [typeddict-unknown-key]
1515
"""
1616
conformance_automated = "Fail"
1717
errors_diff = """
18-
Line 58: Unexpected errors ['typeddicts_class_syntax.py:58: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]']
19-
Line 60: Unexpected errors ['typeddicts_class_syntax.py:60: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]']
20-
Line 64: Unexpected errors ['typeddicts_class_syntax.py:64: error: Extra key "y" for TypedDict "ConditionalField" [typeddict-unknown-key]']
18+
Line 62: Unexpected errors ['typeddicts_class_syntax.py:62: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]']
19+
Line 64: Unexpected errors ['typeddicts_class_syntax.py:64: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]']
20+
Line 68: Unexpected errors ['typeddicts_class_syntax.py:68: error: Extra key "y" for TypedDict "ConditionalField" [typeddict-unknown-key]']
2121
"""

‎conformance/results/pycroscope/typeddicts_class_syntax.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ output = """
55
./typeddicts_class_syntax.py:30:4: Methods are not allowed in TypedDict definitions [invalid_typeddict]
66
./typeddicts_class_syntax.py:35:4: Methods are not allowed in TypedDict definitions [invalid_typeddict]
77
./typeddicts_class_syntax.py:40:4: Methods are not allowed in TypedDict definitions [invalid_typeddict]
8-
./typeddicts_class_syntax.py:45:31: TypedDict definitions cannot specify a metaclass [invalid_typeddict]
9-
./typeddicts_class_syntax.py:50:31: Unexpected keyword argument 'other' in TypedDict definition [invalid_typeddict]
10-
./typeddicts_class_syntax.py:65:0: Got an unexpected keyword argument 'z' [incompatible_call]
8+
./typeddicts_class_syntax.py:49:31: TypedDict definitions cannot specify a metaclass [invalid_typeddict]
9+
./typeddicts_class_syntax.py:54:31: Unexpected keyword argument 'other' in TypedDict definition [invalid_typeddict]
10+
./typeddicts_class_syntax.py:69:0: Got an unexpected keyword argument 'z' [incompatible_call]
1111
"""

‎conformance/results/pyrefly/typeddicts_class_syntax.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ output = """
66
ERROR typeddicts_class_syntax.py:30:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
77
ERROR typeddicts_class_syntax.py:35:9-16: TypedDict members must be declared in the form `field: Annotation` with no assignment [bad-class-definition]
88
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]
11-
ERROR typeddicts_class_syntax.py:65:17-32: No matching overload found for function `ConditionalField.__init__` called with arguments: (x=Literal[1], y=Literal[2], z=Literal[3]) [no-matching-overload]
9+
ERROR typeddicts_class_syntax.py:49:7-20: Typed dictionary definitions may not specify a metaclass [invalid-inheritance]
10+
ERROR typeddicts_class_syntax.py:54:7-20: TypedDict does not support keyword argument `other` [bad-typed-dict]
11+
ERROR typeddicts_class_syntax.py:69:17-32: No matching overload found for function `ConditionalField.__init__` called with arguments: (x=Literal[1], y=Literal[2], z=Literal[3]) [no-matching-overload]
1212
"""

‎conformance/results/pyright/typeddicts_class_syntax.toml‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ output = """
66
typeddicts_class_syntax.py:30:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
77
typeddicts_class_syntax.py:34:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
88
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)
12-
typeddicts_class_syntax.py:60:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
13-
typeddicts_class_syntax.py:65:1 - error: No overloads for "__init__" match the provided arguments
9+
typeddicts_class_syntax.py:49:32 - error: TypedDict does not support __init_subclass__ parameter "metaclass" (reportGeneralTypeIssues)
10+
typeddicts_class_syntax.py:54:32 - error: TypedDict does not support __init_subclass__ parameter "other" (reportGeneralTypeIssues)
11+
typeddicts_class_syntax.py:62:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
12+
typeddicts_class_syntax.py:64:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)
13+
typeddicts_class_syntax.py:69:1 - error: No overloads for "__init__" match the provided arguments
1414
  Argument types: (Literal[1], Literal[2], Literal[3]) (reportCallIssue)
1515
"""
1616
conformance_automated = "Fail"
1717
errors_diff = """
18-
Line 58: Unexpected errors ['typeddicts_class_syntax.py:58:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)']
19-
Line 60: Unexpected errors ['typeddicts_class_syntax.py:60:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)']
18+
Line 62: Unexpected errors ['typeddicts_class_syntax.py:62:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)']
19+
Line 64: Unexpected errors ['typeddicts_class_syntax.py:64:5 - error: TypedDict classes can contain only type annotations (reportGeneralTypeIssues)']
2020
"""

‎conformance/results/ty/typeddicts_class_syntax.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ output = """
88
typeddicts_class_syntax.py:30:5: error[invalid-typed-dict-statement] TypedDict class cannot have methods
99
typeddicts_class_syntax.py:34:5: error[invalid-typed-dict-statement] TypedDict class cannot have methods
1010
typeddicts_class_syntax.py:39:5: error[invalid-typed-dict-statement] TypedDict class cannot have methods
11-
typeddicts_class_syntax.py:45:32: error[invalid-typed-dict-header] Custom metaclasses are not supported in `TypedDict` definitions
12-
typeddicts_class_syntax.py:50:32: error[unknown-argument] Unknown keyword argument `other` in `TypedDict` definition
13-
typeddicts_class_syntax.py:65:28: error[invalid-key] Unknown key "z" for TypedDict `ConditionalField`
11+
typeddicts_class_syntax.py:49:32: error[invalid-typed-dict-header] Custom metaclasses are not supported in `TypedDict` definitions
12+
typeddicts_class_syntax.py:54:32: error[unknown-argument] Unknown keyword argument `other` in `TypedDict` definition
13+
typeddicts_class_syntax.py:69:28: error[invalid-key] Unknown key "z" for TypedDict `ConditionalField`
1414
"""

‎conformance/results/zuban/typeddicts_class_syntax.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ output = """
55
typeddicts_class_syntax.py:30: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
66
typeddicts_class_syntax.py:34: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
77
typeddicts_class_syntax.py:39: error: Invalid statement in TypedDict definition; expected "field_name: field_type" [misc]
8-
typeddicts_class_syntax.py:45: error: Unexpected keyword argument "metaclass" for "TypedDict" [call-arg]
9-
typeddicts_class_syntax.py:50: error: Unexpected keyword argument "other" for "TypedDict" [call-arg]
10-
typeddicts_class_syntax.py:65: error: Extra key "z" for TypedDict "ConditionalField" [typeddict-unknown-key]
8+
typeddicts_class_syntax.py:49: error: Unexpected keyword argument "metaclass" for "TypedDict" [call-arg]
9+
typeddicts_class_syntax.py:54: error: Unexpected keyword argument "other" for "TypedDict" [call-arg]
10+
typeddicts_class_syntax.py:69: error: Extra key "z" for TypedDict "ConditionalField" [typeddict-unknown-key]
1111
"""

‎conformance/tests/typeddicts_class_syntax.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ def method3(): # E[method3]
4141
pass
4242

4343

44-
# > Specifying a metaclass is not allowed.
45-
class BadTypedDict2(TypedDict, metaclass=type): # E
44+
class CustomMeta(type):
45+
pass
46+
47+
48+
# > Specifying a custom metaclass is not allowed.
49+
class BadTypedDict2(TypedDict, metaclass=CustomMeta): # E
4650
name: str
4751

4852

0 commit comments

Comments
 (0)