Skip to content

Commit

Permalink
chore: Switch to strict mypy (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein authored Jul 9, 2024
1 parent bcba12e commit fc501bc
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 19 deletions.
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ requires-python = ">=3.11"
authors = [
{ name = "Hochfrequenz Unternehmensberatung GmbH", email = "info@hochfrequenz.de" },
]
keywords = ["automation", "ahb", "bdew", "edi@energy"]
keywords = ["automation", "ahb", "bdew", "ebd", "edi@energy"]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
Expand Down Expand Up @@ -67,6 +67,12 @@ profile = "black"
[tool.pylint."MESSAGES CONTROL"]
max-line-length = 120

[mypy]
truethy-bool = true

[tool.mypy]
disable_error_code = []

# the following lines are needed if you would like to build a python package
# and you want to use semantic versioning
# [build-system]
Expand Down
12 changes: 6 additions & 6 deletions src/rebdhuhn/add_watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from pathlib import Path
from typing import TextIO, Tuple, Union

from lxml import etree # type:ignore[import]
from svgutils.compose import SVG, Figure # type:ignore[import]
from lxml import etree # type:ignore[import-untyped]
from svgutils.compose import SVG, Figure # type:ignore[import-untyped]

# Sets the size of the watermark compared to the smaller dimension of the ebd diagram
FINAL_SCALING_FACTOR = 0.8
Expand Down Expand Up @@ -62,17 +62,17 @@ def add_background(svg: str) -> str:
background_color = "#f3f1f6"
tree = etree.parse(BytesIO(svg.encode("utf-8"))) # pylint:disable=c-extension-no-member
root = tree.getroot()
xml_element = etree.Element(
xml_element = etree.Element( # pylint:disable=c-extension-no-member
"polygon",
attrib={
"fill": background_color,
"points": f"0,0 {ebd_width_in_px},0 {ebd_width_in_px},{ebd_height_in_px} 0,{ebd_height_in_px}",
},
) # pylint:disable=c-extension-no-member
)
root.insert(0, xml_element)

svg_with_background = Figure(ebd_width_in_px, ebd_height_in_px, root).tostr()
return svg_with_background.decode("utf-8")
return svg_with_background.decode("utf-8") # type:ignore[no-any-return]


# pylint: disable = c-extension-no-member
Expand Down Expand Up @@ -107,4 +107,4 @@ def add_watermark(ebd_svg: str) -> str:
etree.fromstring(ebd_svg_as_bytes),
).tostr()

return ebd_with_watermark.decode("utf-8")
return ebd_with_watermark.decode("utf-8") # type:ignore[no-any-return]
2 changes: 1 addition & 1 deletion src/rebdhuhn/graph_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Dict, List, Optional

from networkx import DiGraph # type:ignore[import]
from networkx import DiGraph # type:ignore[import-untyped]

from rebdhuhn.models import (
DecisionNode,
Expand Down
2 changes: 1 addition & 1 deletion src/rebdhuhn/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing import List, Tuple

from networkx import DiGraph, all_simple_paths # type:ignore[import]
from networkx import DiGraph, all_simple_paths # type:ignore[import-untyped]

from rebdhuhn.models import ToNoEdge, ToYesEdge
from rebdhuhn.models.errors import PathsNotGreaterThanOneError
Expand Down
18 changes: 18 additions & 0 deletions src/rebdhuhn/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,21 @@
ToYesEdge,
)
from rebdhuhn.models.ebd_table import EbdCheckResult, EbdTable, EbdTableMetaData, EbdTableRow, EbdTableSubRow

__all__ = [
"DecisionNode",
"EbdGraph",
"EbdGraphEdge",
"EbdGraphMetaData",
"EbdGraphNode",
"EndNode",
"OutcomeNode",
"StartNode",
"ToNoEdge",
"ToYesEdge",
"EbdCheckResult",
"EbdTable",
"EbdTableMetaData",
"EbdTableRow",
"EbdTableSubRow",
]
4 changes: 2 additions & 2 deletions src/rebdhuhn/models/ebd_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List, Optional

import attrs
from networkx import DiGraph # type:ignore[import]
from networkx import DiGraph # type:ignore[import-untyped]

# pylint:disable=too-few-public-methods
from rebdhuhn.models.ebd_table import RESULT_CODE_REGEX, MultiStepInstruction
Expand Down Expand Up @@ -55,7 +55,7 @@ def get_key(self) -> str:
"""
raise NotImplementedError("The child class has to implement this method")

def __str__(self):
def __str__(self) -> str:
return self.get_key()


Expand Down
4 changes: 3 additions & 1 deletion src/rebdhuhn/models/ebd_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class EbdTableSubRow:


# pylint: disable=unused-argument
def _check_that_both_true_and_false_occur(instance, attribute, value: List[EbdTableSubRow]):
def _check_that_both_true_and_false_occur( # type:ignore[no-untyped-def]
instance: EbdTableSubRow, attribute, value: List[EbdTableSubRow]
) -> None:
"""
Check that the subrows cover both a True and a False outcome
"""
Expand Down
11 changes: 6 additions & 5 deletions src/rebdhuhn/models/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class NotExactlyTwoOutgoingEdgesError(NotImplementedError):
See issue https://github.com/Hochfrequenz/rebdhuhn/issues/99 for a discussion on this topic.
"""

def __init__(self, msg: str, decision_node_key, outgoing_edges: list[str]):
def __init__(self, msg: str, decision_node_key: str, outgoing_edges: list[str]) -> None:
"""
providing the keys allows to easily track down the exact cause of the error
"""
super().__init__(msg)
self.decision_node_key = decision_node_key
self.outgoing_edges = outgoing_edges

def __str__(self):
def __str__(self) -> str:
return f"The node {self.decision_node_key} has more than 2 outgoing edges: {', '.join(self.outgoing_edges)}"


Expand All @@ -33,7 +33,7 @@ class PathsNotGreaterThanOneError(ValueError):
Typically, this is a symptom for loops in the graph (which makes them not a Directed Graph / tree anymore).
"""

def __init__(self, node_key: str, indegree: int, number_of_paths: int):
def __init__(self, node_key: str, indegree: int, number_of_paths: int) -> None:
super().__init__(
f"The indegree of node '{node_key}' is {indegree} > 1, but the number of paths is {number_of_paths} <= 1."
)
Expand Down Expand Up @@ -63,8 +63,9 @@ class GraphTooComplexForPlantumlError(Exception):

def __init__(
self,
message="Plantuml conversion doesn't support multiple nodes for an ancestor node. The graph is too complex.",
):
# pylint:disable=line-too-long
message: str = "Plantuml conversion doesn't support multiple nodes for an ancestor node. The graph is too complex.",
) -> None:
self.message = message
super().__init__(self.message)

Expand Down
2 changes: 1 addition & 1 deletion src/rebdhuhn/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from collections import namedtuple

from networkx import DiGraph # type:ignore[import]
from networkx import DiGraph # type:ignore[import-untyped]

from rebdhuhn.graph_utils import COMMON_ANCESTOR_FIELD, _get_yes_no_edges, _mark_last_common_ancestors
from rebdhuhn.kroki import Kroki, PlantUmlToSvgConverter
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ deps =
-r requirements.txt
-r dev_requirements/requirements-type_check.txt
commands =
mypy --show-error-codes src/rebdhuhn
mypy --show-error-codes --strict src/rebdhuhn
mypy --show-error-codes unittests
# add single files (ending with .py) or packages here

Expand Down

0 comments on commit fc501bc

Please sign in to comment.