Skip to content

Compatibility: pycode parser: adjust dedent-token handling to maintain py3.12+ compatibility #11440

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
wants to merge 8 commits into from
Closed

Compatibility: pycode parser: adjust dedent-token handling to maintain py3.12+ compatibility #11440

wants to merge 8 commits into from

Conversation

jayaddison
Copy link
Contributor

Feature or Bugfix

  • Bugfix

Purpose

  • Resolves test failures in Python code parsing (particularly for named definitions within indented code blocks).

Detail

  • From version 3.12.0a7 of Python, the line positions included on DEDENT tokens (encountered during Sphinx's Python code parsing logic, to determine the line-positioning of definitions) are always within the bounds of the number of lines in the source file.
  • This means that we should not decrement that line position when we encounter it at the end-of-file for version 3.12+ of Python.

Relates

Thank you, @mgmacias95 for guidance on the fix.

Comment on lines 531 to 534
if end_pos == len(self.buffers) + 1 and end_col == 0:
return True
if end_col < len(self.get_line(end_pos)):
return True
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first of these two conditional if blocks handles implicit dedent tokens that occur when the end-of-file is reached (because any open code blocks should be closed at that point).

I'm less confident in explaining what the second conditional is intended to handle - and it may be possible to simplify the logic used for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The second condition catches dedent tokens that are contained on the same line as some code. For example, similar to the 'compact' test code coverage added:

class A:
    def b():
        pass
def c():
    pass

... we want the definition of b to omit the line that c is on. It's a non-empty line, and it emits a dedent token, and it contains a name token.

@picnixz
Copy link
Member

picnixz commented May 28, 2023

I think the issue is now solved with python/cpython#105030. At least my latest PR does not fail on 3.12.

@jayaddison
Copy link
Contributor Author

Thanks @picnixz, glad to hear that the tests are passing again.

I had a read of that thread, was slightly puzzled because it affected a different platform to our test failures, and then through a linked pycodestyle discussion, landed at python/cpython#104980 - which I think was likely the resolution (I could be mistaken.. but from the description and the way that relates to my understanding of the problem here, it fits).

Either way; closing this as unnecessary.

@jayaddison jayaddison closed this May 28, 2023
@jayaddison jayaddison deleted the issue-11436/py312-tokenizer-compatibility branch May 28, 2023 19:17
@picnixz
Copy link
Member

picnixz commented May 28, 2023

Do you want to leave the issue open? (maybe for confirmation?)

@jayaddison
Copy link
Contributor Author

I think we can close it, unless it begins occurring again. Asking for confirmation seems like a good sanity-check though 👍

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test failures: Python 3.12-dev: pycode.parser.DefinitionFinder line numbering assertion failures
2 participants