Jump to solution
Verify

The Fix

Fixes the evaluation of stringified annotations during namespace inspection, addressing an issue with ClassVar attributes in BaseModel when using future annotations.

Based on closed pydantic/pydantic issue #10345 · PR/commit linked

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -11,7 +11,7 @@ from functools import lru_cache, partial from types import FunctionType -from typing import Any, Callable, ForwardRef, Generic, Literal, NoReturn +from typing import Any, Callable, Generic, Literal, NoReturn
repro.py
from __future__ import annotations from typing import ClassVar from pydantic import BaseModel class MyModel(BaseModel): _x: ClassVar[str]
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Apply the official fix\nFixes the evaluation of stringified annotations during namespace inspection, addressing an issue with ClassVar attributes in BaseModel when using future annotations.\nWhen NOT to use: This fix should not be applied if ClassVar attributes are not used in conjunction with future annotations.\n\n

Why This Fix Works in Production

  • Trigger: obj = mod.Model()
  • Mechanism: ClassVar attributes were not evaluated correctly during namespace inspection with future annotations
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • Shows up under Python 3.11 in real deployments (not just unit tests).
  • ClassVar attributes were not evaluated correctly during namespace inspection with future annotations
  • Surfaces as: Traceback (most recent call last):

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“I am running into the same issue here in version 2.9”
@willbakst · 2024-09-06 · source
“Thanks for the bug report, that's on me. class vars should indeed be evaluated with is_class set to True.”
@Viicos · 2024-09-06 · source
“@sydney-runkle I have the same problem with a bit different usecase, importing the file with importlib: # pydanticbug.py # test_pydanticbug.py”
@atti92 · 2024-09-09 · source
“@atti92, see https://github.com/pydantic/pydantic/issues/2363.”
@Viicos · 2024-09-09 · source

Failure Signature (Search String)

  • obj = mod.Model()

Error Message

Stack trace
error.txt
Error Message ------------- Traceback (most recent call last): File "/work/test_pydanticbug.py", line 7, in <module> obj = mod.Model() ^^^^^^^^^^^ File "/venv/lib/python3.11/site-packages/pydantic/main.py", line 211, in __init__ validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/venv/lib/python3.11/site-packages/pydantic/_internal/_mock_val_ser.py", line 99, in __getattr__ raise PydanticUserError(self._error_message, code=self._code) pydantic.errors.PydanticUserError: `Model` is not fully defined; you should define `Optional`, then call `Model.model_rebuild()`. For further information visit https://errors.pydantic.dev/2.9/u/class-not-fully-defined

Minimal Reproduction

repro.py
from __future__ import annotations from typing import ClassVar from pydantic import BaseModel class MyModel(BaseModel): _x: ClassVar[str]

Environment

  • Python: 3.11
  • Pydantic: 2

What Broke

Users experienced errors when creating BaseModel instances with ClassVar attributes.

Why It Broke

ClassVar attributes were not evaluated correctly during namespace inspection with future annotations

Fix Options (Details)

Option A — Apply the official fix

Fixes the evaluation of stringified annotations during namespace inspection, addressing an issue with ClassVar attributes in BaseModel when using future annotations.

When NOT to use: This fix should not be applied if ClassVar attributes are not used in conjunction with future annotations.

Fix reference: https://github.com/pydantic/pydantic/pull/10347

Last verified: 2026-02-11. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be applied if ClassVar attributes are not used in conjunction with future annotations.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
  • Upgrade behind a canary and run integration tests against the canary before 100% rollout.

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.