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-60283: Check for redefined test names in CI #109161

Merged
merged 16 commits into from
Sep 12, 2023

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Sep 8, 2023

Fixes python/core-workflow#505.
Fixes #60283.

Add Ruff to the CI (via pre-commit) to check Lib/test/ for F811 "Redefinition of unused name". Ruff (47s) is faster than Flake8 (1m39s).

This will help find copy/pasted test_* method names, which results in one of them not being run.

It also finds redefined variables, which ISTR people were in favour of.

There are some files which fail to lint or parse, and have been excluded. Some of these are intentional and should never be checked (e.g. Lib/test/badsyntax_pep3120.py), some are due to new 3.12+ syntax not yet supported by the Ruff linter (e.g. Lib/test/test_type_aliases.py). I think we can be pragmatic and exclude any that add new syntax until such a time the linter supports the new version. (This may also help drive adoption of new syntax by the linter.)

This currently fails due to duplicated tests in test_monitoring, which will be fixed in #109139 and has been left as a demonstration in the meantime.

All other test_* duplicates have been fixed (see links in python/core-workflow#505).

Some files have duplicate variables, I've excluded those and they can be fixed and re-included in the future.

.ruff.toml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
Lib/test/.ruff.toml Outdated Show resolved Hide resolved
hugovk and others added 2 commits September 9, 2023 06:22
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! I guess we just have to wait for #109139 to be merged first?

@hugovk
Copy link
Member Author

hugovk commented Sep 10, 2023

Should we have changes to .pre-commit-config.yaml and .ruff.toml NOT trigger the main test build?

That would mean adding to this grep pattern:

git diff --name-only origin/$GITHUB_BASE_REF.. | grep -qvE '(\.rst$|^Doc|^Misc)' && echo "run_tests=true" >> $GITHUB_OUTPUT || true

@AlexWaygood
Copy link
Member

Should we have changes to .pre-commit-config.yaml and .ruff.toml NOT trigger the main test build?

That sounds like a good idea to me!

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@hugovk hugovk enabled auto-merge (squash) September 12, 2023 14:49
@hugovk hugovk added triaged The issue has been accepted as valid by a triager. and removed triaged The issue has been accepted as valid by a triager. labels Sep 12, 2023
@hugovk hugovk merged commit 3cb9a8e into python:main Sep 12, 2023
@hugovk hugovk deleted the pre-commit-ruff-F811 branch September 12, 2023 15:28
@AlexWaygood
Copy link
Member

🥳

@hugovk hugovk added needs backport to 3.11 only security fixes needs backport to 3.12 bug and security fixes labels Sep 13, 2023
@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
Contributor

Sorry, @hugovk, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 3cb9a8edca6e3fa0f0045b03a9a6444cf8f7affe 3.11

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 13, 2023
(cherry picked from commit 3cb9a8e)

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Sep 13, 2023

GH-109365 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Sep 13, 2023
hugovk added a commit to hugovk/cpython that referenced this pull request Sep 13, 2023
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
(cherry picked from commit 3cb9a8e)
@bedevere-app
Copy link

bedevere-app bot commented Sep 13, 2023

GH-109366 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Sep 13, 2023
@hugovk
Copy link
Member Author

hugovk commented Sep 13, 2023

For the backport PRs, I updated the exclude lists, as they were slightly different.

And interestingly, we don't need to explicitly exclude the "Failed to lint/parse" files because they're printed to screen but don't fail the command.

For example, these all have return code = 0 = success:

pre-commit run --all-files ruff
Run Ruff on Lib/test/....................................................Passed
pre-commit run --all-files ruff --verbose
Run Ruff on Lib/test/....................................................Passed
- hook id: ruff
- duration: 0.02s

error: Failed to parse Lib/test/tokenizedata/badsyntax_3131.py:2:1: Got unexpected token €
error: Failed to parse Lib/test/test_fstring.py:671:28: f-string: expecting '}'
warning: Failed to lint Lib/test/badsyntax_pep3120.py: stream did not contain valid UTF-8
warning: Failed to lint Lib/test/encoded_modules/module_koi8_r.py: stream did not contain valid UTF-8
warning: Failed to lint Lib/test/encoded_modules/module_iso_8859_1.py: stream did not contain valid UTF-8
error: Failed to parse Lib/test/support/socket_helper.py:300:33: f-string: expecting '}'
ruff Lib/test
warning: Failed to lint Lib/test/encoded_modules/module_iso_8859_1.py: stream did not contain valid UTF-8
warning: Failed to lint Lib/test/encoded_modules/module_koi8_r.py: stream did not contain valid UTF-8
warning: Failed to lint Lib/test/badsyntax_pep3120.py: stream did not contain valid UTF-8
error: Failed to parse Lib/test/support/socket_helper.py:300:33: f-string: expecting '}'
error: Failed to parse Lib/test/test_fstring.py:671:28: f-string: expecting '}'
error: Failed to parse Lib/test/tokenizedata/badsyntax_3131.py:2:1: Got unexpected token €

Shall we explicitly exclude them (which makes the verbose output cleaner, and in which case I'll update the backports) or keep the default behaviour (in which case I'll update main)?

@AlexWaygood
Copy link
Member

I think it's better to explicitly exclude the unlintable files, as we do on main. It's maybe slightly harder to maintain, but the improvement in UI is worth it if contributors don't see all those warnings printed to the terminal 👍

@erlend-aasland
Copy link
Contributor

Thanks for doing this!

vstinner pushed a commit to vstinner/cpython that referenced this pull request Sep 13, 2023
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Yhg1s pushed a commit that referenced this pull request Sep 14, 2023
…09365)

* gh-60283: Check for redefined test names in CI (GH-109161)
(cherry picked from commit 3cb9a8e)

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>

* Update exclude list for 3.12

* Explicitly exclude files which failed to lint/parse

* Sort to avoid future merge conflicts

---------

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
hugovk added a commit that referenced this pull request Sep 15, 2023
)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
(cherry picked from commit 3cb9a8e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect duplicate method names in CI list duplicate test names with patchcheck
6 participants