diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2022-07-01 15:24:17 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-01 15:24:17 -0400 |
commit | 763c440818cc9ca3d4a45cfae1fa48739233e975 (patch) | |
tree | 388872aed1b7971a71a3fc089b17c0cc6d5f6892 | |
parent | 75ed79d1ad1bfbb30dd684cd3cc55cb1139dc31b (diff) | |
parent | 27350b0b0cf11313509160542f435bfe59782afa (diff) | |
download | python-setuptools-git-763c440818cc9ca3d4a45cfae1fa48739233e975.tar.gz |
Merge pull request #151 from abravalheri/remove-warning
Remove warning about packages not containing `__init__.py` files
-rw-r--r-- | distutils/command/build_py.py | 7 | ||||
-rw-r--r-- | distutils/tests/test_build_py.py | 42 |
2 files changed, 43 insertions, 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 @@ class build_py(Command): "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. 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 @@ import unittest 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 @@ class BuildPyTestCase( 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) |