Repository navigation
Spec: Mandate support for typing-extensions #1532
Description
Activity
- addedtopic: documentationDocumentation-related issues and PRsDocumentation-related issues and PRstopic: typing specFor improving the typing specFor improving the typing spec
on Dec 11, 2023 This was arguably addressed by the type expressions change (#1693), now at https://typing.readthedocs.io/en/latest/spec/annotations.html#type-and-annotation-expressions, which says "Most special forms must be imported from typing or typing_extensions". However, it's probably still useful to have a section mentioning typing-extensions more explicitly.
Reacted by lena and Carl MeyerWe have recently been debating in PyO3 what to do about typing constructs not available in our full range of supported Python versions for the type hints which we are now generating from Rust modules.
Based on this proposal here, it seems like it would be fine to just import these from
typing_extensions(e.g.disjoint_base), rather than having a conditional import based onsys.version_info.It would be helpful to have this formalised in the spec so that we can be certain what we're doing is compliant.
While this is not in the spec and should probably be formalized, I think all type checkers have the understanding that
typing_extensionsshould work exactly liketyping. There are a lot oftyping_extensionsimports out in the wild so as a type checker you have to practically deal withtyping_extensionsanyway.That was our empirical observation too.
@davidhewitt if you do this in stub files it should be fine. If it's in runtime code you need a little more care, as you do have to declare the typing-extensions runtime dependency.
Reacted by David HewittIf it's in runtime code you need a little more care, as you do have to declare the typing-extensions runtime dependency.
You can also import it within
if TYPE_CHECKING:to avoid the dependencyFor now these are all
.pyifiles, yes.
The typing spec should mandate that type checkers support the
typing_extensionsmodule by treating any name imported from it as equivalent to the same name intyping.This is noncontroversial (I hope!), but it's still a substantive change to the spec, so it should go through the procedure in https://github.com/python/typing-council?tab=readme-ov-file#decisions. I'll likely do that in January.