• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

patrickboateng / func-validator / 19455581050

18 Nov 2025 05:43AM UTC coverage: 97.669% (-0.2%) from 97.882%
19455581050

push

github

patrickboateng
bug found and fixed

46 of 46 branches covered (100.0%)

Branch coverage included in aggregate %.

8 of 10 new or added lines in 3 files covered. (80.0%)

792 of 812 relevant lines covered (97.54%)

1.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/func_validator/validators/dependent_argument_validator.py
1
from typing import Type
2✔
2

3
from ._core import T, ValidationError, Validator
2✔
4
from .numeric_arg_validators import MustBeLessThan, MustBeProvided
2✔
5

6
__all__ = ["DependsOn"]
2✔
7

8

9
class DependsOn(Validator):
2✔
10
    """Class to indicate that a function argument depends on another
11
    argument.
12

13
    When an argument is marked as depending on another, it implies that
14
    the presence or value of one argument may influence the validation
15
    or necessity of the other.
16
    """
17

18
    def __init__(
2✔
19
        self,
20
        *args: str,
21
        args_strategy: Type[Validator] = MustBeLessThan,
22
        kw_strategy: Type[Validator] = MustBeProvided,
23
        err_msg: str = "",
24
        **kwargs: T,
25
    ):
26
        """
27
        :param args: The names of the arguments that the current argument
28
                     depends on.
29
        :param args_strategy: The validation strategy to apply based on
30
                              the values of the dependent arguments.
31
        :param kw_strategy: The validation strategy to apply when
32
                            dependent arguments match specific values.
33
        :param kwargs: Key-value pairs where the key is the name of the
34
                       dependent argument and the value is the specific
35
                       value to match for applying the strategy.
36
        """
37
        super().__init__(err_msg=err_msg)
2✔
38
        self.args_dependencies = args
2✔
39
        self.kw_dependencies = kwargs.items()
2✔
40
        self.args_strategy = args_strategy
2✔
41
        self.kw_strategy = kw_strategy
2✔
42
        self.arguments: dict = {}
2✔
43

44
    def _get_depenency_value(self, dep_arg_name: str) -> T:
2✔
45
        try:
2✔
46
            actual_value = self.arguments[dep_arg_name]
2✔
47
        except KeyError:
2✔
48
            try:
2✔
49
                instance = self.arguments["self"]
2✔
50
                actual_value = getattr(instance, dep_arg_name)
2✔
51
            except (AttributeError, KeyError):
2✔
52
                msg = f"Dependency argument '{dep_arg_name}' not found."
2✔
53
                raise ValidationError(msg)
2✔
54
        return actual_value
2✔
55

56
    def _validate_args_dependencies(self, arg_val, arg_name: str):
2✔
57
        for dep_arg_name in self.args_dependencies:
2✔
58
            actual_dep_arg_val = self._get_depenency_value(dep_arg_name)
2✔
59
            if self.err_msg:
2✔
NEW
60
                strategy = self.args_strategy(
×
61
                    actual_dep_arg_val,
62
                    err_msg=self.err_msg,
63
                )
64
            else:
65
                strategy = self.args_strategy(actual_dep_arg_val)
2✔
66
            strategy(arg_val, arg_name)
2✔
67

68
    def _validate_kw_dependencies(self, arg_val, arg_name: str):
2✔
69
        for dep_arg_name, dep_arg_val in self.kw_dependencies:
2✔
70
            actual_dep_arg_val = self._get_depenency_value(dep_arg_name)
2✔
71
            if actual_dep_arg_val == dep_arg_val:
2✔
72
                if self.err_msg:
2✔
NEW
73
                    strategy = self.kw_strategy(err_msg=self.err_msg)
×
74
                else:
75
                    strategy = self.kw_strategy()
2✔
76
                strategy(arg_val, arg_name)
2✔
77

78
    def __call__(self, arg_val, arg_name: str):
2✔
79
        if self.args_dependencies:
2✔
80
            self._validate_args_dependencies(arg_val, arg_name)
2✔
81
        if self.kw_dependencies:
2✔
82
            self._validate_kw_dependencies(arg_val, arg_name)
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc