mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 14:34:31 +08:00
Merge pull request #1554 from PeterJCLaw/fix-nested-tuple-argument
Fix handling of nested tuple arguments
This commit is contained in:
@@ -342,6 +342,18 @@ class Tuple(BaseTypingValueWithGenerics):
|
|||||||
from jedi.inference.gradual.annotation import merge_pairwise_generics, merge_type_var_dicts
|
from jedi.inference.gradual.annotation import merge_pairwise_generics, merge_type_var_dicts
|
||||||
from jedi.inference.gradual.base import GenericClass
|
from jedi.inference.gradual.base import GenericClass
|
||||||
|
|
||||||
|
value_set = value_set.filter(
|
||||||
|
lambda x: x.py__name__().lower() == 'tuple',
|
||||||
|
)
|
||||||
|
|
||||||
|
# Somewhat unusually, this `infer_type_vars` method is on an instance
|
||||||
|
# representation of a type, rather than the annotation or class
|
||||||
|
# representation. This means that as a starting point, we need to
|
||||||
|
# convert the incoming values to their instance style if they're
|
||||||
|
# classes, rather than the reverse.
|
||||||
|
if is_class_value:
|
||||||
|
value_set = value_set.execute_annotation()
|
||||||
|
|
||||||
if self._is_homogenous():
|
if self._is_homogenous():
|
||||||
# The parameter annotation is of the form `Tuple[T, ...]`,
|
# The parameter annotation is of the form `Tuple[T, ...]`,
|
||||||
# so we treat the incoming tuple like a iterable sequence
|
# so we treat the incoming tuple like a iterable sequence
|
||||||
@@ -358,9 +370,12 @@ class Tuple(BaseTypingValueWithGenerics):
|
|||||||
|
|
||||||
type_var_dict = {}
|
type_var_dict = {}
|
||||||
for element in value_set:
|
for element in value_set:
|
||||||
|
if not is_class_value:
|
||||||
py_class = element.get_annotated_class_object()
|
py_class = element.get_annotated_class_object()
|
||||||
if not isinstance(py_class, GenericClass):
|
if not isinstance(py_class, GenericClass):
|
||||||
py_class = element
|
py_class = element
|
||||||
|
else:
|
||||||
|
py_class = element
|
||||||
|
|
||||||
merge_type_var_dicts(
|
merge_type_var_dicts(
|
||||||
type_var_dict,
|
type_var_dict,
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ tpl_typed = ("2", 3) # type: Tuple[str, int]
|
|||||||
collection = {"a": 1}
|
collection = {"a": 1}
|
||||||
collection_typed = {"a": 1} # type: Dict[str, int]
|
collection_typed = {"a": 1} # type: Dict[str, int]
|
||||||
|
|
||||||
|
list_of_ints = [42] # type: List[int]
|
||||||
list_of_funcs = [foo] # type: List[Callable[[T], T]]
|
list_of_funcs = [foo] # type: List[Callable[[T], T]]
|
||||||
|
|
||||||
custom_generic = CustomGeneric(123.45)
|
custom_generic = CustomGeneric(123.45)
|
||||||
@@ -319,3 +320,21 @@ x7
|
|||||||
for a in list_t_to_list_t(12):
|
for a in list_t_to_list_t(12):
|
||||||
#?
|
#?
|
||||||
a
|
a
|
||||||
|
|
||||||
|
|
||||||
|
def list_tuple_t_to_tuple_list_t(the_list: List[Tuple[T]]) -> Tuple[List[T], ...]:
|
||||||
|
return tuple(list(x) for x in the_list)
|
||||||
|
|
||||||
|
|
||||||
|
for b in list_tuple_t_to_tuple_list_t(list_of_ints):
|
||||||
|
#?
|
||||||
|
b[0]
|
||||||
|
|
||||||
|
|
||||||
|
def list_tuple_t_elipsis_to_tuple_list_t(the_list: List[Tuple[T, ...]]) -> Tuple[List[T], ...]:
|
||||||
|
return tuple(list(x) for x in the_list)
|
||||||
|
|
||||||
|
|
||||||
|
for b in list_tuple_t_to_tuple_list_t(list_of_ints):
|
||||||
|
#?
|
||||||
|
b[0]
|
||||||
|
|||||||
@@ -80,6 +80,29 @@ for c2, in list_t_to_list_tuple_t(list_of_ints):
|
|||||||
c2
|
c2
|
||||||
|
|
||||||
|
|
||||||
|
# Test handling of nested tuple input parameters
|
||||||
|
def list_tuple_t_to_tuple_list_t(the_list: List[Tuple[T]]) -> Tuple[List[T], ...]:
|
||||||
|
return tuple(list(x) for x in the_list)
|
||||||
|
|
||||||
|
|
||||||
|
list_of_int_tuples = [(x,) for x in list_of_ints] # type: List[Tuple[int]]
|
||||||
|
|
||||||
|
for b in list_tuple_t_to_tuple_list_t(list_of_int_tuples):
|
||||||
|
#? int()
|
||||||
|
b[0]
|
||||||
|
|
||||||
|
|
||||||
|
def list_tuple_t_elipsis_to_tuple_list_t(the_list: List[Tuple[T, ...]]) -> Tuple[List[T], ...]:
|
||||||
|
return tuple(list(x) for x in the_list)
|
||||||
|
|
||||||
|
|
||||||
|
list_of_int_tuple_elipsis = [tuple(list_of_ints)] # type: List[Tuple[int, ...]]
|
||||||
|
|
||||||
|
for b in list_tuple_t_elipsis_to_tuple_list_t(list_of_int_tuple_elipsis):
|
||||||
|
#? int()
|
||||||
|
b[0]
|
||||||
|
|
||||||
|
|
||||||
# Test handling of nested callables
|
# Test handling of nested callables
|
||||||
def foo(x: int) -> int:
|
def foo(x: int) -> int:
|
||||||
return x
|
return x
|
||||||
|
|||||||
Reference in New Issue
Block a user