Skip to content

[BUG] Options declared in setup.cfg can not override options declared in setup() function #4800

Open
@gaokai320

Description

@gaokai320

setuptools version

setuptools==75.1.0

Python version

Python 3.12.2

OS

Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-51-generic x86_64)

Additional environment information

I use the Miniconda

Description

I am reading the source code of setuptools to understand how it works when building a Python project. I noticed there is a comment in the 158 line in setuptools/_distutils/core.py

Find and parse the config file(s): they will override options from the setup script, but be overridden by the command line.

It suggests that options declared in the setup.cfg file will override options declared in the setup() function. However, when looking at the code of ConfigHandler.__setitem__() in setuptools/config/setupcfg.py, I notice the following weird code:

try:
    current_value = getattr(target_obj, option_name)
except AttributeError as e:
    raise KeyError(option_name) from e

if current_value:
    # Already inhabited. Skipping.
    return

It seems that it will not override the options already declared in the setup() function

Expected behavior

If the comment is right, is the above code unnecessary?

How to Reproduce

  1. create a ``setup.cfg` file with the following content
[options]
include_package_data = False
install_requires =
    requests
  1. open a terminal and type python and the following code
>>> from setuptools.dist import Distribution
>>> attrs = {"name": "test", "include_package_data": True, "install_requires": ["abc"]}
>>> dist = Distribution(attrs)
>>> dist.include_package_data, install_requires
(True, ['abc'])
>>> dist.parse_config_files(["setup.cfg"])
>>> dist.include_package_data, install_requires
(True, ['abc'])

According to the output, options in setup.cfg do not override options in setup(). Is it the correct or expected behavior?

Output

Please see the above section

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssues that need to be evaluated for severity and status.bug

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions