Skip to content

Conditional import of typing_extensions gives "Incompatible import" #9856

Closed

Description

@Dreamsorcerer

When doing:

try:
    from typing import Final
except ImportError:
    from typing_extensions import Final

We get:

error: Incompatible import of "Final" (imported name has type "typing_extensions._SpecialForm", local name has type "typing._SpecialForm")

Unless I'm mistaken, this is a common pattern for supporting older Python versions. Therefore, it would ideally not trigger these errors, even if it has to be special cased.

Activity

  1. hauntsaninja commented on Dec 29, 2020

    @hauntsaninja
    Collaborator

    You can use sys.version_info checks to do this without a type ignore.

    Other, potentially less desirable solutions: If your installation of typing_extensions is unconditional, you can also unconditionally import from typing_extensions. In some cases with from __future__ annotations, you can use if TYPE_CHECKING to unconditionally import from typing_extensions without even installing typing_extensions.

  2. Dreamsorcerer commented on Dec 29, 2020

    @Dreamsorcerer
    ContributorAuthor

    OK, if that's the recommended solution, then let's stick with that. It probably makes it easier to find old code in future as well (e.g. grepping for "3, 8" when dropping support for 3.7).

    Maybe adding this to the common problems in the docs or something would useful though.

  3. added a commit that references this issue on Dec 29, 2020
    b3d6644
  4. hauntsaninja commented on Dec 29, 2020

    @hauntsaninja
    Collaborator

    #9763 contains a lot more documentation for working around different type system features in different Pythons. I just added this commit to it b3d6644 :-)

  5. added a commit that references this issue on Jan 6, 2021
    6e8c0cd
  6. added a commit that references this issue on Jan 19, 2021
    a7d4c67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions