Registry / type-stubs / types-contextvars

types-contextvars

JSON →
library2.4.7.3pypypi✓ verified 87d ago

This package provides static type checking definitions (typing stubs) for the built-in Python `contextvars` standard library module. It enables type checkers like MyPy to correctly analyze code that uses `contextvars`, ensuring type safety without altering runtime behavior. The current version is 2.4.7.3, and releases are tied to updates in the Python standard library and the broader typeshed project.

pip install types-contextvars
INSTALL
IMPORT
SIG · TYPES-CONTEXTVARS
T
types-contextvars
type-stubspythonv2.4.7.3
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.4.7.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ContextVar
from contextvars import ContextVar
from types_contextvars import ContextVar
This package provides type stubs for the built-in `contextvars` module. You install `types-contextvars` but import symbols directly from the standard library `contextvars` module for runtime use, and type checkers will use these installed stubs for analysis.
copy_context
from contextvars import copy_context
from types_contextvars import copy_context
As with other symbols, `copy_context` is imported from the standard library `contextvars` module.
Token
from contextvars import Token
from types_contextvars import Token
As with other symbols, `Token` is imported from the standard library `contextvars` module.

This example demonstrates basic usage of `contextvars` with type annotations. Installing `types-contextvars` allows static type checkers (like MyPy) to validate the types of `my_var`, `ContextVar` methods, and related functions, ensuring your code adheres to type definitions without affecting runtime execution.

import contextvars # Define a ContextVar with a type hint my_var: contextvars.ContextVar[str] = contextvars.ContextVar('my_var', default='default_value') def worker_task(): print(f"Worker: my_var is {my_var.get()}") def main(): print(f"Main: my_var is {my_var.get()}") # Set a new value for my_var in the current context token = my_var.set('new_value_in_main') print(f"Main (after set): my_var is {my_var.get()}") worker_task() # Restore the previous value my_var.reset(token) print(f"Main (after reset): my_var is {my_var.get()}") if __name__ == '__main__': # Installing `types-contextvars` (e.g., `pip install types-contextvars`) # allows static type checkers (like MyPy) to validate these type annotations. # Example: run `mypy your_script.py` after installation. main()
Debug
Known issues
gotchaDo not attempt to import symbols directly from `types_contextvars` (e.g., `from types_contextvars import ContextVar`). This package only provides type stubs for static analysis, not runtime objects.
fix
Install `types-contextvars` via pip, but always import symbols (like `ContextVar`, `copy_context`, `Token`) directly from the standard library `contextvars` module: `from contextvars import ContextVar`.
affects: All versions
gotchaWhile typeshed strives for accuracy, stubs might occasionally lag behind the absolute bleeding edge of Python's standard library, especially when using pre-release Python versions or new, undocumented features.
fix
Ensure your `types-contextvars` package is up-to-date. If encountering type errors for very new features, consider using `typing.Any` for specific annotations as a temporary workaround until updated stubs are released.
affects: All versions, particularly with new Python versions or features.
Errors
Common errors & fixes
Module 'types_contextvars' has no attribute 'ContextVar'
The user is attempting to import symbols directly from the `types_contextvars` package, which is incorrect. `types_contextvars` is a stub package for static type checking, not a runtime module.
fix
Import symbols like `ContextVar` from the standard library `contextvars` module: `from contextvars import ContextVar`.
Missing type annotations for 'contextvars' (reportMissingTypeStubs)
A type checker (like MyPy or Pylance) is reporting that it cannot find type stubs for the `contextvars` module, meaning it cannot properly type-check code using `contextvars`.
fix
Install the `types-contextvars` package: `pip install types-contextvars`. Ensure your type checker is configured to use installed stub packages.
mypy: error: Incompatible default for argument "default" (item is "None", expected "str")
This specific MyPy error (or similar) occurs when a `ContextVar` is defined with a type hint that doesn't match the default value, and `types-contextvars` is correctly enforcing the type.
fix
Ensure the default value provided to `ContextVar` matches the type annotation, or make the type annotation optional (e.g., `ContextVar[Optional[str]]`) if `None` is an expected default and `from typing import Optional` is used.
Upgrade
Version history
2.4.7.3latest on PyPI · released Jul 7, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
23 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
types-contextvars — pip install types-contextvars · libregistry