Considering a class method that takes a "vector" (Tuple or List of either int
or float
) of defined values such as the following:
import sys
from numpy import isnan, array, float64
class Shape:
"""
This is the Shape class.
Create 3D shapes described by a length 3 vector dimension input.
Example:
>>x=y=z=2.5
>>Shape([x,y,z])
"""
def __init__(self,dimension):
self.dimension = array(dimension, dtype=float64, copy=False)
if any(isnan(self.dimension)) or len(self.dimension)!= 3:
sys.exit('Bad dimension input')
### more dimension sensitive code
How could one Type Hint this so it is communicated that I can have valid inputs such as the following?
Shape([1,2,3])
Shape([1.0,2.0,3.0])
Shape((1.0,2.0,3.0))
For now, I have the following hack which I think looks jarring but people like it:
import ...
x=y=z=None ## Placeholder type hinting(????)
class Shape:
"""
Lorem Ipsum
"""
def __init__(self,dimension=[x,y,z]):
self.dimension = array(dimension, dtype=float64, copy=False)
if any(isnan(self.dimension)) or len(self.dimension)!= 3:
sys.exit('Bad dimension input')
### more dimension sensitive code
Of course this suggests nothing about the type of the vector data but at least shows it is a 3 sized vector with known symbols (x,y,z) to my users.
List[None]
) around the corner but gets some of the job "done"; I've been wondering if I've been missing something for situations like these.Tuple[float, float, float]
?