[Python-ideas] Proposal: Use mypy syntax for function annotations

Tony Lownds tony at lownds.com
Wed Aug 27 19:20:18 CEST 2014


Guido van Rossum <guido at ...> writes:

> from typing import no_type_checks
> 
> @no_type_checks
> def speeder(short: 'r2d2', tall: 'c3po') -> 'luke':
>     print('These are not the droids you are looking for')
> 
> @no_type_checks
> def clone_trooper(func):
>     print('Looking for', func.__annotations__, 'in', func.__name__)
>     func.searched = True
>     return func
> 
>  @clone_trooper
> def speeder(short: 'r2d2', tall: 'c3po') -> 'luke':
>    print('These are not the droids you are looking for')
> 

Can I propose a slight modification -- the designated decorator
takes another decorator, in the decoration line:

@non_typing_annotation(None)
def speeder(short: 'r2d2', tall: 'c3po') -> 'luke':
     print('These are not the droids you are looking for')

@non_typing_annotation(clone_trooper)
def speeder(short: 'r2d2', tall: 'c3po') -> 'luke':
   print('These are not the droids you are looking for')

This way the library defining `clone_trooper` doesn't have to 
change to support the typing improvements, and the reader always 
knows whether the annotations will be treated as type hints without
needing to consult another libraries' source.

Also, I'd like to suggest that mypy should use the import of 'typing'
as an indicator that the module's annotations are in fact type hints.

-Tony




More information about the Python-ideas mailing list