Skip to content

Commit 2684be7

Browse files
committed
fix: Use set.discard instead of set.remove to avoid key error
Also discard `class-attribute` label. Issue-26: #26
1 parent 9c3f4c3 commit 2684be7

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/griffe_pydantic/static.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def process_attribute(attr: Attribute, cls: Class, *, processed: set[str]) -> No
9999
return
100100

101101
attr.labels.add("pydantic-field")
102-
attr.labels.remove("instance-attribute")
102+
attr.labels.discard("class-attribute")
103+
attr.labels.discard("instance-attribute")
103104

104105
attr.value = kwargs.get("default", None)
105106
constraints = {kwarg: value for kwarg, value in kwargs.items() if kwarg not in {"default", "description"}}

tests/test_extension.py

+19
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,22 @@ class TestModel(pydantic.BaseModel):
146146
record.levelname == "DEBUG" and "field 'package.TestModel.abc' as literal" in record.message
147147
for record in caplog.records
148148
)
149+
150+
151+
def test_ignore_classvars() -> None:
152+
"""Test the extension ignores class variables."""
153+
code = """
154+
from pydantic import BaseModel
155+
from typing import ClassVar
156+
157+
class Model(BaseModel):
158+
field: str
159+
class_var: ClassVar[int] = 1
160+
"""
161+
with temporary_visited_package(
162+
"package",
163+
modules={"__init__.py": code},
164+
extensions=Extensions(PydanticExtension(schema=False)),
165+
) as package:
166+
assert "pydantic-field" not in package["Model.class_var"].labels
167+
assert "class-attribute" in package["Model.class_var"].labels

0 commit comments

Comments
 (0)