MCPcopy
hub / github.com/scikit-learn/scikit-learn / HasMethods

Class HasMethods

sklearn/utils/_param_validation.py:676–709  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

674
675
676class 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
712class _IterablesNotString(_Constraint):

Callers 15

OneVsRestClassifierClass · 0.90
OneVsOneClassifierClass · 0.90
PipelineClass · 0.90
_BaseChainClass · 0.90
BaseSearchCVClass · 0.90
test_hasmethodsFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_hasmethodsFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…