PEP 526 - var annotations and the spirit of python

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jul 4 01:52:34 EDT 2018


On Sun, 01 Jul 2018 17:22:43 -0500, Tim Daneliuk wrote:

>> x: int = 3
[...]

> This strikes me as syntactic noise.  Python is dynamically typed and
> will remain so. Why clutter the language - even optionally - with stuff
> like this?

There's no need to declare x:int = 3 since any linter worth its salt 
ought to be able to infer that x is an int if it is assigned the value 3. 
Anyone writing that needs to be slapped with a clue-stick, it's not 1971 
any more, type inference ought to be considered bare minimum for even a 
halfway decent type checker or linter.

(If your linter doesn't, your linter sucks.)

A better example would be:

    x: int = None

which ought to be read as "x is an int, or None, and it's currently None".

Better still would be something like this:

    alist: [int] = []

which is read as "x is a list of ints, currently empty".



Guido believes that optional static typing is important and useful enough 
to dedicate syntax to it, so that editors, IDEs, linters and other tools 
can standardise on a single notation rather than every one picking their 
own:

    alist: [int] = []

is much better than:

    alist = []  # [int] List(Int) foolint:type=[integer]

so having one syntax which everyone can agree on is a good thing.

And as a general rule, using comments for semantically significant 
information is an anti-pattern.




-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list