@@ -1086,12 +1086,18 @@ def get_filename(self, fullname):
10861086 return self .path
10871087
10881088
1089- class _NamespacePath :
1090- """Represents a namespace package's path. It uses the module name
1091- to find its parent module, and from there it looks up the parent's
1092- __path__. When this changes, the module's own path is recomputed,
1093- using path_finder. For top-level modules, the parent module's path
1094- is sys.path."""
1089+ class NamespacePath :
1090+ """Represents a namespace package's path.
1091+
1092+ It uses the module *name* to find its parent module, and from there it looks
1093+ up the parent's __path__. When this changes, the module's own path is
1094+ recomputed, using *path_finder*. The initial value is set to *path*.
1095+
1096+ For top-level modules, the parent module's path is sys.path.
1097+
1098+ *path_finder* should be a callable with the same signature as
1099+ MetaPathFinder.find_spec((fullname, path, target=None) -> spec).
1100+ """
10951101
10961102 # When invalidate_caches() is called, this epoch is incremented
10971103 # https://bugs.python.org/issue45703
@@ -1153,7 +1159,7 @@ def __len__(self):
11531159 return len (self ._recalculate ())
11541160
11551161 def __repr__ (self ):
1156- return f'_NamespacePath ({ self ._path !r} )'
1162+ return f'NamespacePath ({ self ._path !r} )'
11571163
11581164 def __contains__ (self , item ):
11591165 return item in self ._recalculate ()
@@ -1162,12 +1168,16 @@ def append(self, item):
11621168 self ._path .append (item )
11631169
11641170
1171+ # For backwards-compatibility for anyone desperate enough to get at the class back in the day.
1172+ _NamespacePath = NamespacePath
1173+
1174+
11651175# This class is actually exposed publicly in a namespace package's __loader__
11661176# attribute, so it should be available through a non-private name.
11671177# https://github.com/python/cpython/issues/92054
11681178class NamespaceLoader :
11691179 def __init__ (self , name , path , path_finder ):
1170- self ._path = _NamespacePath (name , path , path_finder )
1180+ self ._path = NamespacePath (name , path , path_finder )
11711181
11721182 def is_package (self , fullname ):
11731183 return True
@@ -1222,9 +1232,9 @@ def invalidate_caches():
12221232 del sys .path_importer_cache [name ]
12231233 elif hasattr (finder , 'invalidate_caches' ):
12241234 finder .invalidate_caches ()
1225- # Also invalidate the caches of _NamespacePaths
1235+ # Also invalidate the caches of NamespacePaths
12261236 # https://bugs.python.org/issue45703
1227- _NamespacePath ._epoch += 1
1237+ NamespacePath ._epoch += 1
12281238
12291239 from importlib .metadata import MetadataPathFinder
12301240 MetadataPathFinder .invalidate_caches ()
@@ -1310,7 +1320,7 @@ def find_spec(cls, fullname, path=None, target=None):
13101320 # We found at least one namespace path. Return a spec which
13111321 # can create the namespace package.
13121322 spec .origin = None
1313- spec .submodule_search_locations = _NamespacePath (fullname , namespace_path , cls ._get_spec )
1323+ spec .submodule_search_locations = NamespacePath (fullname , namespace_path , cls ._get_spec )
13141324 return spec
13151325 else :
13161326 return None
0 commit comments