@@ -587,6 +587,12 @@ class Data:
587
587
assert data .value == "foo"
588
588
assert data .__module__ == "_src.tests.test_dataclass"
589
589
590
+ # Ensure we do not import the same module again (#11475).
591
+ module2 = import_path (
592
+ fn , mode = "importlib" , root = tmp_path , consider_namespace_packages = ns_param
593
+ )
594
+ assert module is module2
595
+
590
596
def test_importmode_importlib_with_pickle (
591
597
self , tmp_path : Path , ns_param : bool
592
598
) -> None :
@@ -616,6 +622,12 @@ def round_trip():
616
622
action = round_trip ()
617
623
assert action () == 42
618
624
625
+ # Ensure we do not import the same module again (#11475).
626
+ module2 = import_path (
627
+ fn , mode = "importlib" , root = tmp_path , consider_namespace_packages = ns_param
628
+ )
629
+ assert module is module2
630
+
619
631
def test_importmode_importlib_with_pickle_separate_modules (
620
632
self , tmp_path : Path , ns_param : bool
621
633
) -> None :
@@ -816,6 +828,14 @@ def __init__(self) -> None:
816
828
consider_namespace_packages = ns_param ,
817
829
)
818
830
assert len (mod .instance .INSTANCES ) == 1
831
+ # Ensure we do not import the same module again (#11475).
832
+ mod2 = import_path (
833
+ init ,
834
+ root = tmp_path ,
835
+ mode = ImportMode .importlib ,
836
+ consider_namespace_packages = ns_param ,
837
+ )
838
+ assert mod is mod2
819
839
820
840
def test_importlib_root_is_package (self , pytester : Pytester ) -> None :
821
841
"""
@@ -942,6 +962,15 @@ def test_import_using_normal_mechanism_first(
942
962
assert mod .__name__ == "app.core"
943
963
assert mod .__file__ and Path (mod .__file__ ) == core_py
944
964
965
+ # Ensure we do not import the same module again (#11475).
966
+ mod2 = import_path (
967
+ core_py ,
968
+ mode = "importlib" ,
969
+ root = pytester .path ,
970
+ consider_namespace_packages = ns_param ,
971
+ )
972
+ assert mod is mod2
973
+
945
974
# tests are not reachable from sys.path, so they are imported as a standalone modules.
946
975
# Instead of '.tests.a.test_core', we import as "_tests.a.test_core" because
947
976
# importlib considers module names starting with '.' to be local imports.
@@ -952,6 +981,16 @@ def test_import_using_normal_mechanism_first(
952
981
consider_namespace_packages = ns_param ,
953
982
)
954
983
assert mod .__name__ == "_tests.a.test_core"
984
+
985
+ # Ensure we do not import the same module again (#11475).
986
+ mod2 = import_path (
987
+ test_path1 ,
988
+ mode = "importlib" ,
989
+ root = pytester .path ,
990
+ consider_namespace_packages = ns_param ,
991
+ )
992
+ assert mod is mod2
993
+
955
994
mod = import_path (
956
995
test_path2 ,
957
996
mode = "importlib" ,
@@ -960,6 +999,15 @@ def test_import_using_normal_mechanism_first(
960
999
)
961
1000
assert mod .__name__ == "_tests.b.test_core"
962
1001
1002
+ # Ensure we do not import the same module again (#11475).
1003
+ mod2 = import_path (
1004
+ test_path2 ,
1005
+ mode = "importlib" ,
1006
+ root = pytester .path ,
1007
+ consider_namespace_packages = ns_param ,
1008
+ )
1009
+ assert mod is mod2
1010
+
963
1011
def test_import_using_normal_mechanism_first_integration (
964
1012
self , monkeypatch : MonkeyPatch , pytester : Pytester , ns_param : bool
965
1013
) -> None :
@@ -1021,6 +1069,14 @@ def test_import_path_imports_correct_file(
1021
1069
assert mod .__file__ and Path (mod .__file__ ) == x_in_sub_folder
1022
1070
assert mod .X == "a/b/x"
1023
1071
1072
+ mod2 = import_path (
1073
+ x_in_sub_folder ,
1074
+ mode = ImportMode .importlib ,
1075
+ root = pytester .path ,
1076
+ consider_namespace_packages = ns_param ,
1077
+ )
1078
+ assert mod is mod2
1079
+
1024
1080
# Attempt to import root 'x.py'.
1025
1081
with pytest .raises (AssertionError , match = "x at root" ):
1026
1082
_ = import_path (
@@ -1124,6 +1180,12 @@ def test_resolve_pkg_root_and_module_name_ns_multiple_levels(
1124
1180
assert mod .__name__ == "com.company.app.core.models"
1125
1181
assert mod .__file__ == str (models_py )
1126
1182
1183
+ # Ensure we do not import the same module again (#11475).
1184
+ mod2 = import_path (
1185
+ models_py , mode = import_mode , root = tmp_path , consider_namespace_packages = True
1186
+ )
1187
+ assert mod is mod2
1188
+
1127
1189
pkg_root , module_name = resolve_pkg_root_and_module_name (
1128
1190
algorithms_py , consider_namespace_packages = True
1129
1191
)
@@ -1141,6 +1203,15 @@ def test_resolve_pkg_root_and_module_name_ns_multiple_levels(
1141
1203
assert mod .__name__ == "com.company.calc.algo.algorithms"
1142
1204
assert mod .__file__ == str (algorithms_py )
1143
1205
1206
+ # Ensure we do not import the same module again (#11475).
1207
+ mod2 = import_path (
1208
+ algorithms_py ,
1209
+ mode = import_mode ,
1210
+ root = tmp_path ,
1211
+ consider_namespace_packages = True ,
1212
+ )
1213
+ assert mod is mod2
1214
+
1144
1215
@pytest .mark .parametrize ("import_mode" , ["prepend" , "append" , "importlib" ])
1145
1216
def test_incorrect_namespace_package (
1146
1217
self ,
0 commit comments