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

Allow extra keyword arguments in MetaModel.__new__ #867

Closed
retnikt opened this issue Oct 6, 2019 · 0 comments · Fixed by #868
Closed

Allow extra keyword arguments in MetaModel.__new__ #867

retnikt opened this issue Oct 6, 2019 · 0 comments · Fixed by #868

Comments

@retnikt
Copy link
Contributor

retnikt commented Oct 6, 2019

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).

@retnikt retnikt added the bug V1 Bug related to Pydantic V1.X label Oct 6, 2019
samuelcolvin pushed a commit that referenced this issue Oct 8, 2019
…__ (#868)

* Fix #867 by accepting and passing on **kwargs in ModelMetaclass.__new__

pydantic.main.MetaModel.__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.

* MetaModel -> ModelMetaclass

* shrink change notes

* rename and reformat change description
@samuelcolvin samuelcolvin added feature request and removed bug V1 Bug related to Pydantic V1.X labels Nov 22, 2019
andreshndz pushed a commit to cuenca-mx/pydantic that referenced this issue Jan 17, 2020
…ss.__new__ (pydantic#868)

* Fix pydantic#867 by accepting and passing on **kwargs in ModelMetaclass.__new__

pydantic.main.MetaModel.__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.

* MetaModel -> ModelMetaclass

* shrink change notes

* rename and reformat change description
alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants