Skip to content

Runtime TypeError caused by mixing typing and typing_extensions not caught #4380

@not-my-profile

Description

@not-my-profile

The last line of the following code results in the runtime TypeError:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

import typing
import typing_extensions

class Foo(typing.TypedDict): ...

class Bar(typing_extensions.TypedDict): ...

class Baz(Foo, Bar): ...

I just ran into this runtime error because I always default to using types from typing but sometimes use types from typing_extensions to support older versions of Python. In particular typing.TypedDict is incompatible with typing.Generic in Python < 3.11, see python/cpython#27663 (comment), so I sometimes used typing_extensions.TypedDict instead of typing.TypedDict.

It would be nice if pyright could detect this type error, which can be quite sneaky.

I think there could even be an additional optional check that warns about this issue whenever a project uses both typing.TypedDict and typing_extensions.TypedDict in the same code base (because otherwise the public API might have TypedDict classes that are incompatible with each other).

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions