Skip to main content

Backports Python Typing Stubs

Project description

unpy

Backports Python typing stubs to earlier Python versions

unpy - PyPI unpy - Python Versions unpy - license

unpy - CI unpy - pre-commit unpy - basedpyright unpy - ruff


[!IMPORTANT] This project is in the early stages of development; You probably shouldn't use it in production.

Installation

The unpy package is available as on PyPI, and can be installed with e.g.

pip install unpy

Usage

$ 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.

Features

  • 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.Final and typing.ClassVar
  • 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
  • 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]
  • Generated TypeVars
    • Prefix extracted TypeVars names with _
    • De-duplicate extracted TypeVars
    • Prevent TypeVar name 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=Any to default={bound} or default=object
    • Remove bound=Any and bound=object
    • Importing TypeVar's (not recommended)
  • 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 typing modules (like [tool.ruff.lint.typing-modules])
    • Support for from typing[_extensions] import * (not recommended)
  • Interface
    • Backport from stdin / to stdout
    • Backport a single .pyi file
    • Backport all .pyi files in package
    • Configuration options in pyproject.toml as [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
    • .py support
    • Transform self parameters to be positional-only
    • Use None as 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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page