Typing and Generics

typing.ContextManager

A synchronous context manager whose enter result is T. Python 3.13 adds the optional exit return type parameter, defaulting to bool | None. Deprecated typing alias; prefer contextlib.AbstractContextManager in modern code (3.9+).

Formal Syntax & Structure

python syntaxpython
typing.ContextManager[T, ExitT]

Return Value

type expression

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import ContextManager
from contextlib import nullcontextdef read(manager:ContextManager[str])->str:    with manager as value: return valueprint(read(nullcontext("ok")))
Python Reference