Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to disable Python2/comment-style type annotations #13177

Open
dfarley1 opened this issue Jul 18, 2022 · 10 comments · May be fixed by #15068
Open

Option to disable Python2/comment-style type annotations #13177

dfarley1 opened this issue Jul 18, 2022 · 10 comments · May be fixed by #15068

Comments

@dfarley1
Copy link

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.

@hauntsaninja
Copy link
Collaborator

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

Also see #12947 and the associated typing-sig discussion

@ronkorving
Copy link

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?

@mbrancato
Copy link

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

@JelleZijlstra
Copy link
Member

You are welcome to create a PR implementing this feature.

@jttimko5
Copy link

I will help work on this

@jttimko5
Copy link

jttimko5 commented Apr 6, 2023

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
Copy link

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

@ikonst
Copy link
Contributor

ikonst commented Apr 13, 2023

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

mypy/mypy/fastparse.py

Lines 364 to 366 in 70d4bb2

converted = TypeConverter(
errors, line=line, override_column=column, is_evaluated=False
).visit(typ.body)

@jttimko5
Copy link

jttimko5 commented Apr 16, 2023

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

mypy/mypy/fastparse.py

Lines 364 to 366 in 70d4bb2

converted = TypeConverter(
errors, line=line, override_column=column, is_evaluated=False
).visit(typ.body)

Could you elaborate on why these lines do? @ikonst

@jttimko5 jttimko5 linked a pull request Apr 17, 2023 that will close this issue
@jttimko5
Copy link

Added pull request #15068 for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants