forked from VimPlug/jedi
Fix union acess for 3.14
This commit is contained in:
@@ -471,35 +471,28 @@ class DirectObjectAccess:
|
|||||||
op = _OPERATORS[operator]
|
op = _OPERATORS[operator]
|
||||||
return self._create_access_path(op(self._obj, other_access._obj))
|
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, ...]]
|
Returns Tuple[Optional[str], Tuple[AccessPath, ...]]
|
||||||
"""
|
"""
|
||||||
name = None
|
name = None
|
||||||
args = ()
|
args = ()
|
||||||
# Use getattr instead of safe_getattr for __module__ as getattr_static
|
module = getattr_static(self._obj, '__module__', '')
|
||||||
# fails on typing types in Python 3.14+
|
if type(self._obj) is typing.Union: # zuban: ignore[comparison-overlap] # TODO zuban
|
||||||
module = getattr(self._obj, '__module__', '')
|
# This is mostly formatted like `int | str` and we therefor need to
|
||||||
if module == 'typing':
|
# check the type.
|
||||||
import typing
|
args = typing.get_args(self._obj)
|
||||||
|
name = "Union"
|
||||||
|
elif safe_getattr(self._obj, '__module__', default='') == 'typing':
|
||||||
# Try regex first (works for most types)
|
# Try regex first (works for most types)
|
||||||
m = re.match(r'typing.(\w+)\[', repr(self._obj))
|
m = re.match(r'typing.(\w+)\[', repr(self._obj))
|
||||||
if m is not None:
|
if m is not None:
|
||||||
name = m.group(1)
|
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):
|
||||||
if sys.version_info >= (3, 8):
|
args = typing.get_args(self._obj)
|
||||||
args = typing.get_args(self._obj)
|
else:
|
||||||
else:
|
args = safe_getattr(self._obj, '__args__', default=None)
|
||||||
args = safe_getattr(self._obj, '__args__', default=None)
|
|
||||||
if args is None:
|
|
||||||
args = ()
|
|
||||||
return name, tuple(self._create_access_path(arg) for arg in args)
|
return name, tuple(self._create_access_path(arg) for arg in args)
|
||||||
|
|
||||||
def needs_type_completions(self):
|
def needs_type_completions(self):
|
||||||
|
|||||||
@@ -661,8 +661,7 @@ def bar():
|
|||||||
|
|
||||||
# typing is available via globals.
|
# typing is available via globals.
|
||||||
({'return': 'typing.Union[str, int]'}, ['int', 'str'], ''),
|
({'return': 'typing.Union[str, int]'}, ['int', 'str'], ''),
|
||||||
({'return': 'typing.Union["str", int]'},
|
({'return': 'typing.Union["str", int]'}, ['int', 'str'], ''),
|
||||||
['int', 'str'], ''),
|
|
||||||
({'return': 'typing.Union["str", 1]'},
|
({'return': 'typing.Union["str", 1]'},
|
||||||
[] if sys.version_info >= (3, 14) else (['str'] if sys.version_info >= (3, 11) else []), ''),
|
[] if sys.version_info >= (3, 14) else (['str'] if sys.version_info >= (3, 11) else []), ''),
|
||||||
({'return': 'typing.Optional[str]'}, ['NoneType', 'str'], ''),
|
({'return': 'typing.Optional[str]'}, ['NoneType', 'str'], ''),
|
||||||
|
|||||||
Reference in New Issue
Block a user