From fd8d22e8747b0f1275ecba88480c1b1c40b8dc53 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Sat, 14 Jun 2025 09:11:03 -0700 Subject: [PATCH] ansible-test - Fix relative import resolution (#85328) (cherry picked from commit 093ac8df2deb009f3151481a5c029fdc257cb5ec) --- changelogs/fragments/ansible-test-change-detection-fix.yml | 2 ++ test/lib/ansible_test/_internal/classification/python.py | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 changelogs/fragments/ansible-test-change-detection-fix.yml diff --git a/changelogs/fragments/ansible-test-change-detection-fix.yml b/changelogs/fragments/ansible-test-change-detection-fix.yml new file mode 100644 index 00000000000..90effab76f6 --- /dev/null +++ b/changelogs/fragments/ansible-test-change-detection-fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test - Fix Python relative import resolution from ``__init__.py`` files when using change detection. diff --git a/test/lib/ansible_test/_internal/classification/python.py b/test/lib/ansible_test/_internal/classification/python.py index 6bc5c33d076..cdb43a60cf5 100644 --- a/test/lib/ansible_test/_internal/classification/python.py +++ b/test/lib/ansible_test/_internal/classification/python.py @@ -221,6 +221,12 @@ def relative_to_absolute(name: str, level: int, module: str, path: str, lineno: else: parts = module.split('.') + if path.endswith('/__init__.py'): + # Ensure the correct relative module is calculated for both not_init.py and __init__.py: + # a/b/not_init.py -> a.b.not_init # used as-is + # a/b/__init__.py -> a.b # needs "__init__" part appended to ensure relative imports work + parts.append('__init__') + if level >= len(parts): display.warning('Cannot resolve relative import "%s%s" above module "%s" at %s:%d' % ('.' * level, name, module, path, lineno)) absolute_name = 'relative.abovelevel'