-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get_args #35
Comments
|
@Peilonrayz this works, both on 3.7.3 and 3.6.8 (which include "new" and "old" versions of from typing import List, Tuple, TypeVar
import typing_inspect
U = TypeVar("U")
V = TypeVar("V")
W = TypeVar("W")
Z = 1
class L(List[Tuple[U, V]]):
def __init__(self) -> None:
self._type_U = typing_inspect.get_xxx(self, U) # int
self._type_V = typing_inspect.get_xxx(self, V) # str
self._type_W = typing_inspect.get_xxx(self, W) # exception: "type W is not among generic parameters"
self._type_Z = typing_inspect.get_xxx(self, Z) # exception: "1 is not a type"
l = L[int, str]()
l.f() Another interesting alternative I see would be a decorator that injects all types as instance variables: from typing import List, Tuple, TypeVar
import typing_inspect
U = TypeVar("UU")
V = TypeVar("VV")
@typing_inspect.inject_types_or_some_better_name
class L(List[Tuple[U, V]]):
pass
l = L[int, str]()
assert l._type_UU == int
assert l._type_VV == str |
It doesn't look like this is at all possible to do in the constructor, in Python 3.7.2 at least. This is as Otherwise it looks rather simple: from typing import List, Tuple, TypeVar
import typing_inspect
U = TypeVar("U")
V = TypeVar("V")
W = TypeVar("W")
Z = 1
def get_type_argument(instance, type_var):
generic = typing_inspect.get_generic_type(instance)
origin = typing_inspect.get_origin(generic)
params = typing_inspect.get_parameters(origin)
index = params.index(type_var)
return typing_inspect.get_args(generic)[index]
class L(List[Tuple[U, V]]):
pass
l = L[int, str]()
print(get_type_argument(l, U))
print(get_type_argument(l, V))
try:
print(get_type_argument(l, W))
except ValueError as e:
print(e)
try:
print(get_type_argument(l, Z))
except ValueError as e:
print(e) |
So, do you think something like that should be included as a part of |
On one hand, there is a plan to add some extra functionality to |
Maybe pytypes.get_arg_for_TypeVar(typevar, generic) is what you are looking for. |
From my experience, pytypes still lacks proper python 3.7 support.
…On Fri, 3 May 2019, 09:40 Stefan Richthofer, ***@***.***> wrote:
Maybe pytypes.get_arg_for_TypeVar(typevar, generic)
<https://github.com/Stewori/pytypes#get_arg_for_typevartypevar-generic>
is what you are looking for.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#35 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAOCW3S52ZKUQTNG72CAWUTPTPM5JANCNFSM4HJWBBZA>
.
|
Feel free to make a PR for that ;) |
Regretfully, solution from #35 (comment) fails in case of inheritance: from typing import List, TypeVar
import typing_inspect
T = TypeVar("T")
class L(List[T]):
def f(self) -> None:
print(typing_inspect.get_args(typing_inspect.get_generic_type(self)))
class L2(L[int]):
pass
L[int]().f()
L2().f() prints
Digging into from typing import List, TypeVar
import typing_inspect
T = TypeVar("T")
class L(List[T]):
def f(self) -> None:
types = getattr(self, "_L_types", None)
if types is None:
types = typing_inspect.get_args(typing_inspect.get_generic_type(self))
print(types)
class L2(L[int]):
_L_types = (int, )
L[int]().f()
L2().f() |
This is as the arguments are on the base class. Using from typing import List, TypeVar
import typing_inspect
T = TypeVar("T")
class L(List[T]):
def f(self) -> None:
types = typing_inspect.get_args(typing_inspect.get_generic_type(self))
if not types:
types = typing_inspect.get_args(typing_inspect.get_generic_bases(self)[0])
print(types)
class L2(L[int]):
pass
L().f()
L[int]().f()
L2().f() It should be noted that this is hacky solution, and may give unexpected results if the class is Using this should be fairly simple when it does come out: (Note: untested and subject to change) for type_info in get_mro_orig(L2()):
if type_info.typing is L:
print(get_args(type_info.orig)) It should be noted that this won't return |
For the record. In a recent commit I fixed workability of
I know, the name is misleading as it retrieves args, not parameters. That came because it is originally intended to provide control over the parameters to select: the second, optional argument can be some base, e.g. |
Is it possible to use
typing_inspect
to achieve the following?The text was updated successfully, but these errors were encountered: