My type annotations I currently have look similar to the following, and I want to use an typing alias to not repeat myself so much:
A class has class variables, which can be either:
class Foo(object):
  state:  ClassVar[Union[str, Callable[[SomeObject], str]]]  # str or func->str
  number: ClassVar[Union[int, Callable[[SomeObject], int]]]  # int or func->int
  foobar: ClassVar[Union[bool, Callable[[SomeObject], bool]]] # bool or func->bool
# end class
For completion sake, here is an example implementation:
class FooBar(Foo):
   state = "something"
   number = lambda x: int(x.bla)
   
   @classmethod
   def foobar(cls, x):
     return x.blabla == cls.state
   # end def
# end class
However I can't figure out how to make a subscriptable generic. I'm seeking for something kinda like:
ClassValueOrCallable = lambda T: ClassVar[Union[T, Callable[[SomeObject], T]]]
class Foo(object):
  state:  ClassValueOrCallable(str)
  number: ClassValueOrCallable(int)
  foobar: ClassValueOrCallable(bool)
Edit:
Following mypy's generic type aliases section, it seems to should be possible to write it as
T = TypeVar('T')  # Any type.
ClassValueOrCallable = ClassVar[Union[T, Callable[[SomeObject], T]]]
class Foo(object):
  state:  ClassValueOrCallable[str]
  number: ClassValueOrCallable[int]
  foobar: ClassValueOrCallable[bool]
But at least PyCharm doesn't recognise that, and just displays the type as Any, so I'm not too sure if it is correct.

This should work:
from typing import *
T = TypeVar('T')  # Any type.
ValueOrCallable = Union[T, Callable[..., T]]
class Foo(object):
  state:  ClassVar[ValueOrCallable]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With