Skip to content

Commit 8d1fcad

Browse files
committed
3.10 adjustment: Optional and Union
1 parent 8d386ad commit 8d1fcad

File tree

9 files changed

+1176
-40
lines changed

9 files changed

+1176
-40
lines changed

pydifact/parser.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
from collections.abc import Iterator
23-
from typing import Optional, Union
2423

2524
from pydifact.tokenizer import Tokenizer
2625
from pydifact.token import Token
@@ -33,14 +32,14 @@ class Parser:
3332

3433
def __init__(
3534
self,
36-
factory: Optional[SegmentFactory] = None,
37-
characters: Optional[Characters] = None,
35+
factory: SegmentFactory | None = None,
36+
characters: Characters | None = None,
3837
) -> None:
3938
self.factory = factory or SegmentFactory()
4039
self.characters = characters or Characters()
4140

4241
def parse(
43-
self, message: str, characters: Optional[Characters] = None
42+
self, message: str, characters: Characters | None = None
4443
) -> Iterator[Segment]:
4544
"""Parse the message into a list of segments.
4645
@@ -88,8 +87,8 @@ def parse(
8887

8988
@staticmethod
9089
def get_control_characters(
91-
message: str, characters: Optional[Characters] = None
92-
) -> Optional[Characters]:
90+
message: str, characters: Characters | None = None
91+
) -> Characters | None:
9392
"""Read the UNA segment from the passed string and extract/store the control characters from it.
9493
9594
:param message: a valid EDI message string, or UNA segment string,

pydifact/segmentcollection.py

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

2828
from pydifact.api import EDISyntaxError
2929
from pydifact.control import Characters
@@ -61,13 +61,13 @@ class AbstractSegmentsContainer:
6161
The control characters (a :class:`~pydifact.control.Characters` object).
6262
"""
6363

64-
HEADER_TAG: Optional[str] = None
65-
FOOTER_TAG: Optional[str] = None
64+
HEADER_TAG: str | None = None
65+
FOOTER_TAG: str | None = None
6666

6767
def __init__(
6868
self,
69-
extra_header_elements: Optional[Elements] = None,
70-
characters: Optional[Characters] = None,
69+
extra_header_elements: Elements | None = None,
70+
characters: Characters | None = None,
7171
) -> None:
7272
self.segments: list[Segment] = []
7373

@@ -85,8 +85,8 @@ def __init__(
8585
def from_str(
8686
cls: Type[T],
8787
string: str,
88-
parser: Optional[Parser] = None,
89-
characters: Optional[Characters] = None,
88+
parser: Parser | None = None,
89+
characters: Characters | None = None,
9090
) -> T:
9191
"""Create an instance from a string.
9292
@@ -105,7 +105,7 @@ def from_str(
105105
def from_segments(
106106
cls: Type[T],
107107
segments: Iterable[Segment],
108-
characters: Optional[Characters] = None,
108+
characters: Characters | None = None,
109109
) -> T:
110110
"""Create an instance from a list of segments.
111111
@@ -124,7 +124,7 @@ def from_segments(
124124
def get_segments(
125125
self,
126126
name: str,
127-
predicate: Optional[Callable[[Segment], bool]] = None,
127+
predicate: Callable[[Segment], bool] | None = None,
128128
) -> Iterator[Segment]:
129129
"""Get all segments that match the requested name.
130130
@@ -141,8 +141,8 @@ def get_segments(
141141
def get_segment(
142142
self,
143143
name: str,
144-
predicate: Optional[Callable[[Segment], bool]] = None,
145-
) -> Optional[Segment]:
144+
predicate: Callable[[Segment], bool] | None = None,
145+
) -> Segment | None:
146146
"""Get the first segment that matches the requested name.
147147
148148
:param name: The name of the segment to return.
@@ -214,7 +214,7 @@ def add_segment(self, segment: Segment) -> None:
214214
if segment.tag not in (self.HEADER_TAG, self.FOOTER_TAG):
215215
self.segments.append(segment)
216216

217-
def get_header_segment(self) -> Optional[Segment]:
217+
def get_header_segment(self) -> Segment | None:
218218
"""Craft and return a header segment.
219219
220220
:meth:`get_header_segment` creates and returns an appropriate
@@ -228,7 +228,7 @@ def get_header_segment(self) -> Optional[Segment]:
228228
"""
229229
return None
230230

231-
def get_footer_segment(self) -> Optional[Segment]:
231+
def get_footer_segment(self) -> Segment | None:
232232
"""Craft and return a footer segment.
233233
234234
This is similar to :meth:`get_header_segment`, but for the footer segment.
@@ -365,7 +365,7 @@ def __init__(
365365
recipient: Element,
366366
control_reference: Element,
367367
syntax_identifier: tuple[str, int],
368-
timestamp: Optional[datetime.datetime] = None,
368+
timestamp: datetime.datetime | None = None,
369369
*args,
370370
**kwargs,
371371
):
@@ -453,7 +453,7 @@ def add_message(self, message: Message) -> "Interchange":
453453

454454
@classmethod
455455
def from_file(
456-
cls, file: str, encoding: str = "iso8859-1", parser: Optional[Parser] = None
456+
cls, file: str, encoding: str = "iso8859-1", parser: Parser | None = None
457457
) -> "Interchange":
458458
"""Create a Interchange instance from a file.
459459
@@ -471,7 +471,7 @@ def from_file(
471471

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

pydifact/segments.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
from abc import abstractmethod
23-
from typing import Union
2423

2524
from pydifact.api import EDISyntaxError, PluginMount
2625

27-
Element = Union[str, list[str]]
26+
Element = str | list[str]
2827
Elements = list[Element]
2928

3029

pydifact/serializer.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
22-
from typing import Optional
23-
2422
from pydifact.control.characters import Characters
2523
import re
2624

@@ -30,7 +28,7 @@
3028
class Serializer:
3129
"""Serialize a bunch of segments into an EDI message string."""
3230

33-
def __init__(self, characters: Optional[Characters] = None) -> None:
31+
def __init__(self, characters: Characters | None = None) -> None:
3432
super().__init__()
3533
self.characters = characters or Characters()
3634
self.replace_map = self.characters.escaped_syntax_dic
@@ -95,14 +93,14 @@ def serialize(
9593
collection = "".join(collection_parts)
9694
return collection
9795

98-
def escape(self, string: Optional[str]) -> str:
96+
def escape(self, string: str | None) -> str:
9997
"""Escapes control characters.
10098
10199
:param string: The string to be escaped
102100
"""
103101

104102
if string is None:
105103
return ""
106-
assert type(string) == str, "%s is not a str, it is %s" % (string, type(string))
104+
assert isinstance(string, str), "%s is not a str, it is %s" % (string, type(string))
107105

108106
return self.regexp.sub(lambda match: self.replace_map[match.group(0)], string)

pydifact/syntax/common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""
1212

1313
import re
14-
from typing import Union
1514

1615
from pydifact import Segment, Characters
1716

@@ -59,7 +58,7 @@ class UNASegment(Segment):
5958

6059
tag = "UNA"
6160

62-
def __init__(self, characters: Union[Characters, str, None] = None):
61+
def __init__(self, characters: Characters | str | None = None):
6362
if not characters:
6463
characters = Characters()
6564
assert_an(str(characters), 6)

pydifact/tokenizer.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
from collections.abc import Iterator
23-
from typing import Optional
2423

2524
from pydifact.token import Token
2625
from pydifact.control.characters import Characters
@@ -38,20 +37,20 @@ def __init__(self) -> None:
3837
self._current_chars: list[str] = []
3938

4039
# The current character from the message we are dealing with.
41-
self._char: Optional[str] = ""
40+
self._char: str | None = ""
4241

4342
# bool isEscaped If the current character has been escaped.
4443
self.isEscaped = False
4544

4645
# The control characters for the message
47-
self.characters: Optional[Characters] = None
46+
self.characters: Characters | None = None
4847

4948
self.token_selector: dict[str, Token.Type] = {}
5049

5150
self._message_index: int = 0
5251

5352
def get_tokens(
54-
self, message: str, characters: Optional[Characters] = None
53+
self, message: str, characters: Characters | None = None
5554
) -> Iterator[Token]:
5655
"""Convert the passed message into tokens.
5756
:param characters: the Control Characters to use for tokenizing. If omitted, use a default set.
@@ -93,7 +92,7 @@ def read_next_char(self) -> None:
9392
self.isEscaped = True
9493
self._char = self.get_next_char()
9594

96-
def get_next_char(self) -> Optional[str]:
95+
def get_next_char(self) -> str | None:
9796
"""Get the next character from the message."""
9897
try:
9998
return next(self._message)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ classifiers = [
1717
"Programming Language :: Python :: 3",
1818
"Topic :: Software Development :: Libraries"
1919
]
20-
requires-python = ">=3.9"
20+
requires-python = ">=3.10"
2121
dependencies = []
2222

2323
[project.optional-dependencies]

tests/test_tokenizer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#
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/>.
16-
from typing import Optional
1716
import pytest
1817

1918
from pydifact.token import Token
@@ -29,8 +28,8 @@ def tokenizer() -> Tokenizer:
2928

3029
def _assert_tokens(
3130
collection: str,
32-
expected: Optional[list] = None,
33-
error_message: Optional[str] = None,
31+
expected: list | None = None,
32+
error_message: str | None = None,
3433
) -> None:
3534
"""Helper function to accelerate tokenizer testing."""
3635

0 commit comments

Comments
 (0)