Skip to content

Conversation

sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Aug 6, 2025

Summary

Disallow typing.TypedDict in type expressions.

Related reference: python/mypy#11030

Test Plan

New Markdown tests, checked ecosystem and conformance test impact.

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Aug 6, 2025
Copy link
Contributor

github-actions bot commented Aug 6, 2025

Diagnostic diff on typing conformance tests

Changes 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

Copy link
Contributor

github-actions bot commented Aug 6, 2025

mypy_primer results

Changes were detected when running on open source projects
pydantic (https://github.com/pydantic/pydantic)
- pydantic/v1/annotated_types.py:13:73: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- pydantic/v1/annotated_types.py:24:40: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- pydantic/v1/validators.py:621:67: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- pydantic/v1/validators.py:632:70: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 772 diagnostics
+ Found 768 diagnostics
No memory usage changes detected ✅

@sharkdp sharkdp marked this pull request as ready for review August 6, 2025 08:50
@sharkdp sharkdp added internal An internal refactor or improvement and removed internal An internal refactor or improvement labels Aug 6, 2025
@sharkdp sharkdp changed the title [ty] Resolve two TypedDict-related TODOs [ty] Disallow typing.TypedDict in type expressions Aug 6, 2025
```py
from typing import TypedDict

x: TypedDict = {"name": "Alice"} # error: [invalid-type-form] "`typing.TypedDict` is not allowed in type expressions"
Copy link
Member

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:

Suggested change
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]"?

Copy link
Contributor Author

@sharkdp sharkdp Aug 6, 2025

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.

image

Leaving the diagnostic as-is for now, but also not opposed to a change.

Copy link
Member

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!

Copy link
Contributor Author

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.

Copy link
Member

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!

@sharkdp sharkdp force-pushed the david/typeddict-todos branch from 4128c05 to ae1d5b4 Compare August 6, 2025 13:43
@sharkdp sharkdp merged commit b96929e into main Aug 6, 2025
38 checks passed
@sharkdp sharkdp deleted the david/typeddict-todos branch August 6, 2025 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants