First time here? Check out the FAQ!

Revision history  [back]

Type hinting method return based on input type

Hi. I'm wondering if it's possible to have intellisense (or whatever it's call in Wing) to understand what a return type will be based on the input to a method. For example:

apple = fruit.to(Apple)
apple.bite() #peel shouldn't be available for code completion, but currently is

orange = fruit.to(Orange)
orange.peel() #Orange shouldn't have bite, but currently does

To_types = typing.Union['Apple','Orange','Fruit', None]

class Fruit():
  @staticmethod
  def can_transform(fruit):
    raise NotImplementedError

  @overload
  def to(self, subclass:'Apple')-> 'Apple':
    ...

  @overload
  def to(self, subclass:'Orange')-> 'Orange':
    ...

  def to(self, subclass:To_types)->To_types:
    result = subclass(self) if subclass.can_transform(self) else None
    return result

class Apple(Fruit):
    @staticmethod
    def can_transform(fruit):
        fruit.is_red()

    def bite(self):
        pass

class Orange(Fruit):
    def peel(self):
        pass

Thanks for any info on this!