Skip to content

Commit 0dbf958

Browse files
mmzeynallipawamoy
andauthored
fix: Don't label ClassVar-annotated attributes as Pydantic fields
Issue-18: #18 PR-25: #25 Co-authored-by: Timothée Mazzucotelli <[email protected]>
1 parent 93cdc21 commit 0dbf958

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/griffe_pydantic/static.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ def process_attribute(attr: Attribute, cls: Class, *, processed: set[str]) -> No
7272
return
7373
processed.add(attr.canonical_path)
7474

75+
# Presence of `class-attribute` label and absence of `instance-attribute` label
76+
# indicates that the attribute is annotated with `ClassVar` and should be ignored.
77+
if "class-attribute" in attr.labels and "instance-attribute" not in attr.labels:
78+
return
79+
7580
kwargs = {}
7681
if isinstance(attr.value, ExprCall):
7782
kwargs = {
@@ -93,7 +98,9 @@ def process_attribute(attr: Attribute, cls: Class, *, processed: set[str]) -> No
9398
cls.extra[common.self_namespace]["config"] = kwargs
9499
return
95100

96-
attr.labels = {"pydantic-field"}
101+
attr.labels.add("pydantic-field")
102+
attr.labels.remove("instance-attribute")
103+
97104
attr.value = kwargs.get("default", None)
98105
constraints = {kwarg: value for kwarg, value in kwargs.items() if kwarg not in {"default", "description"}}
99106
attr.extra[common.self_namespace]["constraints"] = constraints

0 commit comments

Comments
 (0)