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

Use up-to-date namespace package setup for plugins #5505

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

snejus
Copy link
Member

@snejus snejus commented Nov 15, 2024

Description

Refactor beetsplug to use native namespace packages by removing __init__.py. Update documentation and setup.cfg to support namespace packages.

Changes

  • Deleted beetsplug/__init__.py.
  • Updated docs/dev/plugins.rst to reflect namespace package structure.
  • Updated mypy configuration in setup.cfg

Motivation

Adopt PEP 420 native namespace packages to simplify plugin management and eliminate the need for __init__.py.

See https://realpython.com/python-namespace-package.

This setup is backwards-compatible, so plugins using the old pkgutil-based setup will continue working fine.

The advantage with this setup is that external plugins will now be able to import modules from 'beetsplug' package for typing purposes. Previously, mypy could not resolve these modules due to presence of __init__.py.

@snejus snejus self-assigned this Nov 15, 2024
@snejus snejus requested a review from bal-e November 15, 2024 07:01
@snejus snejus force-pushed the remove-outdated-pkgutil-namespace-package branch from 2acfa64 to fd4e599 Compare November 15, 2024 07:04
@snejus snejus requested a review from Serene-Arc November 22, 2024 06:55
@snejus snejus force-pushed the remove-outdated-pkgutil-namespace-package branch 2 times, most recently from 2a0ee8b to 7981f43 Compare January 19, 2025 16:07
@snejus snejus requested review from JOJ0 and removed request for bal-e January 19, 2025 16:24
@JOJ0 JOJ0 self-assigned this Jan 24, 2025
@snejus snejus force-pushed the remove-outdated-pkgutil-namespace-package branch from 7981f43 to 9ebb51d Compare January 27, 2025 10:44
Copy link
Member

@JOJ0 JOJ0 left a comment

Choose a reason for hiding this comment

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

Built the docs and read them. I like the "attention" box. Some suggestions within.

What I miss in general in our docs is for plugin developers if there is anything specific they need to do in there pyproject.toml or setup.py files.

I'm not sure but is it enough to do this in a setup.py?

packages=find_packages(),

or is it better to do this:

packages=['beetsplug.myawesomeplugin'],

Here is some information about it: https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#finding-simple-packages

Also the realpython article states a namespaces=True directive for a pyproject.toml. Is this required?

[tool.setuptools.packages.find]
where = ["."]
include = ["snake_corp"]
namespaces = true

Should we add the most important parts that need to be in the package config. Maybe only focusing on pyproject.toml and setup.py verisions (I suppose they are the most common) and maybe also link to the above? What do you think? Too much information? Less is more?

docs/dev/plugins.rst Outdated Show resolved Hide resolved
You do not anymore need to add a ``__init__.py`` file to the ``beetsplug``
directory. Python treats your plugin as a namespace package automatically,
thus we do not depend on ``pkgutil``-based setup in the ``__init__.py``
file anymore.
Copy link
Member

Choose a reason for hiding this comment

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

If the general info about (current) namespace packages is already explained in the realpython article, maybe here you can point out this chapter of the python docs just to make sure we document everything potentially interesting for plugin developers (and also those who adopt old/orphaned plugins;-)): https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#legacy-namespace-packages

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed most of this section and added links to poetry and setuptools

setup.cfg Show resolved Hide resolved
@snejus
Copy link
Member Author

snejus commented Jan 28, 2025

Built the docs and read them. I like the "attention" box. Some suggestions within.

What I miss in general in our docs is for plugin developers if there is anything specific they need to do in there pyproject.toml or setup.py files.

I'm not sure but is it enough to do this in a setup.py?

packages=find_packages(),

or is it better to do this:

packages=['beetsplug.myawesomeplugin'],

Here is some information about it: https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#finding-simple-packages

I prefer explicit (latter) over implicit. For example in beetcamp the following works fine:

packages = [{ include = "beetsplug" }]

I'm not sure we should include these instructions in the docs since we've got a variety of build tools and each has its own instructions. We haven't got anything special going on here so as long as they somehow include the beetsplug directory in their package things will work fine. Addings links to setuptools and poetry docs is a good idea though!

See https://realpython.com/python-namespace-package.

This setup is backwards-compatible, so plugins using the old
pkgutil-based setup will continue working fine.

This setup has an advantage where external plugins will now be able to
import modules from 'beetsplug' package for typing purposes. Previously,
mypy could not resolve these modules due to presence of `__init__.py`.
@snejus snejus force-pushed the remove-outdated-pkgutil-namespace-package branch from c0b7ed2 to 89f1ef4 Compare January 30, 2025 12:34
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.

2 participants