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 trailing whitespace without newline at EOF #611

Merged
merged 1 commit into from
Jan 18, 2022

Conversation

zsol
Copy link
Member

@zsol zsol commented Jan 17, 2022

Summary

The native tokenizer was a bit too strict about indentation checking at EOF. This is valid Python: if 1:\n pass\n (note the extra space at the end without a newline), but the tokenizer was trying to match the single space at the end as if it was a dedent from level 4 -> 1, which failed (there was never a level 1 indentation).

This PR relaxes the tokenizer a bit to allow for such cases. Indentation checking is skipped if after consuming all whitespace at the beginning of the line, we encounter EOF.

Fixes #608

Test Plan

Added unit test.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 17, 2022
@zsol
Copy link
Member Author

zsol commented Jan 17, 2022

cc @bgw AFAICT this was a bug in your original implementation as well

@zsol
Copy link
Member Author

zsol commented Jan 17, 2022

CPython pure python tokenizer does the same: https://github.com/python/cpython/blob/main/Lib/tokenize.py#L495-L496

@codecov-commenter
Copy link

Codecov Report

Merging #611 (3bb68a8) into main (332710d) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #611   +/-   ##
=======================================
  Coverage   94.77%   94.77%           
=======================================
  Files         245      245           
  Lines       25140    25140           
=======================================
  Hits        23826    23826           
  Misses       1314     1314           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 332710d...3bb68a8. Read the comment docs.

Copy link
Contributor

@stroxler stroxler left a comment

Choose a reason for hiding this comment

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

Looks good to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Native parser does not allow indentation mismatch at EOF
4 participants