From 7f1b23d57e7199a3f1cc826b4d94f0c66be3574d Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Fri, 10 Jun 2022 21:45:11 +0100 Subject: [PATCH 1/2] Add test capturing expectations --- distutils/tests/test_build_py.py | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/distutils/tests/test_build_py.py b/distutils/tests/test_build_py.py index 4585d799..eb01d81a 100644 --- a/distutils/tests/test_build_py.py +++ b/distutils/tests/test_build_py.py @@ -7,6 +7,7 @@ from distutils.command.build_py import build_py from distutils.core import Distribution from distutils.errors import DistutilsFileError +from unittest.mock import patch from distutils.tests import support from test.support import run_unittest @@ -167,6 +168,47 @@ def test_dont_write_bytecode(self): self.assertIn('byte-compiling is disabled', self.logs[0][1] % self.logs[0][2]) + @patch("distutils.command.build_py.log.warn") + def test_namespace_package_does_not_warn(self, log_warn): + """ + Originally distutils implementation did not account for PEP 420 + and included warns for package directories that did not contain + ``__init__.py`` files. + After the acceptance of PEP 420, these warnings don't make more sense + so we want to ensure there are not displayed to not confuse the users. + """ + # Create a fake project structure with a package namespace: + tmp = self.mkdtemp() + os.chdir(tmp) + os.makedirs("ns/pkg") + open("ns/pkg/module.py", "w").close() + + # Set up a trap if the undesirable effect is observed: + def _trap(msg, *args): + if "package init file" in msg and "not found" in msg: + raise AssertionError(f"Undesired warning: {msg!r} {args!r}") + + log_warn.side_effect = _trap + + # Configure the package: + attrs = { + "name": "ns.pkg", + "packages": ["ns", "ns.pkg"], + "script_name": "setup.py", + } + dist = Distribution(attrs) + + # Run code paths that would trigger the trap: + cmd = dist.get_command_obj("build_py") + cmd.finalize_options() + modules = cmd.find_all_modules() + assert len(modules) == 1 + module_path = modules[0][-1] + assert module_path.replace(os.sep, "/") == "ns/pkg/module.py" + + cmd.run() + # Test should complete successfully with no exception + def test_suite(): return unittest.TestLoader().loadTestsFromTestCase(BuildPyTestCase) From 27350b0b0cf11313509160542f435bfe59782afa Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Fri, 10 Jun 2022 21:03:03 +0100 Subject: [PATCH 2/2] Remove warnings on namespace packages --- distutils/command/build_py.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/distutils/command/build_py.py b/distutils/command/build_py.py index 1b22004e..7723d359 100644 --- a/distutils/command/build_py.py +++ b/distutils/command/build_py.py @@ -201,16 +201,11 @@ def check_package(self, package, package_dir): "but is not a directory" % package_dir ) - # Require __init__.py for all but the "root package" + # Directories without __init__.py are namespace packages (PEP 420). if package: init_py = os.path.join(package_dir, "__init__.py") if os.path.isfile(init_py): return init_py - else: - log.warn( - ("package init file '%s' not found " + "(or not a regular file)"), - init_py, - ) # Either not in a package at all (__init__.py not expected), or # __init__.py doesn't exist -- so don't return the filename.