0

Given the following example:

class A:
    def __init__(self, x: (float, np.ndarray) = 0.05):
        self.x = x

what i intend with it is to give a hint to the user that the argument x can be a float or a numpy array. If nothing is given, set default to 0.05. Is this the right use ? If yes, why does Pycharm warm when i initiate A as follows ? :

 a = A(x=np.random.rand(3, 3))   #Expected type 'float', got 'ndarray' instead

If its not the right use, where is my thinking wrong? Doesnt x:(float,np.ndarray) mean that x can be a float or np.ndarray?

JustANoob
  • 580
  • 1
  • 4
  • 19

1 Answers1

4

Use Union:

from typing import Union

class A:
    def __init__(self, x: Union[float, np.ndarray] = 0.05):
        self.x = x
DeepSpace
  • 78,697
  • 11
  • 109
  • 154