Reputation: 2456
Lets suppose i have the following types
import typing
class A:
pass
class B:
@property
def a_factory(self): -> typing.Type[A]
return A
my problem is howto define a type for a callable that takes B instance as argument and return an instance of
the a_factory
attribute.
What i have so far:
C = typing.Callable[[B], ?]
I don't want to use A directly because i want the C type to convey the requirement of using B.a_factory factory to produce the return value. Any ideas?
Upvotes: 0
Views: 65
Reputation: 163
import typing
class A:
pass
class B:
@property
def a_factory(self) -> typing.Type[A]:
return A
b = B() # an instance of class B
C = typing.get_type_hints(B.a_factory.fget)["return"]
C = typing.get_type_hints(b.__class__.a_factory.fget)["return"]
print(C) # typing.Type[__main__.A]
fget
to retrive the getter function, then use get_type_hints
to get returned type.B
from instance b
, we need __class__
attribute.Upvotes: 1