New Typing Features in Python 3.13: ReadOnly, @warnings.deprecated, TypeIs, is_protocol, get_protocol_members, Default TypeVars, NoDefault, and Performance Improvements
Python 3.13 introduces a suite of typing enhancements—including a ReadOnly type for TypedDict, a @warnings.deprecated decorator, the TypeIs construct, is_protocol and get_protocol_members utilities, default TypeVar support with has_default(), the NoDefault sentinel, and several performance and API clean‑ups—providing developers with more expressive and efficient static type checking capabilities.
Python 3.13 continues to push the limits of efficiency and elegance, adding seven new typing features that aim to improve code reliability and developer productivity.
1. ReadOnly type
The new ReadOnly type is a special construct for marking items in a TypedDict as read‑only.
<code>from typing import TypedDict, ReadOnly
class Leader(TypedDict):
name: ReadOnly[str]
age: int
author: Leader = {'name': 'Yang Zhou', 'age': 30}
author['age'] = 31 # no problem to change
author['name'] = 'Yang' # Type check error: "name" is read‑only
</code>When the name field is declared as ReadOnly[str] , static type checkers will flag any attempt to modify it.
Note: The ReadOnly type can only be used inside a TypedDict .
For a more concise definition you can also use ReadOnly directly in the TypedDict call:
<code>from typing import TypedDict, ReadOnly
Leader = TypedDict("Leader", {"name": ReadOnly[str], "age": int})
author: Leader = {'name': 'Yang Zhou', 'age': 30}
author['age'] = 31 # ok
author['name'] = 'Tim' # Type check error
</code>2. @warnings.deprecated
A new decorator @warnings.deprecated allows developers to mark functions or classes as deprecated, providing early warnings in IDEs while keeping the objects usable for a few more releases.
IDE integrations (e.g., PyCharm) will display a strike‑through on deprecated symbols.
3. TypeIs
The TypeIs concept enables type narrowing: a function declared as def foo(arg: TypeA) -> TypeIs[TypeB]: ... tells static checkers that a True return means arg is an instance of TypeB , and False means it is not.
4. is_protocol
The new is_protocol function provides a convenient way to test whether an object is a Protocol type.
<code>from typing import is_protocol, Protocol
class PersonProto(Protocol):
name: str
age: int
print(is_protocol(PersonProto)) # True
print(is_protocol(int)) # False
</code>5. get_protocol_members
get_protocol_members() returns a frozenset containing all attribute names defined in a Protocol .
<code>from typing import Protocol, get_protocol_members
class PersonProto(Protocol):
name: str
age: int
print(get_protocol_members(PersonProto)) # frozenset({'age', 'name'})
</code>6. Default types for TypeVar, ParamSpec, and TypeVarTuple
Python 3.13 allows specifying default types for type parameters. The has_default() method can check whether a TypeVar has a default.
<code>from typing import TypeVar
T = TypeVar("T", default=int) # default is int
print(T.has_default()) # True
S = TypeVar("S")
print(S.has_default()) # False
</code>7. NoDefault
The NoDefault sentinel indicates that a type variable has no default value.
<code>from typing import TypeVar, NoDefault
T = TypeVar("T")
print(T.__default__ is NoDefault) # True
S = TypeVar("S", default=None)
print(S.__default__ is NoDefault) # False
</code>Performance improvements and API removals
Import time for the typing module is reduced by about one‑third thanks to the removal of dependencies on re and contextlib . Additionally, the deprecated namespaces typing.io and typing.re have been removed, and the keyword‑argument method for creating TypedDict types (deprecated since Python 3.11) is no longer available.
These changes streamline the typing ecosystem and encourage developers to import needed symbols directly from typing .
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.