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

Allow ignore to be on any line of multi-line statement #12341

Open
Zeckie opened this issue Mar 13, 2022 · 1 comment
Open

Allow ignore to be on any line of multi-line statement #12341

Zeckie opened this issue Mar 13, 2022 · 1 comment
Labels
feature topic-type-ignore # type: ignore comments

Comments

@Zeckie
Copy link

Zeckie commented Mar 13, 2022

Feature

Currently, comments can be added to a line to ignore either all errors (# type: ignore), or a specific error code (eg. # type: ignore[call-arg]). When a single statement spans multiple lines, the comment has to be added on the line that mypy "decides" is the one with the error (is it the first line of the expression, rather than first line of the statement?). In some cases, it would make sense to allow the comment to be on a different line of the same statement. I propose allowing it to be on any line of the statement, and therefore to apply to the whole statement.

Pitch
Sometimes it makes more sense for it to be another line, such as when importing something that mypy thinks does not exist, it makes sense to have the ignore on the same line as the attribute that is reported as not existing.

from typing import ( # ignore currently needs to be here
    Union,
    DoesNotExist, # type: ignore[error attr-defined]
)

Sometimes, a formatter like black changes a long line into a multiple line statement. The comment that was at the end of the line is moved to the end of the last line.

def foo(bar:int) -> str: ...
def baz() -> int: ...

a = ( # type: ignore[call-arg]
    "something" 
    if baz() > 0 else 
    foo() # ignore currently needs to be here
)  # formatters (eg. black) often put ignore here

https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict%2Cwarn-unused-ignores&gist=e41c530d401ac4f21cb7a21e68fea141

@Zeckie Zeckie added the feature label Mar 13, 2022
@DetachHead
Copy link
Contributor

i agree with the first example because you should be able to split an expression up such that the part with the error is isolated on a single line, to minimize the chance of the type:ignore comment effecting other parts of the expression too

in the second example i think it's preferable for it to require the type:ignore comment to be on the line with the error, because otherwise it's easy to accidentally insert a new error and ignore it:

def foo(bar:int) -> str: ...
def baz(bar: int) -> int: ... # new change, `baz` now requires an arg

a = ( # type: ignore[call-arg]
    "something" 
    if baz() > 0 else 
    foo()
)  # formatters (eg. black) often put ignore here

if it allowed the comment on any line in the statement, now we get no new error. the type:ignore that was intended to only suppress the error on foo now also suppresses the new error as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-ignore # type: ignore comments
Projects
None yet
Development

No branches or pull requests

3 participants