validation#

Validation suppression utilities.

This module provides context managers and class decorators to temporarily disable validation methods in a controlled, nestable, and context-local way.

Suppression can be specified at two levels:

  • Class level: Suppress validation for specific classes using fnmatch patterns

  • Method level: Suppress validation for specific methods using fnmatch patterns

Examples

>>> @validation_aware(allowed=["validate", "_check_inner"], default=["validate"])
... class MyClass:
...     def __init__(self) -> None:
...         self.validate_calls = 0
...         self.check_calls = 0
...         # You can also suppress this initial validate via no_validation().
...         self.validate()  # triggers validate (and _check_inner) once on init
...
...     def validate(self) -> bool:
...         self.validate_calls += 1
...         self._check_inner()
...         return True
...
...     def _check_inner(self) -> bool:
...         self.check_calls += 1
...         return True
...
>>> obj = MyClass()
>>> (obj.validate_calls, obj.check_calls)
(1, 1)
>>> # Suppress validation during construction (including the init call)
>>> # Uses class defaults when no methods specified
>>> with no_validation():
...     obj2 = MyClass()
>>> (obj2.validate_calls, obj2.check_calls)
(0, 0)
>>> # Suppress only the inner check; outer validate still runs
>>> with no_validation(methods=["_check_inner"]):
...     obj.validate()
>>> (obj.validate_calls, obj.check_calls)
(2, 1)
>>> # Suppress both validate and its inner call
>>> with no_validation(methods=["validate"]):
...     obj.validate()
>>> (obj.validate_calls, obj.check_calls)
(2, 1)
>>> # Suppress validation for specific classes only
>>> @validation_aware(allowed=["validate"], default=["validate"])
... class ClassA:
...     def validate(self) -> bool:
...         return True
>>> @validation_aware(allowed=["validate"], default=["validate"])
... class ClassB:
...     def validate(self) -> bool:
...         return True
>>> with no_validation(classes=["ClassA"]):
...     a = ClassA()  # validate suppressed
...     b = ClassB()  # validate NOT suppressed
>>> # Suppress specific methods for specific classes
>>> with no_validation(classes=["ClassA"], methods=["validate"]):
...     a = ClassA()  # validate suppressed
>>> # Use wildcards for classes
>>> with no_validation(classes=["*Generator"]):  # All classes ending in "Generator"
...     gen = DirectedGenerator(...)  # validate suppressed
>>> # Nested suppression: outer block suppresses validate; inner would suppress check,
>>> # but validate itself is already skipped
>>> with no_validation(methods=["validate"]):
...     with no_validation(methods=["_check_inner"]):
...         obj.validate()
>>> (obj.validate_calls, obj.check_calls)
(2, 1)
>>> # Outside the context, validation runs normally
>>> obj.validate()
True
>>> (obj.validate_calls, obj.check_calls)
(3, 2)
phylozoo.utils.validation.no_validation(classes: Iterable[str] | None = None, methods: Iterable[str] | None = None) Iterator[None][source]#

Temporarily disable validation within the current context.

Parameters:
  • classes (Iterable[str], optional) – Class names or fnmatch patterns to suppress validation for. If None, considers any validation_aware class. Examples: ["DirectedGenerator"], ["*Generator"], ["Directed*"]

  • methods (Iterable[str], optional) – Method names or fnmatch patterns to suppress. If None, uses the class defaults supplied by validation_aware (active defaults in this context). Examples: ["validate"], ["_validate_*"], ["validate", "_check"]

Yields:

None – Context scope where validation is suppressed for matching classes and methods.

Examples

>>> # Suppress all validation (uses class defaults)
>>> with no_validation():
...     obj = MyClass()
>>> # Suppress only specific methods for all classes
>>> with no_validation(methods=["validate"]):
...     obj.validate()
>>> # Suppress only specific classes (uses their defaults)
>>> with no_validation(classes=["DirectedGenerator"]):
...     gen = DirectedGenerator(...)  # validate suppressed
...     net = DirectedPhyNetwork(...)  # validate NOT suppressed
>>> # Suppress specific methods for specific classes
>>> with no_validation(classes=["DirectedGenerator"], methods=["validate"]):
...     gen = DirectedGenerator(...)  # validate suppressed
phylozoo.utils.validation.validation_aware(allowed: Sequence[str], default: Sequence[str] | None = None) Callable[[Type[T]], Type[T]][source]#

Decorate a class so selected methods honor validation suppression.

Parameters:
  • allowed (Sequence[str]) – Method names or fnmatch patterns that may be suppressed.

  • default (Sequence[str], optional) – Patterns suppressed by default for this class (must match at least one allowed pattern).

Returns:

Class decorator that wraps allowed methods.

Return type:

Callable[[Type[T]], Type[T]]

Raises:

PhyloZooValueError – If a default pattern does not match any allowed pattern.