-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ty] Disallow typing.TypedDict
in type expressions
#19777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-08-06 13:45:47.198830453 +0000
+++ new-output.txt 2025-08-06 13:45:47.262830278 +0000
@@ -885,4 +885,5 @@
tuples_type_form.py:36:1: error[invalid-assignment] Object of type `tuple[Literal[1], Literal[2], Literal[3], Literal[""]]` is not assignable to `tuple[int, ...]`
typeddicts_operations.py:60:1: error[type-assertion-failure] Argument does not have asserted type `str | None`
typeddicts_type_consistency.py:101:1: error[invalid-assignment] Object of type `Unknown | None` is not assignable to `str`
-Found 886 diagnostics
+typeddicts_usage.py:40:24: error[invalid-type-form] The special form `typing.TypedDict` is not allowed in type expressions. Did you mean to use a concrete TypedDict or `collections.abc.Mapping[str, object]` instead?
+Found 887 diagnostics |
|
typing.TypedDict
in type expressions
```py | ||
from typing import TypedDict | ||
|
||
x: TypedDict = {"name": "Alice"} # error: [invalid-type-form] "`typing.TypedDict` is not allowed in type expressions" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit (and it's fine to leave this for a followup): I wonder if we could be a bit more helpful with this error message -- naively this might feel like a sensible thing to do if you're new to typing? Maybe something like this:
x: TypedDict = {"name": "Alice"} # error: [invalid-type-form] "`typing.TypedDict` is not allowed in type expressions" | |
x: TypedDict = {"name": "Alice"} # error: [invalid-type-form] "Function `typing.TypedDict` is not allowed in type expressions" |
with a subdiagnostic "note: Consider using collections.abc.Mapping[str, object]
"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quickly looked into it but it's not straightforward to add subdiagnostics/hints at this position.
Concerning "Function typing.TypedDict
": I know that it's a function at runtime, but if you're unfamiliar with Python typing, then you might be even more confused by this? "I wanted to use a type, not a function…".
Even the Python documentation doesn't reveal that it's a function.

Leaving the diagnostic as-is for now, but also not opposed to a change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concerning "Function
typing.TypedDict
": I know that it's a function at runtime, but if you're unfamiliar with Python typing, then you might be even more confused by this? "I wanted to use a type, not a function…".
Yeah — the main thing I think might be nice to clarify is that obviously TypedDict types are allowed in type expressions; it's just the TypedDict symbol itself that is not really a type, and is thus disallowed in type expressions.
Anyway, fine to leave as-is for now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I made an attempt to improve it. With a long message instead of a .info
for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, the revised message is great!
4128c05
to
ae1d5b4
Compare
Summary
Disallow
typing.TypedDict
in type expressions.Related reference: python/mypy#11030
Test Plan
New Markdown tests, checked ecosystem and conformance test impact.