GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-109413: Fix some trivial mypy nitpicks in libregrtest by AlexWaygood · Pull Request #109454 · python/cpython

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Sep 15, 2023 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This PR fixes various mypy nitpicks with Lib/test/libregrtest that require small changes to the existing code, but can all be resolved fairly trivially.

Currently, if you cd into Lib/test and then run mypy --config-file regrtest/mypy.ini, mypy reports 23 errors. This PR brings the number of errors reported down to 9.

  • Issue: libregrtest: Improve type hints; explore running mypy in CI #109413

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
pythongh-109413: Fix some trivial mypy nitpicks in libregrtest
95f2e95
@AlexWaygood AlexWaygood added the skip news label Sep 15, 2023
@AlexWaygood
AlexWaygood requested a review from vstinner September 15, 2023 14:49
@bedevere-app bedevere-app Bot added the awaiting review label Sep 15, 2023
@bedevere-app bedevere-app Bot mentioned this pull request Sep 15, 2023
libregrtest: Improve type hints; explore running mypy in CI #109413
Closed
AlexWaygood
AlexWaygood commented Sep 15, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Some explanatory comments:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/libregrtest/logger.py Outdated
Comment on lines +35 to +37
formatted_test_time = "%d:%02d:%02d" % (hours, mins, secs)

line = f"{test_time} {line}"
line = f"{formatted_test_time} {line}"

@AlexWaygood AlexWaygood Sep 15, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This variable has been renamed here because there's already a variable earlier in this function called test_time, and it has type float. Mypy enforces that a variable should only ever have one consistent type within any given scope, and here the name is reassigned to a str. We can fix the mypy complaints by using a new variable name for the str value, rather than reusing the test_time name

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 15, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

test_time is a bad name, in fact, it's just time relative to regrtest start time.

I suggest renaming test_time to log_time and the second variable to formatted_log_time.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AlexWaygood AlexWaygood Sep 15, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Good shout, I'll do the renames

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Sep 15, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In the past, I put "test" everywhere :-) "test" now means nothing, since everything is called "test" in libregrtest :-) That's why I renamed "runtest.py" to "single.py" and rename "runtest_mp.py" to "run_worker.py" ;-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood reacted with heart emoji
All reactions
  • ❤️ 1 reaction
Comment thread Lib/test/libregrtest/results.py
report.append(f'skipped={stats.skipped:,}')
report = ' '.join(report)
print(f"Total tests: {report}")
print(f"Total tests: {' '.join(report)}")

@AlexWaygood AlexWaygood Sep 15, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

There's the same issue here: report is a list earlier on in the function, but at this location, it's being assigned to a str. We could use a new variable name, or just do what I suggest here (make it an "anonymous variable")

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/libregrtest/single.py
Comment on lines -139 to +146
except support.ResourceDenied as msg:
except support.ResourceDenied as exc:
if not quiet and not pgo:
print(f"{test_name} skipped -- {msg}", flush=True)
print(f"{test_name} skipped -- {exc}", flush=True)
result.state = State.RESOURCE_DENIED
return
except unittest.SkipTest as msg:
except unittest.SkipTest as exc:
if not quiet and not pgo:
print(f"{test_name} skipped -- {msg}", flush=True)
print(f"{test_name} skipped -- {exc}", flush=True)

@AlexWaygood AlexWaygood Sep 15, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This passage was causing mypy to get very agitated:

libregrtest\single.py:150: error: Assignment to variable "msg" outside
except: block  [misc]
            msg = f"test {test_name} failed"
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~
libregrtest\single.py:152: error: Trying to read deleted variable "msg"
[misc]
                msg = f"{msg} -- {exc}"
                      ^~~~~~
libregrtest\single.py:152: error: Assignment to variable "msg" outside
except: block  [misc]
                msg = f"{msg} -- {exc}"
                      ^~~~~~~~~~~~~~~~~
libregrtest\single.py:153: error: Trying to read deleted variable "msg"
[misc]
            print(msg, file=sys.stderr, flush=True)
                  ^~~
libregrtest\single.py:160: error: Assignment to variable "msg" outside
except: block  [misc]
            msg = f"test {test_name} failed"
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~
libregrtest\single.py:162: error: Trying to read deleted variable "msg"
[misc]
                msg = f"{msg} -- {exc}"
                      ^~~~~~
libregrtest\single.py:162: error: Assignment to variable "msg" outside
except: block  [misc]
                msg = f"{msg} -- {exc}"
                      ^~~~~~~~~~~~~~~~~
libregrtest\single.py:163: error: Trying to read deleted variable "msg"
[misc]
            print(msg, file=sys.stderr, flush=True)
                  ^~~
libregrtest\single.py:176: error: Assignment to variable "msg" outside
except: block  [misc]
                msg = traceback.format_exc()
                      ^~~~~~~~~~~~~~~~~~~~~~
libregrtest\single.py:177: error: Trying to read deleted variable "msg"
[misc]
                print(f"test {test_name} crashed -- {msg}",
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Mypy was getting confused because the msg variable had type Exception here, but lower down in the same function (on line 150), the same variable name is assigned to a str. By renaming the variable here as exc, the exc variable is now always an Exception instance in this function, and the msg variable is now always a str in this function.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/libregrtest/worker.py
executable = python_cmd
else:
executable = [sys.executable]
executable = (sys.executable,)

@AlexWaygood AlexWaygood Sep 15, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Mypy complained about this line because on line 26, the executable variable has type tuple[str, ...], but here, the same name is being assigned to an object of type list[str]. We can make mypy happy by just making it a tuple in both the branches.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
dcollison
dcollison approved these changes Sep 15, 2023
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Sep 15, 2023
vstinner
vstinner approved these changes Sep 15, 2023
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/libregrtest/logger.py Outdated
Comment on lines +35 to +37
formatted_test_time = "%d:%02d:%02d" % (hours, mins, secs)

line = f"{test_time} {line}"
line = f"{formatted_test_time} {line}"

@vstinner vstinner Sep 15, 2023

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

test_time is a bad name, in fact, it's just time relative to regrtest start time.

I suggest renaming test_time to log_time and the second variable to formatted_log_time.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
test_time -> log_time
24ec358
@AlexWaygood
AlexWaygood enabled auto-merge (squash) September 15, 2023 16:35
@AlexWaygood
AlexWaygood merged commit 92ed7e4 into python:main Sep 15, 2023
@AlexWaygood
AlexWaygood deleted the regrtest-confusing-vars branch September 15, 2023 17:01
@bedevere-app bedevere-app Bot removed the awaiting core review label Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
+1 more reviewer
@dcollison dcollison dcollison approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@AlexWaygood @vstinner @dcollison
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.