@@ -1161,6 +1161,39 @@ def test_import_sets_module_as_attribute(pytester: Pytester) -> None:
11611161 assert bar_2 is bar
11621162
11631163
1164+ def test_import_sets_module_as_attribute_without_init_files (pytester : Pytester ) -> None :
1165+ """Similar to test_import_sets_module_as_attribute, but without __init__.py files."""
1166+ pytester .path .joinpath ("foo/bar" ).mkdir (parents = True )
1167+ pytester .path .joinpath ("foo/bar/baz.py" ).touch ()
1168+ pytester .syspathinsert ()
1169+
1170+ # Import foo.bar.baz and ensure parent modules also ended up imported.
1171+ baz = import_path (
1172+ pytester .path .joinpath ("foo/bar/baz.py" ),
1173+ mode = ImportMode .importlib ,
1174+ root = pytester .path ,
1175+ consider_namespace_packages = False ,
1176+ )
1177+ assert baz .__name__ == "foo.bar.baz"
1178+ foo = sys .modules ["foo" ]
1179+ assert foo .__name__ == "foo"
1180+ bar = sys .modules ["foo.bar" ]
1181+ assert bar .__name__ == "foo.bar"
1182+
1183+ # Check parent modules have an attribute pointing to their children.
1184+ assert bar .baz is baz
1185+ assert foo .bar is bar
1186+
1187+ # Ensure we returned the "foo.bar" module cached in sys.modules.
1188+ baz_2 = import_path (
1189+ pytester .path .joinpath ("foo/bar/baz.py" ),
1190+ mode = ImportMode .importlib ,
1191+ root = pytester .path ,
1192+ consider_namespace_packages = False ,
1193+ )
1194+ assert baz_2 is baz
1195+
1196+
11641197def test_import_sets_module_as_attribute_regression (pytester : Pytester ) -> None :
11651198 """Regression test for #12194."""
11661199 pytester .path .joinpath ("foo/bar/baz" ).mkdir (parents = True )
0 commit comments