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

gh-131507: Clean up tests and type checking for _pyrepl #131509

Merged
merged 6 commits into from
Mar 21, 2025

Conversation

ambv
Copy link
Contributor

@ambv ambv commented Mar 20, 2025

This is a prerequisite for the syntax highlighting change, but a separate PR so that this can be backported to 3.13 for ease of maintenance in the future.

The separate commits explain the separate steps:

  • remove a hack for zero-width escape code bracketing
  • refactor tests to allow assert_screen_equal in more places
  • make _colorize better: enumerate all ANSI colors, make type checking pass
  • upgrade mypy checks to use 3.13 and allow using Lib/_colorize.py in the checks (via a Misc/mypy/ trick with symlinks, let's see how that works out)

ambv added a commit to ambv/cpython that referenced this pull request Mar 20, 2025
Copy link
Contributor

@StanFromIreland StanFromIreland 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 in general, minor nit in docstring

Comment on lines +426 to +428
The prompt string has the zero-width brackets recognized by shells
(\x01 and \x02) removed. The length ignores anything between those
brackets as well as any ANSI escape sequences.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The prompt string has the zero-width brackets recognized by shells
(\x01 and \x02) removed. The length ignores anything between those
brackets as well as any ANSI escape sequences.
The prompt string has the zero-width brackets (\x01 and \x02)
recognized by shells removed. The length ignores anything between
those brackets as well as any ANSI escape sequences.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this an improvement? There are many kinds of zero-width brackets, the ones I am interested in are those recognized by shells (\x01 and \x02). The original docstring uses that ordering then.

return length - sum(len(i) for i in sequence) + ctrl_z_cnt


def unbracket(s: str, including_content: bool = False) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

👍 I like this approach. I was concerned about the regular expressions performance but couldn't find anything too bad when testing



def multiline_input(reader: ReadlineAlikeReader, namespace: dict | None = None):
saved = reader.more_lines
try:
reader.more_lines = partial(more_lines, namespace=namespace)
reader.ps1 = reader.ps2 = ">>>"
reader.ps3 = reader.ps4 = "..."
reader.ps1 = reader.ps2 = ">>> "
Copy link
Member

Choose a reason for hiding this comment

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

Why the extra space?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because that's how the real prompts are in production. I didn't want the difference in tests since it was confusing for syntax highlighting later.

@@ -421,42 +421,15 @@ def calc_screen(self) -> list[str]:

@staticmethod
def process_prompt(prompt: str) -> tuple[str, int]:
Copy link
Member

Choose a reason for hiding this comment

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

I may be missing something but looks like the old function was tracking nested ANSI escape seas outside \x01 and \x02 brackets but this one drops this no? What is the rationale?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are three things the old function was trying to do (badly):

  • ignore anything between \001 and \002 for length calculation; and
  • remove \001 and \002 characters from the emitted prompt; and
  • also ignore any ANSI escape sequences from length calculation.

Since wlen() is already doing 3., a contributor here needed to recreate this function to not do that, because we were doing that later anyway. But it's simpler to just use the main wlen() with a string stripped of \001 .. \002 bracketed content.

I find the new form much easier on the eyes.

Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

LGTM left some comments

@ambv ambv merged commit 5d8e981 into python:main Mar 21, 2025
51 checks passed
@miss-islington-app
Copy link

Thanks @ambv for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @ambv, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 5d8e981c8477ce483374b2fe6cd309a08c956299 3.13

ambv added a commit to ambv/cpython that referenced this pull request Mar 21, 2025
pythonGH-131509)

(cherry picked from commit 5d8e981)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@bedevere-app
Copy link

bedevere-app bot commented Mar 21, 2025

GH-131546 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Mar 21, 2025
ambv added a commit to ambv/cpython that referenced this pull request Mar 21, 2025
@ambv ambv deleted the pyrepl-sh-refactor-tests branch March 21, 2025 14:59
ambv added a commit that referenced this pull request Mar 21, 2025
ambv added a commit that referenced this pull request Mar 21, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 21, 2025
…thonGH-131547)

This is based off pythonGH-131509.
(cherry picked from commit 4cc82ff)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
ambv added a commit that referenced this pull request Mar 21, 2025
…H-131547) (GH-131557)

gh-131507: Refactor screen and cursor position calculations (GH-131547)

This is based off GH-131509.
(cherry picked from commit 4cc82ff)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants