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%.
@@ -653,46 +653,52 @@ def _validate(cls, input_value: NetworkType, /) -> IPv4Interface | IPv6Interface
-class IPvAnyNetwork:
- """Validate an IPv4 or IPv6 network."""
+IPvAnyNetworkType: TypeAlias = 'IPv4Network | IPv6Network'
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
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
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
- 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
- GitHub issue: #9638
- Fix PR: https://github.com/pydantic/pydantic/pull/9640
- Reproduced locally: No (not executed)
- Last verified: 2026-02-11
- Confidence: 0.70
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.64
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!”
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
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 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
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`.
Fix reference: https://github.com/pydantic/pydantic/pull/9640
Last verified: 2026-02-11. Validate in your environment.
When NOT to Use This Fix
- Do not apply this fix if the type hinting behavior is intended to remain unchanged.
Verify Fix
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.