Skip to content

Commit e74aa66

Browse files
authored
Feature/614 move path filters to base config (#615)
* Add addition_to_excluded_paths and excluded_paths to BaseConfig * Move PTB Config to using BaseConfig for excluded_paths * Fix title to right capitalization * Fix test to use BaseConfig provided values instead * Make names more explicit as just for python files * Rename python_files to get_filtered_python_files * Rename fixture to match more closely to attribute name * Add directory found in other PTB projects * Alphabetize paths so less messy * Add test for test_excluded_python_paths and update description * Update cookiecutter template as now in BaseConfig * Add blurb in documentation for * Add changelog entry * Add type hint ignore * Remove empty test file * Fix old reference in migrating.rst * Fix changelog entry to sub-issue #614 * Skip get_poetry.py as not maintained by us, but added for convenience * Add tests for lint:security, lint:typing, lint:code
1 parent b36544d commit e74aa66

File tree

16 files changed

+223
-55
lines changed

16 files changed

+223
-55
lines changed

doc/changes/unreleased.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Unreleased
22

3+
This major release removes `project:fix` and `project:format`
4+
and replaces them with `format:fix` and `format:check`.
5+
36
## Refactoring
47

58
* #606: Renamed nox session `project:fix` more aptly to `format:fix` and `project:format` to `format:check`
9+
10+
## Feature
11+
12+
* #614: Replaced `path_filters` with `BaseConfig.add_to_excluded_python_paths` and `BaseConfig.excluded_python_paths`

doc/github_actions/github_actions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.. _github_actions:
22

3-
:octicon:`play` Github Actions
3+
:octicon:`play` GitHub Actions
44
===============================
55

66
.. toctree::

doc/user_guide/features/formatting_code/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ experience across projects.
1818
Nox sessions
1919
++++++++++++
2020

21+
.. note::
22+
To prevent Python files from being formatted, you can do one of the following:
23+
* For a single file, use a comment in the files as described in :ref:`this table <prevent_auto_format>`.
24+
* If it is a directory (i.e. ``.workspace``), then you can exclude it by
25+
adding it to the ``add_to_excluded_python_paths`` in the project's ``Config``
26+
defined in the ``noxconfig.py``.
27+
2128
For autoformatting, the following tools are used:
2229

2330
* `black <https://black.readthedocs.io/en/stable/the_black_code_style/index.html>`__ -

doc/user_guide/features/formatting_code/troubleshooting.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ you receive an error from ``format:check`` (i.e. ``isort`` or ``black``), it it
1515
likely that you need to update your configuration to align with
1616
:ref:`formatting_configuration`.
1717

18+
.. _prevent_auto_format:
19+
1820
The automatic formatting is doing x, but we shouldn't do that because of y
1921
---------------------------------------------------------------------------
2022
Usually, automatic formatting is helpful, but there are rare cases where a developer

doc/user_guide/migrating.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ For example, if test execution isn't performed in the standard way (e.g., :code:
7272
# ATTENTION:
7373
# In cases where it is reasonable to use "internal" functions, please do those imports
7474
# within the function to keep them isolated and simplify future removal or replacement.
75-
from exasol.toolbox.nox._shared import python_files
75+
from exasol.toolbox.nox._shared import get_filtered_python_files
7676
77-
py_files = [f"{file}" for file in python_files(PROJECT_CONFIG.root)]
77+
py_files = get_filtered_python_files(PROJECT_CONFIG.root)
7878
print("The original 'format:fix' task has been taken hostage by this overwrite")
7979
print("Files:\n{files}".format(files="\n".join(py_files))
8080

exasol/toolbox/config.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def valid_version_string(version_string: str) -> str:
2020

2121
ValidVersionStr = Annotated[str, AfterValidator(valid_version_string)]
2222

23+
DEFAULT_EXCLUDED_PATHS = {
24+
".eggs",
25+
".html-documentation",
26+
".poetry",
27+
".sonar",
28+
".venv",
29+
"dist",
30+
"venv",
31+
}
32+
2333

2434
class BaseConfig(BaseModel):
2535
"""
@@ -49,6 +59,15 @@ class BaseConfig(BaseModel):
4959
default=False,
5060
description="If true, creates also the major version tags (v*) automatically",
5161
)
62+
add_to_excluded_python_paths: tuple[str, ...] = Field(
63+
default=(),
64+
description="""
65+
This is used to extend the default excluded_python_paths. If a more general
66+
path that would be seen in other projects, like .venv, needs to be added into
67+
this argument, please instead modify the
68+
`exasol.toolbox.config.DEFAULT_EXCLUDED_PATHS`.
69+
""",
70+
)
5271
model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True)
5372

5473
@computed_field # type: ignore[misc]
@@ -65,6 +84,24 @@ def minimum_python_version(self) -> str:
6584
index_min_version = versioned.index(min_version)
6685
return self.python_versions[index_min_version]
6786

87+
@computed_field # type: ignore[misc]
88+
@property
89+
def excluded_python_paths(self) -> tuple[str, ...]:
90+
"""
91+
There are certain nox sessions:
92+
- lint:code
93+
- lint:security
94+
- lint:typing
95+
- format:fix
96+
- format:check
97+
where it is desired restrict which Python files are considered within the
98+
source_path, like excluding `dist`, `.eggs`. As such, this property is used to
99+
exclude such undesired paths.
100+
"""
101+
return tuple(
102+
DEFAULT_EXCLUDED_PATHS.union(set(self.add_to_excluded_python_paths))
103+
)
104+
68105
@computed_field # type: ignore[misc]
69106
@property
70107
def pyupgrade_argument(self) -> tuple[str, ...]:

exasol/toolbox/nox/_format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Mode,
1010
_version,
1111
check_for_config_attribute,
12-
python_files,
12+
get_filtered_python_files,
1313
)
1414
from noxconfig import (
1515
PROJECT_CONFIG,
@@ -45,7 +45,7 @@ def command(*args: str) -> Iterable[str]:
4545
@nox.session(name="format:fix", python=False)
4646
def fix_format(session: Session) -> None:
4747
"""Runs all automated format fixes on the code base"""
48-
py_files = python_files(PROJECT_CONFIG.root)
48+
py_files = get_filtered_python_files(PROJECT_CONFIG.root)
4949
_version(session, Mode.Fix)
5050
_pyupgrade(session, config=PROJECT_CONFIG, files=py_files)
5151
_ruff(session, mode=Mode.Fix, files=py_files)
@@ -55,6 +55,6 @@ def fix_format(session: Session) -> None:
5555
@nox.session(name="format:check", python=False)
5656
def check_format(session: Session) -> None:
5757
"""Checks the project for correct formatting"""
58-
py_files = python_files(PROJECT_CONFIG.root)
58+
py_files = get_filtered_python_files(PROJECT_CONFIG.root)
5959
_ruff(session, mode=Mode.Check, files=py_files)
6060
_code_format(session=session, mode=Mode.Check, files=py_files)

exasol/toolbox/nox/_lint.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@
1010
import tomlkit
1111
from nox import Session
1212

13-
from exasol.toolbox.nox._shared import python_files
13+
from exasol.toolbox.nox._shared import get_filtered_python_files
1414
from exasol.toolbox.util.dependencies.shared_models import PoetryFiles
1515
from noxconfig import PROJECT_CONFIG
1616

1717

1818
def _pylint(session: Session, files: Iterable[str]) -> None:
19+
json_file = PROJECT_CONFIG.root / ".lint.json"
20+
txt_file = PROJECT_CONFIG.root / ".lint.txt"
21+
1922
session.run(
2023
"pylint",
2124
"--output-format",
22-
"colorized,json:.lint.json,text:.lint.txt",
25+
f"colorized,json:{json_file},text:{txt_file}",
2326
*files,
2427
)
2528

@@ -47,7 +50,7 @@ def _security_lint(session: Session, files: Iterable[str]) -> None:
4750
"--format",
4851
"json",
4952
"--output",
50-
".security.json",
53+
PROJECT_CONFIG.root / ".security.json",
5154
"--exit-zero",
5255
*files,
5356
)
@@ -120,22 +123,22 @@ def report_illegal(illegal: dict[str, list[str]], console: rich.console.Console)
120123
@nox.session(name="lint:code", python=False)
121124
def lint(session: Session) -> None:
122125
"""Runs the static code analyzer on the project"""
123-
py_files = python_files(PROJECT_CONFIG.root / PROJECT_CONFIG.source)
124-
_pylint(session, py_files)
126+
py_files = get_filtered_python_files(PROJECT_CONFIG.root / PROJECT_CONFIG.source)
127+
_pylint(session=session, files=py_files)
125128

126129

127130
@nox.session(name="lint:typing", python=False)
128131
def type_check(session: Session) -> None:
129132
"""Runs the type checker on the project"""
130-
py_files = [f"{file}" for file in python_files(PROJECT_CONFIG.root)]
131-
_type_check(session, py_files)
133+
py_files = get_filtered_python_files(PROJECT_CONFIG.root)
134+
_type_check(session=session, files=py_files)
132135

133136

134137
@nox.session(name="lint:security", python=False)
135138
def security_lint(session: Session) -> None:
136139
"""Runs the security linter on the project"""
137-
py_files = python_files(PROJECT_CONFIG.root / PROJECT_CONFIG.source)
138-
_security_lint(session, py_files)
140+
py_files = get_filtered_python_files(PROJECT_CONFIG.root / PROJECT_CONFIG.source)
141+
_security_lint(session=session, files=py_files)
139142

140143

141144
@nox.session(name="lint:dependencies", python=False)

exasol/toolbox/nox/_shared.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import argparse
44
from collections import ChainMap
55
from collections.abc import (
6-
Iterable,
76
MutableMapping,
87
)
98
from enum import (
@@ -20,7 +19,6 @@
2019
Config,
2120
)
2221

23-
DEFAULT_PATH_FILTERS = {"dist", ".eggs", "venv", ".poetry"}
2422
DOCS_OUTPUT_DIR = ".html-documentation"
2523

2624

@@ -40,14 +38,17 @@ class Mode(Enum):
4038
Check = auto()
4139

4240

43-
def python_files(project_root: Path) -> Iterable[str]:
41+
def get_filtered_python_files(project_root: Path) -> list[str]:
4442
"""
45-
Returns iterable of python files after removing unwanted paths
43+
Returns iterable of Python files after removing excluded paths
4644
"""
47-
deny_list = DEFAULT_PATH_FILTERS.union(set(PROJECT_CONFIG.path_filters))
48-
45+
check_for_config_attribute(config=PROJECT_CONFIG, attribute="excluded_python_paths")
4946
files = project_root.glob("**/*.py")
50-
return [f"{path}" for path in files if not set(path.parts).intersection(deny_list)]
47+
return [
48+
f"{path}"
49+
for path in files
50+
if not set(path.parts).intersection(PROJECT_CONFIG.excluded_python_paths)
51+
]
5152

5253

5354
def _version(session: Session, mode: Mode) -> None:

exasol/toolbox/nox/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
def check(session: Session) -> None:
3535
"""Runs all available checks on the project"""
3636
context = _context(session, coverage=True)
37-
py_files = python_files(PROJECT_CONFIG.root)
37+
py_files = get_filtered_python_files(PROJECT_CONFIG.root)
3838
_version(session, Mode.Check)
3939
_code_format(session, Mode.Check, py_files)
4040
_pylint(session, py_files)
@@ -65,7 +65,7 @@ def check(session: Session) -> None:
6565
Mode,
6666
_context,
6767
_version,
68-
python_files,
68+
get_filtered_python_files,
6969
)
7070

7171
from exasol.toolbox.nox._ci import (

0 commit comments

Comments
 (0)