forked from VimPlug/jedi
Format unions with | consistently
This commit is contained in:
@@ -507,19 +507,17 @@ class DirectObjectAccess:
|
|||||||
|
|
||||||
def _annotation_to_str(self, annotation):
|
def _annotation_to_str(self, annotation):
|
||||||
# In Python 3.14+, Union types are displayed as X | Y instead of Union[X, Y]
|
# In Python 3.14+, Union types are displayed as X | Y instead of Union[X, Y]
|
||||||
# We normalize to Union[X, Y] for consistency
|
# We normalize to that for consistency
|
||||||
if sys.version_info >= (3, 8):
|
import typing
|
||||||
import typing
|
origin = typing.get_origin(annotation)
|
||||||
origin = typing.get_origin(annotation)
|
if origin is typing.Union:
|
||||||
if origin is typing.Union:
|
# Get the args and format them as Union[...]
|
||||||
# Get the args and format them as Union[...]
|
args = typing.get_args(annotation)
|
||||||
args = typing.get_args(annotation)
|
return ' | '.join(
|
||||||
formatted_args = ', '.join(
|
self._annotation_to_str(arg) if hasattr(arg, '__origin__')
|
||||||
self._annotation_to_str(arg) if hasattr(arg, '__origin__')
|
else getattr(arg, '__name__', str(arg))
|
||||||
else getattr(arg, '__name__', str(arg))
|
for arg in args
|
||||||
for arg in args
|
)
|
||||||
)
|
|
||||||
return f'Union[{formatted_args}]'
|
|
||||||
return inspect.formatannotation(annotation)
|
return inspect.formatannotation(annotation)
|
||||||
|
|
||||||
def get_signature_params(self):
|
def get_signature_params(self):
|
||||||
|
|||||||
@@ -111,4 +111,4 @@ def test_compiled_signature_annotation_string():
|
|||||||
|
|
||||||
s, = jedi.Interpreter('func()', [locals()]).get_signatures(1, 5)
|
s, = jedi.Interpreter('func()', [locals()]).get_signatures(1, 5)
|
||||||
assert s.params[0].description == 'param x: Type'
|
assert s.params[0].description == 'param x: Type'
|
||||||
assert s.params[1].description == 'param y: Union[Type, int]'
|
assert s.params[1].description == 'param y: Type | int'
|
||||||
|
|||||||
Reference in New Issue
Block a user