Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/validataclass/validators/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ class Validator(Generic[T_Validated], ABC):
"""
Base class for building extendable validator classes that validate, sanitize and transform input.
"""
_validate_accepts_kwargs: bool = False

@override
def __init_subclass__(cls, **kwargs: Any):
cls._validate_accepts_kwargs = inspect.getfullargspec(cls.validate).varkw is not None

# Check if subclasses are future-proof
if inspect.getfullargspec(cls.validate).varkw is None:
if not cls._validate_accepts_kwargs:
warnings.warn(
"Validator classes will be required to accept arbitrary keyword arguments in their validate() method "
f"in the future. Please add **kwargs to the list of parameters of {cls.__name__}.validate().",
Expand Down Expand Up @@ -67,7 +70,7 @@ def validate_with_context(self, input_data: Any, **kwargs: Any) -> T_Validated:
Use this method only if you want/need to pass context arguments to a validator and don't know for sure that the
validator accepts keyword arguments (e.g. because you don't know the class of the validator).
"""
if inspect.getfullargspec(self.validate).varkw is not None:
if self._validate_accepts_kwargs:
return self.validate(input_data, **kwargs)
else:
return self.validate(input_data)
Expand Down