asottile / pyupgrade

A tool (and pre-commit hook) to automatically upgrade syntax for newer versions of the language.
MIT License
3.57k stars 179 forks source link

Suggestion: Convert `a = 1 # type: int` type annotations to `a: int = 1` for python >= 3.6 #410

Closed klieret closed 3 years ago

klieret commented 3 years ago

If you use mypy or just want to help your IDE understand your types better, you often want to explicitly set the types of your variables (especially if you start with an empty list but already want to specify the type of the content).

In python < 3.6 you do that with comments of the form # type: int, e.g.

a = 1  # type: int

whereas python >= 3.6 has

a: int = 1

cf eg the mypy cheatsheet

It would be very cool to do this conversion automagically with pyupgrade :)

MarcoGorelli commented 3 years ago

Hey @klieret

Dupe of #403 ?

klieret commented 3 years ago

Oh thanks! Must have forgotten to search the closed issues...

Great to also learn about com2ann, but I'd still vote for including this functionality in pyupgrade as well.