[Django]-Python Django ImportError: cannot import name 'Required' from 'typing_extensions'

4👍

Update

Support for Required and NotRequired have been added to typing_extensions version 4.0.0 as experimental features.

Everything should work for now like it’s stated in PEP 655.

Old Answer

It seems like Required and NotRequired aren’t implemented yet in typing_extensions.

PEP 655 states:

The goal is to be able to make the following statement:

The mypy type checker supports Required and NotRequired. A reference implementation of the runtime component is provided in the typing_extensions module.

It’s just the goal — it isn’t the current state. It is neither listed in typing_extensionsREADME nor does it appear in the source code.

I think it is really confusing that vscode’s pylance/pyright can resolve typing_extensions.Required and typing_extensions.NotRequired, even though it isn’t implemented in the module.

As a workaround you could try to replace from typing_extensions import Required with

try:
    from typing_extensions import Required
except ImportError:
    from typing import Generic, TypeVar

    T = TypeVar("T")

    class Required(Generic[T]):
        pass
👤d-k-bo

Leave a comment