0

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.

4
  • 1
    I do not think you can hint at the length of a list or tuple since those types do not specify a particular length. Feb 19, 2019 at 11:53
  • If that's the case, would the provided hack be a sensible solution? It still gives me the wrong type (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.
    – lucasgcb
    Feb 19, 2019 at 12:14
  • Something like Tuple[float, float, float]?
    – jonrsharpe
    Feb 19, 2019 at 12:22
  • So I was very wrong. Feb 19, 2019 at 12:37

1 Answer 1

2

Python does have optional typing, which you can use like this:

from typing import TypeVar, Tuple
from numpy import array, float64, isnan
import sys

T = TypeVar('T', int, float)

class Shape:
   def __init__(self, dimension: Tuple[T, T, T]=[None,None,None]) -> None: 
        self.dimension = array(dimension, dtype=float64, copy=False)
        if any(isnan(self.dimension))  or  len(self.dimension)!= 3:
            sys.exit('Bad dimension input')

This doesn't really do too much, but will show up if for example someone calls help(Shape) (give it a try). There are also linters which will use this typing and pull someone up with a warning if they do something wrong, but the program will run anyway.

1
  • This is fantastic, TypeVar is easier to work with than I thought! I believe this can be used to specify Tuples within Tuples as well which could come in handy.
    – lucasgcb
    Feb 19, 2019 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.