Fix: fastapi_utils/cbv.py performed `if PYDANTIC_VERSION[0] == "2": from... - #383
Open
M001N wants to merge 1 commit into
Open
Fix: fastapi_utils/cbv.py performed `if PYDANTIC_VERSION[0] == "2": from...#383M001N wants to merge 1 commit into
M001N wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaced the typing_inspect import (for the Pydantic 2 branch) with a small stdlib-only
is_classvarimplementation usingtyping.get_origin(hint) is typing.ClassVar(plus a check for the baretyping.ClassVarsentinel), which is available since Python 3.8 and matches typing_inspect.is_classvar's behavior for both bareClassVarand subscriptedClassVar[...]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_classvarat 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