diff --git a/changelog.d/3547.change.rst b/changelog.d/3547.change.rst new file mode 100644 index 0000000000..88583b1b63 --- /dev/null +++ b/changelog.d/3547.change.rst @@ -0,0 +1 @@ +Stop ``ConfigDiscovery.analyse_name`` from splatting the ``Distribution.name`` attribute -- by :user:`jeamland` diff --git a/setuptools/config/pyprojecttoml.py b/setuptools/config/pyprojecttoml.py index 9ff0c87fa8..d995f0bcc7 100644 --- a/setuptools/config/pyprojecttoml.py +++ b/setuptools/config/pyprojecttoml.py @@ -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) @@ -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): @@ -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: diff --git a/setuptools/discovery.py b/setuptools/discovery.py index 6a3d2c9d2e..98fc2a7f48 100644 --- a/setuptools/discovery.py +++ b/setuptools/discovery.py @@ -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""" diff --git a/setuptools/tests/test_config_discovery.py b/setuptools/tests/test_config_discovery.py index fac365f410..85b64b31dd 100644 --- a/setuptools/tests/test_config_discovery.py +++ b/setuptools/tests/test_config_discovery.py @@ -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