beetbox / beets

music library manager and MusicBrainz tagger
http://beets.io/
MIT License
12.57k stars 1.8k forks source link

Simplifying item/album handling in the plugin API #5310

Open bal-e opened 2 weeks ago

bal-e commented 2 weeks ago

I'm trying to improve the type annotations in beets/plugins.py, and I noticed that the plugin API deals with item and album types really weirdly. Plugins are expected to (optionally) define item_types and album_types (which are not defined in the BeetsPlugin superclass, losing out on type information), which are then dynamically queried using getattr(..., f"{cls.__name__.lower()}_types") for the Item and Album classes. The same applies to {item,album}_queries. See here.

Getting good type-checking with such a system is really difficult, and it doesn't look like this is used for anything more than the Item and Album types. I propose deprecating any functions relying on such a system (e.g. plugins.types() and plugins.named_queries()). There are two possible solutions here:

  1. Support the possibility of other kinds of objects. Define a single field for {item,album}_types, as a dict mapping from the kind of object (either as a string or the Item / Album classes) to a list of database types specific to that kind of object.

  2. Only allow for Item and Album. Explicitly define item_types and album_types in BeetsPlugin and provide plugins.item_types() and plugins.album_types().

I'm happy to make a PR implementing either solution. Regardless of what is picked, the old functions can be maintained for backward compatibility with external code (should any exist?).