Debian Codegraph @

CONTRIBUTING.rst

raw
1 Contributing
2 ============
3
4 .. contents::
5 :depth: 3
6
7 Thank you!
8 ----------
9
10 First off, thank you for considering contributing to beets! It’s people like you
11 that make beets continue to succeed.
12
13 These guidelines describe how you can help most effectively. By following these
14 guidelines, you can make life easier for the development team as it indicates
15 you respect the maintainers’ time; in return, the maintainers will reciprocate
16 by helping to address your issue, review changes, and finalize pull requests.
17
18 Types of Contributions
19 ----------------------
20
21 We love to get contributions from our community—you! There are many ways to
22 contribute, whether you’re a programmer or not.
23
24 The first thing to do, regardless of how you'd like to contribute to the
25 project, is to check out our :doc:`Code of Conduct <code_of_conduct>` and to
26 keep that in mind while interacting with other contributors and users.
27
28 Non-Programming
29 ~~~~~~~~~~~~~~~
30
31 - Promote beets! Help get the word out by telling your friends, writing a blog
32 post, or discussing it on a forum you frequent.
33 - Improve the documentation_. It’s incredibly easy to contribute here: just find
34 a page you want to modify and hit the “Edit on GitHub” button in the
35 upper-right. You can automatically send us a pull request for your changes.
36 - GUI design. For the time being, beets is a command-line-only affair. But
37 that’s mostly because we don’t have any great ideas for what a good GUI should
38 look like. If you have those great ideas, please get in touch.
39 - Benchmarks. We’d like to have a consistent way of measuring speed improvements
40 in beets’ tagger and other functionality as well as a way of comparing beets’
41 performance to other tools. You can help by compiling a library of
42 freely-licensed music files (preferably with incorrect metadata) for testing
43 and measurement.
44 - Think you have a nice config or cool use-case for beets? We’d love to hear
45 about it! Submit a post to our `discussion board
46 <https://github.com/beetbox/beets/discussions/categories/show-and-tell>`__
47 under the “Show and Tell” category for a chance to get featured in `the docs
48 <https://beets.readthedocs.io/en/stable/guides/advanced.html>`__.
49 - Consider helping out fellow users by `responding to support requests
50 <https://github.com/beetbox/beets/discussions/categories/q-a>`__ .
51
52 Programming
53 ~~~~~~~~~~~
54
55 - As a programmer (even if you’re just a beginner!), you have a ton of
56 opportunities to get your feet wet with beets.
57 - For developing plugins, or hacking away at beets, there’s some good
58 information in the `“For Developers” section of the docs
59 <https://beets.readthedocs.io/en/stable/dev/>`__.
60
61 .. _development-tools:
62
63 Development Tools
64 +++++++++++++++++
65
66 In order to develop beets, you will need a few tools installed:
67
68 - uv_ for packaging, virtual environment and dependency management
69 - poethepoet_ to run tasks, such as linting, formatting, testing
70
71 Follow the `uv installation instructions
72 <https://docs.astral.sh/uv/getting-started/installation/>`__ to install uv_.
73
74 poethepoet_ is a stand-alone CLI tool. uv can install such tools in isolated
75 environments so their dependencies don't interfere with your system.
76
77 Then install poethepoet_:
78
79 ::
80
81 $ uv tool install "poethepoet>=0.26,<0.47.0"
82
83 .. _getting-the-source:
84
85 Getting the Source
86 ++++++++++++++++++
87
88 The easiest way to get started with the latest beets source is to clone the
89 repository and install ``beets`` in a local virtual environment using uv_. This
90 can be done with:
91
92 .. code-block:: bash
93
94 $ git clone https://github.com/beetbox/beets.git
95 $ cd beets
96 $ uv sync
97
98 This will install ``beets`` and all development dependencies into a ``.venv``
99 virtual environment. See ``uv sync --help`` for options, including installing
100 ``extra`` dependencies for plugins (e.g. ``uv sync --extra fetchart``).
101
102 To run commands within this virtual environment, either activate it:
103
104 ::
105
106 $ source .venv/bin/activate
107 $ pytest
108
109 Or prefix commands with ``uv run``:
110
111 ::
112
113 $ uv run pytest
114
115 Additionally, poethepoet_ task runner assists us with the most common
116 operations. Formatting, linting, testing are defined as ``poe`` tasks in
117 pyproject.toml_. Run:
118
119 ::
120
121 $ poe
122
123 to see all available tasks. They can be used like this, for example
124
125 .. code-block:: sh
126
127 $ poe lint # check code style
128 $ poe format # fix formatting issues
129 $ poe test # run tests
130 # ... fix failing tests
131 $ poe test --lf # re-run failing tests (note the additional pytest option)
132 $ poe check-types --pretty # check types with an extra option for mypy
133
134 Code Contribution Ideas
135 +++++++++++++++++++++++
136
137 - We maintain a set of `issues marked as “good first issue”
138 <https://github.com/beetbox/beets/labels/good%20first%20issue>`__. These are
139 issues that would serve as a good introduction to the codebase. Claim one and
140 start exploring!
141 - Like testing? Our `test coverage
142 <https://app.codecov.io/github/beetbox/beets>`__ is somewhat low. You can help
143 out by finding low-coverage modules or checking out other `testing-related
144 issues <https://github.com/beetbox/beets/labels/testing>`__.
145 - There are several ways to improve the tests in general (see :ref:`testing` and
146 some places to think about performance optimization (see `Optimization
147 <https://github.com/beetbox/beets/wiki/Optimization>`__).
148 - Not all of our code is up to our coding conventions. In particular, the
149 `library API documentation
150 <https://beets.readthedocs.io/en/stable/dev/library.html>`__ are currently
151 quite sparse. You can help by adding to the docstrings in the code and to the
152 documentation pages themselves. beets follows `PEP-257
153 <https://peps.python.org/pep-0257/>`__ for docstrings and in some places, we
154 also sometimes use `ReST autodoc syntax for Sphinx
155 <https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html>`__ to,
156 for example, refer to a class name.
157
158 Your First Contribution
159 -----------------------
160
161 If this is your first time contributing to an open source project, welcome! If
162 you are confused at all about how to contribute or what to contribute, take a
163 look at `this great tutorial <https://makeapullrequest.com/>`__, or stop by our
164 `discussion board`_ if you have any questions.
165
166 We maintain a list of issues we reserved for those new to open source labeled
167 `first timers only`_. Since the goal of these issues is to get users comfortable
168 with contributing to an open source project, please do not hesitate to ask any
169 questions.
170
171 .. _first timers only: https://github.com/beetbox/beets/issues?q=is%3Aopen+is%3Aissue+label%3A%22first+timers+only%22
172
173 How to Submit Your Work
174 -----------------------
175
176 Do you have a great bug fix, new feature, or documentation expansion you’d like
177 to contribute? Follow these steps to create a GitHub pull request and your code
178 will ship in no time.
179
180 1. Fork the beets repository and clone it (see above) to create a workspace.
181 2. Install pre-commit, following the instructions `here
182 <https://pre-commit.com/>`_.
183 3. Make your changes.
184 4. Add tests. If you’ve fixed a bug, write a test to ensure that you’ve actually
185 fixed it. If there’s a new feature or plugin, please contribute tests that
186 show that your code does what it says.
187 5. Add documentation. If you’ve added a new command flag, for example, find the
188 appropriate page under ``docs/`` where it needs to be listed.
189 6. Add a changelog entry to ``docs/changelog.rst`` near the top of the document.
190 7. Run the tests and style checker, see :ref:`testing`.
191 8. Push to your fork and open a pull request! We’ll be in touch shortly.
192 9. If you add commits to a pull request, please add a comment or re-request a
193 review after you push them since GitHub doesn’t automatically notify us when
194 commits are added.
195
196 Remember, code contributions have four parts: the code, the tests, the
197 documentation, and the changelog entry. Thank you for contributing!
198
199 .. admonition:: Ownership
200
201 If you are the owner of a plugin, please consider reviewing pull requests
202 that affect your plugin. If you are not the owner of a plugin, please
203 consider becoming one! You can do so by adding an entry to
204 ``.github/CODEOWNERS``. This way, you will automatically receive a review
205 request for pull requests that adjust the code that you own. If you have any
206 questions, please ask on our `discussion board`_.
207
208 The Code
209 --------
210
211 The documentation has a section on the `library API
212 <https://beets.readthedocs.io/en/stable/dev/library.html>`__ that serves as an
213 introduction to beets’ design.
214
215 Coding Conventions
216 ------------------
217
218 General
219 ~~~~~~~
220
221 There are a few coding conventions we use in beets:
222
223 - Whenever you access the library database, do so through the provided Library
224 methods or via a Transaction object. Never call ``lib.conn.*`` directly. For
225 example, do this:
226
227 .. code-block:: python
228
229 with g.lib.transaction() as tx:
230 rows = tx.query("SELECT DISTINCT {field} FROM {model._table} ORDER BY {sort_field}")
231
232 To fetch Item objects from the database, use lib.items(…) and supply a query
233 as an argument. Resist the urge to write raw SQL for your query. If you must
234 use lower-level queries into the database, do this, for example:
235
236 .. code-block:: python
237
238 with lib.transaction() as tx:
239 rows = tx.query("SELECT path FROM items WHERE album_id = ?", (album_id,))
240
241 Transaction objects help control concurrent access to the database and assist
242 in debugging conflicting accesses.
243
244 - f-strings should be used instead of the ``%`` operator and ``str.format()``
245 calls.
246 - Never ``print`` informational messages; use the `logging
247 <https://docs.python.org/3/library/logging.html>`__ module instead. In
248 particular, we have our own logging shim, so you’ll see ``from beets import
249 logging`` in most files.
250
251 - The loggers use `str.format
252 <https://docs.python.org/3/library/stdtypes.html>`__-style logging instead
253 of ``%``-style, so you can type ``log.debug("{}", obj)`` to do your
254 formatting.
255
256 - Exception handlers must use ``except A as B:`` instead of ``except A, B:``.
257
258 Style
259 ~~~~~
260
261 We use `ruff <https://docs.astral.sh/ruff/>`__ to format and lint the codebase.
262
263 Run ``poe check-format`` and ``poe lint`` to check your code for style and
264 linting errors. Running ``poe format`` will automatically format your code
265 according to the specifications required by the project.
266
267 Similarly, run ``poe format-docs`` and ``poe lint-docs`` to ensure consistent
268 documentation formatting and check for any issues.
269
270 Blame Ignore Revisions
271 ~~~~~~~~~~~~~~~~~~~~~~
272
273 The ``.git-blame-ignore-revs`` file lists mechanical commits that make history
274 harder to inspect, such as repository-wide formatting changes, large renames,
275 and other behavior-preserving rewrites.
276
277 Configure Git to use this file locally:
278
279 .. code-block:: sh
280
281 $ git config blame.ignoreRevsFile .git-blame-ignore-revs
282
283 When you make a mechanical-only change that should not distract future ``git
284 blame`` output, keep it in its own commit. Then, add its **commit summary** with
285 the commit hash to ``.git-blame-ignore-revs`` under the appropriate year. Do not
286 add commits that mix mechanical edits with behavior changes; those should remain
287 visible in blame output.
288
289 Editor Settings
290 ~~~~~~~~~~~~~~~
291
292 Personally, I work on beets with vim_. Here are some ``.vimrc`` lines that might
293 help with PEP 8-compliant Python coding:
294
295 ::
296
297 filetype indent on
298 autocmd FileType python setlocal shiftwidth=4 tabstop=4 softtabstop=4 expandtab shiftround autoindent
299
300 Consider installing `this alternative Python indentation plugin
301 <https://github.com/mitsuhiko/vim-python-combined>`__. I also like `neomake
302 <https://github.com/neomake/neomake>`__ with its flake8 checker.
303
304 .. _testing:
305
306 Testing
307 -------
308
309 Running the Tests
310 ~~~~~~~~~~~~~~~~~
311
312 Use ``poe`` to run tests:
313
314 ::
315
316 $ poe test [pytest options]
317
318 Coverage
319 ++++++++
320
321 The ``test`` command does not include coverage as it slows down testing. In
322 order to measure it, use the ``test-with-coverage`` task
323
324 $ poe test-with-coverage [pytest options]
325
326 You are welcome to explore coverage by opening the HTML report in
327 ``.reports/html/index.html``.
328
329 Note that for each covered line the report shows **which tests cover it**
330 (expand the list on the right-hand side of the affected line).
331
332 You can find project coverage status on Codecov_.
333
334 Red Flags
335 +++++++++
336
337 The pytest-random_ plugin makes it easy to randomize the order of tests. ``poe
338 test --random`` will occasionally turn up failing tests that reveal ordering
339 dependencies—which are bad news!
340
341 Test Dependencies
342 +++++++++++++++++
343
344 The tests have a few more dependencies than beets itself. (The additional
345 dependencies consist of testing utilities and dependencies of non-default
346 plugins exercised by the test suite.) The dependencies are listed under the
347 ``test`` group in the ``dependency-groups`` section of pyproject.toml_.
348
349 Writing Tests
350 ~~~~~~~~~~~~~
351
352 Writing tests is done by adding or modifying files in folder test_. Take a look
353 at test-query_ to get a basic view on how tests are written. Since we are
354 currently migrating the tests from unittest_ to pytest_, new tests should be
355 written using pytest_. Contributions migrating existing tests are welcome!
356
357 External API requests under test should be mocked with requests-mock_, However,
358 we still want to know whether external APIs are up and that they return expected
359 responses, therefore we test them weekly with our `integration test`_ suite.
360
361 In order to add such a test, mark your test with the ``integration_test`` marker
362
363 .. code-block:: python
364
365 @pytest.mark.integration_test
366 def test_external_api_call(): ...
367
368 This way, the test will be run only in the integration test suite.
369
370 beets also defines custom pytest markers in ``test/conftest.py``:
371
372 - ``integration_test``: runs only when ``INTEGRATION_TEST=true`` is set.
373 - ``on_lyrics_update``: runs only when ``LYRICS_UPDATED=true`` is set.
374 - ``requires_import("module", force_ci=True)``: runs the test only when the
375 module is importable. With the default ``force_ci=True``, this import check is
376 bypassed on GitHub Actions for ``beetbox/beets`` so CI still runs the test.
377 Set ``force_ci=False`` to allow CI to skip when the module is missing.
378
379 .. code-block:: python
380
381 @pytest.mark.integration_test
382 def test_external_api_call(): ...
383
384
385 @pytest.mark.on_lyrics_update
386 def test_real_lyrics_backend(): ...
387
388
389 @pytest.mark.requires_import("langdetect")
390 def test_language_detection(): ...
391
392
393 @pytest.mark.requires_import("librosa", force_ci=False)
394 def test_autobpm_command(): ...
395
396 Notes on AI Usage
397 -----------------
398
399 We are not opposed to AI-generated contributions, but communication should be
400 handled by a real person. We will likely have questions about your PR, and we
401 need you to understand the proposed changes in order to discuss with us what the
402 implications are.
403
404 > Currently we value human oversight and accountability, AI as a tool, not a
405 contributor.
406
407 .. _codecov: https://app.codecov.io/github/beetbox/beets
408
409 .. _discussion board: https://github.com/beetbox/beets/discussions
410
411 .. _documentation: https://beets.readthedocs.io/en/stable/
412
413 .. _integration test: https://github.com/beetbox/beets/actions?query=workflow%3A%22integration+tests%22
414
415 .. _poethepoet: https://poethepoet.natn.io/index.html
416
417 .. _pyproject.toml: https://github.com/beetbox/beets/blob/master/pyproject.toml
418
419 .. _pytest: https://docs.pytest.org/en/stable/
420
421 .. _pytest-random: https://github.com/klrmn/pytest-random
422
423 .. _requests-mock: https://requests-mock.readthedocs.io/en/latest/response.html
424
425 .. _test: https://github.com/beetbox/beets/tree/master/test
426
427 .. _test-query: https://github.com/beetbox/beets/blob/master/test/dbcore/test_query.py
428
429 .. _unittest: https://docs.python.org/3/library/unittest.html
430
431 .. _uv: https://docs.astral.sh/uv/
432
433 .. _vim: https://www.vim.org/