6
class A:
   pass 

class A1(A):
   pass

class A2(A):
   pass

def help_here(s: WHAT_SHOULD_I_TYPE_HERE ):
   # How to hint this function that accepts list,
   # where all elements should be instances of same subclass of A.
   # Example 1: [A1(), A1(), A1()] - good
   #            all components all elements are same class,
   #            and it is subclass of A
   # Example 2: [A2(), A2(), A2(), A2()] - valid. Same reason.
   # Example 3: [A1(), A1(), A2()] - invalid, not same classes.
   # Example 4: [1, 2] - invalid. Same class, but not subclass of A.

   if isinstance(s[0], A1):
       # If I use Union[List[A1], List[A2]] as type hint,
       # next line will have no warnings.
       # But if where will be List[A] then warning arrives:
       # Expected type 'List[A1]', got 'List[A]' instead.
       # It's Pycharm IDE.
       process_a1_list(s)


def process_a1_list(lst: List[A1]):
    pass


def another_test():
    # This is just to see, whether IDE will show warnings or not:

    # No warning should be on this block
    good_list: List[A1] = []
    help_here(good_list)
    
    # On both next calls IDE should warn 
    bad_list_1: List[A] = []
    help_here(bad_list_1)

    bad_list_2: List[int] = []
    help_here(bad_list_2)

The only idea I have is: Union[List[A1], List[A2]].

But what if I don't know all subclasses of A?

Are there any beautiful way to hint it?

Update: Not beautiful, but let IDE (Pycharm) check types the way I want.

Update2: I need IDE type hinting solution not Runtime check. I am not sure that solution I want exists at all.

Python 3.8

Alexander C
  • 3,597
  • 1
  • 23
  • 39
  • @MisterMiyagi Any kind of Collection. Do you have solution for Sequence? – Alexander C Nov 07 '21 at 15:49
  • You can get the "``s`` is a ``List[A]`` or ``List[A1] or ``List[A2]`` or ..." with a ``TypeVar``. You cannot get ``if isinstance(s[0], A1)`` to constraint ``s``; using a type guard instead might work. – MisterMiyagi Nov 07 '21 at 16:40
  • 1
    This could almost be a bounded TypeVar, but you don't want to include the boundary type. – juanpa.arrivillaga Nov 07 '21 at 19:59
  • 1
    @juanpa.arrivillaga Yes. For now I did it in my project. It is the closest to my needs. `_A = TypeVar("_A", bound=A)` and `s:List[_A]` – Alexander C Nov 08 '21 at 08:01

0 Answers0