{"id":2344,"library":"typing","title":"Type Hints for Python (Backport)","description":"The `typing` library provides type hints for Python. The `typing` *package* on PyPI (version 3.10.0.0) is a backport of the standard library `typing` module, designed specifically for Python versions older than 3.5. It allows developers to add type annotations to their code for improved readability, maintainability, and static analysis by tools like MyPy. The `typing` *module* became a standard part of Python 3.5 (PEP 484) and has since received continuous enhancements and new features in subsequent Python releases. The PyPI package's versioning often reflects the Python version whose typing features it aims to backport.","status":"active","version":"3.10.0.0","language":"en","source_language":"en","source_url":"https://github.com/python/typing","tags":["typing","type hints","static analysis","backport","python2","python3"],"install":[{"cmd":"pip install typing","lang":"bash","label":"Install for Python < 3.5"},{"cmd":"pip install \"typing; python_version < '3.5'\"","lang":"bash","label":"Conditional install (recommended for library authors)"}],"dependencies":[],"imports":[{"note":"While built-in generics (list[int]) are preferred in Python 3.9+, using `typing.List` is correct for broader compatibility (Python 3.5-3.8 and the backport package). `collections.abc.List` is a runtime type, not typically used for type hints directly.","wrong":"from collections.abc import List","symbol":"List","correct":"from typing import List"},{"note":"Same reasoning as List.","symbol":"Dict","correct":"from typing import Dict"},{"symbol":"Optional","correct":"from typing import Optional"},{"symbol":"Union","correct":"from typing import Union"},{"note":"For type hints, `typing.Callable` is standard. `collections.abc.Callable` is for runtime checks, but `typing.Callable` itself is deprecated in favor of `collections.abc.Callable` in some contexts for runtime checking if strict runtime compatibility is needed. However, for type hint *annotations*, `typing.Callable` is typically what's imported.","wrong":"from collections.abc import Callable","symbol":"Callable","correct":"from typing import Callable"},{"symbol":"Any","correct":"from typing import Any"}],"quickstart":{"code":"from __future__ import annotations # For Python < 3.9, enables future syntax for type hints\nfrom typing import List, Union, Optional\n\ndef greet(name: str) -> str:\n    return f\"Hello, {name}!\"\n\ndef get_item(items: List[str], index: int) -> Optional[str]:\n    if 0 <= index < len(items):\n        return items[index]\n    return None\n\ndef process_value(value: Union[int, str]) -> str:\n    if isinstance(value, int):\n        return f\"Received an integer: {value}\"\n    return f\"Received a string: {value}\"\n\nprint(greet(\"Alice\"))\nprint(get_item([\"apple\", \"banana\"], 0))\nprint(get_item([\"apple\", \"banana\"], 2))\nprint(process_value(123))\nprint(process_value(\"hello\"))","lang":"python","description":"This quickstart demonstrates basic type hinting using common constructs from the `typing` module, including type annotations for variables, function parameters, and return values. It also shows the use of `List`, `Optional`, and `Union` types for more complex scenarios. The `from __future__ import annotations` import is recommended for broader compatibility and allows for 'postponed evaluation' of type annotations."},"warnings":[{"fix":"For Python 3.5+, do not install the `typing` PyPI package. Rely on the built-in `typing` module. For libraries supporting older Pythons, use conditional installation (`pip install \"typing; python_version < '3.5'\"`).","message":"Installing the `typing` PyPI package on Python 3.5 or later has *no effect* because the standard library `typing` module takes precedence. In some scenarios (e.g., `pip install -t . typing`), it can even cause `AttributeError` by shadowing the standard library module.","severity":"gotcha","affected_versions":"Python >= 3.5"},{"fix":"Use the built-in generics directly, e.g., `list[str]` instead of `typing.List[str]`, and `dict[str, int]` instead of `typing.Dict[str, int]`.","message":"For built-in generic types like `list`, `dict`, `set`, and `tuple`, using their `typing` module counterparts (e.g., `typing.List`, `typing.Dict`) is deprecated since Python 3.9.","severity":"deprecated","affected_versions":"Python >= 3.9"},{"fix":"Integrate a static type checker into your development workflow (e.g., MyPy, Pyright) to leverage the benefits of type hints.","message":"Type hints are primarily for static analysis tools (type checkers like MyPy, IDEs) and are *not* enforced by the Python runtime by default. Incorrect type hints will not cause runtime errors but will be flagged by static analyzers.","severity":"gotcha","affected_versions":"All Python versions"},{"fix":"Consider using `collections.abc.Callable` for runtime checks. For annotations, `typing.Callable` remains broadly understood but be aware of the shift.","message":"`typing.Callable` is deprecated for runtime type checking in some contexts. While still commonly used for type annotations, `collections.abc.Callable` may be preferred for runtime checks where strict compatibility is desired, or when working with `isinstance` checks.","severity":"deprecated","affected_versions":"Python >= 3.9 (for deprecation awareness)"},{"fix":"Upgrade to Python 3.11+ if `NewType` performance or its class-based nature in 3.10 is an issue.","message":"`typing.NewType` changed from a function to a class in Python 3.10, which introduced a slight runtime overhead. This change was reverted in Python 3.11, restoring performance to Python 3.9 levels. Code relying on the exact type or performance of `NewType` instantiation in 3.10 could be affected.","severity":"breaking","affected_versions":"Python 3.10"},{"fix":"Avoid using undocumented internal attributes of the `typing` module. Use stable public APIs like `typing.get_args()` and `typing.get_origin()` when inspecting types at runtime.","message":"Relying on internal `typing` module attributes (e.g., `__union_params__`) prior to Python 3.8 was risky due to the module's provisional status and led to breaking changes. Public APIs like `get_args()` were introduced to provide stable access.","severity":"gotcha","affected_versions":"Python < 3.8"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}