Skip to content

Commit

Permalink
fix: allow submodels to overwrite extra field info (#3935)
Browse files Browse the repository at this point in the history
* fix: allow submodels to overwrite extra field info

* fix(deps): pin jinja2 for mkdocs
  • Loading branch information
PrettyWood committed Aug 8, 2022
1 parent ef4a5d3 commit ea87011
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/3934-PrettyWood.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow submodels to overwrite extra field info
3 changes: 2 additions & 1 deletion pydantic/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def update_from_config(self, from_config: Dict[str, Any]) -> None:
current_value = getattr(self, attr_name)
except AttributeError:
# attr_name is not an attribute of FieldInfo, it should therefore be added to extra
self.extra[attr_name] = value
# (except if extra already has this value!)
self.extra.setdefault(attr_name, value)
else:
if current_value is self.__field_constraints__.get(attr_name, None):
setattr(self, attr_name, value)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3001,3 +3001,27 @@ class Model(BaseModel):
},
},
}


def test_extra_inheritance():
class A(BaseModel):
root: Optional[str]

class Config:
fields = {
'root': {'description': 'root path of data', 'level': 1},
}

class Model(A):
root: str = Field('asa', description='image height', level=3)

m = Model()
assert m.schema()['properties'] == {
'root': {
'title': 'Root',
'type': 'string',
'description': 'image height',
'default': 'asa',
'level': 3,
}
}

0 comments on commit ea87011

Please sign in to comment.