Warnings

A warning can be raised similar to an exception, but it doesn’t interrupt execution at the position where it was raised. The warning travels upwards the call-stack until it’s handled by a WarningCollector similar to a try .. except statement. If a warning isn’t handled within the call-stack, it raises an exception.

A warning is raised by Calling the class-method WarningCollector.Raise. This function expects a single parameter: an instance of Warning.

To handle a raised warning, a with-statement is used to collect raised warnings. Usually, a list is handed over to a WarningCollector context.

from pyTooling.Warning import WarningCollector

class ClassA:
  def methA(self) -> None:
    WarningCollector.Raise(Warning("Warning from ClassA.methA"))
from pyTooling.Warning import WarningCollector

class Caller:

  def operation(self) -> None:
    warnings = []

    a = ClassA()
    with WarningCollector(warnings) as warning:
      a.methA()

    print("Warnings:)
    for warning in warnings:
      print(f"  {warning}")