mypy
mypy copied to clipboard
Option to disable Python2/comment-style type annotations
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.
I'd welcome a PR adding an option for this!
Also see #12947 and the associated typing-sig discussion
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?
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
You are welcome to create a PR implementing this feature.
I will help work on this
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!
Could i get some help locating where the syntax is checked for type annotations?
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
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
Added pull request #15068 for this