From 6a88f7db55b171b7cf396129f2e955d5d46a0843 Mon Sep 17 00:00:00 2001 From: Michael Johansen Date: Mon, 8 Sep 2025 15:08:42 -0500 Subject: [PATCH 1/4] Update _get_application_import_names to have fall-backs Signed-off-by: Michael Johansen --- ni_python_styleguide/_cli.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ni_python_styleguide/_cli.py b/ni_python_styleguide/_cli.py index 1ec7596b..80851ff9 100644 --- a/ni_python_styleguide/_cli.py +++ b/ni_python_styleguide/_cli.py @@ -39,14 +39,22 @@ def _read_pyproject_toml(ctx, param, value): def _get_application_import_names(pyproject): """Returns the application package name the config.""" - # Otherwise override with what was specified + # Use the tool-specific import names if specified app_name = ( pyproject.get("tool", {}) .get("ni-python-styleguide", {}) .get("application-import-names", "") ) - # Allow the poetry name as a fallback + # Next fall back to project.import-names + if not app_name: + app_name = pyproject.get("project", {}).get("import-names", "") + + # Next fall back to project.name (replace hyphens) + if not app_name: + app_name = pyproject.get("project", {}).get("name", "").replace("-", "_") + + # Next fall back to tool.poetry.name (replace hyphens) if not app_name: app_name = pyproject.get("tool", {}).get("poetry", {}).get("name", "").replace("-", "_") From 40ddc1b74bb418b91023d82acd5a78ac27b7b566 Mon Sep 17 00:00:00 2001 From: Michael Johansen Date: Tue, 9 Sep 2025 09:39:40 -0500 Subject: [PATCH 2/4] Handle import-names as an array Signed-off-by: Michael Johansen --- ni_python_styleguide/_cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ni_python_styleguide/_cli.py b/ni_python_styleguide/_cli.py index 80851ff9..55bba043 100644 --- a/ni_python_styleguide/_cli.py +++ b/ni_python_styleguide/_cli.py @@ -48,7 +48,8 @@ def _get_application_import_names(pyproject): # Next fall back to project.import-names if not app_name: - app_name = pyproject.get("project", {}).get("import-names", "") + import_names = pyproject.get("project", {}).get("import-names", []) + app_name = ",".join(import_names) # Next fall back to project.name (replace hyphens) if not app_name: From af8362967e746802c7d06e2abcaa4c3a2fe9d308 Mon Sep 17 00:00:00 2001 From: Michael Johansen Date: Tue, 9 Sep 2025 13:46:14 -0500 Subject: [PATCH 3/4] Add unit tests for _get_application_import_names(). Signed-off-by: Michael Johansen --- .../test_cli/test_application_import_names.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/test_cli/test_application_import_names.py diff --git a/tests/test_cli/test_application_import_names.py b/tests/test_cli/test_application_import_names.py new file mode 100644 index 00000000..740f68d1 --- /dev/null +++ b/tests/test_cli/test_application_import_names.py @@ -0,0 +1,70 @@ +"""Tests for the "_get_application_import_names" method of _cli.py.""" + +import pytest + +from ni_python_styleguide._cli import _get_application_import_names + + +@pytest.mark.parametrize( + "pyproject_obj, expected_names", + [ + ( + { + "tool": { + "ni-python-styleguide": { + "application-import-names": "ain1,ain2" + }, + "poetry": { + "name": "tool.poetry.name" + } + }, + "project": { + "import-names": ["import-names1", "import-names2"], + "name": "project.name" + } + }, + "ain1,ain2,tests" + ), + ( + { + "tool": { + "poetry": { + "name": "tool.poetry.name" + } + }, + "project": { + "import-names": ["import-names1", "import-names2"], + "name": "project.name" + } + }, + "import-names1,import-names2,tests" + ), + ( + { + "tool": { + "poetry": { + "name": "tool.poetry.name" + } + }, + "project": { + "name": "project.name" + } + }, + "project.name,tests" + ), + ( + { + "tool": { + "poetry": { + "name": "tool.poetry.name" + } + } + }, + "tool.poetry.name,tests" + ) + ], +) +def test_get_application_import_names_returns_valid_data(pyproject_obj, expected_names): + """Tests that _get_application_import_names returns valid data.""" + result = _get_application_import_names(pyproject_obj) + assert result == expected_names From aecbdfef17d16237ca028ffc1d6d5f40504d9b56 Mon Sep 17 00:00:00 2001 From: Michael Johansen Date: Tue, 9 Sep 2025 13:51:56 -0500 Subject: [PATCH 4/4] Fix linting error. Signed-off-by: Michael Johansen --- .../test_cli/test_application_import_names.py | 50 +++++-------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/tests/test_cli/test_application_import_names.py b/tests/test_cli/test_application_import_names.py index 740f68d1..819cd57b 100644 --- a/tests/test_cli/test_application_import_names.py +++ b/tests/test_cli/test_application_import_names.py @@ -11,57 +11,31 @@ ( { "tool": { - "ni-python-styleguide": { - "application-import-names": "ain1,ain2" - }, - "poetry": { - "name": "tool.poetry.name" - } + "ni-python-styleguide": {"application-import-names": "ain1,ain2"}, + "poetry": {"name": "tool.poetry.name"}, }, "project": { "import-names": ["import-names1", "import-names2"], - "name": "project.name" - } + "name": "project.name", + }, }, - "ain1,ain2,tests" + "ain1,ain2,tests", ), ( { - "tool": { - "poetry": { - "name": "tool.poetry.name" - } - }, + "tool": {"poetry": {"name": "tool.poetry.name"}}, "project": { "import-names": ["import-names1", "import-names2"], - "name": "project.name" - } - }, - "import-names1,import-names2,tests" - ), - ( - { - "tool": { - "poetry": { - "name": "tool.poetry.name" - } + "name": "project.name", }, - "project": { - "name": "project.name" - } }, - "project.name,tests" + "import-names1,import-names2,tests", ), ( - { - "tool": { - "poetry": { - "name": "tool.poetry.name" - } - } - }, - "tool.poetry.name,tests" - ) + {"tool": {"poetry": {"name": "tool.poetry.name"}}, "project": {"name": "project.name"}}, + "project.name,tests", + ), + ({"tool": {"poetry": {"name": "tool.poetry.name"}}}, "tool.poetry.name,tests"), ], ) def test_get_application_import_names_returns_valid_data(pyproject_obj, expected_names):