[Python-ideas] PEP 484 (Type Hints) -- first draft round

Steven D'Aprano steve at pearwood.info
Thu Jan 22 15:55:33 CET 2015

On Thu, Jan 22, 2015 at 12:32:40PM +0000, Ed Kellett wrote:
> On Thu Jan 22 2015 at 12:17:37 Chris Angelico <rosuav at gmail.com> wrote:
> > The absence of a decorator that uses the annotations some other way
> > should make it clear that they're type hints, just as the absence of a
> > "global" statement should make it clear that assigned names are local
> > to a function.
> >
> What about code that already exists? 

What about them?

I have lots of code that already exists. (Some of it even works *wink*) 
Since that code doesn't use annotations, this proposal will not make one 
iota of difference to that code. *Literally* nothing will change.

Some of my code has annotations. But since I'm not using a PEP 484 
type-checker on that code, it too will continue to work completely 

The *only* code that will change is that which *I* decide to start using 
annotations for type-checking. If I'm using Python 3.4 or older, I go to 
PyPI and download the typing.py module. If I'm using 3.5, it will be 
included in the standard library. So I add the annotations to my code: 
as much, or as little, of the code as I choose. I could annotate as 
little as one function or as much as the entire application.

Even then, I still have choices. I can choose whether to perform 
type-checking or not:

- I can just run my code directly with the standard CPython interpreter 
  (the one you are probably using now), and no type-checks will take 
  place. CPython will not include a type-checker, not in 3.5 and perhaps
  not ever.

- I can run my code with Mypy, and it will perform static type-checks
  before executing the code.

- I can run my code with a static linter or type-checker and not 
  run the code.

- Or just let my IDE or editor give me better completion hints and flag 
  errors as I type them.

> What about modules that want to
> support Python installations that already exist (hence don't have the
> `typing` module)?

typing.py will be available on PyPI.

> What about module authors who simply don't want to make
> their code ugly with a no-op decorator just to signify that they're not
> making it ugly with type annotations?

You don't need to flag functions with a decorator to signify that you 
aren't using annotations. You just don't use annotations. Nobody (except 
maybe your boss) will force you to use annotations. Python will continue 
to work with them or without them.

The only people who end up a little worse off are those who already use 
annotations for something else. But even they won't be *much* worse off, 
just a little:

- Nobody will force them to use a type-checker or mypy, they can just 
  keep using their annotations and *not* use a type-checker.

- If they want to interoperate with a type-checker, they will have to 
  take steps to tell the type-checker to skip their annotations. This
  might be a directive "# typing=OFF" or a "@notyping" decorator.
  Either way, it won't be onerous.

- The worst case is if they want to use their existing annotations 
  AND the new type-hints at the same time, for the same functions.
  In that case, they will have to find an alternative for their 
  annotations, perhaps moving them into a decorator.


More information about the Python-ideas mailing list