Jump to solution
Verify

The Fix

pip install pydantic==2.7

Based on closed pydantic/pydantic issue #9068 · 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
@@ -126,10 +126,6 @@ def plugin(version: str) -> type[Plugin]: -class _DeferAnalysis(Exception): - pass -
repro.py
import pydantic class ModelNested(pydantic.BaseModel): my_var: str | None = pydantic.Field(default=None) class Model(ModelNested): pass m = Model() if m.my_var is None: print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
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==2.7\nWhen NOT to use: Do not apply this fix if using a version of Pydantic prior to 2.7.\n\n

Why This Fix Works in Production

  • Trigger: print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
  • Mechanism: Mypy incorrectly infers that a config variable can never be None in inherited BaseModel classes
  • Why the fix works: Fix handling of optionals in mypy plugin to resolve mypy issues with optional types. (first fixed release: 2.7).
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

  • Mypy incorrectly infers that a config variable can never be None in inherited BaseModel classes
  • Production symptom (often without a traceback): print("hello there") # test.py:14: error: Statement is unreachable [unreachable]

Proof / Evidence

Discussion

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

“Should be fixed in 2.7, coming out soon! Fixed by @dmontagu here: https://github.com/pydantic/pydantic/pull/9008”
@sydney-runkle · 2024-03-22 · confirmation · source

Failure Signature (Search String)

  • print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
Copy-friendly signature
signature.txt
Failure Signature ----------------- print("hello there") # test.py:14: error: Statement is unreachable [unreachable] python version: 3.12.2 (main, Feb 14 2024, 11:13:34) [Clang 15.0.0 (clang-1500.1.0.2.5)]

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- print("hello there") # test.py:14: error: Statement is unreachable [unreachable] python version: 3.12.2 (main, Feb 14 2024, 11:13:34) [Clang 15.0.0 (clang-1500.1.0.2.5)]

Minimal Reproduction

repro.py
import pydantic class ModelNested(pydantic.BaseModel): my_var: str | None = pydantic.Field(default=None) class Model(ModelNested): pass m = Model() if m.my_var is None: print("hello there") # test.py:14: error: Statement is unreachable [unreachable]

Environment

  • Pydantic: 2

What Broke

Mypy reports unreachable code errors when checking optional variables in models.

Why It Broke

Mypy incorrectly infers that a config variable can never be None in inherited BaseModel classes

Fix Options (Details)

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

pip install pydantic==2.7

When NOT to use: Do not apply this fix if using a version of Pydantic prior to 2.7.

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/9008

First fixed release: 2.7

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

  • Do not apply this fix if using a version of Pydantic prior to 2.7.

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
2.7 Fixed

Related Issues

No related fixes found.

Sources

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