'Use generic in type alias

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:

  • some specified type, or
  • a function (with a parameter) returning that same type.
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. pycharm quick-docs showing 'Any'



Solution 1:[1]

This should work:

from typing import *

T = TypeVar('T')  # Any type.
ValueOrCallable = Union[T, Callable[..., T]]

class Foo(object):
  state:  ClassVar[ValueOrCallable]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Victor Ruiz