@@ -264,8 +264,6 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
264264 self ._top_level_dir = top_level_dir
265265
266266 is_not_importable = False
267- is_namespace = False
268- tests = []
269267 if os .path .isdir (os .path .abspath (start_dir )):
270268 start_dir = os .path .abspath (start_dir )
271269 if start_dir != top_level_dir :
@@ -281,50 +279,25 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
281279 top_part = start_dir .split ('.' )[0 ]
282280 try :
283281 start_dir = os .path .abspath (
284- os .path .dirname ((the_module .__file__ )))
282+ os .path .dirname ((the_module .__file__ )))
285283 except AttributeError :
286- # look for namespace packages
287- try :
288- spec = the_module .__spec__
289- except AttributeError :
290- spec = None
291-
292- if spec and spec .loader is None :
293- if spec .submodule_search_locations is not None :
294- is_namespace = True
295-
296- for path in the_module .__path__ :
297- if (not set_implicit_top and
298- not path .startswith (top_level_dir )):
299- continue
300- self ._top_level_dir = \
301- (path .split (the_module .__name__
302- .replace ("." , os .path .sep ))[0 ])
303- tests .extend (self ._find_tests (path ,
304- pattern ,
305- namespace = True ))
306- elif the_module .__name__ in sys .builtin_module_names :
284+ if the_module .__name__ in sys .builtin_module_names :
307285 # builtin module
308286 raise TypeError ('Can not use builtin modules '
309287 'as dotted module names' ) from None
310288 else :
311289 raise TypeError (
312- ' don\ ' t know how to discover from {!r}'
313- . format ( the_module ) ) from None
290+ f" don't know how to discover from { the_module !r} "
291+ ) from None
314292
315293 if set_implicit_top :
316- if not is_namespace :
317- self ._top_level_dir = \
318- self ._get_directory_containing_module (top_part )
319- sys .path .remove (top_level_dir )
320- else :
321- sys .path .remove (top_level_dir )
294+ self ._top_level_dir = self ._get_directory_containing_module (top_part )
295+ sys .path .remove (top_level_dir )
322296
323297 if is_not_importable :
324298 raise ImportError ('Start directory is not importable: %r' % start_dir )
325299
326- if not is_namespace :
327- tests = list (self ._find_tests (start_dir , pattern ))
300+ tests = list (self ._find_tests (start_dir , pattern ))
328301 return self .suiteClass (tests )
329302
330303 def _get_directory_containing_module (self , module_name ):
@@ -359,7 +332,7 @@ def _match_path(self, path, full_path, pattern):
359332 # override this method to use alternative matching strategy
360333 return fnmatch (path , pattern )
361334
362- def _find_tests (self , start_dir , pattern , namespace = False ):
335+ def _find_tests (self , start_dir , pattern ):
363336 """Used by discovery. Yields test suites it loads."""
364337 # Handle the __init__ in this package
365338 name = self ._get_name_from_path (start_dir )
@@ -368,8 +341,7 @@ def _find_tests(self, start_dir, pattern, namespace=False):
368341 if name != '.' and name not in self ._loading_packages :
369342 # name is in self._loading_packages while we have called into
370343 # loadTestsFromModule with name.
371- tests , should_recurse = self ._find_test_path (
372- start_dir , pattern , namespace )
344+ tests , should_recurse = self ._find_test_path (start_dir , pattern )
373345 if tests is not None :
374346 yield tests
375347 if not should_recurse :
@@ -380,20 +352,19 @@ def _find_tests(self, start_dir, pattern, namespace=False):
380352 paths = sorted (os .listdir (start_dir ))
381353 for path in paths :
382354 full_path = os .path .join (start_dir , path )
383- tests , should_recurse = self ._find_test_path (
384- full_path , pattern , namespace )
355+ tests , should_recurse = self ._find_test_path (full_path , pattern )
385356 if tests is not None :
386357 yield tests
387358 if should_recurse :
388359 # we found a package that didn't use load_tests.
389360 name = self ._get_name_from_path (full_path )
390361 self ._loading_packages .add (name )
391362 try :
392- yield from self ._find_tests (full_path , pattern , namespace )
363+ yield from self ._find_tests (full_path , pattern )
393364 finally :
394365 self ._loading_packages .discard (name )
395366
396- def _find_test_path (self , full_path , pattern , namespace = False ):
367+ def _find_test_path (self , full_path , pattern ):
397368 """Used by discovery.
398369
399370 Loads tests from a single file, or a directories' __init__.py when
@@ -437,8 +408,7 @@ def _find_test_path(self, full_path, pattern, namespace=False):
437408 msg % (mod_name , module_dir , expected_dir ))
438409 return self .loadTestsFromModule (module , pattern = pattern ), False
439410 elif os .path .isdir (full_path ):
440- if (not namespace and
441- not os .path .isfile (os .path .join (full_path , '__init__.py' ))):
411+ if not os .path .isfile (os .path .join (full_path , '__init__.py' )):
442412 return None , False
443413
444414 load_tests = None
0 commit comments