Skip to content

Commit

Permalink
Merge branch 'pydantic:main' into issues-8153
Browse files Browse the repository at this point in the history
  • Loading branch information
ibleedicare committed Nov 30, 2023
2 parents fc3e4d0 + 667cd37 commit 5627aea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
5 changes: 5 additions & 0 deletions pydantic/_internal/_validate_call.py
Expand Up @@ -118,6 +118,11 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
def __get__(self, obj: Any, objtype: type[Any] | None = None) -> ValidateCallWrapper:
"""Bind the raw function and return another ValidateCallWrapper wrapping that."""
if obj is None:
# It's possible this wrapper is dynamically applied to a class attribute not allowing
# name to be populated by __set_name__. In this case, we'll manually acquire the name
# from the function reference.
if self._name is None:
self._name = self.raw_function.__name__
try:
# Handle the case where a method is accessed as a class attribute
return objtype.__getattribute__(objtype, self._name) # type: ignore
Expand Down
19 changes: 0 additions & 19 deletions tests/benchmarks/test_north_star.py
Expand Up @@ -7,7 +7,6 @@
import json
from datetime import date, datetime, time
from decimal import Decimal
from hashlib import md5
from pathlib import Path
from typing import List, Union
from uuid import UUID
Expand Down Expand Up @@ -64,7 +63,6 @@ class Person(BaseModel):


_NORTH_STAR_DATA_PATH = Path(__file__).parent / 'north_star_data.json'
_EXPECTED_NORTH_STAR_DATA_MD5 = '4d30ce33e301fd00c656f95e736c7785'


@pytest.fixture(scope='module')
Expand All @@ -82,23 +80,6 @@ def _north_star_data_bytes() -> bytes:
else:
data = _NORTH_STAR_DATA_PATH.read_bytes()

# To make benchmarks a stable metric, validate the MD5 hash of the
# existing generated data. If the data is deliberately changed,
# update _EXPECTED_NORTH_STAR_DATA_MD5 above.
#
# NB updating Faker will almost certainly change the benchmark data.
data_md5 = md5(data).hexdigest()
if data_md5 != _EXPECTED_NORTH_STAR_DATA_MD5:
if needs_generating:
raise ValueError(
f'Expected hash {_EXPECTED_NORTH_STAR_DATA_MD5} for north star data, but generated {data_md5}'
)
else:
# MD5 hash mismatch, maybe shape of the data has changed. Delete
# and regenerate.
_NORTH_STAR_DATA_PATH.unlink()
return _north_star_data_bytes()

return data


Expand Down
12 changes: 12 additions & 0 deletions tests/test_validate_call.py
Expand Up @@ -684,6 +684,18 @@ def test(self, x: int):
assert bar.test('1') == (bar, 1)


def test_dynamic_method_decoration():
class Foo:
def bar(self, value: str) -> str:
return f'bar-{value}'

Foo.bar = validate_call(Foo.bar)
assert Foo.bar

foo = Foo()
assert foo.bar('test') == 'bar-test'


@pytest.mark.parametrize('decorator', [staticmethod, classmethod])
def test_classmethod_order_error(decorator):
name = decorator.__name__
Expand Down

0 comments on commit 5627aea

Please sign in to comment.