Skip to content

Commit

Permalink
fix: Don't label ClassVar-annotated attributes as Pydantic fields
Browse files Browse the repository at this point in the history
Issue-18: #18
PR-25: #25
Co-authored-by: Timothée Mazzucotelli <[email protected]>
  • Loading branch information
mmzeynalli and pawamoy authored Feb 17, 2025
1 parent 93cdc21 commit 0dbf958
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/griffe_pydantic/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def process_attribute(attr: Attribute, cls: Class, *, processed: set[str]) -> No
return
processed.add(attr.canonical_path)

# Presence of `class-attribute` label and absence of `instance-attribute` label
# indicates that the attribute is annotated with `ClassVar` and should be ignored.
if "class-attribute" in attr.labels and "instance-attribute" not in attr.labels:
return

kwargs = {}
if isinstance(attr.value, ExprCall):
kwargs = {
Expand All @@ -93,7 +98,9 @@ def process_attribute(attr: Attribute, cls: Class, *, processed: set[str]) -> No
cls.extra[common.self_namespace]["config"] = kwargs
return

attr.labels = {"pydantic-field"}
attr.labels.add("pydantic-field")
attr.labels.remove("instance-attribute")

attr.value = kwargs.get("default", None)
constraints = {kwarg: value for kwarg, value in kwargs.items() if kwarg not in {"default", "description"}}
attr.extra[common.self_namespace]["constraints"] = constraints
Expand Down

0 comments on commit 0dbf958

Please sign in to comment.