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

Steven D'Aprano steve at pearwood.info
Fri Aug 15 02:14:39 CEST 2014


On Thu, Aug 14, 2014 at 01:54:20PM -0700, Bob Ippolito wrote:

> What is part of this proposal?
> 
> * An effort to standardize on a particular syntax for optional type
> annotations using Python 3 function annotations

+1 on that.

> * The syntax will be handwavingly based on what's in mypy, and thus implies
> some semantics but not the implementation of the type checker/inference/etc.

I have some reservations as to whether the mypy syntax is mature enough 
to bake in as standard, but other than that reservation, I prefer the 
mypy approach over putting type declarations in docstrings.

+1

> * A suggestion to use tools such as mypy to provide a static type checking
> pass in much the same way that people use linters today

"Tools such as mypy" is a very important point. If there is a standard 
syntax for type annotations, there's no reason why other linters 
couldn't support it as well.

+1

> * A suggestion that IDEs, documentation tools, etc. take advantage of the
> information provided by the type annotations

+1

> * Deprecation of using function annotations for any other purpose. They
> can't really be composed, and ideally type checkers can work primarily at
> the syntax level and not have to evaluate the module with a full Python
> interpreter in order to extract the annotations, so it's best to keep the
> syntax uniform.

-1

I don't think this one is justified. At the very least, I think the 
decision to deprecate or not should be deferred until at least 3.7. It's 
enough to say that:

- you can use function annotations for type checking; or

- you can use function annotations for something else;

but not both at the same time. I don't think it is a big burden to have 
mypy and other linters support an "opt-out" decorator, say, so that 
projects can use annotations for something else without confusing the 
linter.

As I understand it, the current behaviour of mypy is that you have to 
import typing in the module before it will type check the module, so 
that already gives you a way to skip type annotations on a per-module 
basis: just don't import typing.


-- 
Steven


More information about the Python-ideas mailing list