Skip to content

Commit

Permalink
Merge pull request #151 from abravalheri/remove-warning
Browse files Browse the repository at this point in the history
Remove warning about packages not containing `__init__.py` files
  • Loading branch information
jaraco authored Jul 1, 2022
2 parents 75ed79d + 27350b0 commit 763c440
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
7 changes: 1 addition & 6 deletions distutils/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions distutils/tests/test_build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 763c440

Please sign in to comment.