Skip to content

Support conditionally importing from typing_extensions #14220

@JukkaL

Description

@JukkaL

This code tries to import TypeAlias from typing if possible and falls back to typing_extensions if it isn't available (e.g. on Python 3.8):

try:
    from typing import TypeAlias  # type: ignore
except ImportError:
    from typing_extensions import TypeAlias

Mypy doesn't actually support this as expected in older Python versions that are missing the definition in typing. They will just get an Any value, and the except block has no significance.

Perhaps the import from typing_extensions should take precedence? Or can we at least generate a more specific error about this?

A possible workaround is to flip the order of the imports.

This probably affects any feature that requires an import of a backported definition from typing_extensions.

Related issue: #14219

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions