Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly prevent the instantiation of abstract classes #202

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ myst:

## Unreleased

- {gh-pr}`202` {gh-issue}`188` Explicitly prevent instantiation of abstract classes.
- {gh-pr}`201` {gh-issue}`185` Add `type` attribute to the load classes and rename branches `branch_type`
attribute to `type` for consistency. Please replace `branch.branch_type` by `branch.type` in your code.
In addition, loads data frames gained two new columns:
Expand Down
2 changes: 2 additions & 0 deletions roseau/load_flow/models/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def __init__(
geometry:
The geometry of the branch.
"""
if type(self) is AbstractBranch:
raise TypeError("Can't instantiate abstract class AbstractBranch")
super().__init__(id, **kwargs)
self._check_phases(id, phases1=phases1)
self._check_phases(id, phases2=phases2)
Expand Down
2 changes: 2 additions & 0 deletions roseau/load_flow/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __init__(self, id: Id, **kwargs: Any) -> None:
A unique ID of the element in the network. Two elements of the same type cannot
have the same ID.
"""
if type(self) is Element:
raise TypeError("Can't instantiate abstract class Element")
super().__init__(id)
self._connected_elements: list[Element] = []
self._network: ElectricalNetwork | None = None
Expand Down
2 changes: 2 additions & 0 deletions roseau/load_flow/models/loads/loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def __init__(self, id: Id, bus: Bus, *, phases: str | None = None, **kwargs: Any
:attr:`allowed_phases`. All phases of the load, except ``"n"``, must be present in
the phases of the connected bus. By default, the phases of the bus are used.
"""
if type(self) is AbstractLoad:
raise TypeError("Can't instantiate abstract class AbstractLoad")
super().__init__(id, **kwargs)
if phases is None:
phases = bus.phases
Expand Down
14 changes: 14 additions & 0 deletions roseau/load_flow/models/tests/test_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest

from roseau.load_flow.models import AbstractBranch, AbstractLoad, Bus, Element


def test_abstract_classes():
with pytest.raises(TypeError, match="Can't instantiate abstract class Element"):
Element("element_id")
bus1 = Bus("bus1", phases="an")
bus2 = Bus("bus2", phases="an")
with pytest.raises(TypeError, match="Can't instantiate abstract class AbstractBranch"):
AbstractBranch("branch_id", bus1=bus1, bus2=bus2, phases1="an", phases2="an")
with pytest.raises(TypeError, match="Can't instantiate abstract class AbstractLoad"):
AbstractLoad("load_id", bus=bus1, phases="an")
Loading