Skip to content

Allow extra keyword arguments in MetaModel.__new__ #867

@retnikt

Description

@retnikt

Bug

pydantic.main.ModelMetaclass.__new__ should include **kwargs at the end of the method definition and pass them on to the super call at the end in order to allow the special method __init_subclass__ to be defined with custom parameters on extended BaseModel classes.

Environment:

  • OS: Ubuntu 1804
  • Python version import sys; print(sys.version): 3.7.3 (default, Apr 3 2019, 19:16:38) \n[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
  • Pydantic version import pydantic; print(pydantic.VERSION): 0.31

For example:

import pydantic
class CustomBaseModel(pydantic.BaseModel):
    def __init_subclass__(cls, special_parameter):
        # do something with special_parameter
        ...

class NewModel(CustomBaseModel, special_parameter=42):
    # model definition here
    ...

should not raise any exceptions. But:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    class NewModel(CustomBaseModel, special_parameter=42):
  File "pydantic/main.py", line 178, in pydantic.main.ModelMetaclass.__new__
TypeError: __new__() got an unexpected keyword argument 'special_parameter'

I can (and will) submit a PR for this if you accept it; it is an extremely simple fix (plus not-so-simple tests because metaclasses).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions