Constraint representing objects that expose specific methods. It is useful for parameters following a protocol and where we don't want to impose an affiliation to a specific module or class. Parameters ---------- methods : str or list of str The method(s) that the objec
| 674 | |
| 675 | |
| 676 | class HasMethods(_Constraint): |
| 677 | """Constraint representing objects that expose specific methods. |
| 678 | |
| 679 | It is useful for parameters following a protocol and where we don't want to impose |
| 680 | an affiliation to a specific module or class. |
| 681 | |
| 682 | Parameters |
| 683 | ---------- |
| 684 | methods : str or list of str |
| 685 | The method(s) that the object is expected to expose. |
| 686 | """ |
| 687 | |
| 688 | @validate_params( |
| 689 | {"methods": [str, list]}, |
| 690 | prefer_skip_nested_validation=True, |
| 691 | ) |
| 692 | def __init__(self, methods): |
| 693 | super().__init__() |
| 694 | if isinstance(methods, str): |
| 695 | methods = [methods] |
| 696 | self.methods = methods |
| 697 | |
| 698 | def is_satisfied_by(self, val): |
| 699 | return all(callable(getattr(val, method, None)) for method in self.methods) |
| 700 | |
| 701 | def __str__(self): |
| 702 | if len(self.methods) == 1: |
| 703 | methods = f"{self.methods[0]!r}" |
| 704 | else: |
| 705 | methods = ( |
| 706 | f"{', '.join([repr(m) for m in self.methods[:-1]])} and" |
| 707 | f" {self.methods[-1]!r}" |
| 708 | ) |
| 709 | return f"an object implementing {methods}" |
| 710 | |
| 711 | |
| 712 | class _IterablesNotString(_Constraint): |
no outgoing calls
searching dependent graphs…