Backports Python typing stubs to earlier Python versions
Important
This project is in the early stages of development; You probably shouldn't use it in production.
The unpy package is available as on PyPI, and can be installed with e.g.
pip install unpy$ unpy --help
Usage: unpy [OPTIONS] SOURCE [OUTPUT]
Arguments:
SOURCE Path to the input .pyi file or '-' to read from stdin. [required]
[OUTPUT] Path to the output .pyi file. Defaults to stdout.
Options:
-V, --version Show the version and exit
-P, --python [3.11] The minimum Python version that should be supported.
[default: 3.11]
--help Show this message and exit.- Python 3.13 => 3.12
- PEP 742:
typing.TypeIs=>typing_extensions.TypeIs - PEP 705:
typing.ReadOnly=>typing_extensions.ReadOnly - PEP 702:
warnings.deprecated=>typing_extensions.deprecated - PEP 696: Backport PEP 695 type signatures i.f.f. it includes a type parameter with default
- PEP 696:
typing.NoDefault=>typing_extensions.NoDefault -
typing.get_protocol_members=>typing_extensions.get_protocol_members -
typing.is_protocol=>typing_extensions.is_protocol -
typing.is_protocol=>typing_extensions.is_protocol -
types.CapsuleType=>typing_extensions.CapsuleType - nested
typing.Finalandtyping.ClassVar
- PEP 742:
- Python 3.12 => 3.11
- PEP 698:
typing.override=>typing_extensions.override - PEP 695: Backport generic functions
- PEP 695: Backport generic classes
- PEP 695: Backport generic protocols
- PEP 695:
type {} = ...=>{}: TypeAlias = ...or - PEP 695:
typing.TypeAliasType=>typing_extensions.TypeAliasType - PEP 688:
collections.abc.Buffer=>typing_extensions.Buffer - PEP 688:
inspect.BufferFlags=>int - Backport subclasses of
path.Path
- PEP 698:
- Python 3.11 => 3.10
- PEP 681:
typing.dataclass_transform=>typing_extensions.dataclass_transform - PEP 680:
tomllib=>tomli - PEP 675:
typing.LiteralString=>typing_extensions.LiteralString - PEP 673:
typing.Self=>typing_extensions.Self - PEP 655:
typing.[Not]Required=>typing_extensions.[Not]Required - PEP 646:
*Ts=>typing_extensions.Unpack[Ts]
- PEP 681:
- Generated
TypeVars- Prefix extracted
TypeVars names with_ - De-duplicate extracted
TypeVars - Prevent
TypeVarname clashes (rename or merge) - Infer variance of
typing_extensions.TypeVar(..., infer_variance=True)whose name does not end with_contra/_in(contravariant=True) or_co/_out(covariant=True) - Convert
default=Anytodefault={bound}ordefault=object - Remove
bound=Anyandbound=object - Importing
TypeVar's (not recommended)
- Prefix extracted
- Imports
- Reuse existing
from typing[_extensions] import {name}imports instead of adding new ones - Reuse
from {module} import {name} as {alias}import aliases if present, e.g.from typing import TypeVar as TypeParam - Reuse
import {module} as {alias}if present, e.g.import typing as tp - Support for custom
typingmodules (like[tool.ruff.lint.typing-modules]) - Support for
from typing[_extensions] import *(not recommended)
- Reuse existing
- Interface
- Backport from stdin / to stdout
- Backport a single
.pyifile - Backport all
.pyifiles in package - Configuration options in
pyproject.tomlas[tools.unpy] - Project build tools
- Configurable type-checker integration
- Configurable post processing with e.g.
ruff format
- Integration
- File watcher
- Pre-commit
- VSCode extension
- (based)mypy plugin
- Misc
-
.pysupport - Transform
selfparameters to be positional-only - Use
Noneas the default return type - De-duplicate and flatten unions and literals
-
type[S] | type[T]=>type[S | T]
-
- Extended syntax
- Write
_: True | 1 | "a"instead of_: Literal[True, 1, "a"] - Allow omitting
@overload - Intersection types
- Mapped types
- Higher-kinded types
- Write