[stable-2.18] ansible-test - Fix relative import resolution (#85328) (#85445)

(cherry picked from commit 093ac8df2d)
pull/85454/head
Matt Clay 5 months ago committed by GitHub
parent 9b83ff7b3f
commit 7d3ae41ad9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- ansible-test - Fix Python relative import resolution from ``__init__.py`` files when using change detection.

@ -220,6 +220,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'

Loading…
Cancel
Save