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

Methods dynamically decorated with validate_call raises TypeError when accessed #8245

Closed
1 task done
tibdex opened this issue Nov 28, 2023 · 3 comments · Fixed by #8249
Closed
1 task done

Methods dynamically decorated with validate_call raises TypeError when accessed #8245

tibdex opened this issue Nov 28, 2023 · 3 comments · Fixed by #8249
Assignees
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@tibdex
Copy link

tibdex commented Nov 28, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Decorating a method manually with @validate_call works fine but decorating one dynamically raises the following error when accessing the method from the class (not an instance):

Traceback (most recent call last):
  File "/Users/.../test/test.py", line 20, in <module>
    assert Foo.baz
  File "/Users/.../test/.venv/lib/python3.9/site-packages/pydantic/_internal/_validate_call.py", line 123, in __get__
    return objtype.__getattribute__(objtype, self._name)  # type: ignore
TypeError: attribute name must be string, not 'NoneType'

I'm not sure how much related to #8146 this is.

Example Code

from pydantic import validate_call


class Foo:
    @validate_call
    def bar(self, value: str) -> str:
        return f"bar-{value}"

    def baz(self, value: str) -> str:
        return f"baz-{value}"


Foo.baz = validate_call(Foo.baz)

foo = Foo()
assert foo.bar("qux") == "bar-qux"
assert foo.baz("qux") == "baz-qux"

assert Foo.bar
assert Foo.baz  # Raises `TypeError: attribute name must be string, not 'NoneType'`
assert hasattr(Foo, "baz")  # Raises too.

Python, Pydantic & OS Version

             pydantic version: 2.5.2
        pydantic-core version: 2.14.5
          pydantic-core build: profile=release pgo=false
                 install path: /Users/.../test/.venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.18 (main, Nov  9 2023, 15:24:25)  [Clang 15.0.0 (clang-1500.0.40.1)]
                     platform: macOS-14.1.1-arm64-arm-64bit
             related packages: mypy-1.6.1 typing_extensions-4.6.1
@tibdex tibdex added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Nov 28, 2023
@sydney-runkle sydney-runkle self-assigned this Nov 29, 2023
@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Nov 29, 2023
@sydney-runkle
Copy link
Member

@tibdex,

Thanks for reporting this. Should be a pretty easy fix. I'll fix this tomorrow 🐛!

@jusexton
Copy link
Contributor

I know you said you'd tackle this tomorrow @sydney-runkle but looked like a simple and fun fix. I went ahead and created a PR, I hope that's alright.

I'm not super knowledgeable on custom python descriptors so if there is a better solution than the one presented in the PR let me know!

@sydney-runkle
Copy link
Member

@jusexton,

Fantastic! Thanks a bunch! We always welcome PRs 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
3 participants