Skip to content

Commit 537d248

Browse files
committed
Add type aliases that reflect their semantics
Using Any or str in type annotations might increase the need for extra comments to explain the real valid values. However, such needs can be drastically reduced with the help of semanticly named type aliases.
1 parent a050e9d commit 537d248

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

src/wireviz/DataClasses.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
from typing import Optional, List, Any, Union
4+
from typing import Optional, List, Union
55
from dataclasses import dataclass, field, InitVar
66
from pathlib import Path
77
from wireviz.wv_helper import int2tuple, aspect_ratio
@@ -10,20 +10,29 @@
1010
# Literal type aliases below are commented to avoid requiring python 3.8
1111
ConnectorMultiplier = str # = Literal['pincount', 'populated']
1212
CableMultiplier = str # = Literal['wirecount', 'terminations', 'length', 'total_length']
13+
ImageScale = str # = Literal['false', 'true', 'width', 'height', 'both']
14+
15+
Designator = str # Case insensitive unique name of connector or cable
16+
Pin = Union[int, str] # Pin identifier
17+
Wire = Union[int, str] # Wire number or 's' for shield
18+
MLstr = str # Multi-line string where any newline is properly handled
19+
Color = str # Two-letter color name = Literal[wv_colors._color_hex.keys()]
20+
Colors = str # One or more two-letter color names (Color) concatenated into one string
21+
ColorScheme = str # Color scheme name = Literal[wv_colors.COLOR_CODES.keys()]
1322

1423

1524
@dataclass
1625
class Image:
1726
gv_dir: InitVar[Path] # Directory of .gv file injected as context during parsing
1827
# Attributes of the image object <img>:
1928
src: str
20-
scale: Optional[str] = None # false | true | width | height | both
29+
scale: Optional[ImageScale] = None
2130
# Attributes of the image cell <td> containing the image:
2231
width: Optional[int] = None
2332
height: Optional[int] = None
2433
fixedsize: Optional[bool] = None
2534
# Contents of the text cell <td> just below the image cell:
26-
caption: Optional[str] = None
35+
caption: Optional[MLstr] = None
2736
# See also HTML doc at https://graphviz.org/doc/info/shapes.html#html
2837

2938
def __post_init__(self, gv_dir):
@@ -49,10 +58,10 @@ def __post_init__(self, gv_dir):
4958

5059
@dataclass
5160
class AdditionalComponent:
52-
type: str
53-
subtype: Optional[str] = None
54-
manufacturer: Optional[str] = None
55-
mpn: Optional[str] = None
61+
type: MLstr
62+
subtype: Optional[MLstr] = None
63+
manufacturer: Optional[MLstr] = None
64+
mpn: Optional[MLstr] = None
5665
pn: Optional[str] = None
5766
qty: float = 1
5867
unit: Optional[str] = None
@@ -65,25 +74,25 @@ def description(self) -> str:
6574

6675
@dataclass
6776
class Connector:
68-
name: str
69-
manufacturer: Optional[str] = None
70-
mpn: Optional[str] = None
77+
name: Designator
78+
manufacturer: Optional[MLstr] = None
79+
mpn: Optional[MLstr] = None
7180
pn: Optional[str] = None
7281
style: Optional[str] = None
7382
category: Optional[str] = None
74-
type: Optional[str] = None
75-
subtype: Optional[str] = None
83+
type: Optional[MLstr] = None
84+
subtype: Optional[MLstr] = None
7685
pincount: Optional[int] = None
7786
image: Optional[Image] = None
78-
notes: Optional[str] = None
79-
pinlabels: List[Any] = field(default_factory=list)
80-
pins: List[Any] = field(default_factory=list)
81-
color: Optional[str] = None
87+
notes: Optional[MLstr] = None
88+
pinlabels: List[Pin] = field(default_factory=list)
89+
pins: List[Pin] = field(default_factory=list)
90+
color: Optional[Color] = None
8291
show_name: Optional[bool] = None
8392
show_pincount: Optional[bool] = None
8493
hide_disconnected_pins: bool = False
8594
autogenerate: bool = False
86-
loops: List[Any] = field(default_factory=list)
95+
loops: List[List[Pin]] = field(default_factory=list)
8796
ignore_in_bom: bool = False
8897
additional_components: List[AdditionalComponent] = field(default_factory=list)
8998

@@ -155,23 +164,23 @@ def get_qty_multiplier(self, qty_multiplier: Optional[ConnectorMultiplier]) -> i
155164

156165
@dataclass
157166
class Cable:
158-
name: str
159-
manufacturer: Optional[Union[str, List[str]]] = None
160-
mpn: Optional[Union[str, List[str]]] = None
161-
pn: Optional[Union[str, List[str]]] = None
167+
name: Designator
168+
manufacturer: Union[MLstr, List[MLstr], None] = None
169+
mpn: Union[MLstr, List[MLstr], None] = None
170+
pn: Union[str, List[str], None] = None
162171
category: Optional[str] = None
163-
type: Optional[str] = None
172+
type: Optional[MLstr] = None
164173
gauge: Optional[float] = None
165174
gauge_unit: Optional[str] = None
166175
show_equiv: bool = False
167176
length: float = 0
168-
color: Optional[str] = None
177+
color: Optional[Color] = None
169178
wirecount: Optional[int] = None
170-
shield: Union[bool, str] = False # False | True | color
179+
shield: Union[bool, Color] = False
171180
image: Optional[Image] = None
172-
notes: Optional[str] = None
173-
colors: List[Any] = field(default_factory=list)
174-
color_code: Optional[str] = None
181+
notes: Optional[MLstr] = None
182+
colors: List[Colors] = field(default_factory=list)
183+
color_code: Optional[ColorScheme] = None
175184
show_name: bool = True
176185
show_wirecount: bool = True
177186
ignore_in_bom: bool = False
@@ -264,8 +273,8 @@ def get_qty_multiplier(self, qty_multiplier: Optional[CableMultiplier]) -> float
264273

265274
@dataclass
266275
class Connection:
267-
from_name: Any
268-
from_port: Any
269-
via_port: Any
270-
to_name: Any
271-
to_port: Any
276+
from_name: Optional[Designator]
277+
from_port: Optional[Pin]
278+
via_port: Wire
279+
to_name: Optional[Designator]
280+
to_port: Optional[Pin]

0 commit comments

Comments
 (0)