# Query parsing: numeric flex attributes

**URL:** <https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160>\
**Category:** Help\
**Created:** [March 16, 2020, 10:00am UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160 "2020-03-16T10:00:42Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 16, 2020, 10:00am UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/1 "2020-03-16T10:00:42Z")

</div>

Do you agree with me that this is unexpected behaviour?:

```
$ beet ls mood_happy:0.5..1 -f 

```

I get no results and by examining the query I see that the above query was parsed to:

```
AndQuery([SubstringQuery('mood_happy', '0.5..1', False)])

```

By hacking into the parser and making it yield this:

```
AndQuery([NumericQuery('mood_happy', '0.5..1', False)])

```

Everything works as expected.

I am working on the [beets-goingrunning plugin](https://discourse.beets.io/t/new-plugin-beets-goingrunning/1124/3) which makes extensive use of these numeric flex attributes. In the plugin I created a custom parser which is based on a hardcoded list of attribute names used to indicate for which attributes should the NumericQuery be used.

I don’t really like the idea of the hardcoded list (I might move it to the plugin’s configuration) but it does the job for now. Do you have any better ideas? Also, could this be something to be integrated into the core parser?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [March 16, 2020, 12:53pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/2 "2020-03-16T12:53:53Z")

</div>

You’ll want to mark your field `mood_happy` with an integer type:  
[https://beets.readthedocs.io/en/stable/dev/plugins.html#flexible-field-types](https://beets.readthedocs.io/en/stable/dev/plugins.html#flexible-field-types)

Then the query parsing will work as you expect it to.

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 16, 2020, 1:37pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/3 "2020-03-16T13:37:13Z")

</div>

WooooW! You guys have thought of everything 😲!

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 16, 2020, 8:15pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/4 "2020-03-16T20:15:09Z")

</div>

It works like a charm (I just cut out 200 lines of code). 😃

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 18, 2020, 11:19pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/5 "2020-03-18T23:19:15Z")

</div>

😢 but it makes my plugin incompatible with acousticbrainz…

```bash
Traceback (most recent call last):
  File "/Users/jackisback/opt/miniconda3/bin/beet", line 8, in <module>
    sys.exit(main())
  File "/Users/jackisback/opt/miniconda3/lib/python3.7/site-packages/beets/ui/ __init__.py", line 1266, in main
    _raw_main(args)
  File "/Users/jackisback/opt/miniconda3/lib/python3.7/site-packages/beets/ui/ __init__.py", line 1249, in _raw_main
    subcommands, plugins, lib = _setup(options, lib)
  File "/Users/jackisback/opt/miniconda3/lib/python3.7/site-packages/beets/ui/ __init__.py", line 1148, in _setup
    library.Item._types.update(plugins.types(library.Item))
  File "/Users/jackisback/opt/miniconda3/lib/python3.7/site-packages/beets/plugins.py", line 344, in types
    u'another type.'.format(plugin.name, field)
beets.plugins.PluginConflictException: Plugin goingrunning defines flexible field danceable which has already been defined with another type.

```

Do you have any ideas how it could be solved whilst keeping both plugins active?

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 18, 2020, 11:37pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/6 "2020-03-18T23:37:30Z")

</div>

Actually, it looks like a bug.

 ![Screen Shot 2020-03-19 at 00.32.33](https://canada1.discourse-cdn.com/flex029/uploads/beets/original/1X/c46179369e33de296dbceea499160d4b8ea5f1c1.png)

The `goingrunning` plugin and the `acousctibrainz` plugin define the danceable attribute with the exact same type: `'danceable': types.Float(6)`.

Looking at the check here:

> <https://github.com/adamjakab/Beets/blob/00c6d1439ee29b3b4592d6f65cfa57b1d1f96575/beets/plugins.py#L342>

the `plugin_types[field] != types[field]` is comparing the two instances and not their types. Of course they are different. In fact, this:

> if field in types and type(plugin\_types[field]) != type(types[field]):

works.

What do you think? Should I open an Issue for this?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [March 19, 2020, 1:14am UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/7 "2020-03-19T01:14:35Z")

</div>

Hi! Yes, please do open an issue for this! It would be great to fix it.

However, the right fix is unfortunately not to add `type(...)` calls. We want to compare the objects, which represent beets data types—not the _types_ of those types, which are Python classes. (I know it’s confusing.) The problem is that the type objects need to define ` __eq__ ` or ` __cmp__ ` in order to be comparable.

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 19, 2020, 8:58am UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/8 "2020-03-19T08:58:07Z")

</div>

> [@adrian](#):
>
> The problem is that the type objects need to define ` __eq__ ` or ` __cmp__ ` in order to be comparable.

Ok, let’s do it! I’ll create the issue and have a go at the fix. I think I understand what you mean by comparing not the built-in types but the classes those instances created from. I never used ` __eq__ ` or ` __cmp__ ` but there is always a first time.

I am also thinking about proposing a different way beet handles this issue. At the moment it exits with a `PluginConflictException`. What if we load the plugins in the order they are indicated in the configuration file and we adopt a “first plugin wins” strategy with only a warning when fields are redeclared with a different type? What do you think?

have a great day

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 19, 2020, 1:59pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/9 "2020-03-19T13:59:32Z")

</div>

My plugin suffers of this issue only in one specific point. That is when I parse the query string composed from the command line queries + those picked up from the configuration file for a specific selection. If I do not override the types (the way you showed me) the parsed query will be wrong (for my needs). If I do the application will crash because of the bug mentioned above. So, this would be a no-go situation.

However, I really want to create a new release of my plugin and I want it to work. And I also want it to play nicely along with the `acousticbrainz` plugin and potentially any other plugin that might declare these attributes.

So, this is what I came up with:

```python
    def _retrieve_library_items(self, training: Subview):
        full_query = self._gather_query_elements(training)

        # Store a copy of defined types and update them with our own overrides
        orig_types = library.Item._types.copy()
        override_types = common.get_item_attribute_type_overrides()
        library.Item._types.update(override_types)

        # Execute the query parsing (this will use our own overrides)
        parsed_query = parse_query_string(" ".join(full_query), Item)[0]

        # restore the original types
        library.Item._types = orig_types.copy()

        self._say("Song selection query: {}".format(parsed_query), log_only=True)
        return self.lib.items(parsed_query)

```

I don’t think it needs much explanation. It’s a hack but it works great. It works with AB plugin activated and the query gets parsed correctly.

Obviously I do not have the insight into Beets as you do.  
Do you see any issues with this solution?

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [March 19, 2020, 2:13pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/10 "2020-03-19T14:13:16Z")

</div>

Seems pretty good for now! Of course, the real fix will be in beets itself (or in just disabling one or the other plugin for now), but this should do in the interim.

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [March 19, 2020, 2:19pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/11 "2020-03-19T14:19:59Z")

</div>

I should also have mentioned: it should work to let one plugin specify the type. That is, if you have the acousticbrainz plugin enabled and it declares the types for all the fields you care about, that should be enough—it shouldn’t be necessary to _also_ declare the same type in your plugin. (Of course, that would prevent people who only want to use _one_ plugin from taking advantage of the types.)

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [March 19, 2020, 2:21pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/12 "2020-03-19T14:21:57Z")

</div>

Oops, and I missed this one above:

> [@jakabadambalazs](#):
>
> What if we load the plugins in the order they are indicated in the configuration file and we adopt a “first plugin wins” strategy with only a warning when fields are redeclared with a different type? What do you think?

To be honest, I think we should _not_ do this. It would make things more confusing for end users—they would be able to see that one plugin declares a field with type A, and another plugin makes it have type B, and it would be unclear why one plugin “succeeded” in this declaration and the other “failed.” I think it’s better for the plugins to just resolve their conflict and avoid declaring differing types, hence the ugly exception.

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 19, 2020, 2:30pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/13 "2020-03-19T14:30:56Z")

</div>

> [@adrian](#):
>
> That is, if you have the acousticbrainz plugin enabled and it declares the types for all the fields you care about, that should be enough—it shouldn’t be necessary to _also_ declare the same type in your plugin.

Sure, that totally makes sense. But my plugin can not depend on what the AB plugin declares or on how it declares them. Also, many people will not have the AB plugin enabled so I do need to have my own implementation that works.

At one point I also thought about checking if the AB plugin was loaded and only when it was not I declared my fields. It also worked. However, the ‘hack’ solution should be compatible also with other plugins not only AB.

---

<div class="post-metadata">

**Author:** ![jakabadambalazs](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jakabadambalazs/32/360_2.png) [@jakabadambalazs](https://discourse.beets.io/u/jakabadambalazs)\
**Post date:** [March 19, 2020, 2:32pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/14 "2020-03-19T14:32:56Z")

</div>

> [@adrian](#):
>
> To be honest, I think we should _not_ do this. It would make things more confusing for end users—they would be able to see that one plugin declares a field with type A, and another plugin makes it have type B, and it would be unclear why one plugin “succeeded” in this declaration and the other “failed.” I think it’s better for the plugins to just resolve their conflict and avoid declaring differing types, hence the ugly exception.

👍🏿 OK. It was a wild idea

---

<div class="post-metadata">

**Author:** ![k03n](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@k03n](https://discourse.beets.io/u/k03n)\
**Post date:** [May 1, 2020, 7:35pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/15 "2020-05-01T19:35:04Z")

</div>

I ran into the same issue as described by Adam above, but not while writing a plugin.  
Is there a way to declare the attribute type from the cli directly?

For reference: I have added an index field to sort and/or select series of albums out of a larger collection.

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [May 1, 2020, 9:03pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/16 "2020-05-01T21:03:00Z")

</div>

Yes! Use the types plugin:  
[https://beets.readthedocs.io/en/stable/plugins/types.html](https://beets.readthedocs.io/en/stable/plugins/types.html)

---

<div class="post-metadata">

**Author:** ![k03n](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@k03n](https://discourse.beets.io/u/k03n)\
**Post date:** [May 1, 2020, 9:32pm UTC](https://discourse.beets.io/t/query-parsing-numeric-flex-attributes/1160/17 "2020-05-01T21:32:36Z")

</div>

Great, thank you for your swift response!
