Skip to content

Commit

Permalink
Add test without __init__.py files
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Apr 13, 2024
1 parent 59f0112 commit 8b393d7
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,39 @@ def test_import_sets_module_as_attribute(pytester: Pytester) -> None:
assert bar_2 is bar


def test_import_sets_module_as_attribute_without_init_files(pytester: Pytester) -> None:
"""Similar to test_import_sets_module_as_attribute, but without __init__.py files."""
pytester.path.joinpath("foo/bar").mkdir(parents=True)
pytester.path.joinpath("foo/bar/baz.py").touch()
pytester.syspathinsert()

# Import foo.bar.baz and ensure parent modules also ended up imported.
baz = import_path(
pytester.path.joinpath("foo/bar/baz.py"),
mode=ImportMode.importlib,
root=pytester.path,
consider_namespace_packages=False,
)
assert baz.__name__ == "foo.bar.baz"
foo = sys.modules["foo"]
assert foo.__name__ == "foo"
bar = sys.modules["foo.bar"]
assert bar.__name__ == "foo.bar"

# Check parent modules have an attribute pointing to their children.
assert bar.baz is baz
assert foo.bar is bar

# Ensure we returned the "foo.bar.baz" module cached in sys.modules.
baz_2 = import_path(
pytester.path.joinpath("foo/bar/baz.py"),
mode=ImportMode.importlib,
root=pytester.path,
consider_namespace_packages=False,
)
assert baz_2 is baz


def test_import_sets_module_as_attribute_regression(pytester: Pytester) -> None:
"""Regression test for #12194."""
pytester.path.joinpath("foo/bar/baz").mkdir(parents=True)
Expand Down

0 comments on commit 8b393d7

Please sign in to comment.