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

python3 setup.py lint is deprecated: Let's lint with pre-commit #4200

Merged
merged 1 commit into from
Apr 14, 2024

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Apr 7, 2024

Running python3 setup.py lint is deprecated. https://packaging.python.org/en/latest/discussions/setup-py-deprecated

The setup.py logic for running black and clang-format is ~100 lines long and is skipping many Python and C files.

pygame/setup.py

Lines 871 to 964 in 9cb30af

class LintFormatCommand(Command):
""" Used for formatting or linting. See Lint and Format Sub classes.
"""
user_options = []
lint = False
format = False
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
"""Check the existence and launch linters."""
import subprocess
import sys
import warnings
import pathlib
def check_linter_exists(linter):
if shutil.which(linter) is None:
msg = "Please install '%s' in your environment. (hint: 'python3 -m pip install %s')"
warnings.warn(msg % (linter, linter))
sys.exit(1)
def filter_files(path_obj, all_files, allowed_files, disallowed_files):
files = []
for file in all_files:
for disallowed in disallowed_files:
if file.match(str(path_obj / disallowed)):
break
else: # no-break
files.append(str(file))
continue
for allowed in allowed_files:
if file.match(str(path_obj / allowed)):
files.append(str(file))
break
return files
path_obj = pathlib.Path(path, "src_c")
c_files_unfiltered = path_obj.glob("**/*.[ch]")
c_file_disallow = [
"_sdl2/**",
"pypm.c",
"SDL_gfx/**",
"**/sse2neon.h",
"doc/**",
"_sprite.c",
]
c_file_allow = ["_sdl2/touch.c"]
c_files = filter_files(path_obj, c_files_unfiltered, c_file_allow, c_file_disallow)
# Other files have too many issues for now. setup.py, buildconfig, etc
python_directories = ["src_py", "test", "examples"]
if self.lint:
commands = {
"clang-format": ["--dry-run", "--Werror", "-i"] + c_files,
"black": ["--check", "--diff"] + python_directories,
# Test directory has too much pylint warning for now
"pylint": ["src_py"],
}
else:
commands = {
"clang-format": ["-i"] + c_files,
"black": python_directories,
}
formatters = ["black", "clang-format"]
for linter, option in commands.items():
print(" ".join([linter] + option))
check_linter_exists(linter)
result = subprocess.run([linter] + option)
if result.returncode:
msg = f"'{linter}' failed."
msg += " Please run: python setup.py format" if linter in formatters else ""
msg += f" Do you have the latest version of {linter}?"
raise SystemExit(msg)
@add_command("lint")
class LintCommand(LintFormatCommand):
lint = True
@add_command("format")
class FormatCommand(LintFormatCommand):
format = True

This pull request proposes using pre-commit with ~25 lines of configuration to rapidly run these two tools locally on contributors' machines and also in our GitHub Actions to cover any contributors who do not have pre-commit installed.

The 15 exclude lines should be removed gradually as more of the codebase passes the formatting and linting process.

How to use:

brew install pre-commit  # or `python3 -m pip install pre-commit`

pre-commit install
    > pre-commit installed at .git/hooks/pre-commit

pre-commit autoupdate
    > [https://github.com/psf/black-pre-commit-mirror] already up to date!
    > [https://github.com/pre-commit/mirrors-clang-format] already up to date!

pre-commit run --all-files
    > black....................................................................Passed
    > clang-format.............................................................Passed

Copy link
Member

@illume illume left a comment

Choose a reason for hiding this comment

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

👍 thanks

@illume illume merged commit 9f6523d into pygame:main Apr 14, 2024
17 checks passed
@cclauss cclauss deleted the patch-2 branch April 14, 2024 14:49
krobinson395 pushed a commit to henserobbie/pygame that referenced this pull request Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants