Skip to content
Snippets Groups Projects
Commit 9908d612 authored by Martin Lange's avatar Martin Lange
Browse files

Merge branch 'relaxing_component_implementation' into 'main'

Component: don't require to overwrite _validate or _finalize

See merge request !156
parents 8db78a62 3cf9b5b3
No related branches found
No related tags found
1 merge request!156Component: don't require to overwrite _validate or _finalize
Pipeline #126486 passed with stages
in 3 minutes and 4 seconds
......@@ -189,6 +189,10 @@ This is done in :meth:`.TimeComponent._connect`.
After this connection phase, models can validate their state in :meth:`.TimeComponent._validate`. We do nothing there.
.. note::
It is not strictly required to implement `_validate` but it is highly encouraged to do so.
.. code-block:: Python
# imports...
......@@ -315,6 +319,10 @@ In method :meth:`.TimeComponent._finalize`, the component can do any cleanup req
We do nothing special here.
.. note::
It is not strictly required to implement `_finalize` but it is highly encouraged to do so.
.. code-block:: Python
# imports...
......
......@@ -116,11 +116,9 @@ class Component(IComponent, Loggable, ABC):
def _validate(self):
"""Validate the correctness of the component's settings and coupling.
Components must overwrite this method.
Components should overwrite this method.
"""
raise NotImplementedError(
f"Method `_validate` must be implemented by all components, but implementation is missing in {self.name}."
)
self.logger.debug("Method `_validate` not implemented by user.")
@final
def update(self):
......@@ -163,11 +161,9 @@ class Component(IComponent, Loggable, ABC):
def _finalize(self):
"""Finalize and clean up the component.
Components must overwrite this method.
Components should overwrite this method.
"""
raise NotImplementedError(
f"Method `_finalize` must be implemented by all components, but implementation is missing in {self.name}."
)
self.logger.debug("Method `_finalize` not implemented by user.")
@property
def inputs(self):
......
"""
Unit tests for the sdk implementations.
"""
import logging
import unittest
from datetime import datetime
......@@ -466,20 +467,32 @@ class TestNotImplemented(unittest.TestCase):
with self.assertRaises(NotImplementedError):
comp._connect()
with self.assertRaises(NotImplementedError):
# check that the debug log for not implementing _validate is there
with self.assertLogs(level=logging.DEBUG) as captured:
comp.validate()
with self.assertRaises(NotImplementedError):
self.assertEqual(len(captured.records), 2)
self.assertEqual(captured.records[0].levelno, logging.DEBUG)
self.assertEqual(captured.records[1].levelno, logging.DEBUG)
with self.assertLogs(level=logging.DEBUG) as captured:
comp._validate()
self.assertEqual(len(captured.records), 1)
self.assertEqual(captured.records[0].levelno, logging.DEBUG)
with self.assertRaises(NotImplementedError):
comp.update()
with self.assertRaises(NotImplementedError):
comp._update()
with self.assertRaises(NotImplementedError):
# check that the debug log for not implementing _finalize is there
with self.assertLogs(level=logging.DEBUG) as captured:
comp.finalize()
with self.assertRaises(NotImplementedError):
self.assertEqual(len(captured.records), 2)
self.assertEqual(captured.records[0].levelno, logging.DEBUG)
self.assertEqual(captured.records[1].levelno, logging.DEBUG)
with self.assertLogs(level=logging.DEBUG) as captured:
comp._finalize()
self.assertEqual(len(captured.records), 1)
self.assertEqual(captured.records[0].levelno, logging.DEBUG)
def test_adapter_not_implemented(self):
adapter = NotImplAdapter()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment