Raise RelationshipError for all errors in @relationship declarations

Summary

RelationshipError, a subclass of ValueError, is raised for errors in the dependencies declared by @relationship methods (MR !551 (merged)). Until now, it only covered errors detected at graph analysis, while errors detected by the decorator itself, when the class is defined, raised a plain ValueError.

This MR makes RelationshipError the single exception for errors in @relationship declarations, and exposes it in cosapp.base, next to SingularJacobianError:

from cosapp.base import RelationshipError
Error in a declaration Detected Exception
Variable related to itself, e.g. @relationship("y", wrt="y") when the class is defined RelationshipError (was ValueError)
Method decorated twice when the class is defined RelationshipError (was ValueError)
Unknown variable at graph analysis RelationshipError (unchanged)
Matrix of wrong shape at graph analysis RelationshipError (unchanged)

Malformed calls of the decorator, e.g. a missing argument wrt, still raise TypeError, as they concern how the decorator is called, not what it declares.

RelationshipError deriving from ValueError, existing except ValueError handlers still catch all these errors. A dedicated GraphAnalysisError was ruled out: failures of graph analysis itself are never raised (dependencies are then assumed full, with a warning), so that only errors in user declarations need an exception.

Changes

  • cosapp/base/__init__.py: RelationshipError exposed, and added to __all__.
  • cosapp/core/ir/ops.py: decorator relationship raises RelationshipError for variables related to themselves, and methods decorated twice. Its docstring lists the errors raising RelationshipError.

Tests

  • cosapp/core/ir/tests/test_relationship.py: the tests of variables related to themselves, and of methods decorated twice, expect RelationshipError, imported from cosapp.base.
  • Imports: RelationshipError can be imported from cosapp.base whatever the first imported module (cosapp.base, cosapp.core.ir, cosapp.drivers or cosapp.systems), without circular imports.
  • Results: tests of cosapp/core/ir pass.

Merge request reports

Loading
Loading