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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibcpy 3.10–3.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.fixInstall `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.fixEnsure 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.
fixImport 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`.
fixInstall 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.
fixEnsure 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.