Skip to content

Commit

Permalink
#3234 exclude extra field when represent model (#3241)
Browse files Browse the repository at this point in the history
* exclude extra field when represent model

* add test code

* fix W293

* add change md

* Update changes/3234-cocolman.md

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update pydantic/main.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update tests/test_main.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
  • Loading branch information
Hyun Sol and samuelcolvin committed Dec 19, 2021
1 parent 93faefb commit c532e83
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/3234-cocolman.md
@@ -0,0 +1 @@
Fix display of `extra` fields with model `__repr__`
4 changes: 3 additions & 1 deletion pydantic/main.py
Expand Up @@ -873,7 +873,9 @@ def __eq__(self, other: Any) -> bool:
return self.dict() == other

def __repr_args__(self) -> 'ReprArgs':
return [(k, v) for k, v in self.__dict__.items() if self.__fields__[k].field_info.repr]
return [
(k, v) for k, v in self.__dict__.items() if k not in self.__fields__ or self.__fields__[k].field_info.repr
]


_is_base_model_class_defined = True
Expand Down
10 changes: 10 additions & 0 deletions tests/test_main.py
Expand Up @@ -208,6 +208,16 @@ class Config:
assert Model(a='10.2', b=12).dict() == {'a': 10.2, 'b': 12}


def test_allow_extra_repr():
class Model(BaseModel):
a: float = ...

class Config:
extra = Extra.allow

assert str(Model(a='10.2', b=12)) == 'a=10.2 b=12'


def test_forbidden_extra_success():
class ForbiddenExtra(BaseModel):
foo = 'whatever'
Expand Down

0 comments on commit c532e83

Please sign in to comment.