Skip to content

__str__ and __repr__ are squashed on child classes #1022

@khaeru

Description

@khaeru

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions