-
-
Notifications
You must be signed in to change notification settings - Fork 12
bug: Validators on an inherited field cause a bug #19
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Description of the bug
I believe if you create a validator method on a field called id that it will cause an error:
.../lib/python3.10/site-packages/griffe_pydantic/common.py", line 72, in <listcomp>
targets = [cls.members[field] for field in fields]
KeyError: 'id'Docs were building fine until I added the following validator to my model:
@field_validator("id", mode="before")
@classmethod
def convert_null_id_to_empty(cls, v: Any) -> Any:
return v if v is not None else ""Since the cls.members is coming from a griffe.Class object I'm not sure if this is a bug there rather than in this extension, but I suspect that it's much more likely to crop up here as typically you would avoid using id as an object attribute.
UPDATE: I loaded in my package using griffe in my interpreter and I found the real cause of the bug. It seems that the field doesn't exist on the Class object because I guess griffe does not include the inherited attributes from the base class (where id is defined)?
So I think MRE:
from typing import Any
from pydantic import BaseModel, field_validator
class MyBase(BaseModel):
id: str
class AllowNones(MyBase):
@field_validator("id", mode="before")
@classmethod
def convert_null_id_to_empty(cls, v: Any) -> Any:
return v if v is not None else ""I would guess a fix might be to use cls.all_members?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working