Skip to content

Raise a clearer error for a non-string TypedDict name - #828

Merged
AlexWaygood merged 3 commits into
python:mainfrom
vmphase:fix/typeddict-typename
Nov 29, 2026
Merged

AlexWaygood merged 3 commits into
python:mainfrom
vmphase:fix/typeddict-typename

Conversation

@vmphase

@vmphase vmphase commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

typing_extensions.TypedDict(3, {"a": int}) currently raises TypeError: can only assign string to Protocol.__name__, not 'int', meanwhile the stdlib raises TypeError: type.__new__() argument 1 must be str, not int. As far as I understand, the error comes from the placeholder name "Protocol" that _TypedDictMeta.__new__ uses internally, which is misleading.

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you! Heh, that's quite a funny bug :-)

This fix looks reasonable to me, but could you add a test for this? It should be reasonably easy to do so with self.assertRaisesRegex

@vmphase

vmphase commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Thank you! Heh, that's quite a funny bug :-)

This fix looks reasonable to me, but could you add a test for this? It should be reasonably easy to do so with self.assertRaisesRegex

Added the test ;)

@vmphase
vmphase requested a review from AlexWaygood November 29, 2026 14:40
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.07%. Comparing base (a202455) to head (00faeb4).
⚠️ Report is 4 commits behind head on main.

@@            Coverage Diff             @@
##             main     #828      +/-   ##
==========================================
+ Coverage   94.50%   97.07%   +2.57%     
==========================================
  Files           3        3              
  Lines        7860     7588     -272     
==========================================
- Hits         7428     7366      -62     
+ Misses        432      222     -210     
Flag Coverage Δ
3.10 91.23% <100.00%> (+2.38%) ⬆️
3.10.4 91.23% <100.00%> (+2.38%) ⬆️
3.11 90.44% <100.00%> (+2.41%) ⬆️
3.11.0 89.68% <100.00%> (+2.40%) ⬆️
3.12 90.37% <100.00%> (+2.41%) ⬆️
3.12.0 90.36% <100.00%> (+2.41%) ⬆️
3.13 85.65% <77.77%> (+2.26%) ⬆️
3.13.0 86.38% <77.77%> (+2.28%) ⬆️
3.14 81.72% <77.77%> (+2.18%) ⬆️
3.14.0 82.46% <77.77%> (+2.21%) ⬆️
3.15 78.84% <77.77%> (+2.13%) ⬆️
3.15.0 78.84% <77.77%> (+2.13%) ⬆️
pypy3.10 91.06% <100.00%> (+2.37%) ⬆️
pypy3.11 90.30% <100.00%> (+2.41%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/test_typing_extensions.py 98.32% <100.00%> (+0.31%) ⬆️
src/typing_extensions.py 92.33% <100.00%> (+9.65%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AlexWaygood
AlexWaygood merged commit dfd735e into python:main Nov 29, 2026
25 checks passed
@vmphase
vmphase deleted the fix/typeddict-typename branch November 29, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants