Skip to content

Fix: fastapi_utils/cbv.py performed `if PYDANTIC_VERSION[0] == "2": from... - #383

Open
M001N wants to merge 1 commit into
fastapiutils:masterfrom
M001N:oss-engine/4621fd27-0f9174b1
Open

Fix: fastapi_utils/cbv.py performed `if PYDANTIC_VERSION[0] == "2": from...#383
M001N wants to merge 1 commit into
fastapiutils:masterfrom
M001N:oss-engine/4621fd27-0f9174b1

Conversation

@M001N

@M001N M001N commented Aug 17, 2026

Copy link
Copy Markdown

Summary

Replaced the typing_inspect import (for the Pydantic 2 branch) with a small stdlib-only is_classvar implementation using typing.get_origin(hint) is typing.ClassVar (plus a check for the bare typing.ClassVar sentinel), which is available since Python 3.8 and matches typing_inspect.is_classvar's behavior for both bare ClassVar and subscripted ClassVar[...] annotations as used in cbv.py. The Pydantic 1 branch (pydantic.typing.is_classvar) is unchanged. No pyproject.toml changes were needed since the dependency is eliminated entirely rather than made required.

Problem

fastapiutils/fastapi-utils issue reference: #318

Root Cause

fastapi_utils/cbv.py performed if PYDANTIC_VERSION[0] == "2": from typing_inspect import is_classvar at module import time. Since Pydantic 2 is now the common case, this made the optional typing-inspect dependency a de-facto hard requirement for most users.

Testing

PASS - all 13 tests passed (12 pre-existing + 1 new regression test), including test_class_var which exercises is_classvar-dependent behavior.

Related Issue

#318

…nspect (fastapiutils#318)

cbv.py unconditionally imported typing_inspect.is_classvar for Pydantic 2,
even though typing-inspect is an optional dependency. Replace it with an
equivalent stdlib typing.get_origin check so importing fastapi_utils no
longer requires the optional 'all' extra.

Adds a regression test that reloads fastapi_utils.cbv with typing_inspect
blocked in sys.modules to ensure the import stays optional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant