Skip to content

Commit af1ff35

Browse files
committed
Remove inadvertent splatting of the name attribute (#3547)
2 parents 785646c + 9a4b45f commit af1ff35

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

changelog.d/3547.change.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Stop ``ConfigDiscovery.analyse_name`` from splatting the ``Distribution.name`` attribute -- by :user:`jeamland`

setuptools/config/pyprojecttoml.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ def expand(self):
234234

235235
# A distribution object is required for discovering the correct package_dir
236236
dist = self._ensure_dist()
237-
238-
with _EnsurePackagesDiscovered(dist, self.setuptools_cfg) as ensure_discovered:
237+
ctx = _EnsurePackagesDiscovered(dist, self.project_cfg, self.setuptools_cfg)
238+
with ctx as ensure_discovered:
239239
package_dir = ensure_discovered.package_dir
240240
self._expand_data_files()
241241
self._expand_cmdclass(package_dir)
@@ -428,8 +428,11 @@ def _ignore_errors(ignore_option_errors: bool):
428428

429429

430430
class _EnsurePackagesDiscovered(_expand.EnsurePackagesDiscovered):
431-
def __init__(self, distribution: "Distribution", setuptools_cfg: dict):
431+
def __init__(
432+
self, distribution: "Distribution", project_cfg: dict, setuptools_cfg: dict
433+
):
432434
super().__init__(distribution)
435+
self._project_cfg = project_cfg
433436
self._setuptools_cfg = setuptools_cfg
434437

435438
def __enter__(self):
@@ -443,8 +446,10 @@ def __enter__(self):
443446

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

446-
# Set `py_modules` and `packages` in dist to short-circuit auto-discovery,
447-
# but avoid overwriting empty lists purposefully set by users.
449+
# Set `name`, `py_modules` and `packages` in dist to short-circuit
450+
# auto-discovery, but avoid overwriting empty lists purposefully set by users.
451+
if dist.metadata.name is None:
452+
dist.metadata.name = self._project_cfg.get("name")
448453
if dist.py_modules is None:
449454
dist.py_modules = cfg.get("py-modules")
450455
if dist.packages is None:

setuptools/discovery.py

-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ def analyse_name(self):
481481
)
482482
if name:
483483
self.dist.metadata.name = name
484-
self.dist.name = name
485484

486485
def _find_name_single_package_or_module(self) -> Optional[str]:
487486
"""Exactly one module or package"""

setuptools/tests/test_config_discovery.py

+43
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,49 @@ def test_compatible_with_numpy_configuration(tmp_path):
508508
assert dist.packages is None
509509

510510

511+
def test_name_discovery_doesnt_break_cli(tmpdir_cwd):
512+
jaraco.path.build({"pkg.py": ""})
513+
dist = Distribution({})
514+
dist.script_args = ["--name"]
515+
dist.set_defaults()
516+
dist.parse_command_line() # <-- no exception should be raised here.
517+
assert dist.get_name() == "pkg"
518+
519+
520+
def test_preserve_explicit_name_with_dynamic_version(tmpdir_cwd, monkeypatch):
521+
"""According to #3545 it seems that ``name`` discovery is running,
522+
even when the project already explicitly sets it.
523+
This seems to be related to parsing of dynamic versions (via ``attr`` directive),
524+
which requires the auto-discovery of ``package_dir``.
525+
"""
526+
files = {
527+
"src": {
528+
"pkg": {"__init__.py": "__version__ = 42\n"},
529+
},
530+
"pyproject.toml": DALS("""
531+
[project]
532+
name = "myproj" # purposefully different from package name
533+
dynamic = ["version"]
534+
[tool.setuptools.dynamic]
535+
version = {"attr" = "pkg.__version__"}
536+
""")
537+
}
538+
jaraco.path.build(files)
539+
dist = Distribution({})
540+
orig_analyse_name = dist.set_defaults.analyse_name
541+
542+
def spy_analyse_name():
543+
# We can check if name discovery was triggered by ensuring the original
544+
# name remains instead of the package name.
545+
orig_analyse_name()
546+
assert dist.get_name() == "myproj"
547+
548+
monkeypatch.setattr(dist.set_defaults, "analyse_name", spy_analyse_name)
549+
dist.parse_config_files()
550+
assert dist.get_version() == "42"
551+
assert set(dist.packages) == {"pkg"}
552+
553+
511554
def _populate_project_dir(root, files, options):
512555
# NOTE: Currently pypa/build will refuse to build the project if no
513556
# `pyproject.toml` or `setup.py` is found. So it is impossible to do

0 commit comments

Comments
 (0)