Jump to solution
Verify

The Fix

Fix type hint on `IpvAnyAddress` to resolve compatibility issues with `IPvAnyNetwork`.

Based on closed pydantic/pydantic issue #9638 · 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
@@ -653,46 +653,52 @@ def _validate(cls, input_value: NetworkType, /) -> IPv4Interface | IPv6Interface -class IPvAnyNetwork: - """Validate an IPv4 or IPv6 network.""" +IPvAnyNetworkType: TypeAlias = 'IPv4Network | IPv6Network'
repro.py
from ipaddress import IPv4Network from pydantic import IPvAnyNetwork def test_(): def to_v4(arg: IPvAnyNetwork): return IPv4Network(arg) other = IPv4Network('192.168.127.12/24') assert to_v4(other) == other
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\nFix type hint on `IpvAnyAddress` to resolve compatibility issues with `IPvAnyNetwork`.\nWhen NOT to use: Do not apply this fix if the type hinting behavior is intended to remain unchanged.\n\n

Why This Fix Works in Production

  • Trigger: `IPv4Network` is marked as incompatible to `IPvAnyNetwork`
  • Mechanism: The type hint for `IPvAnyNetwork` was incorrectly defined, causing mypy compatibility issues
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.12 in real deployments (not just unit tests).
  • The type hint for `IPvAnyNetwork` was incorrectly defined, causing mypy compatibility issues
  • Production symptom (often without a traceback): `IPv4Network` is marked as incompatible to `IPvAnyNetwork`

Proof / Evidence

Discussion

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

“@chbndrhnns, Thanks for reporting this. Should be an easy fix. I'll get a PR up today!”
@sydney-runkle · 2024-06-12 · source

Failure Signature (Search String)

  • `IPv4Network` is marked as incompatible to `IPvAnyNetwork`
  • tests/test_.py:8: error: Incompatible return value type (got "IPv4Network", expected "IPvAnyNetwork") [return-value]
Copy-friendly signature
signature.txt
Failure Signature ----------------- `IPv4Network` is marked as incompatible to `IPvAnyNetwork` tests/test_.py:8: error: Incompatible return value type (got "IPv4Network", expected "IPvAnyNetwork") [return-value]

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- `IPv4Network` is marked as incompatible to `IPvAnyNetwork` tests/test_.py:8: error: Incompatible return value type (got "IPv4Network", expected "IPvAnyNetwork") [return-value]

Minimal Reproduction

repro.py
from ipaddress import IPv4Network from pydantic import IPvAnyNetwork def test_(): def to_v4(arg: IPvAnyNetwork): return IPv4Network(arg) other = IPv4Network('192.168.127.12/24') assert to_v4(other) == other

Environment

  • Python: 3.12
  • Pydantic: 2

What Broke

Mypy raises type errors when using `IPv4Network` as an argument for `IPvAnyNetwork`.

Why It Broke

The type hint for `IPvAnyNetwork` was incorrectly defined, causing mypy compatibility issues

Fix Options (Details)

Option A — Apply the official fix

Fix type hint on `IpvAnyAddress` to resolve compatibility issues with `IPvAnyNetwork`.

When NOT to use: Do not apply this fix if the type hinting behavior is intended to remain unchanged.

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

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

  • Do not apply this fix if the type hinting behavior is intended to remain unchanged.

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.