python / python/mypy

Support conditionally importing from typing_extensions

Open
#14,220 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is named. Start by reviewing the conditional TypeAlias import behavior described here and related issue #14219; determine whether import precedence or a diagnostic is intended, then verify the chosen behavior for older Python versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.