Skip to content

Commit

Permalink
fields: allow to use any sequence to specify fields
Browse files Browse the repository at this point in the history
The reason we expect `Sequence` instead of `Iterable` is because we
use the `len()` method which is not available for `Iterable`.

thanks to [at]kayoub5 for the catch!

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Gerrit Ecke <[email protected]>
  • Loading branch information
andlaus committed May 7, 2024
1 parent f81b40b commit 7c5a56c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions odxtools/dynamicendmarkerfield.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: MIT
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List
from typing import TYPE_CHECKING, Any, Dict, List, Sequence
from xml.etree import ElementTree

from typing_extensions import override
Expand Down Expand Up @@ -65,7 +65,7 @@ def encode_into_pdu(self, physical_value: ParameterValue, encode_state: EncodeSt

odxassert(encode_state.cursor_bit_position == 0,
"No bit position can be specified for dynamic endmarker fields!")
if not isinstance(physical_value, (tuple, list)):
if not isinstance(physical_value, Sequence):
odxraise(
f"Expected a sequence of values for dynamic endmarker field {self.short_name}, "
f"got {type(physical_value).__name__}", EncodeError)
Expand Down
4 changes: 2 additions & 2 deletions odxtools/dynamiclengthfield.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: MIT
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List
from typing import TYPE_CHECKING, Any, Dict, List, Sequence
from xml.etree import ElementTree

from typing_extensions import override
Expand Down Expand Up @@ -55,7 +55,7 @@ def encode_into_pdu(self, physical_value: ParameterValue, encode_state: EncodeSt
odxassert(encode_state.cursor_bit_position == 0,
"No bit position can be specified for dynamic length fields!")

if not isinstance(physical_value, (tuple, list)):
if not isinstance(physical_value, Sequence):
odxraise(
f"Expected a list of values for dynamic length field {self.short_name}, "
f"got {type(physical_value)}", EncodeError)
Expand Down
4 changes: 2 additions & 2 deletions odxtools/endofpdufield.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: MIT
from dataclasses import dataclass
from typing import List, Optional
from typing import List, Optional, Sequence
from xml.etree import ElementTree

from typing_extensions import override
Expand Down Expand Up @@ -49,7 +49,7 @@ def encode_into_pdu(self, physical_value: Optional[ParameterValue],
odxassert(encode_state.is_end_of_pdu,
"End-of-pdu fields can only be located at the end of PDUs!")

if not isinstance(physical_value, (tuple, list)):
if not isinstance(physical_value, Sequence):
odxraise(
f"Invalid type {type(physical_value).__name__} of physical "
f"value for end-of-pdu field, expected a list", EncodeError)
Expand Down
5 changes: 3 additions & 2 deletions odxtools/odxtypes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: MIT
from enum import Enum
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Type, Union, overload
from typing import (TYPE_CHECKING, Any, Callable, Dict, Iterable, Optional, Tuple, Type, Union,
overload)
from xml.etree import ElementTree

from .exceptions import odxassert, odxraise, odxrequire
Expand Down Expand Up @@ -28,7 +29,7 @@ def bytefield_to_bytearray(bytefield: str) -> bytearray:
# multiple items, so this can be a list of objects.
TableStructParameterValue = Tuple[str, "ParameterValue"]
ParameterValue = Union[AtomicOdxType, "ParameterValueDict", TableStructParameterValue,
List["ParameterValue"], "DiagnosticTroubleCode"]
Iterable["ParameterValue"], "DiagnosticTroubleCode"]
ParameterValueDict = Dict[str, ParameterValue]


Expand Down
4 changes: 2 additions & 2 deletions odxtools/staticfield.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: MIT
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List
from typing import TYPE_CHECKING, Any, Dict, List, Sequence
from xml.etree import ElementTree

from typing_extensions import override
Expand Down Expand Up @@ -51,7 +51,7 @@ def _resolve_snrefs(self, diag_layer: "DiagLayer") -> None:
def encode_into_pdu(self, physical_value: ParameterValue, encode_state: EncodeState) -> None:

if not isinstance(physical_value,
(tuple, list)) or len(physical_value) != self.fixed_number_of_items:
Sequence) or len(physical_value) != self.fixed_number_of_items:
odxraise(f"Value for static field '{self.short_name}' "
f"must be a list of size {self.fixed_number_of_items}")

Expand Down

0 comments on commit 7c5a56c

Please sign in to comment.