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

ci: replace Python linter flake8 with ruff #3302

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

14 changes: 14 additions & 0 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# https://beta.ruff.rs
name: Lint Python
on:
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@cclauss cclauss Apr 12, 2023

Choose a reason for hiding this comment

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

This is a bad practice when tools are fast.

build git:(ruff) % time ruff .

ruff .  0.01s user 0.03s system 74% cpu 0.065 total

Ruff will lint the entire CPython codebase in 0.29 seconds with no cache so we are saving no time by attempting to hide files from the linter and could easily miss faults in code added to this repo in the future.

Copy link
Member

Choose a reason for hiding this comment

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

It's not about tool speed, it's about not running jobs that can have unrelated job failures on a PR

push:
branches: [main]
pull_request:
branches: [main]
jobs:
lint_python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pip install --user ruff
- run: ruff --format=github .
24 changes: 0 additions & 24 deletions .github/workflows/push.yml

This file was deleted.

3 changes: 2 additions & 1 deletion ansible/plugins/inventory/nodejs_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from os import path

import yaml

try:
import configparser
except ImportError:
Expand Down Expand Up @@ -278,7 +279,7 @@ def parse_yaml(hosts, config):
def parse_host(host):
"""Parses a host and validates it against our naming conventions"""

hostinfo = dict()
hostinfo = {}
info = host.split('-')

expected = ['type', 'provider', 'os', 'arch', 'uid']
Expand Down
4 changes: 3 additions & 1 deletion ansible/plugins/library/remmina_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import base64
import os

from ansible.module_utils.basic import AnsibleModule
from Crypto.Cipher import DES3
from jinja2 import Environment

from ansible.module_utils.basic import AnsibleModule

try:
import configparser # Python 3
except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion ansible/plugins/library/ssh_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import os
import re

from ansible.module_utils.basic import AnsibleModule
from jinja2 import Environment

from ansible.module_utils.basic import AnsibleModule

pre_match = '# begin: node.js template'
post_match = '# end: node.js template'
Expand Down
5 changes: 3 additions & 2 deletions ansible/www-standalone/tools/metrics/country-lookup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python

import sys
import csv
import geoip2.database
import os
import sys

import geoip2.database

reader = geoip2.database.Reader(os.path.dirname(os.path.realpath(__file__)) + '/GeoLite2-City.mmdb')

Expand Down
52 changes: 52 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[tool.ruff]
select = [
"A", # flake8-builtins
"ARG", # flake8-unused-arguments
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"C90", # McCabe cyclomatic complexity
"E", # pycodestyle
"EM", # flake8-errmsg
"EXE", # flake8-executable
"F", # Pyflakes
"G", # flake8-logging-format
"I", # isort
"ICN", # flake8-import-conventions
"INT", # flake8-gettext
"ISC", # flake8-implicit-str-concat
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # Pylint
"PT", # flake8-pytest-style
"PYI", # flake8-pyi
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # Ruff-specific rules
"S", # flake8-bandit
"SIM", # flake8-simplify
"SLF", # flake8-self
"T10", # flake8-debugger
"TCH", # flake8-type-checking
"TID", # flake8-tidy-imports
"W", # pycodestyle
"YTT", # flake8-2020
]
ignore = [
"B904",
Copy link
Member

Choose a reason for hiding this comment

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

Are this ignores TODOs or forever things? Sometimes useful to document why/how the might be fixed if someone has an interest to try and address them later

Copy link
Contributor Author

@cclauss cclauss Apr 12, 2023

Choose a reason for hiding this comment

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

I will let others determine if/when they want to reduce the ignore list

Rule information is easy to acquire: ruff rule B904

raise-without-from-inside-except (B904)

Derived from the flake8-bugbear linter.

Message formats:

  • Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

"PLR2004",
"RET505",
"RUF005",
"S110",
"S701"
]
line-length = 223
Copy link
Member

Choose a reason for hiding this comment

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

Guessing this was to get it passing, rather than a preferred line length like the old 250 value

Copy link
Contributor Author

@cclauss cclauss Apr 12, 2023

Choose a reason for hiding this comment

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

88 is the preferred value for Python code. The goal here is to set high water marks so that if a contributor wants to go beyond that level they will need to justify their actions in the code reviews of their pull requests. The same is true of max-args, max-branches, max-complexity, etc.

target-version = "py37"
Copy link
Member

Choose a reason for hiding this comment

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

If the target is going to stay 3.7 for now, maybe the CI should keep the pinned version that I dropped in the other PR

Copy link
Contributor Author

@cclauss cclauss Apr 12, 2023

Choose a reason for hiding this comment

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

See #3301 (comment)

Ruff does not run on Python so it changes in similar ways to flake8 but only based on --target-version=pyXX.

Just like Node.js, Python has a release cycle: https://devguide.python.org/versions

Py37 will reach EOL in two months.


[tool.ruff.mccabe]
max-complexity = 15

[tool.ruff.pylint]
max-args = 5
max-branches = 15
max-returns = 7
max-statements = 50