23
23
import codecs
24
24
import datetime
25
25
from collections .abc import Callable , Iterable , Iterator , Sequence
26
- from typing import Optional , Type , TypeVar , Union
26
+ from typing import Optional , Type , TypeVar
27
27
28
28
from pydifact .api import EDISyntaxError
29
29
from pydifact .control import Characters
30
30
from pydifact .parser import Parser
31
- from pydifact .segments import Elements , Segment
31
+ from pydifact .segments import Element , Elements , Segment
32
32
from pydifact .serializer import Serializer
33
33
34
34
T = TypeVar ("T" , bound = "AbstractSegmentsContainer" )
@@ -361,10 +361,10 @@ class Interchange(AbstractSegmentsContainer):
361
361
362
362
def __init__ (
363
363
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 ,
368
368
timestamp : Optional [datetime .datetime ] = None ,
369
369
* args ,
370
370
** kwargs ,
@@ -471,7 +471,7 @@ def from_file(
471
471
472
472
@classmethod
473
473
def from_segments (
474
- cls , segments : Union [ list , Iterable ], characters : Optional [Characters ] = None
474
+ cls , segments : Iterable [ Segment ], characters : Optional [Characters ] = None
475
475
) -> "Interchange" :
476
476
segments = iter (segments )
477
477
@@ -489,10 +489,13 @@ def from_segments(
489
489
# In syntax version 3 the year is formatted using two digits, while in version 4 four digits are used.
490
490
# Since some EDIFACT files in the wild don't adhere to this specification, we just use whatever format seems
491
491
# 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." )
496
499
else :
497
500
raise EDISyntaxError ("Timestamp of file-creation malformed." )
498
501
@@ -508,7 +511,7 @@ def from_segments(
508
511
extra_header_elements = unb .elements [5 :],
509
512
)
510
513
511
- if first_segment .tag == "UNA" :
514
+ if first_segment .tag == "UNA" and isinstance ( first_segment . elements [ 0 ], str ) :
512
515
interchange .has_una_segment = True
513
516
interchange .characters = Characters .from_str (first_segment .elements [0 ])
514
517
0 commit comments