Skip to content

inherit_config overwrites json_encoders from class creation arguments #2521

@layday

Description

@layday

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.8.1
            pydantic compiled: False
                 install path: [...]/lib/python3.9/site-packages/pydantic
               python version: 3.9.2 (default, Feb 21 2021, 06:38:26)  [Clang 7.1.0 (tags/RELEASE_710/final)]
                     platform: macOS-10.14.6-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']
import pydantic

class Foo(pydantic.BaseModel):
    class Config:
        json_encoders = {int: str}

print('Foo json_encoders:', Foo.__config__.json_encoders)


class Bar(pydantic.BaseModel, json_encoders={int: str}):
    pass

print('Bar json_encoders:', Bar.__config__.json_encoders)

Output:

Foo json_encoders: {<class 'int'>: <class 'str'>}
Bar json_encoders: {}

Culprit:

https://github.com/samuelcolvin/pydantic/blob/62bb2ad4921016df51abf3922c3fe51113b08939/pydantic/main.py#L184-L187

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