Skip to content

Commit b29fc2e

Browse files
authored
Use literal type instead of enum type for _typ (#905)
* Use literal type instead of enum type for `_typ` * 🔥 * 🩹mypy * 🩹pylint * 🩹coverage * 🩹tests * 🩹docs * 🩹pylint
1 parent c4c6d12 commit b29fc2e

39 files changed

Lines changed: 111 additions & 129 deletions

docs/compatibility/change_schemas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ChangeType(StrEnum):
1717
FIELD_REMOVED = "field_removed"
1818
FIELD_DEFAULT_CHANGED = "field_default_changed"
1919
FIELD_DESCRIPTION_CHANGED = "field_description_changed"
20+
FIELD_TITLE_CHANGED = "field_title_changed"
2021
# field type change types
2122
FIELD_CARDINALITY_CHANGED = "field_cardinality_changed"
2223
FIELD_REFERENCE_CHANGED = "field_reference_changed"

docs/compatibility/diff.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import itertools
6+
import logging
67
import re
78
from pathlib import Path
89
from typing import Iterable, Sequence
@@ -11,6 +12,8 @@
1112

1213
from . import change_schemas, loader
1314

15+
logger = logging.getLogger(__name__)
16+
1417
REGEX_IGNORE_VERSION = re.compile(r"v\d+\.\d+\.\d+(-rc\d+)?")
1518

1619

@@ -21,11 +24,23 @@ def _diff_type_base(
2124
This function compares two type base schemas and yields the changes.
2225
"""
2326
if schema_old.title != schema_new.title:
24-
raise RuntimeError(
27+
logger.warning(
2528
(
2629
"Title should not change. Renaming is not detectable and the titles are autogenerated.\n"
27-
f"{schema_old.title} -> {schema_new.title}"
28-
)
30+
"'%s' -> '%s'\n"
31+
"%s -> %s"
32+
),
33+
schema_old.title,
34+
schema_new.title,
35+
old_trace,
36+
new_trace,
37+
)
38+
yield change_schemas.Change(
39+
type=change_schemas.ChangeType.FIELD_TITLE_CHANGED,
40+
old=schema_old.title,
41+
new=schema_new.title,
42+
old_trace=old_trace,
43+
new_trace=new_trace,
2944
)
3045
if REGEX_IGNORE_VERSION.sub(schema_old.description, "{__gh_version__}") != REGEX_IGNORE_VERSION.sub(
3146
schema_new.description, "{__gh_version__}"
@@ -288,7 +303,9 @@ def _diff_root_schemas(
288303
yield from _diff_schema_type(schema_old, schema_new, old_trace, new_trace)
289304

290305

291-
def diff_schemas(schemas_old: Path, schemas_new: Path) -> Iterable[change_schemas.Change]:
306+
def diff_schemas(
307+
schemas_old: Path, schemas_new: Path, old_trace: str, new_trace: str
308+
) -> Iterable[change_schemas.Change]:
292309
"""
293310
This function compares two BO4E versions and yields the changes.
294311
Note: The paths to the old and the new schemas should correspond to the same root node of the tree structure.
@@ -302,23 +319,23 @@ def diff_schemas(schemas_old: Path, schemas_new: Path) -> Iterable[change_schema
302319
type=change_schemas.ChangeType.CLASS_REMOVED,
303320
old=loader.load_schema_file(schemas_old / schema_file),
304321
new=None,
305-
old_trace=f"{'/'.join(schema_file.with_suffix('').parts)}#",
306-
new_trace="#",
322+
old_trace=f"{old_trace}/{'/'.join(schema_file.with_suffix('').parts)}#",
323+
new_trace=f"{new_trace}/#",
307324
)
308325
for schema_file in new_schema_files - old_schema_files:
309326
yield change_schemas.Change(
310327
type=change_schemas.ChangeType.CLASS_ADDED,
311328
old=None,
312329
new=loader.load_schema_file(schemas_new / schema_file),
313-
old_trace="#",
314-
new_trace=f"{'/'.join(schema_file.with_suffix('').parts)}#",
330+
old_trace=f"{old_trace}/#",
331+
new_trace=f"{new_trace}/{'/'.join(schema_file.with_suffix('').parts)}#",
315332
)
316333
for schema_file in old_schema_files & new_schema_files:
317334
yield from _diff_root_schemas(
318335
loader.load_schema_file(schemas_old / schema_file),
319336
loader.load_schema_file(schemas_new / schema_file),
320-
f"{'/'.join(schema_file.with_suffix('').parts)}#",
321-
f"{'/'.join(schema_file.with_suffix('').parts)}#",
337+
f"{old_trace}/{'/'.join(schema_file.with_suffix('').parts)}#",
338+
f"{new_trace}/{'/'.join(schema_file.with_suffix('').parts)}#",
322339
)
323340

324341

@@ -333,7 +350,7 @@ def compare_bo4e_versions(
333350
dir_old_schemas = loader.pull_or_reuse_bo4e_version(version_old, gh_token)
334351
dir_new_schemas = loader.pull_or_reuse_bo4e_version(version_new, gh_token, from_local=from_local)
335352
print(f"Comparing {version_old} with {version_new}")
336-
yield from diff_schemas(dir_old_schemas, dir_new_schemas)
353+
yield from diff_schemas(dir_old_schemas, dir_new_schemas, version_old, version_new)
337354

338355

339356
def compare_bo4e_versions_iteratively(

src/bo4e/bo/angebot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# pylint: disable=too-few-public-methods, too-many-instance-attributes
66
# pylint: disable=no-name-in-module
7-
from typing import TYPE_CHECKING, Annotated, Optional
7+
from typing import TYPE_CHECKING, Annotated, Literal, Optional
88

99
import pydantic
1010
from pydantic import Field
@@ -38,7 +38,7 @@ class Angebot(Geschaeftsobjekt):
3838
3939
"""
4040

41-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.ANGEBOT
41+
typ: Annotated[Literal[Typ.ANGEBOT], Field(alias="_typ")] = Typ.ANGEBOT
4242
#: Eindeutige Nummer des Angebotes
4343
angebotsnummer: Optional[str] = None
4444
#: Erstellungsdatum des Angebots

src/bo4e/bo/ausschreibung.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# pylint: disable=too-few-public-methods, too-many-instance-attributes
66
# pylint: disable=no-name-in-module
7-
from typing import TYPE_CHECKING, Annotated, Optional
7+
from typing import TYPE_CHECKING, Annotated, Literal, Optional
88

99
import pydantic
1010
from pydantic import Field
@@ -36,7 +36,7 @@ class Ausschreibung(Geschaeftsobjekt):
3636
3737
"""
3838

39-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.AUSSCHREIBUNG
39+
typ: Annotated[Literal[Typ.AUSSCHREIBUNG], Field(alias="_typ")] = Typ.AUSSCHREIBUNG
4040
#: Vom Herausgeber der Ausschreibung vergebene eindeutige Nummer
4141
ausschreibungsnummer: Optional[str] = None
4242
#: Aufzählung für die Typisierung von Ausschreibungen

src/bo4e/bo/buendelvertrag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# pylint: disable=too-few-public-methods
66
# pylint: disable=no-name-in-module
7-
from typing import TYPE_CHECKING, Annotated, Optional
7+
from typing import TYPE_CHECKING, Annotated, Literal, Optional
88

99
import pydantic
1010
from pydantic import Field
@@ -38,7 +38,7 @@ class Buendelvertrag(Geschaeftsobjekt):
3838
3939
"""
4040

41-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.BUENDELVERTRAG
41+
typ: Annotated[Literal[Typ.BUENDELVERTRAG], Field(alias="_typ")] = Typ.BUENDELVERTRAG
4242

4343
# pylint: disable=duplicate-code
4444
#: Eine im Verwendungskontext eindeutige Nummer für den Vertrag

src/bo4e/bo/energiemenge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
and corresponding marshmallow schema for de-/serialization
44
"""
55

6-
from typing import TYPE_CHECKING, Annotated, Optional
6+
from typing import TYPE_CHECKING, Annotated, Literal, Optional
77

88
from pydantic import Field
99

@@ -34,7 +34,7 @@ class Energiemenge(Geschaeftsobjekt):
3434
3535
"""
3636

37-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.ENERGIEMENGE
37+
typ: Annotated[Literal[Typ.ENERGIEMENGE], Field(alias="_typ")] = Typ.ENERGIEMENGE
3838
#: Eindeutige Nummer der Marktlokation bzw. der Messlokation, zu der die Energiemenge gehört
3939
lokations_id: Optional[str] = None
4040
# todo: add validator such that only mess- or marktlokations IDs are accepted + cross check with lokationstyp

src/bo4e/bo/fremdkosten.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Contains Fremdkosten class and corresponding marshmallow schema for de-/serialization
33
"""
44

5-
from typing import TYPE_CHECKING, Annotated, Optional
5+
from typing import TYPE_CHECKING, Annotated, Literal, Optional
66

77
from pydantic import Field
88

@@ -35,7 +35,7 @@ class Fremdkosten(Geschaeftsobjekt):
3535
3636
"""
3737

38-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.FREMDKOSTEN
38+
typ: Annotated[Literal[Typ.FREMDKOSTEN], Field(alias="_typ")] = Typ.FREMDKOSTEN
3939
#: Für diesen Zeitraum wurden die Kosten ermittelt
4040
gueltigkeit: Optional["Zeitraum"] = None
4141
#: Die Gesamtsumme über alle Kostenblöcke und -positionen

src/bo4e/bo/geraet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
and corresponding marshmallow schema for de-/serialization
44
"""
55

6-
from typing import TYPE_CHECKING, Annotated, Optional
6+
from typing import TYPE_CHECKING, Annotated, Literal, Optional
77

88
from pydantic import Field
99

@@ -33,7 +33,7 @@ class Geraet(Geschaeftsobjekt):
3333
3434
"""
3535

36-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.GERAET
36+
typ: Annotated[Literal[Typ.GERAET], Field(alias="_typ")] = Typ.GERAET
3737

3838
#: Die auf dem Gerät aufgedruckte Nummer, die vom MSB vergeben wird.
3939
geraetenummer: Optional[str] = None

src/bo4e/bo/geschaeftsobjekt.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
from bo4e.version import __version__
1111
from bo4e.zusatzattribut import ZusatzAttribut
1212

13-
from ..enum.typ import Typ
1413
from ..utils import postprocess_docstring
1514

1615
# pylint: disable=too-few-public-methods
1716

1817

1918
@postprocess_docstring
20-
class Geschaeftsobjekt(BaseModel):
19+
class Geschaeftsobjekt(BaseModel): # pragma: no cover
2120
"""
2221
Das BO Geschäftsobjekt ist der Master für alle Geschäftsobjekte.
2322
Alle Attribute, die hier in diesem BO enthalten sind, werden an alle BOs vererbt.
@@ -35,9 +34,6 @@ class Geschaeftsobjekt(BaseModel):
3534
version: Annotated[Optional[str], Field(alias="_version")] = (
3635
__version__ #: Version der BO-Struktur aka "fachliche Versionierung"
3736
)
38-
# src/_bo4e_python_version.py
39-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.GESCHAEFTSOBJEKT #: Der Typ des Geschäftsobjektes
40-
# bo_typ is used as discriminator f.e. for databases or deserialization
4137

4238
zusatz_attribute: Optional[list["ZusatzAttribut"]] = None
4339
# zusatz_attribute is a list of ZusatzAttribut objects which are used to store additional information

src/bo4e/bo/geschaeftspartner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
# pylint: disable=too-many-instance-attributes, too-few-public-methods, disable=duplicate-code
7-
from typing import TYPE_CHECKING, Annotated, Optional
7+
from typing import TYPE_CHECKING, Annotated, Literal, Optional
88

99
from pydantic import Field
1010

@@ -39,7 +39,7 @@ class Geschaeftspartner(Geschaeftsobjekt):
3939
4040
"""
4141

42-
typ: Annotated[Optional["Typ"], Field(alias="_typ")] = Typ.GESCHAEFTSPARTNER
42+
typ: Annotated[Literal[Typ.GESCHAEFTSPARTNER], Field(alias="_typ")] = Typ.GESCHAEFTSPARTNER
4343
#: Mögliche Anrede der Person
4444
anrede: Optional["Anrede"] = None
4545
individuelle_anrede: Optional[str] = None

0 commit comments

Comments
 (0)