Skip to content

Commit 9cbbcf7

Browse files
committed
Additional test
1 parent f62efb9 commit 9cbbcf7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

testing/test_pathlib.py

+40
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,46 @@ def test_foo() -> None:
12191219
assert result.ret == 0
12201220

12211221

1222+
def test_import_submodule_not_namespace(pytester: Pytester) -> None:
1223+
"""
1224+
Regression test for importing a submodule 'foo.bar' while there is a 'bar' directory
1225+
reachable from sys.path -- ensuring the top-level module does not end up imported as a namespace
1226+
package.
1227+
1228+
#12194
1229+
https://github.com/pytest-dev/pytest/pull/12208#issuecomment-2056458432
1230+
"""
1231+
pytester.syspathinsert()
1232+
# Create package 'foo' with a submodule 'bar'.
1233+
pytester.path.joinpath("foo").mkdir()
1234+
foo_path = pytester.path.joinpath("foo/__init__.py")
1235+
foo_path.touch()
1236+
bar_path = pytester.path.joinpath("foo/bar.py")
1237+
bar_path.touch()
1238+
# Create top-level directory in `sys.path` with the same name as that submodule.
1239+
pytester.path.joinpath("bar").mkdir()
1240+
1241+
# Import `foo`, then `foo.bar`, and check they were imported from the correct location.
1242+
foo = import_path(
1243+
foo_path,
1244+
mode=ImportMode.importlib,
1245+
root=pytester.path,
1246+
consider_namespace_packages=False,
1247+
)
1248+
bar = import_path(
1249+
bar_path,
1250+
mode=ImportMode.importlib,
1251+
root=pytester.path,
1252+
consider_namespace_packages=False,
1253+
)
1254+
assert foo.__name__ == "foo"
1255+
assert bar.__name__ == "foo.bar"
1256+
assert foo.__file__ is not None
1257+
assert bar.__file__ is not None
1258+
assert Path(foo.__file__) == foo_path
1259+
assert Path(bar.__file__) == bar_path
1260+
1261+
12221262
class TestNamespacePackages:
12231263
"""Test import_path support when importing from properly namespace packages."""
12241264

0 commit comments

Comments
 (0)