{"id":10322,"library":"types-contextvars","title":"Typing Stubs for contextvars","description":"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.","status":"active","version":"2.4.7.3","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","type hints","contextvars","standard library"],"install":[{"cmd":"pip install types-contextvars","lang":"bash","label":"Install stubs"}],"dependencies":[],"imports":[{"note":"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.","wrong":"from types_contextvars import ContextVar","symbol":"ContextVar","correct":"from contextvars import ContextVar"},{"note":"As with other symbols, `copy_context` is imported from the standard library `contextvars` module.","wrong":"from types_contextvars import copy_context","symbol":"copy_context","correct":"from contextvars import copy_context"},{"note":"As with other symbols, `Token` is imported from the standard library `contextvars` module.","wrong":"from types_contextvars import Token","symbol":"Token","correct":"from contextvars import Token"}],"quickstart":{"code":"import contextvars\n\n# Define a ContextVar with a type hint\nmy_var: contextvars.ContextVar[str] = contextvars.ContextVar('my_var', default='default_value')\n\ndef worker_task():\n    print(f\"Worker: my_var is {my_var.get()}\")\n\ndef main():\n    print(f\"Main: my_var is {my_var.get()}\")\n    \n    # Set a new value for my_var in the current context\n    token = my_var.set('new_value_in_main')\n    print(f\"Main (after set): my_var is {my_var.get()}\")\n    \n    worker_task()\n    \n    # Restore the previous value\n    my_var.reset(token)\n    print(f\"Main (after reset): my_var is {my_var.get()}\")\n\nif __name__ == '__main__':\n    # Installing `types-contextvars` (e.g., `pip install types-contextvars`)\n    # allows static type checkers (like MyPy) to validate these type annotations.\n    # Example: run `mypy your_script.py` after installation.\n    main()","lang":"python","description":"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."},"warnings":[{"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`.","message":"Do 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"While 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.","severity":"gotcha","affected_versions":"All versions, particularly with new Python versions or features."}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Import symbols like `ContextVar` from the standard library `contextvars` module: `from contextvars import ContextVar`.","cause":"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.","error":"Module 'types_contextvars' has no attribute 'ContextVar'"},{"fix":"Install the `types-contextvars` package: `pip install types-contextvars`. Ensure your type checker is configured to use installed stub packages.","cause":"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`.","error":"Missing type annotations for 'contextvars' (reportMissingTypeStubs)"},{"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.","cause":"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.","error":"mypy: error: Incompatible default for argument \"default\" (item is \"None\", expected \"str\")"}]}