Skip to content

Commit 2977b21

Browse files
committed
fix: Don't process properties as fields
Issue-29: #29
1 parent 8d4cc61 commit 2977b21

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ classifiers = [
3030
"Typing :: Typed",
3131
]
3232
dependencies = [
33-
"griffe>=0.49",
33+
"griffe>=1.6.3",
3434
]
3535

3636
[project.urls]

src/griffe_pydantic/_internal/static.py

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def _process_attribute(attr: Attribute, cls: Class, *, processed: set[str]) -> N
7171
return
7272
processed.add(attr.canonical_path)
7373

74+
# Properties are not fields.
75+
if "property" in attr.labels:
76+
return
77+
7478
# Presence of `class-attribute` label and absence of `instance-attribute` label
7579
# indicates that the attribute is annotated with `ClassVar` and should be ignored.
7680
if "class-attribute" in attr.labels and "instance-attribute" not in attr.labels:

tests/test_extension.py

+40
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,43 @@ def set_if_none(cls, v: Any, info):
192192
assert validator.labels == {"pydantic-validator"}
193193
assert validator in package["Schema.a"].extra["griffe_pydantic"]["validators"]
194194
assert validator in package["Schema.b"].extra["griffe_pydantic"]["validators"]
195+
196+
197+
def test_ignoring_properties() -> None:
198+
"""Properties are not fields and must be ignored."""
199+
code = """
200+
from pydantic import BaseModel, field
201+
202+
class Base(BaseModel):
203+
@property
204+
def a(self) -> int:
205+
return 0
206+
207+
class Model(Base):
208+
b: int = field(default=1)
209+
"""
210+
with temporary_visited_package(
211+
"package",
212+
modules={"__init__.py": code},
213+
extensions=Extensions(PydanticExtension(schema=False)),
214+
) as package:
215+
assert "pydantic-field" not in package["Model.a"].labels
216+
217+
218+
def test_process_non_model_base_class_fields() -> None:
219+
"""Fields in a non-model base class must be processed."""
220+
code = """
221+
from pydantic import BaseModel, field
222+
223+
class A:
224+
a: int = 0
225+
226+
class B(BaseModel, A):
227+
b: int = 1
228+
"""
229+
with temporary_visited_package(
230+
"package",
231+
modules={"__init__.py": code},
232+
extensions=Extensions(PydanticExtension(schema=False)),
233+
) as package:
234+
assert "pydantic-field" in package["B.a"].labels

0 commit comments

Comments
 (0)