Skip to content

Commit 7b52177

Browse files
authored
Merge pull request #450 from andlaus/non-strict_robustness2
further improve robustness of non-strict mode
2 parents 90c669c + c1a9cb5 commit 7b52177

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

odxtools/nameditemlist.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: MIT
22
import abc
33
import typing
4-
import weakref
54
from collections.abc import Collection, Iterable
65
from keyword import iskeyword
76
from typing import Any, SupportsIndex, TypeVar, overload, runtime_checkable
@@ -59,6 +58,8 @@ def append(self, item: T) -> None:
5958

6059
def _add_attribute_item(self, item: T) -> None:
6160
item_name = self._get_item_key(item)
61+
if item_name is None:
62+
return
6263

6364
# eliminate conflicts between the name of the new item and
6465
# existing attributes of the ItemAttributeList object
@@ -204,25 +205,15 @@ def _get_item_key(self, item: T) -> str:
204205
205206
"""
206207

207-
# isinstance() does not support checking a `weakref.proxy()`
208-
# object against a type protocol. (checking `weakref.proxy()`
209-
# objects against regular types works fine, though.) If the
210-
# item is a `weakref.proxy()` object, we thus check the object
211-
# which the item points to for its adherence to the `OdxNamed`
212-
# type protocol. Since there seems to be no "official" way to
213-
# determine this type, we need to go via the
214-
# `__repr__.__self__` route...
215-
if isinstance(item, (weakref.ProxyType, weakref.CallableProxyType)):
216-
if not isinstance(item.__repr__.__self__, OdxNamed): # type: ignore[attr-defined]
217-
odxraise()
218-
sn = item.short_name
219-
elif not isinstance(item, OdxNamed):
220-
odxraise()
221-
else:
222-
sn = item.short_name
223-
224-
if not isinstance(sn, str):
225-
odxraise()
208+
if (sn := getattr(item, "short_name", None)) is None:
209+
odxraise(f"Object does not exhibit a .short_name attribute")
210+
return
211+
elif not isinstance(sn, str):
212+
odxraise(f".short_name is not a string. (is: '{sn}')")
213+
return
214+
elif not sn:
215+
odxraise(f".short_name is the empty string")
216+
return
226217

227218
# make sure that the name of the item in question is not a python
228219
# keyword (this would lead to syntax errors) and that does not

0 commit comments

Comments
 (0)