Jump to solution
Verify

The Fix

pip install pydantic==1.10.18

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

Jump to Verify Open PR/Commit
@@ -4,13 +4,14 @@ import builtins import operator +import sys import typing import warnings
repro.py
# from __future__ import annotations import os from pydantic import BaseModel, PrivateAttr from typing_extensions import Annotated class ParentModel(BaseModel): name: str age: int class ChildModel(ParentModel): _cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)] child_instance = ChildModel(name="John Doe", age=30) count = child_instance._cpu_count print(count) # > 16 # ----------------- from __future__ import annotations import os from pydantic import BaseModel, PrivateAttr from typing_extensions import Annotated class ParentModel(BaseModel): name: str age: int class ChildModel(ParentModel): _cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)] child_instance = ChildModel(name="John Doe", age=30) count = child_instance._cpu_count print(count) # >Traceback (most recent call last): # > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 807, in # >__getattr__ # > return self.__pydantic_private__[item] # type: ignore # > ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^ # >KeyError: '_cpu_count' # > # >The above exception was the direct cause of the following exception: # > # >Traceback (most recent call last): # > File "/home/xx/xx/testing/pydanticbugg.py", line 19, in <module> # > count = child_instance._cpu_count # > ^^^^^^^^^^^^^^^^^^^^^^^^^ # > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 809, in # >__getattr__ # > raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') from exc # > AttributeError: 'ChildModel' object has no attribute '_cpu_count'
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\npip install pydantic==1.10.18\nWhen NOT to use: This fix should not be used if the `Annotated` pattern is not supported by the type checker.\n\n

Why This Fix Works in Production

  • Trigger: AttributeError when using __future__ import with PrivateAttr in Pydantic
  • Mechanism: The `PrivateAttr` attribute does not initialize correctly when using `__future__` import for annotations
  • Why the fix works: Adds support for stringified annotations when using `PrivateAttr` with `Annotated`, resolving the AttributeError issue. (first fixed release: 1.10.18).

Why This Breaks in Prod

  • Shows up under Python 3.12 in real deployments (not just unit tests).
  • The `PrivateAttr` attribute does not initialize correctly when using `__future__` import for annotations
  • Surfaces as: AttributeError when using __future__ import with PrivateAttr in Pydantic

Proof / Evidence

Discussion

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

“I would suggest avoiding the Annotated pattern for private attrs, as type checkers won't recognize them: But as it was supported in https://github.com/pydantic/pydantic/pull/8004, we'll have…”
@Viicos · 2024-08-16 · source
“Should be fixed by https://github.com/pydantic/pydantic/pull/10157”
@sydney-runkle · 2024-08-16 · source

Failure Signature (Search String)

  • AttributeError when using __future__ import with PrivateAttr in Pydantic

Error Message

Stack trace
error.txt
Error Message ------------- AttributeError when using __future__ import with PrivateAttr in Pydantic

Minimal Reproduction

repro.py
# from __future__ import annotations import os from pydantic import BaseModel, PrivateAttr from typing_extensions import Annotated class ParentModel(BaseModel): name: str age: int class ChildModel(ParentModel): _cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)] child_instance = ChildModel(name="John Doe", age=30) count = child_instance._cpu_count print(count) # > 16 # ----------------- from __future__ import annotations import os from pydantic import BaseModel, PrivateAttr from typing_extensions import Annotated class ParentModel(BaseModel): name: str age: int class ChildModel(ParentModel): _cpu_count: Annotated[int, PrivateAttr(default_factory=os.cpu_count)] child_instance = ChildModel(name="John Doe", age=30) count = child_instance._cpu_count print(count) # >Traceback (most recent call last): # > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 807, in # >__getattr__ # > return self.__pydantic_private__[item] # type: ignore # > ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^ # >KeyError: '_cpu_count' # > # >The above exception was the direct cause of the following exception: # > # >Traceback (most recent call last): # > File "/home/xx/xx/testing/pydanticbugg.py", line 19, in <module> # > count = child_instance._cpu_count # > ^^^^^^^^^^^^^^^^^^^^^^^^^ # > File "/home/xx/xx/testing/venv/lib/python3.12/site-packages/pydantic/main.py", line 809, in # >__getattr__ # > raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') from exc # > AttributeError: 'ChildModel' object has no attribute '_cpu_count'

Environment

  • Python: 3.12
  • Pydantic: 2

What Broke

Users encounter an `AttributeError` when accessing private attributes in models with future annotations.

Why It Broke

The `PrivateAttr` attribute does not initialize correctly when using `__future__` import for annotations

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==1.10.18

When NOT to use: This fix should not be used if the `Annotated` pattern is not supported by the type checker.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

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

First fixed release: 1.10.18

Last verified: 2026-02-09. 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 used if the `Annotated` pattern is not supported by the type checker.

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.

Version Compatibility Table

VersionStatus
1.10.18 Fixed

Related Issues

No related fixes found.

Sources

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