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

Fix class/def line reporting and ignoring for 3.8+. #6753

Merged
merged 4 commits into from
May 7, 2019
Merged

Fix class/def line reporting and ignoring for 3.8+. #6753

merged 4 commits into from
May 7, 2019

Conversation

brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented May 1, 2019

Fixes #3871. This change fixes a few things concerning class/function definition line numbers.

For users running mypy with Python 3.8+:

  • Function definitions are now reported on the correct line, rather than the line of the first decorator plus the number of decorators.
  • Class definitions are now reported on the correct line, rather than the line of the first decorator.
  • These changes are fully backward compatible with respect to # type: ignore lines. In other words, existing comments will not need to be moved. This is accomplished by defining an ignore "scope" just like we added to expressions with Expression-scoped ignores in Python 3.8. #6648. See the recent discussion at Function line number information is not always correct #3871 for reasoning.

For other users:

  • Class definition lines are reported by using the same decorator-offset estimate as functions. This is both more accurate, and necessary to get 15 or so tests to pass for both pre- and post-3.8 versions. This seems fine, since there doesn't appear to be a compelling reason why it was different in the first place.
  • This change is also backward-compatible with existing ignores, as above.

About 15 tests had to have their error messages / nodes moved down one line, and 4 new regression tests have been added to check-38.test.

@brandtbucher
Copy link
Member Author

Bump. Hopefully this doesn't get too stale from all of the new sprint PRs!

@gvanrossum
Copy link
Member

Alas, it's already stale. Could you merge the conflicts?

@brandtbucher
Copy link
Member Author

@gvanrossum Merged, tests are passing.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting close, but I have a couple of questions, since this looks like it will be fairly invasive...

# the opposite end of the range to search for "# type: ignore"
# comments in. This reports the correct line in the event of an
# error, but keeps old pre-3.8 "# type: ignore" comments working
# correctly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment confused me. The key thing to note here is that we want to point lineno to the line that has the def keyword, while we want to point end_lineno to the line that would have been used for Python 3.7 and before. And we must compute end_lineno (i.e. the "old start line") using the crufty way given here since the old way of computing it (i.e. n.lineno + len(n.decorator_list) won't have the same result in 3.8.

Sorry that this is still so confusing. The code is right!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you going to update these comments? I hope so!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to reword this to be more concise. Maybe something like:

# Set end_lineno to the old pre-3.8 lineno, in order to keep
# existing "# type: ignore" comments working:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good -- also for the other location!

# comments in. This reports the correct line in the event of an
# error, but keeps old pre-3.8 "# type: ignore" comments working
# correctly.
lineno = n.lineno
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you even need this? It seems this was already set on line 432 above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I believe that's left over from an earlier iteration of this change.

# lineno is where the error is reported, and end_lineno defines the
# opposite end of the range to search for "# type: ignore" comments in.
# This reports the correct line in the event of an error, but keeps old
# pre-3.8 "# type: ignore" comments working correctly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment (except I wasn't really confused here, since I had just figured out the previous one :-).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above:

# Set end_lineno to the old mypy 0.700 lineno, in order to keep
# existing "# type: ignore" comments working:

test-data/unit/check-attr.test Show resolved Hide resolved
@brandtbucher
Copy link
Member Author

New changes are passing, ready for another review.

@gvanrossum gvanrossum merged commit 2f99e09 into python:master May 7, 2019
@gvanrossum
Copy link
Member

Thanks! Congratulations on getting this thorny PR in. Excellent work.

@brandtbucher
Copy link
Member Author

Thanks, that really means a lot coming from you. Always happy to help!

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

Successfully merging this pull request may close these issues.

Function line number information is not always correct
2 participants