mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 23:04:48 +08:00
Merge branch 'master' of github.com:davidhalter/jedi
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
from jedi._compatibility import unicode, force_unicode
|
from jedi._compatibility import unicode, force_unicode
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.inference.base_value import ValueSet, NO_VALUES
|
from jedi.inference.base_value import ValueSet, NO_VALUES, ValueWrapper
|
||||||
from jedi.inference.gradual.base import BaseTypingValue
|
from jedi.inference.gradual.base import BaseTypingValue
|
||||||
|
|
||||||
|
|
||||||
@@ -108,8 +108,24 @@ class TypeVar(BaseTypingValue):
|
|||||||
return self._get_classes().execute_annotation()
|
return self._get_classes().execute_annotation()
|
||||||
|
|
||||||
def infer_type_vars(self, value_set):
|
def infer_type_vars(self, value_set):
|
||||||
|
def iterate():
|
||||||
|
for v in value_set:
|
||||||
|
cls = v.py__class__()
|
||||||
|
if v.is_function() or v.is_class():
|
||||||
|
cls = TypeWrapper(cls, v)
|
||||||
|
yield cls
|
||||||
|
|
||||||
annotation_name = self.py__name__()
|
annotation_name = self.py__name__()
|
||||||
return {annotation_name: value_set.py__class__()}
|
return {annotation_name: ValueSet(iterate())}
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s>' % (self.__class__.__name__, self.py__name__())
|
return '<%s: %s>' % (self.__class__.__name__, self.py__name__())
|
||||||
|
|
||||||
|
|
||||||
|
class TypeWrapper(ValueWrapper):
|
||||||
|
def __init__(self, wrapped_value, original_value):
|
||||||
|
super(TypeWrapper, self).__init__(wrapped_value)
|
||||||
|
self._original_value = original_value
|
||||||
|
|
||||||
|
def execute_annotation(self):
|
||||||
|
return ValueSet({self._original_value})
|
||||||
|
|||||||
@@ -1,9 +1,23 @@
|
|||||||
# python >= 3.4
|
# python >= 3.4
|
||||||
from typing import Any, Iterable, List, Sequence, Tuple, TypeVar, Union, Generic
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Callable,
|
||||||
|
Iterable,
|
||||||
|
List,
|
||||||
|
Sequence,
|
||||||
|
Tuple,
|
||||||
|
Type,
|
||||||
|
TypeVar,
|
||||||
|
Union,
|
||||||
|
Generic,
|
||||||
|
)
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
U = TypeVar('U')
|
U = TypeVar('U')
|
||||||
TList = TypeVar('TList', bound=List[Any])
|
TList = TypeVar('TList', bound=List[Any])
|
||||||
|
TType = TypeVar('TType', bound=Type)
|
||||||
|
TTypeAny = TypeVar('TTypeAny', bound=Type[Any])
|
||||||
|
TCallable = TypeVar('TCallable', bound=Callable[..., Any])
|
||||||
|
|
||||||
untyped_list_str = ['abc', 'def']
|
untyped_list_str = ['abc', 'def']
|
||||||
typed_list_str = ['abc', 'def'] # type: List[str]
|
typed_list_str = ['abc', 'def'] # type: List[str]
|
||||||
@@ -40,6 +54,9 @@ def typed_fully_generic_passthrough(x: T) -> T:
|
|||||||
return x
|
return x
|
||||||
|
|
||||||
def typed_bound_generic_passthrough(x: TList) -> TList:
|
def typed_bound_generic_passthrough(x: TList) -> TList:
|
||||||
|
#? list()
|
||||||
|
x
|
||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
@@ -109,6 +126,8 @@ for m in typed_variadic_tuple_generic_passthrough(variadic_tuple_str_int):
|
|||||||
#? str() int()
|
#? str() int()
|
||||||
m
|
m
|
||||||
|
|
||||||
|
#? float
|
||||||
|
typed_fully_generic_passthrough(float)
|
||||||
|
|
||||||
for n in typed_fully_generic_passthrough(untyped_list_str):
|
for n in typed_fully_generic_passthrough(untyped_list_str):
|
||||||
#? str()
|
#? str()
|
||||||
@@ -144,6 +163,73 @@ typed_fully_generic_passthrough(CustomList[str]())[0]
|
|||||||
typed_list_generic_passthrough(CustomList[str])[0]
|
typed_list_generic_passthrough(CustomList[str])[0]
|
||||||
|
|
||||||
|
|
||||||
|
def typed_bound_type_implicit_any_generic_passthrough(x: TType) -> TType:
|
||||||
|
#? Type()
|
||||||
|
x
|
||||||
|
return x
|
||||||
|
|
||||||
|
def typed_bound_type_any_generic_passthrough(x: TTypeAny) -> TTypeAny:
|
||||||
|
# Should be Type(), though we don't get the handling of the nested argument
|
||||||
|
# to `Type[...]` quite right here.
|
||||||
|
x
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
class MyClass:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def my_func(a: str, b: int) -> float:
|
||||||
|
pass
|
||||||
|
|
||||||
|
#? MyClass
|
||||||
|
typed_fully_generic_passthrough(MyClass)
|
||||||
|
|
||||||
|
#? MyClass()
|
||||||
|
typed_fully_generic_passthrough(MyClass())
|
||||||
|
|
||||||
|
#? my_func
|
||||||
|
typed_fully_generic_passthrough(my_func)
|
||||||
|
|
||||||
|
#? CustomList()
|
||||||
|
typed_bound_generic_passthrough(CustomList[str]())
|
||||||
|
|
||||||
|
# should be list(), but we don't validate generic typevar upper bounds
|
||||||
|
#? int()
|
||||||
|
typed_bound_generic_passthrough(42)
|
||||||
|
|
||||||
|
#? MyClass
|
||||||
|
typed_bound_type_implicit_any_generic_passthrough(MyClass)
|
||||||
|
|
||||||
|
#? MyClass
|
||||||
|
typed_bound_type_any_generic_passthrough(MyClass)
|
||||||
|
|
||||||
|
# should be Type(), but we don't validate generic typevar upper bounds
|
||||||
|
#? int()
|
||||||
|
typed_bound_type_implicit_any_generic_passthrough(42)
|
||||||
|
|
||||||
|
# should be Type(), but we don't validate generic typevar upper bounds
|
||||||
|
#? int()
|
||||||
|
typed_bound_type_any_generic_passthrough(42)
|
||||||
|
|
||||||
|
|
||||||
|
def decorator(fn: TCallable) -> TCallable:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def will_be_decorated(the_param: complex) -> float:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
is_decorated = decorator(will_be_decorated)
|
||||||
|
|
||||||
|
#? will_be_decorated
|
||||||
|
is_decorated
|
||||||
|
|
||||||
|
#? ['the_param=']
|
||||||
|
is_decorated(the_para
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class That(Generic[T]):
|
class That(Generic[T]):
|
||||||
def __init__(self, items: List[Tuple[str, T]]) -> None:
|
def __init__(self, items: List[Tuple[str, T]]) -> None:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user