Skip to content

Commit 0e997af

Browse files
authored
fix: fix AttributeError: 'FieldInfo' object has no attribute 'field_info' (#32)
* fix: fix issue AttributeError: 'FieldInfo' object has no attribute 'field_info' #31 * add another test
1 parent 3c2bd15 commit 0e997af

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/fieldz/adapters/_pydantic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def fields(
180180
| type[pydantic.BaseModel]
181181
| type[PydanticV1BaseModel],
182182
) -> tuple[Field, ...]:
183-
if hasattr(type(obj), "model_fields") or hasattr(obj, "__pydantic_fields__"):
183+
cls = obj if isinstance(obj, type) else type(obj)
184+
if hasattr(cls, "model_fields") or hasattr(obj, "__pydantic_fields__"):
184185
obj = cast("pydantic.BaseModel | type[pydantic.BaseModel]", obj)
185186
return tuple(_fields_v2(obj))
186187
if hasattr(obj, "__pydantic_model__"):

tests/test_pydantic.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ class M(BaseModel):
2222
e: e_constr = "abc" # type: ignore
2323
f: str = f_field
2424

25-
for f in fields(M):
26-
assert f.constraints
27-
if f.name in {"e", "f"}:
28-
assert f.constraints.pattern == PATTERN
29-
else:
30-
assert f.constraints.ge == 42
31-
assert f.constraints.le == 100
32-
assert f.default == 50
33-
assert f.default == 50
25+
for obj in (M, M()):
26+
for f in fields(obj):
27+
assert f.constraints
28+
if f.name in {"e", "f"}:
29+
assert f.constraints.pattern == PATTERN
30+
else:
31+
assert f.constraints.ge == 42
32+
assert f.constraints.le == 100
33+
assert f.default == 50
34+
assert f.default == 50

0 commit comments

Comments
 (0)