You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conditional import of typing_extensions gives "Incompatible import" #9856
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.
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.
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.
#9763 contains a lot more documentation for working around different type system features in different Pythons. I just added this commit to it b3d6644 :-)
Activity
hauntsaninja commentedon Dec 29, 2020
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 useif TYPE_CHECKINGto unconditionally import from typing_extensions without even installing typing_extensions.Dreamsorcerer commentedon Dec 29, 2020
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.
Mention conditional import of typing_extensions
hauntsaninja commentedon Dec 29, 2020
#9763 contains a lot more documentation for working around different type system features in different Pythons. I just added this commit to it b3d6644 :-)
Document PEP 585, 563, 604 and more (#9763)
Document PEP 585, 563, 604 and more (#9763)