Skip to content

type: ignore needs to be placed partway through a multi-line variable assigment in some cases #3251

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

Closed
timabbott opened this issue Apr 26, 2017 · 6 comments

Comments

@timabbott
Copy link

timabbott commented Apr 26, 2017

Usually, mypy does this correctly, but today I had to merge zulip/zulip@c4716ae because mypy was throwingreporting an error (caused by an unrelated typeshed bug) that had been suppressed by that type: ignore, but stopped being when we line-wrapped the assignment.

We're using mypy 0.501.

@ilevkivskyi
Copy link
Member

This looks quite similar to #3177, but probably not an exact duplicate. Here it is less clear what is the most obvious line for mypy to report an error. I think the current behaviour is OK. Maybe we should just document some rules for line numbers in errors in multi-line statements/expressions?

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 26, 2017

Yeah, this is a known issue, and doesn't just affect assignments. Do you have a specific improvement in mind? It's not obvious to me what would a generally better way to do this.

@gvanrossum
Copy link
Member

I think it's different from #3177. That issue is specifically about everything related to a function heading being attributed to the line where the first decorator starts.

This issue is about whether the scope of a type-ignore on a statement that's split across multiple lines should be the entire statement rather than the specific line number on which the type-ignore occurs. I think it's an implementation weakness but it may be hard to fix, since AFAIK the typed_ast parser just gives us line numbers for type-ignores (rather than working them into the parse tree).

Either typed_ast or mypy would need a significant re-architecting of the code around type-ignores to fulfill this wish. Also, sometimes it's a useful feature, e.g. when you have a long list of expressions like this:

handlers = [
    foo,
    bar,  # type: ignore # There's something funny about bar
    baz,
]

@ddfisher ^^

@ddfisher
Copy link
Collaborator

ddfisher commented Apr 26, 2017

AFAIK the typed_ast parser just gives us line numbers for type-ignores (rather than working them into the parse tree)

Yeah, that's the current behavior. That's because type ignores don't really fit into the parse tree in a particularly sensible way -- they're valid anywhere. If this is something we want to fix, changing that is probably not the way forward -- I think it'd be easier to understand the start and end points of statements, and filter errors that way.

@brandtbucher
Copy link
Member

#6648 fixed this, I think. At least for Python 3.8.

@gvanrossum
Copy link
Member

Yup!

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

No branches or pull requests

6 participants