0

I have the concrete example with a method that accepts two timestamps (which are floats) but a type: float in the annotation doesn't tell you anything about the semantics ..

def myfunc( start_date: float, end_date: float ) -> dict:
  """
    :param float start_date: a timestamp of the starting date
    :param float end_date: a timestamp of the starting date
    
  """
  
# Versus

def myfunc( start_date: datetime.datetime.timestamp,
            end_date: datetime.datetime.timestamp ) -> dict:
  """
    probably no need to explain.....
  """

The idea would be to have some additional information about the semantic of the annotated type. Maybe this is already possible .. but I am unaware currently.

Thank you for any input on this.

klaas
  • 1,661
  • 16
  • 24
  • Classes and types have been the [same thing since Python 2.2](https://stackoverflow.com/a/35959047/534674). You can do `def myfunc(arr: numpy.ndarray) -> numpy.ndarray:`. If you want semantics more restrictive than "is a member of a class", you have to (a) describe it in docsting, (b) check and raise `ValueError` in function body, and/or (c) decorate with a validator function whose name is semantically meaningful, e.g. `@future_dates_only` – Jake Stevens-Haas Jun 15 '22 at 21:44
  • I'd recommend to rename `start_date` to `start_timestamp`. – SergeiMinaev Jun 15 '22 at 21:46
  • @SergeiMinaev: Yes, you are right. In this case I chose the name on purpose to show the problem ... I think it would be nicer to have some way to transport the needed type in the type annotation itself. – klaas Jun 16 '22 at 11:59

0 Answers0