Skip to content

Commit 3309def

Browse files
committed
stricter typing
1 parent 06260cf commit 3309def

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

pydifact/parser.py

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

2525
from pydifact.tokenizer import Tokenizer
2626
from pydifact.token import Token
27-
from pydifact.segments import Segment, SegmentFactory
27+
from pydifact.segments import Element, Elements, Segment, SegmentFactory
2828
from pydifact.control import Characters
2929

3030

@@ -134,10 +134,10 @@ def convert_tokens_to_segments(
134134
:rtype list of Segment
135135
"""
136136

137-
segments: list[list[Union[str, list[str]]]] = []
138-
current_segment = []
137+
segments: list[Elements] = []
138+
current_segment: Elements = []
139139
data_element: list[str] = []
140-
data_element_value: Union[list[str], str]
140+
data_element_value: Element
141141
in_segment = False
142142
empty_component_counter = 0
143143

pydifact/segmentcollection.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import codecs
2424
import datetime
2525
from collections.abc import Callable, Iterable, Iterator, Sequence
26-
from typing import Optional, Type, TypeVar, Union
26+
from typing import Optional, Type, TypeVar
2727

2828
from pydifact.api import EDISyntaxError
2929
from pydifact.control import Characters
3030
from pydifact.parser import Parser
31-
from pydifact.segments import Elements, Segment
31+
from pydifact.segments import Element, Elements, Segment
3232
from pydifact.serializer import Serializer
3333

3434
T = TypeVar("T", bound="AbstractSegmentsContainer")
@@ -361,10 +361,10 @@ class Interchange(AbstractSegmentsContainer):
361361

362362
def __init__(
363363
self,
364-
sender: str,
365-
recipient: str,
366-
control_reference: str,
367-
syntax_identifier: tuple[str, int],
364+
sender: Element,
365+
recipient: Element,
366+
control_reference: Element,
367+
syntax_identifier: Element,
368368
timestamp: Optional[datetime.datetime] = None,
369369
*args,
370370
**kwargs,
@@ -471,7 +471,7 @@ def from_file(
471471

472472
@classmethod
473473
def from_segments(
474-
cls, segments: Union[list, Iterable], characters: Optional[Characters] = None
474+
cls, segments: Iterable[Segment], characters: Optional[Characters] = None
475475
) -> "Interchange":
476476
segments = iter(segments)
477477

@@ -489,10 +489,13 @@ def from_segments(
489489
# In syntax version 3 the year is formatted using two digits, while in version 4 four digits are used.
490490
# Since some EDIFACT files in the wild don't adhere to this specification, we just use whatever format seems
491491
# more appropriate according to the length of the date string.
492-
if len(unb.elements[3][0]) == 6:
493-
datetime_fmt = "%y%m%d-%H%M"
494-
elif len(unb.elements[3][0]) == 8:
495-
datetime_fmt = "%Y%m%d-%H%M"
492+
if isinstance(unb.elements[3], list) and len(unb.elements[3]) > 0:
493+
if len(unb.elements[3][0]) == 6:
494+
datetime_fmt = "%y%m%d-%H%M"
495+
elif len(unb.elements[3][0]) == 8:
496+
datetime_fmt = "%Y%m%d-%H%M"
497+
else:
498+
raise EDISyntaxError("Timestamp of file-creation malformed.")
496499
else:
497500
raise EDISyntaxError("Timestamp of file-creation malformed.")
498501

@@ -508,7 +511,7 @@ def from_segments(
508511
extra_header_elements=unb.elements[5:],
509512
)
510513

511-
if first_segment.tag == "UNA":
514+
if first_segment.tag == "UNA" and isinstance(first_segment.elements[0], str):
512515
interchange.has_una_segment = True
513516
interchange.characters = Characters.from_str(first_segment.elements[0])
514517

pydifact/segments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from pydifact.api import EDISyntaxError, PluginMount
2626

27-
Element = Union[str, list[str], None]
27+
Element = Union[str, list[str]]
2828
Elements = list[Element]
2929

3030

pydifact/tokenizer.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ def __init__(self) -> None:
4040
# The current character from the message we are dealing with.
4141
self._char: Optional[str] = ""
4242

43-
# The stored characters for the next token.
44-
self._string = ""
45-
4643
# bool isEscaped If the current character has been escaped.
4744
self.isEscaped = False
4845

tests/test_segmentcollection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# You should have received a copy of the GNU Lesser General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
import datetime
17-
from typing import Iterable, list
17+
from typing import Iterable
1818

1919
import pytest
2020

0 commit comments

Comments
 (0)