Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 673
luckydonald Avatar asked May 26 '19 21:05

luckydonald


1 Answers

This should work:

from typing import *

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

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

like image 188
Victor Ruiz Avatar answered Oct 13 '22 18:10

Victor Ruiz