|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import dataclasses |
4 | 3 | import functools |
5 | 4 | import inspect |
6 | 5 | import types |
@@ -92,7 +91,8 @@ def __str__(self): |
92 | 91 | errors += f"\n `{name}`: {value!r} of type {type(value)} is not {pattern.describe()}" |
93 | 92 |
|
94 | 93 | sig = f"{self.func.__name__}{self.sig}" |
95 | | - cause = str(self.__cause__).lstrip('bind_fn() ') if self.__cause__ else "" |
| 94 | + # remove the leading "_custom_bind_fn()" that comes from the custom bind function generated in SignatureBinder |
| 95 | + cause = str(self.__cause__).lstrip('_custom_bind_fn() ') if self.__cause__ else "" |
96 | 96 |
|
97 | 97 | return self.msg.format(sig=sig, call=call, cause=cause, errors=errors) |
98 | 98 |
|
@@ -347,10 +347,10 @@ def __init__(self, signature: Signature): |
347 | 347 |
|
348 | 348 | # build a new signature with default values replaced with generated variable names |
349 | 349 | processed_signature = inspect.Signature(parameters=processed_params) |
350 | | - self.bind_fn_str = f'def bind_fn{processed_signature}:\n return locals()' |
| 350 | + self.bind_fn_str = f'def _custom_bind_fn{processed_signature}:\n return locals()' |
351 | 351 |
|
352 | 352 | exec(compile(self.bind_fn_str, '<string>', 'exec'), namespace) |
353 | | - self._bind_fn = namespace['bind_fn'] |
| 353 | + self._bind_fn = namespace['_custom_bind_fn'] |
354 | 354 |
|
355 | 355 | def __call__(self, *args, **kwargs): |
356 | 356 | return self._bind_fn(*args, **kwargs) |
@@ -581,7 +581,7 @@ def validate(self, func, args, kwargs): |
581 | 581 | return this |
582 | 582 |
|
583 | 583 | def validate_fast(self, func, args, kwargs): |
584 | | - """Faster validation using internal dataclass to bind args/kwargs to names instead of Signature.bind.""" |
| 584 | + """Faster validation using custom bind function for this signature (instead of Signature.bind).""" |
585 | 585 | try: |
586 | 586 | bound_kwargs = self._binder_fn(*args, **kwargs) |
587 | 587 | except TypeError as err: |
@@ -647,18 +647,6 @@ def validate_return(self, func, value): |
647 | 647 | ) |
648 | 648 |
|
649 | 649 | return result |
650 | | - |
651 | | - |
652 | | -class DefaultFactory: |
653 | | - """Helper to create default factories for dataclass fields.""" |
654 | | - |
655 | | - __slots__ = ("value",) |
656 | | - |
657 | | - def __init__(self, value): |
658 | | - self.value = value |
659 | | - |
660 | | - def __call__(self): |
661 | | - return self.value |
662 | 650 |
|
663 | 651 |
|
664 | 652 | def annotated(_1=None, _2=None, _3=None, **kwargs): |
|
0 commit comments