|
1 | 1 | # SPDX-License-Identifier: MIT |
2 | 2 | import abc |
3 | 3 | import typing |
4 | | -import weakref |
5 | 4 | from collections.abc import Collection, Iterable |
6 | 5 | from keyword import iskeyword |
7 | 6 | from typing import Any, SupportsIndex, TypeVar, overload, runtime_checkable |
@@ -59,6 +58,8 @@ def append(self, item: T) -> None: |
59 | 58 |
|
60 | 59 | def _add_attribute_item(self, item: T) -> None: |
61 | 60 | item_name = self._get_item_key(item) |
| 61 | + if item_name is None: |
| 62 | + return |
62 | 63 |
|
63 | 64 | # eliminate conflicts between the name of the new item and |
64 | 65 | # existing attributes of the ItemAttributeList object |
@@ -204,25 +205,15 @@ def _get_item_key(self, item: T) -> str: |
204 | 205 |
|
205 | 206 | """ |
206 | 207 |
|
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 |
226 | 217 |
|
227 | 218 | # make sure that the name of the item in question is not a python |
228 | 219 | # keyword (this would lead to syntax errors) and that does not |
|
0 commit comments