Fix union acess for 3.14

This commit is contained in:
Dave Halter
2026-04-27 15:21:26 +02:00
parent 04d45a8e1e
commit 44600ea194
2 changed files with 13 additions and 21 deletions
+12 -19
View File
@@ -471,35 +471,28 @@ class DirectObjectAccess:
op = _OPERATORS[operator]
return self._create_access_path(op(self._obj, other_access._obj))
def get_annotation_name_and_args(self):
def get_annotation_name_and_args(self) -> tuple[str | None, tuple[AccessPath, ...]]:
"""
Returns Tuple[Optional[str], Tuple[AccessPath, ...]]
"""
name = None
args = ()
# Use getattr instead of safe_getattr for __module__ as getattr_static
# fails on typing types in Python 3.14+
module = getattr(self._obj, '__module__', '')
if module == 'typing':
import typing
module = getattr_static(self._obj, '__module__', '')
if type(self._obj) is typing.Union: # zuban: ignore[comparison-overlap] # TODO zuban
# This is mostly formatted like `int | str` and we therefor need to
# check the type.
args = typing.get_args(self._obj)
name = "Union"
elif safe_getattr(self._obj, '__module__', default='') == 'typing':
# Try regex first (works for most types)
m = re.match(r'typing.(\w+)\[', repr(self._obj))
if m is not None:
name = m.group(1)
elif sys.version_info >= (3, 8):
# Fallback to get_origin() for Python 3.8+ when regex fails
# In Python 3.14+, Union/Optional repr changed to use | syntax
origin = typing.get_origin(self._obj)
if origin is typing.Union:
name = 'Union'
# Get args
if sys.version_info >= (3, 8):
args = typing.get_args(self._obj)
else:
args = safe_getattr(self._obj, '__args__', default=None)
if args is None:
args = ()
if sys.version_info >= (3, 8):
args = typing.get_args(self._obj)
else:
args = safe_getattr(self._obj, '__args__', default=None)
return name, tuple(self._create_access_path(arg) for arg in args)
def needs_type_completions(self):
+1 -2
View File
@@ -661,8 +661,7 @@ def bar():
# typing is available via globals.
({'return': 'typing.Union[str, int]'}, ['int', 'str'], ''),
({'return': 'typing.Union["str", int]'},
['int', 'str'], ''),
({'return': 'typing.Union["str", int]'}, ['int', 'str'], ''),
({'return': 'typing.Union["str", 1]'},
[] if sys.version_info >= (3, 14) else (['str'] if sys.version_info >= (3, 11) else []), ''),
({'return': 'typing.Optional[str]'}, ['NoneType', 'str'], ''),