Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards no-config packaging: Automatic discovery of packages, py_modules and name #2894

Merged
merged 32 commits into from
Mar 24, 2022

Conversation

abravalheri
Copy link
Contributor

@abravalheri abravalheri commented Nov 17, 2021

As discussed in #2887 and #2888, it would be nice to have some configurations automatically derived.
This is definitely possible for packages and py_modules, and (based on these two) also for name (as suggested by @jaraco).

This implementation supersedes #2888 (closes #2888).

Summary of changes

  • A new class setuptools.discovery.ConfigDiscovery implementing the automatic discovery logic
  • Moved PackageFinder and PEP420PackageFinder to the setuptools.discovery module due to the implementation affinity.
  • New FlatLayoutPackageFinder and FlatLayoutModuleFinder classes implementing a more careful discovery (more exclude patterns) since they are meant to be used directly with the project directory.
  • ConfigDiscovery changes the Distribution object just before running commands.
    Unfortunately, it is not possible to call ConfigDiscovery in finalize_options, since options coming from configuration files (such as setup.cfg) can be still missing (parsed in a later stage).

Please notice all these class/module names can be changed upon suggestion. I just adopted them for the lack of better ones 😝.

Closes #2887

Differences from #2888

In #2888, I assumed that name have to be statically defined (this assumption follows the ideas in PEP 621). Therefore it is possible to implement a more restricted discovery, only finding modules/packages that match the distribution name when the flat-layout is employed.

The implementation in this PR is "eager" (can find more files), since it does not rely on the given metadata.

Moreover this PR goes in the opposite direction and derives name for packages and py_modules as per the #2887 suggestion.

How the user can "exclude" modules/packages not meant for distribution?

One of the main motivations of this PR is to simplify package configuration. Currently there is already a lot of options for configuring setuptools behaviour (py_modules, packages and package_dir). Something I do not wish to do is to complicate even more the understanding of the current existing options by introducing new ones or overloading the existing ones.

Instead if the users want to change which files/folder are discovered they can manually specify them via the existing fields in setup.cfg, or switch to the src-layout (which is supposed to solve most of these problems) when possible. In other words, they have to configure setuptools as it was done before the changes in this PR (so the worst case scenario: automatic discovery doesn't change anything).

How about backward compatibility?

The same approach used in #2888 was adopted here: automatic values for packages and py_modules will only be discovered if the user don't provide both (which would result, I believe, in an empty distribution prior to this PR -- in the majority of the cases, this would mean that some configuration was missing/wrong).

Empty distributions are still possible (e.g. for those that wish to create "metapackages"), as long as they don't create Python module/packages in the project directory (files/folder associated with common tools are automatically excluded, e.g. docs, fabfile.py, tasks.py -- as well as "private"/"hidden" ones, i.e. files/folders starting with an _ or `.) -- which sound as a reasonable requirement for empty distributions.

Pull Request Checklist

print((root / "setup.cfg").read_text())


def _get_sdist_members(sdist_path):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both _get_sdist_members and _get_wheel_members could be added to the Archive test helper class in the case PR #2863 gets merged.

@abravalheri abravalheri marked this pull request as ready for review November 17, 2021 22:03
@abravalheri
Copy link
Contributor Author

PS: I am having some trouble with the CI. The macos instances seem to be suffering and throwing some unexpected errors when installing tox dependencies (which were kept unchanged regarding upstream/main), socket.timeout or weird OSError: [Errno 22] Invalid argument:.

Hopefully the implementation is not too broken (I run the tests under Ubuntu 20.04), but some OS-specific adjustment might be required for Windows/OSX.

"site_scons", # SCons
# ---- Hidden directories/Private packages ----
"[._]*",
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other directory that should be added here?

This comment was marked as resolved.

This comment was marked as resolved.

"manage", # Django
# ---- Hidden files/Private modules ----
"[._]*",
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other top-level Python file that should be added here?

@abravalheri
Copy link
Contributor Author

@henryiii this is the PR I prepared to improve automatic package files discovery.
It follows the topics discussed in #2887.

# and directories not meant for distribution (e.g. tool-related)


class FlatLayoutPackageFinder(PEP420PackageFinder):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe to be less intrusive the FlatLayoutPackageFinder should inherit from PackageFinder?

abravalheri added a commit to abravalheri/setuptools that referenced this pull request Feb 20, 2022
…pa#2894)

Adds automatic discovery of `packages`, `py_modules` and `name` for
`flat-` and `src-` layouts.
abravalheri added a commit that referenced this pull request Feb 20, 2022
)

Adds automatic discovery of `packages`, `py_modules` and `name` for
`flat-` and `src-` layouts.
setuptools/discovery.py Outdated Show resolved Hide resolved
Following up the discussion in pypa#2887 and pypa#2329, it seems that setuptools
is moving towards more automatic discovery features.

PackageFinder and PEP420PackageFinder are fundamental pieces of this
puzzle and grouping together them togheter with the code implementing these
new discovery features make a lot of sense.
abravalheri added a commit that referenced this pull request Mar 5, 2022
It is desirable to have some configurations automatically derived.
This is definitely possible for packages and py_modules, and (based on
these two) also for name.

This change adds a new class `setuptools.discovery.ConfigDiscovery`
implementing the automatic discovery logic for packages, py_modules and
name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR] Default values for packages and py_modules
2 participants