Skip to content

Update typeddicts_class_syntax.py test to use a custom metaclass. - #2327

Merged
davidhalter merged 1 commit into
python:mainfrom
rchen152:typeddict
Aug 3, 2026
Merged

Update typeddicts_class_syntax.py test to use a custom metaclass.#2327
davidhalter merged 1 commit into
python:mainfrom
rchen152:typeddict

Conversation

@rchen152

@rchen152 rchen152 commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

The TypedDict chapter of the spec says (emphasis mine):

It is invalid to specify a base class other than TypedDict, Generic, or another TypedDict type in a class-based TypedDict definition. 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.

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.
@srittau srittau added the topic: conformance tests Issues with the conformance test suite label Aug 1, 2026

@davidhalter davidhalter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to map with runtime behavior:

>>> from typing import *
>>> class BadTypedDict2(TypedDict, metaclass=type): pass
... 
>>> class CustomMeta(type):
...     pass
... 
>>> class BadTypedDict2(TypedDict, metaclass=CustomMeta): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

@davidhalter
davidhalter merged commit a2e6356 into python:main Aug 3, 2026
5 checks passed
@rchen152
rchen152 deleted the typeddict branch August 3, 2026 16:50
meta-codesync Bot pushed a commit to facebook/pyrefly that referenced this pull request Aug 3, 2026
Summary: Updates conformance tests to pull in python/typing#2327.

Reviewed By: ndmitchell

Differential Revision: D114618365

fbshipit-source-id: 0b81efb5df3f5356e0b94df6dea551898031b1e9
rchen152 added a commit to rchen152/pyrefly that referenced this pull request Aug 3, 2026
Summary:
It's pointless but perfectly legal to write `metaclass=type`.

~~The conformance change is because pyrefly previously accidentally passed a test due to emitting a FP on a line on which an entirely different error is expected. I believe pyrefly's behavior as of this diff is correct and the test is overly strict: I've opened python/typing#2327 to adjust the test.~~ The conformance test has been updated :)

Reviewed By: yangdanny97

Differential Revision: D114434451
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: conformance tests Issues with the conformance test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants