mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Option to disable Python2/comment-style type annotations

Open dfarley1 opened this issue 2 years ago • 3 comments

Our codebase (all Python3) has a few places where we ended up writing comments that look like # type: some description ... prior to adding Mypy. They're usually within a larger multiline comment block when we're describing an expected Dict layout, e.g.

❯ cat foo.py
# Each Foo has the following fields:
# name: name of the foo
# display_name: human name
# type: type of foo it is
# tags: list of tags that apply to the foo
❯ mypy foo.py
foo.py:4: error: invalid syntax
Found 1 error in 1 file (errors prevented further checking)

Since we're only using Python3 and "proper" type annotations, it'd be nice if we could stop Mypy from trying to parse these types of comments. I know we could change our comment style to use dashes or something else that won't anger Mypy but it's annoying that it's necessary; and honestly colons are the better delimter anyways.

dfarley1 avatar Jul 18 '22 17:07 dfarley1

I'd welcome a PR adding an option for this!

Also see #12947 and the associated typing-sig discussion

hauntsaninja avatar Jul 18 '22 17:07 hauntsaninja

Yes, please. I run into the same issue where completely innocent comments written for humans are being interpreted by mypy as a type annotation. I can imagine that turning off that feature completely could also make mypy run a bit faster?

ronkorving avatar Dec 14 '22 01:12 ronkorving

Adding some sample code here since I ran across this in my issue. This not only applies to random comments, but also to commented out Python 3 code with PEP 526 type hints. I'm not sure why mypy processes these since there is no code prior to the comment.

# class A():
#     type: str

mbrancato avatar Jan 12 '23 13:01 mbrancato

You are welcome to create a PR implementing this feature.

JelleZijlstra avatar Mar 23 '23 11:03 JelleZijlstra

I will help work on this

jttimko5 avatar Mar 28 '23 23:03 jttimko5

Can someone please provide some guidance on where in the code base I should look to implement this new feature? I am new to the codebase so any help pointing me in the right direction would be super appreciated! Thanks!

jttimko5 avatar Apr 06 '23 17:04 jttimko5

Could i get some help locating where the syntax is checked for type annotations?

jttimko5 avatar Apr 13 '23 18:04 jttimko5

Probably can be done by making these lines not run and have converted = None instead: https://github.com/python/mypy/blob/70d4bb27a2733e43958dc5046befdc16ed297a5c/mypy/fastparse.py#L364-L366

ikonst avatar Apr 13 '23 18:04 ikonst

Probably can be done by making these lines not run and have converted = None instead:

https://github.com/python/mypy/blob/70d4bb27a2733e43958dc5046befdc16ed297a5c/mypy/fastparse.py#L364-L366

Could you elaborate on why these lines do? @ikonst

jttimko5 avatar Apr 16 '23 18:04 jttimko5

Added pull request #15068 for this

jttimko5 avatar Apr 17 '23 15:04 jttimko5