question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: TypedDict does not support instance and class checks

See original GitHub issue

I 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 😅

https://github.com/beartype/beartype/blob/b7ab8b071430bd69768db0a05cfdde2d85841930/beartype/_data/hint/pep/sign/datapepsigns.py#L131

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
leyceccommented, Oct 7, 2021

Welp, this is even more embarrassing…

Turns out we actually never supported typing.TypedDict at all! Our documentation claiming we supported typing.TypedDict shallow type-checking from beartype 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.

the shame is real

This is a pretty critical omission, as typing.TypedDict is a core type. So, I’ve slated this for inclusion in the upcoming beartype 0.9.0 release. Let’s optimistically pretend that I’ll get everything pushed out this Friday. 🤕

2reactions
leyceccommented, Oct 7, 2021

…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-checked typing.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 both typing.TypedDict and typing.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. 😓 💦

Klingon-like forehead

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found