Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BaseModel.__repr__ with extra fields #3324

Closed

Conversation

povilasb
Copy link

@povilasb povilasb commented Oct 14, 2021

Change Summary

Consider this example:

from pydantic import BaseModel


class Person(BaseModel):
    name: str

    class Config:
        extra = "allow"


me = Person(name="povilas")
me.age = 30
print(me)

Latest pydantic master branch would crash with

Traceback (most recent call last):
  File "/private/tmp/main.py", line 13, in <module>
    print(me)
  File "/private/tmp/pydantic/pydantic/utils.py", line 361, in __str__
    return self.__repr_str__(' ')
  File "/private/tmp/pydantic/pydantic/utils.py", line 343, in __repr_str__
    return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
  File "/private/tmp/pydantic/pydantic/main.py", line 856, in __repr_args__
    return [(k, v) for k, v in self.__dict__.items() if self.__fields__[k].field_info.repr]
  File "/private/tmp/pydantic/pydantic/main.py", line 856, in <listcomp>
    return [(k, v) for k, v in self.__dict__.items() if self.__fields__[k].field_info.repr]
KeyError: 'age'

Pydantic 1.8.2 works fine and this is a regression after 362f4a5 .

This commit makes sure we only try to access defined fields.
As a side effect this means extra fields do not get into model representation.

I'm not sure if that's the expected behavior though.

Related issue number

#3234

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable - no public interface changes
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review.

Before this pydantic would crash with
```py
  return [(k, v) for k, v in self.__dict__.items() if self.__fields__[k].field_info.repr]
E   KeyError: 'b'
```

The culprit is pydantic@362f4a5

This commit makes sure we only try to access defined fields.
As a side effect this means extra fields do not get into model
representation.

I'm not sure if that's the expected behavior though.
@povilasb
Copy link
Author

please review

Copy link
Contributor

@christianbundy christianbundy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, and resolves an error that I was able to reproduce locally.

@samuelcolvin
Copy link
Member

This is a duplicate of #3241

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants