Skip to content

Commit

Permalink
馃Ч Switch to mypy --strict
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein committed May 17, 2024
1 parent 9f380dd commit f11b615
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/kohlrahbi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@click.group()
@click.version_option(version=version)
def cli():
def cli() -> None:
"""Kohlrahbi CLI tool"""


Expand Down
6 changes: 3 additions & 3 deletions src/kohlrahbi/ahb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def process_ahb_table(
pruefi: str,
output_path: Path,
file_type: str,
):
) -> None:
"""
Process the ahb table.
"""
Expand Down Expand Up @@ -98,7 +98,7 @@ def process_pruefi(
path_to_ahb_docx_file: Path,
output_path: Path,
file_type: str,
):
) -> None:
"""
Process one pruefi.
If the input path ends with .docx, we assume that the file containing the pruefi is given.
Expand Down Expand Up @@ -183,7 +183,7 @@ def extract_pruefis_from_table(table: Table) -> list[str]:

def table_header_contains_text_pruefidentifikator(table: Table) -> bool:
"""Checks if the table header contains the text 'Pr眉fidentifikator'."""
return table.row_cells(0)[-1].paragraphs[-1].text.startswith("Pr眉fidentifikator")
return table.row_cells(0)[-1].paragraphs[-1].text.startswith("Pr眉fidentifikator") # type:ignore[no-any-return]


def get_pruefi_to_file_mapping(basic_input_path: Path, format_version: EdifactFormatVersion) -> dict[str, str]:
Expand Down
6 changes: 3 additions & 3 deletions src/kohlrahbi/ahb/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from kohlrahbi.enums.ahbexportfileformat import AhbExportFileFormat


def check_python_version():
def check_python_version() -> None:
"""
Check if the Python interpreter is greater or equal to 3.11
"""
Expand All @@ -24,7 +24,7 @@ def check_python_version():


# pylint: disable=unused-argument
def validate_path(ctx, param, value):
def validate_path(ctx, param, value) -> Path: # type:ignore[no-untyped-def]
"""
Ensure the path exists or offer to create it.
"""
Expand Down Expand Up @@ -97,7 +97,7 @@ def ahb(
format_version: EdifactFormatVersion | str,
assume_yes: bool, # pylint: disable=unused-argument
# it is used by the callback function of the output-path
):
) -> None:
"""
Scrape AHB documents for pruefidentifikatoren.
This is a command line interface for the pruefis module.
Expand Down
3 changes: 2 additions & 1 deletion src/kohlrahbi/ahbtable/ahbcondtions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def collect_conditions(
logger.info("The package conditions for %s were collected.", edifact_format)
return conditions_dict

def include_condition_dict(self, to_add=dict[EdifactFormat, dict[str, str]] | None) -> None:
def include_condition_dict(self, to_add: dict[EdifactFormat, dict[str, str]] | None) -> None:
""" " Include a dict of conditions to the conditions_dict"""
if to_add is None:
logger.info("Conditions dict to be added is empty.")
return
for edifact_format, edi_cond_dict in to_add.items():
for condition_key, condition_text in edi_cond_dict.items():
if edifact_format in self.conditions_dict:
Expand Down
5 changes: 3 additions & 2 deletions src/kohlrahbi/ahbtable/ahbpackagetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def provide_conditions(self, edifact_format: EdifactFormat) -> dict[EdifactForma
logger.info("The package conditions for %s were collected.", edifact_format)
return conditions_dict

def provide_packages(self, edifact_format: EdifactFormat):
def provide_packages(self, edifact_format: EdifactFormat) -> None:
"""collect conditions from package table and store them in conditions dict."""
package_dict: dict[EdifactFormat, dict[str, str]] = {edifact_format: {}}

Expand Down Expand Up @@ -80,10 +80,11 @@ def provide_packages(self, edifact_format: EdifactFormat):
logger.info("Packages for %s were collected.", edifact_format)
self.package_dict = package_dict

def include_package_dict(self, to_add=dict[EdifactFormat, dict[str, str]] | None) -> None:
def include_package_dict(self, to_add: dict[EdifactFormat, dict[str, str]] | None) -> None:
"""Include a dict of conditions to the conditions_dict"""
if to_add is None:
logger.info("Packages dict to be added is empty.")
return
for edifact_format, edi_cond_dict in to_add.items():
for package_key, package_conditions in edi_cond_dict.items():
if edifact_format in self.package_dict:
Expand Down
7 changes: 4 additions & 3 deletions src/kohlrahbi/ahbtable/ahbsubtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import pandas as pd
from docx.table import Table as DocxTable
from docx.table import _Cell
from docx.table import _Cell, _Row
from pydantic import BaseModel, ConfigDict

from kohlrahbi.ahbtable.ahbtablerow import AhbTableRow
from kohlrahbi.row_type_checker import RowType, get_row_type
from kohlrahbi.enums import RowType
from kohlrahbi.row_type_checker import get_row_type
from kohlrahbi.seed import Seed


Expand Down Expand Up @@ -122,7 +123,7 @@ def from_headless_table(cls, tmd: Seed, docx_table: DocxTable) -> "AhbSubTable":
return cls(table_meta_data=tmd, table=ahb_table_dataframe)

@staticmethod
def iter_visible_cells(row) -> Generator[_Cell, None, None]:
def iter_visible_cells(row: _Row) -> Generator[_Cell, None, None]:
"""
This function makes sure that you will iterate over the cells you see in the word document.
For more information go to https://github.com/python-openxml/python-docx/issues/970#issuecomment-877386927
Expand Down
2 changes: 1 addition & 1 deletion src/kohlrahbi/ahbtable/ahbtablerow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydantic import BaseModel, ConfigDict

from kohlrahbi.docxtablecells import BedingungCell, BodyCell, EdifactStrukturCell
from kohlrahbi.row_type_checker import RowType
from kohlrahbi.enums import RowType
from kohlrahbi.seed import Seed


Expand Down
2 changes: 1 addition & 1 deletion src/kohlrahbi/changehistory/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def changehistory(
format_version: EdifactFormatVersion | str,
assume_yes: bool, # pylint: disable=unused-argument
# it is used by the callback function of the output-path
):
) -> None:
"""
Scrape change histories from the input path and save them to the output path.
Expand Down
1 change: 0 additions & 1 deletion src/kohlrahbi/conditions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def scrape_conditions(
for edifact_format, files in all_format_files.items():
for file in files:
# pylint: disable=too-many-function-args
# type: ignore[call-arg, arg-type]
path: Path = basic_input_path / path_to_file / Path(file)
doc = docx.Document(str(path.absolute()))
logger.info("Start scraping conditions for %s in %s", edifact_format, file)
Expand Down
6 changes: 3 additions & 3 deletions src/kohlrahbi/conditions/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from kohlrahbi.conditions import scrape_conditions


def check_python_version():
def check_python_version() -> None:
"""
Check if the Python interpreter is greater or equal to 3.11
"""
Expand All @@ -40,7 +40,7 @@ def check_python_version():


# pylint: disable=unused-argument
def validate_path(ctx, param, value):
def validate_path(ctx, param, value) -> Path: # type:ignore[no-untyped-def]
"""
Ensure the path exists or offer to create it.
"""
Expand Down Expand Up @@ -91,7 +91,7 @@ def validate_path(ctx, param, value):
)
def conditions(
edi_energy_mirror_path: Path, output_path: Path, format_version: EdifactFormatVersion | str, assume_yes: bool
):
) -> None:
"""
Scrape AHB documents for conditions.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/kohlrahbi/docxfilefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def filter_docx_files_for_edifact_format(self, edifact_format: EdifactFormat) ->

self.paths_to_docx_files = [path for path in self.paths_to_docx_files if str(edifact_format) in path.name]

def remove_temporary_files(self):
def remove_temporary_files(self) -> None:
"""
This method removes all temporary files from paths_to_docx_files.
Temporary files lead to the exception `BadZipFile: File is not a zip file`.
Expand Down
2 changes: 2 additions & 0 deletions src/kohlrahbi/docxtablecells/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
from .bedinungscell import BedingungCell
from .bodycell import BodyCell
from .edifactstrukturcell import EdifactStrukturCell

__all__ = ["BedingungCell", "BodyCell", "EdifactStrukturCell"]
5 changes: 3 additions & 2 deletions src/kohlrahbi/docxtablecells/bodycell.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pandas as pd
from docx.table import _Cell
from docx.text.paragraph import Paragraph
from maus.reader.flat_ahb_reader import FlatAhbCsvReader
from pydantic import BaseModel, ConfigDict

Expand Down Expand Up @@ -103,9 +104,9 @@ def parse(self, ahb_row_dataframe: pd.DataFrame) -> pd.DataFrame:

return ahb_row_dataframe

def has_paragraph_tabstops(self, paragraph) -> bool:
def has_paragraph_tabstops(self, paragraph: Paragraph) -> bool:
"""
Checks if the given paragraph contains tabstops
"""
tab_stops = list(paragraph.paragraph_format.tab_stops)
return len(tab_stops) > 0
return any(tab_stops)
2 changes: 2 additions & 0 deletions src/kohlrahbi/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

from .row_type import RowType
from .row_type_color import RowTypeColor

__all__ = ["RowType", "RowTypeColor"]
53 changes: 33 additions & 20 deletions src/kohlrahbi/read_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def table_header_starts_with_text_edifact_struktur(table: Table) -> bool:
return table.cell(row_idx=0, col_idx=0).text.strip() == "EDIFACT Struktur"


def is_item_header_of_change_history_section(item: Paragraph | Table | None, style_name: str) -> bool:
def is_item_header_of_change_history_section(item: Paragraph | Table | None, style_name: str) -> TypeGuard[Paragraph]:
"""
Checks if the given item is a header of the change history section.
"""
Expand All @@ -57,14 +57,14 @@ def is_item_header_of_change_history_section(item: Paragraph | Table | None, sty
return isinstance(item, Paragraph) and "脛nderungshistorie" in item.text and "Heading" in style_name


def is_item_text_paragraph(item: Paragraph | Table | None, style_name: str) -> bool:
def is_item_text_paragraph(item: Paragraph | Table | None, style_name: str) -> TypeGuard[Paragraph]:
"""
Checks if the given item is a text paragraph.
"""
return isinstance(item, Paragraph) and "Heading" not in style_name


def is_item_table_with_pruefidentifikatoren(item: Paragraph | Table | None) -> bool:
def is_item_table_with_pruefidentifikatoren(item: Paragraph | Table | None) -> TypeGuard[Table]:
"""
Check if the item is a Table and contains Pruefidentifikatoren.
Expand All @@ -81,7 +81,7 @@ def is_item_headless_table(
item: Paragraph | Table | None,
# seed: Seed | None,
ahb_table: AhbTable | None,
) -> bool:
) -> TypeGuard[Table]:
"""
Checks if the given item is a headless table.
Expand All @@ -97,7 +97,7 @@ def is_item_headless_table(
return isinstance(item, Table) and ahb_table is not None


def get_ahb_table(document, pruefi: str) -> Optional[AhbTable]:
def get_ahb_table(document: Document, pruefi: str) -> Optional[AhbTable]:
"""
Reads a docx file and extracts all information for a given Pr眉fidentifikator.
If the Pr眉fidentifikator is not found or we reach the end of the AHB document
Expand All @@ -111,8 +111,8 @@ def get_ahb_table(document, pruefi: str) -> Optional[AhbTable]:
AhbTable or None: The extracted AHB table or None if not found
"""

ahb_table = None
seed = None
ahb_table: AhbTable | None = None
seed: Seed | None = None
searched_pruefi_is_found = False

for item in get_all_paragraphs_and_tables(document):
Expand All @@ -133,37 +133,47 @@ def get_ahb_table(document, pruefi: str) -> Optional[AhbTable]:

searched_pruefi_is_found, ahb_table = process_table(item, pruefi, searched_pruefi_is_found, ahb_table, seed)

if ahb_table:
if ahb_table is not None:
ahb_table.sanitize()
return ahb_table

log_pruefi_not_found(pruefi)
return None


def get_style_name(item) -> str:
def get_style_name(item: Paragraph | Table) -> str:
"""Extracts and normalizes the style name of a document item."""
return item.style.name if item.style else "None"
return item.style.name if item.style else "None" # type:ignore[no-any-return]


def reached_end_of_document(style_name, item) -> bool:
def reached_end_of_document(style_name: str, item: Paragraph | Table | None) -> bool:
"""Checks if the current item marks the end of the document."""
return is_item_header_of_change_history_section(item, style_name)


def update_seed(item, seed):
def update_seed(item: Paragraph | Table | None, seed: Seed | None) -> Seed | None:
"""Updates the seed if the current item is a table with Pr眉fidentifikatoren."""
if is_item_table_with_pruefidentifikatoren(item):
return Seed.from_table(docx_table=item)
return seed


def should_end_search(pruefi, seed, searched_pruefi_is_found):
def should_end_search(pruefi: str, seed: Seed | None, searched_pruefi_is_found: bool) -> bool:
"""Determines if the search for the AHB table should end."""
return seed and pruefi not in seed.pruefidentifikatoren and searched_pruefi_is_found
if seed is None:
return False
if pruefi not in seed.pruefidentifikatoren:
return False
return searched_pruefi_is_found


def process_table(item, pruefi, searched_pruefi_is_found, ahb_table, seed=None):
def process_table(
item: Paragraph | Table | None,
pruefi: str,
searched_pruefi_is_found: bool,
ahb_table: AhbTable,
seed: Seed | None = None,
) -> tuple[bool, AhbTable]:
"""Processes tables to find and build the AHB table."""
if is_item_table_with_pruefidentifikatoren(item):
seed = Seed.from_table(docx_table=item)
Expand All @@ -176,35 +186,36 @@ def process_table(item, pruefi, searched_pruefi_is_found, ahb_table, seed=None):

# elif is_item_headless_table(item, seed, ahb_table):
elif is_item_headless_table(item, ahb_table):
assert seed is not None
ahb_sub_table = AhbSubTable.from_headless_table(docx_table=item, tmd=seed)
ahb_table.append_ahb_sub_table(ahb_sub_table=ahb_sub_table)

return searched_pruefi_is_found, ahb_table


# Logging functions
def log_end_of_document(pruefi):
def log_end_of_document(pruefi: str) -> None:
"""
Logs that the end of the document was reached before finding the table for a given Pr眉fi.
"""
logger.info("Reached the end of the document before finding the table for Pr眉fi '%s'.", pruefi)


def log_end_of_ahb_table(pruefi):
def log_end_of_ahb_table(pruefi: str) -> None:
"""
Logs that the end of the AHB table was reached for a given Pr眉fi.
"""
logger.info("Reached the end of the AHB table for Pr眉fi '%s'.", pruefi)


def log_found_pruefi(pruefi):
def log_found_pruefi(pruefi: str) -> None:
"""
Logs that the AHB table for a given Pr眉fi was found.
"""
logger.info("Found the AHB table for Pr眉fi '%s'.", pruefi)


def log_pruefi_not_found(pruefi):
def log_pruefi_not_found(pruefi: str) -> None:
"""
Logs that the Pr眉fi was not found in the provided document.
"""
Expand Down Expand Up @@ -275,7 +286,9 @@ def is_last_row_unt_0062(item: Table | Paragraph) -> bool:
return isinstance(item, Table) and "UNT\t0062" == item.cell(row_idx=-1, col_idx=0).text.strip()


def is_relevant_pruefi_table(item: Paragraph | Table, seed: Seed, edifact_format) -> TypeGuard[Table]:
def is_relevant_pruefi_table(
item: Paragraph | Table, seed: Seed | None, edifact_format: EdifactFormat
) -> TypeGuard[Table]:
"""compares new pruefis to last pruefi and thus checks whether new table"""
return (
isinstance(item, Table)
Expand Down
5 changes: 3 additions & 2 deletions src/kohlrahbi/row_type_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from kohlrahbi.enums import RowType


def set_table_header_bg_color(cell, hex_color: str):
def set_table_header_bg_color(cell: _Cell, hex_color: str) -> _Cell:
"""
set background shading for Header Rows
"""
Expand Down Expand Up @@ -48,7 +48,8 @@ def is_row_segmentname(edifact_struktur_cell: _Cell) -> bool:
bool:
"""
try:
return edifact_struktur_cell.paragraphs[0].runs[0].font.color.rgb == RGBColor(128, 128, 128) # grey
colour_is_grey: bool = edifact_struktur_cell.paragraphs[0].runs[0].font.color.rgb == RGBColor(128, 128, 128)
return colour_is_grey
except IndexError:
return False

Expand Down
Loading

0 comments on commit f11b615

Please sign in to comment.