Skip to content

"TypedDict does not support instance and class checks": interaction between typing_extension, typeguard_ignore, attrs #443

@ryan-williams

Description

@ryan-williams

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.1.5

Python version

3.11.8

What happened?

See test_typeddict.py:

  • test_typeddict below fails, as written, with error TypeError: TypedDict does not support instance and class checks.
  • For some reason, it passes if either:
    • typing.TypedDict is used (instead of typing_extensions.TypedDict)
    • typeguard_ignore is imported directly: from typeguard import typeguard_ignore
from typing import Any

import attrs
from typing_extensions import TypedDict
# from typing import TypedDict  # ✅ uncommenting this fixes ``test_typeddict`` below

import typeguard

# ✅ uncommenting this also fixes ``test_typeddict`` below (even though it's not used!)
# from typeguard import typeguard_ignore
typeguard_ignore = typeguard.typeguard_ignore


class Spec(TypedDict):
    num: int


@typeguard_ignore
def converter(arg: dict[str, Spec]) -> dict[str, Any]:
    return {}


@attrs.define()
class Class:
    field: dict[str, Any] = attrs.field(factory=dict, converter=converter)


# ❌ As written, this fails with `TypeError: TypedDict does not support instance and class checks`
# Uncommenting either of the "✅" lines above makes it work though 😵‍💫
def test_typeddict():
    Class(field={'aaa': { 'num': 111 }})

How can we reproduce the bug?

ryan-williams/typeguard-issues has a working repro; here's the error in Github Actions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions