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

__str__ and __repr__ are squashed on child classes #1022

Closed
khaeru opened this issue Nov 22, 2019 · 1 comment · Fixed by #1023
Closed

__str__ and __repr__ are squashed on child classes #1022

khaeru opened this issue Nov 22, 2019 · 1 comment · Fixed by #1023
Labels

Comments

@khaeru
Copy link

khaeru commented Nov 22, 2019

After #884, a child class of a model does not inherit its parent's __str__ and __repr__ overloads.

import pydantic

class Foo(pydantic.BaseModel):
    x: int = 3
    y: float = 4.2

    def __str__(self):
        return str(self.y + self.x)


f = Foo()
# Expected: 7.2
# Observed: 7.2
print(str(f))


class Bar(Foo):
    z: bool = False


b = Bar()

# Expected: 7.2
# Observed: x=3 y=4.2 z=False
print(str(b))
  • OS: Ubuntu 19.10
  • Python version import sys; print(sys.version): 3.7.5 (default, Nov 20 2019, 09:21:52) [GCC 9.2.1 20191008]
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.1.1
@samuelcolvin
Copy link
Collaborator

samuelcolvin commented Nov 22, 2019

thanks for reporting, should be fixed in #1023, please check that works for you.

Bobronium pushed a commit to Bobronium/pydantic that referenced this issue Nov 28, 2019
samuelcolvin added a commit that referenced this issue Feb 27, 2020
* Refactor ._iter() method:
Moved all keys-related stuff (include, exclude, etc.) to ._iter()
Removed redundant iteration through default values
Almost all arguments checks moved out of loops, so checks happen once
Fast yield from .__dict__ on plain .iter() (x10 boost)
Removed redundant set(dict.keys()) in ._calculate_keys()

* Moved back from nested generator checks to checks, optimized copy a bit

* Bump pytest-mock from 1.12.0 to 1.12.1 (#1018)

Bumps [pytest-mock](https://github.com/pytest-dev/pytest-mock) from 1.12.0 to 1.12.1.
- [Release notes](https://github.com/pytest-dev/pytest-mock/releases)
- [Changelog](https://github.com/pytest-dev/pytest-mock/blob/master/CHANGELOG.rst)
- [Commits](pytest-dev/pytest-mock@v1.12.0...v1.12.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* __str__ and __repr__ inheritance for models, fix #1022 (#1023)

* add testimonials section to docs with reference to python bytes podcast episode (#1025)

* add testimonials section with reference to python bytes podcast episode

* added description to changes directory

* Bump twine from 3.0.0 to 3.1.0 (#1029)

Bumps [twine](https://github.com/pypa/twine) from 3.0.0 to 3.1.0.
- [Release notes](https://github.com/pypa/twine/releases)
- [Changelog](https://github.com/pypa/twine/blob/master/docs/changelog.rst)
- [Commits](pypa/twine@3.0.0...3.1.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Support typing.Literal in python 3.8 (#1027)

* Support typing.Literal in python 3.8

* Improve import pattern for Literal

* Update references to  in docs

* Try to get build to pass

* Add support for mapping types as custom root (#958)

* Add support for mapping types as custom root

* Incorporate feedback

* Add changes

* Incorporate feedback

* Add docs and tests

* Fix linting issue

* Incorporate more feedback

* Add more specific match

* Add parse_as_type function (#934)

* Add parse_as_type function

* Add changes

* Incorporate feedback

* Add naming tests

* Fix double quotes

* Fix docs example

* Reorder parameters; add dataclass and mapping tests

* Rename parse_as_type to parse_obj, and add parse_file

* Incorporate feedback

* Incorporate feedback

* use custom root types

* Add better support for validator reuse (#941)

* Add better support for validator reuse

* Clean up classmethod unpacking

* Add changes

* Fix coverage check

* Make 3.8 compatible

* Update changes/940-dmontagu.md

Co-Authored-By: Samuel Colvin <s@muelcolvin.com>

* Make allow_reuse discoverable by adding to error message

* switch _check_validator_name to _prepare_validator

* Add changes file

* Delete unrelated files

* Add check that k in fields before using alias

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Remove redundant call to __iter__

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Use typing.AbstractSet

* Update pydantic/main.py

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Colin Sullivan <csullivan@brandwatch.com>
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants