TypeError: TypedDict does not support instance and class checks
See original GitHub issueI saw in #7 that TypedDict should be supported from 0.7.0 and partial support has been in beartype since 0.1.0, but I’m running into errors with Python 3.7.12 and 3.9.7, so I’m not sure if this snippet should be working:
# beartype==0.8.1
from beartype import beartype
try:
from typing import TypedDict
except ImportError: # For Python < 3.8
from typing_extensions import TypedDict # typing-extensions = "^3.10.0"
class Point2D(TypedDict):
x: int
y: int
label: str
@beartype
def convert(point: Point2D) -> Point2D:
return point
convert({'x': 1, 'y': 2, 'label': 'good'})
> python tmp.py ─╯
Traceback (most recent call last):
File "/Users/kyleking/.pyenv/versions/3.9.7/lib/python3.9/site-packages/beartype/_util/cls/utilclstest.py", line 159, in die_unless_type_isinstanceable
isinstance(None, cls) # type: ignore[arg-type]
File "/Users/kyleking/.pyenv/versions/3.9.7/lib/python3.9/typing.py", line 1956, in __subclasscheck__
raise TypeError('TypedDict does not support instance and class checks')
TypeError: TypedDict does not support instance and class checks
Should beartype properly check TypedDicts? There were similar errors found in Pydantic and some notes here. TypedDict can’t be used with isinstance by design. There is a reference to tests for TypedDict, but they don’t appear to be present
And this isn’t promising 😅
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
TypedDict does not support instance and class checks" for ...
Models with TypedDict fields currently result in an error when they are instantiated, due to the fact that TypedDict does not support isinstance ......
Read more >Python 3.10.8 TypeError: TypedDict does not support instance ...
The TypeError: TypedDict does not support instance and class checks error occurs because you are trying to use the @typechecked decorator on ...
Read more >TypedDict does not support instance and class checks
I am getting following error in Browser library, TypeError: TypedDict does not support instance and class checks KEYWORD Browser .
Read more >TypedDict subtypes ignore any other metaclasses in 3.9+
Because it raises `TypeError('TypedDict does not support instance and class checks')`. Ok. Let's change `__instancecheck__` method then. We can ...
Read more >Exploring TypedDict in Python 3.8
TypedDict was proposed in PEP-589 and accepted in Python 3.8. ... TypeError: TypedDict does not support instance and class checks ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Welp, this is even more embarrassing…
Turns out we actually never supported
typing.TypedDict
at all! Our documentation claiming we supportedtyping.TypedDict
shallow type-checking frombeartype 0.1.0
onward was, in fact, complete balls.On the bright side, at least we now know why a non-existent feature was never tested.
This is a pretty critical omission, as
typing.TypedDict
is a core type. So, I’ve slated this for inclusion in the upcomingbeartype 0.9.0
release. Let’s optimistically pretend that I’ll get everything pushed out this Friday. 🤕…ohnoes. You caught us red-handed – or honey-pawed, as it were. badum ching
A long time ago in a codebase far, far away,
@beartype
once shallowly type-checkedtyping.TypedDict
… I think, anyway. Tragically, our unavoidable laziness coupled with aggressive release schedules means we kinda failed to properly test that.Welp, this is awkward. Some unspecified recent release of
beartype
(I am glaring at you,beartype 0.8.0
) silently regressed that and broke bothtyping.TypedDict
andtyping.NamedTuple
. I am now shaking my emaciated fists at my younger self, on whose behalf I now extend my most grievous apologies.Thanks so much for the compassionate assistance, Master Kyle. Let’s get this patched up for everyone and pretend this never happened! The sweat pouring off my Klingon-like forehead is real. 😓 💦