JustANoob
JustANoob

Reputation: 620

Type warning Pycharm

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?

Upvotes: 0

Views: 229

Answers (1)

DeepSpace
DeepSpace

Reputation: 81594

Use Union:

from typing import Union

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

Upvotes: 4

Related Questions