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

Conversation

@brandtbucher
Copy link
Contributor

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 #6648. See the recent discussion at #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

This comment has been minimized.

Copy link
Contributor Author

commented May 6, 2019

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

@gvanrossum

This comment has been minimized.

Copy link
Member

commented May 6, 2019

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

@brandtbucher

This comment has been minimized.

Copy link
Contributor Author

commented May 6, 2019

@gvanrossum Merged, tests are passing.

@gvanrossum
Copy link
Member

left a comment

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.

This comment has been minimized.

Copy link
@gvanrossum

gvanrossum May 6, 2019

Member

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!

This comment has been minimized.

Copy link
@gvanrossum

gvanrossum May 6, 2019

Member

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

This comment has been minimized.

Copy link
@brandtbucher

brandtbucher May 6, 2019

Author Contributor

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:

This comment has been minimized.

Copy link
@gvanrossum

gvanrossum May 6, 2019

Member

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

This comment has been minimized.

Copy link
@gvanrossum

gvanrossum May 6, 2019

Member

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

This comment has been minimized.

Copy link
@brandtbucher

brandtbucher May 6, 2019

Author Contributor

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.

This comment has been minimized.

Copy link
@gvanrossum

gvanrossum May 6, 2019

Member

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

This comment has been minimized.

Copy link
@brandtbucher

brandtbucher May 6, 2019

Author Contributor

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

This comment has been minimized.

Copy link
Contributor Author

commented May 6, 2019

New changes are passing, ready for another review.

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

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@gvanrossum

This comment has been minimized.

Copy link
Member

commented May 7, 2019

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

@brandtbucher

This comment has been minimized.

Copy link
Contributor Author

commented May 7, 2019

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

@brandtbucher brandtbucher deleted the brandtbucher:def-lines branch May 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.