This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest.defaultTestLoader.discover fails for namespace packages
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.snow, ezio.melotti, michael.foord, ncoghlan, rbcollins, tpapastylianou
Priority: normal Keywords:

Created on 2022-02-04 11:35 by tpapastylianou, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg412505 - (view) Author: Tasos Papastylianou (tpapastylianou) Date: 2022-02-04 11:35
Back in python 3.6.9, attempting to import __file__ on a namespace package resulted in an attribute error. From at least 3.8 onwards, this behaviour seems to have changed, and __file__ simply returns None instead.

This seems to have broken unittest discovery. Looking at the code, it seems that discover still seems to rely on a try/except block in order to test for a namespace package. Now that the attribute error is no longer present in later python versions, discover simply accepts the None value for __file__, and fails further down the line when attempting to canonicalise a path containing a None value (error effectively expects a string).

On my system with python 3.8, the relevant files/lines are:

- /usr/lib/python3.8/unittest/loader.py()discover()
  The try block starting at line 304 checks for the module's __file__ attribute, expecting to redirect to 307 to "look for namespace packages" in case of an attribute error. Obviously, now that __file__ returns None instead, this logic fails.

- The call to dirname in line 306 therefore proceeds normally, passing a None as a file, which then fails with a TypeError: expected str, bytes or os.PathLike object, not NoneType
 

See https://github.com/tpapastylianou/self-contained-runnable-python-package-template/issues/13# for the example in the wild that prompted the discovery of this bug.
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90793
2022-02-04 18:25:49eric.araujosetnosy: + brett.cannon, ncoghlan, rbcollins, ezio.melotti, michael.foord, eric.snow, - eric.araujo, dstufft

components: + Library (Lib), - Distutils
versions: - Python 3.8
2022-02-04 11:37:25tpapastylianousetnosy: + eric.araujo, dstufft
components: + Distutils, - Tests
2022-02-04 11:35:56tpapastylianoucreate