Skip to content

@validate_arguments on instance methods #1222

@NovaNekmit

Description

@NovaNekmit

Bug

Hello, I tried using the new @validate_arguments decorator and it doesn't work when used on instance methods.

I didn't see it on the ToDo in #1205 and it seems like an oversight, maybe due to the special treatment of self.

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

$ python3 -c "import pydantic.utils; print(pydantic.utils.version_info())"
             pydantic version: 1.4a1
            pydantic compiled: False
                 install path: /home/[user]/git/pydantic/pydantic
               python version: 3.7.5 (default, Nov 20 2019, 09:21:52)  [GCC 9.2.1 20191008]
                     platform: Linux-5.3.0-29-generic-x86_64-with-Ubuntu-19.10-eoan
     optional deps. installed: []
from pydantic import validate_arguments


class SomeObject:
    @validate_arguments
    def some_function(self, i: int):
        print(type(self), self)
        print(type(i), i)

o = SomeObject()
o.some_function(1)  # doesn't work, instead of `i` `self` becomes 1
#pydantic.error_wrappers.ValidationError: 1 validation error for SomeFunction
#i
#  field required (type=value_error.missing)

o.some_function(o, 1)  # works, but not the way instance methods are meant to be used
#<class '__main__.SomeObject'> <__main__.SomeObject object at 0x7f32911af3d0>
#<class 'int'> 1

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