Skip to content

Commit

Permalink
Migrate code quality tools to Ruff (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Oct 12, 2023
1 parent 63e8216 commit 17eea9a
Show file tree
Hide file tree
Showing 49 changed files with 298 additions and 384 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ docs/_build/
.vscode
/temp/
docs/dev/reference/
.flakeheaven_cache/
.ruff_cache/
65 changes: 9 additions & 56 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.1
rev: v3.0.3
hooks:
- id: prettier
types_or:
Expand All @@ -25,12 +25,6 @@ repos:
name: check-hooks-apply
- id: check-useless-excludes
name: check-useless-excludes
- repo: https://github.com/asottile/pyupgrade
rev: v3.10.1
hooks:
- id: pyupgrade
args:
- --py37-plus
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
Expand All @@ -39,82 +33,41 @@ repos:
exclude: tests/test_unit/(test_po2md/translate-examples/code-blocks.*|test_event\.py)
- id: end-of-file-fixer
name: end-of-file-fixer
- id: double-quote-string-fixer
name: double-quote-string-fixer
- id: debug-statements
name: debug-statements
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.1
hooks:
- id: remove-crlf
files: \.bat$
name: "*.bat end of lines"
- repo: https://github.com/asottile/add-trailing-comma
rev: v3.0.1
hooks:
- id: add-trailing-comma
name: add-trailing-comma
- repo: https://github.com/flakeheaven/flakeheaven
rev: 3.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
hooks:
- id: flakeheaven
additional_dependencies:
- flake8-builtins
- flake8-comprehensions
- flake8-docstrings
- flake8-executable
- flake8-implicit-str-concat
- flake8-print
- flake8-printf-formatting
- flake8-pytest-style
- flake8-bugbear
- flake8-encodings
- flake8-no-pep420
- flake8-absolute-import
- flake8-slots
- flake8-unused-arguments
- dlint
- pysetenv
entry: pysetenv FLAKEHEAVEN_CACHE_TIMEOUT=0 flakeheaven lint
- id: ruff
args:
- --fix
- --exit-non-zero-on-fix
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.2
rev: v2.0.4
hooks:
- id: autopep8
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/adrienverge/yamllint
rev: v1.32.0
hooks:
- id: yamllint
args:
- -c
- .yamllint.yaml
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
rev: 2.7.2
hooks:
- id: editorconfig-checker
name: editorconfig-checker
alias: ec
- repo: https://github.com/myint/autoflake
rev: v2.2.0
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- --remove-unused-variables
- --remove-duplicate-keys
- --ignore-init-module-imports
- repo: https://github.com/tcort/markdown-link-check
rev: v3.11.2
hooks:
- id: markdown-link-check
name: markdown-link-check
files: ^README\.md$
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.8.1
rev: v0.10.0
hooks:
- id: markdownlint-cli2
name: markdownlint-po2md-tests
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
' = ',
)[1].strip().strip('"').strip("'")
elif line.startswith('authors ='):
metadata['author'] = re.search("name = \"([^\"]+)\"", line).group(1)
metadata['author'] = re.search('name = \"([^\"]+)\"', line).group(1)
elif not line:
break

Expand Down
157 changes: 78 additions & 79 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ targets = [{ file = "pyproject.toml" }, { file = "docs/pre-commit-hooks.rst" }]
[tool.project-config]
cache = "2 days"
style = [
"gh://mondeja/[email protected]/python/base.json5",
"gh://mondeja/[email protected]/python/single-quotes.json5",
"gh://mondeja/[email protected]/python/tests.json5",
"gh://mondeja/[email protected]/python/sphinx.json5",
"gh://mondeja/[email protected]/python/readthedocs.json5",
"gh://mondeja/[email protected]/python/google-docstrings.json5"
"gh://mondeja/project-config-styles@v5/python/base.json5",
"gh://mondeja/project-config-styles@v5/python/sphinx.json5",
"gh://mondeja/project-config-styles@v5/python/readthedocs.json5",
]

[tool.pytest.ini_options]
Expand All @@ -132,81 +129,62 @@ exclude_lines = [
"if '-h' in args or '--help' in args:"
]

[tool.flakeheaven]
inline-quotes = "single"
max-line-length = 80
pytest-fixture-no-parentheses = true
pytest-parametrize-values-type = "tuple"
docstring-convention = "google"
unused-arguments-ignore-abstract-functions = true

[tool.flakeheaven.plugins]
pycodestyle = ["+*", "-W503"]
pyflakes = ["+*"]
pylint = ["+*"]
flake8-builtins = ["+*"]
flake8-comprehensions = ["+*"]
flake8-docstrings = ["+*", "-D107", "-D105"]
flake8-executable = ["+*"]
flake8-implicit-str-concat = ["+*"]
flake8-print = ["+*"]
flake8-printf-formatting = ["+*"]
flake8-pytest-style = ["+*"]
flake8-bugbear = ["+*"]
flake8-encodings = ["+*"]
flake8-no-pep420 = ["+*"]
flake8-absolute-import = ["+*"]
flake8-unused-arguments = ["+*"]
flake8-slots = ["+*"]
dlint = ["+*"]

[tool.flakeheaven.exceptions."docs/**"]
flake8-print = ["-T201"]
flake8-docstrings = ["-D100", "-D101", "-D102"]
flake8-no-pep420 = ["-INP001"]

[tool.flakeheaven.exceptions."tests/**"]
flake8-docstrings = [
"-D100",
"-D101",
"-D102",
"-D103",
"-D104",
"-D107",
"-D205",
"-D415"
[tool.ruff]
line-length = 80
target-version = "py37"
select = [
"W",
"B",
"E",
"I",
"F",
"A",
"D",
"G",
"Q",
"PL",
"UP",
"PT",
"C4",
"EXE",
"ISC",
"T20",
"INP",
"ARG",
"SIM",
"RET",
"FBT",
"ERA",
"T10",
"COM",
"SLOT",
]
flake8-no-pep420 = ["-INP001"]

[tool.flakeheaven.exceptions."setup.py"]
flake8-docstrings = ["-D205"]
flake8-no-pep420 = ["-INP001"]

[tool.flakeheaven.exceptions."src/**/*.py"]
flake8-docstrings = ["-D101", "-D102", "-D103", "-D107"]

[tool.flakeheaven.exceptions."src/md.py"]
flake8-docstrings = ["-D101", "-D102", "-D107"]

[tool.isort]
lines_after_imports = 2
multi_line_output = 3
line_length = 79
use_parentheses = true
combine_as_imports = true
include_trailing_comma = true
remove_redundant_aliases = true
known_tests = "tests"
sections = [
"FUTURE",
"STDLIB",
"THIRDPARTY",
"FIRSTPARTY",
"TESTS",
"LOCALFOLDER"
ignore = [
"PLR0911",
"PLR0912",
"PLR0915",
"PLR0913",
"FBT002",
]
py_version = 37
extra_standard_library = [

[tool.ruff.pydocstyle]
convention = "google"

[tool.ruff.flake8-quotes]
inline-quotes = "single"
multiline-quotes = "single"

[tool.ruff.flake8-pytest-style]
fixture-parentheses = false
parametrize-values-type = "tuple"
parametrize-values-row-type = "tuple"

[tool.ruff.isort]
lines-after-imports = 2
combine-as-imports = true
force-wrap-aliases = true
known-local-folder = ["tests"]
extra-standard-library = [
"contextvars",
"dataclasses",
"importlib.resources",
Expand All @@ -217,6 +195,27 @@ extra_standard_library = [
"wsgiref.types"
]

[tool.ruff.per-file-ignores]
"tests/**" = [
"I002",
"D100",
"D101",
"D102",
"D103",
"D104",
"D107",
"D205",
"D415",
"INP001",
"PLR0913",
"PLR2004",
]
"setup.py" = ["D205", "INP001", "I002"]
"src/**/*.py" = ["D101", "D102", "D103", "D107"]
"src/md.py" = ["D101", "D102", "D107"]
"docs/conf.py" = ["INP001"]
"tests/test_unit/test_event.py" = ["E501", "ISC003"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
5 changes: 3 additions & 2 deletions src/mdpo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""mdpo package."""


func_package_map = {
'markdown_pofile_to_html': 'mdpo2html',
'markdown_to_pofile': 'md2po',
'markdown_to_pofile_to_markdown': 'md2po2md',
'pofile_to_markdown': 'po2md',
}

__all__ = list(func_package_map.keys())
__all__ = list(func_package_map) # noqa: PLE0605 (not implemented by Ruff)


def __getattr__(name):
Expand All @@ -21,7 +22,7 @@ def __getattr__(name):
)
except KeyError:
raise ImportError(
f'cannot import name \'{name}\' from \'mdpo\' ({__file__})',
f"cannot import name '{name}' from 'mdpo' ({__file__})",
name=name,
path='mdpo',
) from None
18 changes: 9 additions & 9 deletions src/mdpo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
CLOSE_QUOTE_CHAR = '”' if SPHINX_IS_RUNNING else '"'


def cli_codespan(value, cli=True, sphinx=True):
def cli_codespan(value, cli=True):
"""Command line codespan wrapper.
This is a compatibility function to make CLI codespans looks good in
Expand All @@ -25,12 +25,12 @@ def cli_codespan(value, cli=True, sphinx=True):
Args:
value (str): Value to wrap.
cli (bool): Wrap when used from command line.
sphinx (bool): Wrap when used from Sphinx.
"""
if SPHINX_IS_RUNNING:
return f'``{value}``' if sphinx else value
else:
return f'\'{value}\'' if cli else value
return f'``{value}``'
if cli: # pragma: no cover
return f"'{value}'"
return value


def parse_escaped_pairs_cli_argument(
Expand Down Expand Up @@ -213,7 +213,7 @@ def add_nolocation_option(parser):
"""
parser.add_argument(
'--no-location', '--nolocation', dest='location', action='store_false',
help='Do not write \'#: filename:line\' lines. Note that using this'
help="Do not write '#: filename:line' lines. Note that using this"
' option makes it harder for technically skilled translators to'
' understand the context of each message. Same as'
f' {cli_codespan("gettext --no-location")}.',
Expand Down Expand Up @@ -246,8 +246,8 @@ def add_encoding_arguments(
po_encoding_help = (
'PO files encoding. If you need different encodings for each'
' file, you must define them in the "Content-Type" field of each'
' PO file metadata, in the form \'Content-Type: text/plain;'
' charset=<ENCODING>\'.'
" PO file metadata, in the form 'Content-Type: text/plain;"
" charset=<ENCODING>'."
) if po_encoding_help is None else po_encoding_help
parser.add_argument(
'--po-encoding', dest='po_encoding', default=None, metavar='ENCODING',
Expand Down Expand Up @@ -296,7 +296,7 @@ def add_wrapwidth_argument(
else:
to_render = 'the Markdown output, when possible'
kwargs['help'] = (
f'Maximum width rendering {to_render}. If negative, \'0\' or \'inf\','
f"Maximum width rendering {to_render}. If negative, '0' or 'inf',"
' the content will not be wrapped.'
)
parser.add_argument(*args, **kwargs)
Expand Down
Loading

0 comments on commit 17eea9a

Please sign in to comment.