summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnderson Bravalheri <andersonbravalheri@gmail.com>2022-08-24 10:30:49 +0100
committerAnderson Bravalheri <andersonbravalheri@gmail.com>2022-08-24 10:30:49 +0100
commitaf1ff35b3715b5f2baf4b2fbe22a818b471b4513 (patch)
treed0f0d0239ccf0ed961e1526014d140934dd5b5e9
parent785646c3b52dc423a6e90e7424d4985eeb0f2ad1 (diff)
parent9a4b45f3163985fce18fd52fad3269982c052508 (diff)
downloadpython-setuptools-git-af1ff35b3715b5f2baf4b2fbe22a818b471b4513.tar.gz
Remove inadvertent splatting of the name attribute (#3547)
-rw-r--r--changelog.d/3547.change.rst1
-rw-r--r--setuptools/config/pyprojecttoml.py15
-rw-r--r--setuptools/discovery.py1
-rw-r--r--setuptools/tests/test_config_discovery.py43
4 files changed, 54 insertions, 6 deletions
diff --git a/changelog.d/3547.change.rst b/changelog.d/3547.change.rst
new file mode 100644
index 00000000..88583b1b
--- /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 9ff0c87f..d995f0bc 100644
--- a/setuptools/config/pyprojecttoml.py
+++ b/setuptools/config/pyprojecttoml.py
@@ -234,8 +234,8 @@ class _ConfigExpander:
# 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 @@ class _EnsurePackagesDiscovered(_expand.EnsurePackagesDiscovered):
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 6a3d2c9d..98fc2a7f 100644
--- a/setuptools/discovery.py
+++ b/setuptools/discovery.py
@@ -481,7 +481,6 @@ class ConfigDiscovery:
)
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 fac365f4..85b64b31 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