Skip to content

Commit

Permalink
Remove inadvertent splatting of the name attribute (#3547)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Aug 24, 2022
2 parents 785646c + 9a4b45f commit af1ff35
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/3547.change.rst
@@ -0,0 +1 @@
Stop ``ConfigDiscovery.analyse_name`` from splatting the ``Distribution.name`` attribute -- by :user:`jeamland`
15 changes: 10 additions & 5 deletions setuptools/config/pyprojecttoml.py
Expand Up @@ -234,8 +234,8 @@ def expand(self):

# A distribution object is required for discovering the correct package_dir
dist = self._ensure_dist()

with _EnsurePackagesDiscovered(dist, self.setuptools_cfg) as ensure_discovered:
ctx = _EnsurePackagesDiscovered(dist, self.project_cfg, self.setuptools_cfg)
with ctx as ensure_discovered:
package_dir = ensure_discovered.package_dir
self._expand_data_files()
self._expand_cmdclass(package_dir)
Expand Down Expand Up @@ -428,8 +428,11 @@ def _ignore_errors(ignore_option_errors: bool):


class _EnsurePackagesDiscovered(_expand.EnsurePackagesDiscovered):
def __init__(self, distribution: "Distribution", setuptools_cfg: dict):
def __init__(
self, distribution: "Distribution", project_cfg: dict, setuptools_cfg: dict
):
super().__init__(distribution)
self._project_cfg = project_cfg
self._setuptools_cfg = setuptools_cfg

def __enter__(self):
Expand All @@ -443,8 +446,10 @@ def __enter__(self):

dist.set_defaults._ignore_ext_modules() # pyproject.toml-specific behaviour

# Set `py_modules` and `packages` in dist to short-circuit auto-discovery,
# but avoid overwriting empty lists purposefully set by users.
# Set `name`, `py_modules` and `packages` in dist to short-circuit
# auto-discovery, but avoid overwriting empty lists purposefully set by users.
if dist.metadata.name is None:
dist.metadata.name = self._project_cfg.get("name")
if dist.py_modules is None:
dist.py_modules = cfg.get("py-modules")
if dist.packages is None:
Expand Down
1 change: 0 additions & 1 deletion setuptools/discovery.py
Expand Up @@ -481,7 +481,6 @@ def analyse_name(self):
)
if name:
self.dist.metadata.name = name
self.dist.name = name

def _find_name_single_package_or_module(self) -> Optional[str]:
"""Exactly one module or package"""
Expand Down
43 changes: 43 additions & 0 deletions setuptools/tests/test_config_discovery.py
Expand Up @@ -508,6 +508,49 @@ def test_compatible_with_numpy_configuration(tmp_path):
assert dist.packages is None


def test_name_discovery_doesnt_break_cli(tmpdir_cwd):
jaraco.path.build({"pkg.py": ""})
dist = Distribution({})
dist.script_args = ["--name"]
dist.set_defaults()
dist.parse_command_line() # <-- no exception should be raised here.
assert dist.get_name() == "pkg"


def test_preserve_explicit_name_with_dynamic_version(tmpdir_cwd, monkeypatch):
"""According to #3545 it seems that ``name`` discovery is running,
even when the project already explicitly sets it.
This seems to be related to parsing of dynamic versions (via ``attr`` directive),
which requires the auto-discovery of ``package_dir``.
"""
files = {
"src": {
"pkg": {"__init__.py": "__version__ = 42\n"},
},
"pyproject.toml": DALS("""
[project]
name = "myproj" # purposefully different from package name
dynamic = ["version"]
[tool.setuptools.dynamic]
version = {"attr" = "pkg.__version__"}
""")
}
jaraco.path.build(files)
dist = Distribution({})
orig_analyse_name = dist.set_defaults.analyse_name

def spy_analyse_name():
# We can check if name discovery was triggered by ensuring the original
# name remains instead of the package name.
orig_analyse_name()
assert dist.get_name() == "myproj"

monkeypatch.setattr(dist.set_defaults, "analyse_name", spy_analyse_name)
dist.parse_config_files()
assert dist.get_version() == "42"
assert set(dist.packages) == {"pkg"}


def _populate_project_dir(root, files, options):
# NOTE: Currently pypa/build will refuse to build the project if no
# `pyproject.toml` or `setup.py` is found. So it is impossible to do
Expand Down

0 comments on commit af1ff35

Please sign in to comment.