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

Add support for subclassing in Groups to the QueryBuilder #3903

Merged

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Apr 7, 2020

Fixes #3902

This now allows to search for instances of Group and all its
subclasses through the QueryBuilder just as implemented for the Node
class. The subclassing is based on the type_string of the group
instances, which in turn is defined by the entry point name. When
querying for Group instances through the QueryBuilder:

QueryBuilder().append(Group).all()

by default subclassing is enabled and should return all instances of a
subclass of Group as well. The same goes for subclasses of Group
themselves. Imagine a plugin defines the following entry points:

'plugin.sub = aiida_plugin.groups:SubGroup'
'plugin.sub.a = aiida_plugin.groups:SubAGroup'
'plugin.sub.b = aiida_plugin.groups:SubBGroup'

The following query:

QueryBuilder().append(SubGroup).all()

will return all instances of SubGroup but also of SubAGroup and
SubBGroup. This is because their entry point names both start with the
entry point name plugin.sub of the class that is being appended.

Note that in order for all group instances to be returned when querying
for the Group base class, a small hack had to be applied in the query
builder code building the filters. Note that the entry point name of
Group is core and so with the rules described above, the plugin
subclasses would never be matched since their entry point names do not
start with core. This is a problem and could be fixed by making the
type string of base group instances an empty string, however, this would
require using an empty string for the Group base class which is not
allowed in Python. To still provide the desired behavior, when querying
for Group instances the type_string filter is set to match anything.

@codecov
Copy link

codecov bot commented Apr 7, 2020

Codecov Report

Merging #3903 into develop will increase coverage by 0.04%.
The diff coverage is 93.93%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3903      +/-   ##
===========================================
+ Coverage    78.08%   78.13%   +0.04%     
===========================================
  Files          458      460       +2     
  Lines        33916    33978      +62     
===========================================
+ Hits         26485    26549      +64     
+ Misses        7431     7429       -2     
Flag Coverage Δ
#django 70.15% <81.81%> (+0.01%) ⬆️
#sqlalchemy 71.00% <85.45%> (+0.02%) ⬆️
Impacted Files Coverage Δ
aiida/orm/implementation/groups.py 72.30% <0.00%> (ø)
aiida/plugins/entry_point.py 88.18% <ø> (ø)
aiida/orm/nodes/data/upf.py 80.42% <75.00%> (-0.41%) ⬇️
aiida/tools/groups/paths.py 90.50% <82.35%> (+0.14%) ⬆️
aiida/plugins/factories.py 98.87% <87.50%> (-1.13%) ⬇️
aiida/orm/querybuilder.py 79.90% <94.87%> (+0.47%) ⬆️
aiida/orm/groups.py 91.66% <97.43%> (+4.27%) ⬆️
...s/djsite/db/migrations/0044_dbgroup_type_string.py 100.00% <100.00%> (ø)
aiida/backends/djsite/db/migrations/__init__.py 68.83% <100.00%> (ø)
...tions/versions/bf591f31dd12_dbgroup_type_string.py 100.00% <100.00%> (ø)
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d609e5e...cac9f8b. Read the comment docs.

@sphuber sphuber force-pushed the feature/3902/group-query-subclassing branch from 3053e06 to ac8470d Compare April 8, 2020 07:30
@sphuber sphuber force-pushed the feature/3902/group-query-subclassing branch 2 times, most recently from e0bc2e7 to 3ead824 Compare April 8, 2020 19:52
@sphuber sphuber requested review from chrisjsewell and removed request for chrisjsewell April 8, 2020 19:53
This now allows to search for instances of `Group` and all its
subclasses through the `QueryBuilder` just as implemented for the `Node`
class. The subclassing is based on the `type_string` of the group
instances, which in turn is defined by the entry point name. When
querying for `Group` instances through the `QueryBuilder`:

    QueryBuilder().append(Group).all()

by default subclassing is enabled and should return all instances of a
subclass of `Group` as well. The same goes for subclasses of `Group`
themselves. Imagine a plugin defines the following entry points:

    'plugin.sub = aiida_plugin.groups:SubGroup'
    'plugin.sub.a = aiida_plugin.groups:SubAGroup'
    'plugin.sub.b = aiida_plugin.groups:SubBGroup'

The following query:

    QueryBuilder().append(SubGroup).all()

will return all instances of `SubGroup` but also of `SubAGroup` and
`SubBGroup`. This is because their entry point names both start with the
entry point name `plugin.sub` of the class that is being appended.

Note that in order for all group instances to be returned when querying
for the `Group` base class, a small hack had to be applied in the query
builder code building the filters. Note that the entry point name of
`Group` is `core` and so with the rules described above, the plugin
subclasses would never be matched since their entry point names do not
start with `core`. This is a problem and could be fixed by making the
type string of base group instances an empty string, however, this would
require using an empty string for the `Group` base class which is not
allowed in Python. To still provide the desired behavior, when querying
for `Group` instances the `type_string` filter is set to match anything.
@sphuber sphuber force-pushed the feature/3902/group-query-subclassing branch from 3ead824 to cac9f8b Compare April 9, 2020 09:12
@sphuber sphuber requested a review from chrisjsewell April 9, 2020 09:13
Copy link
Member

@chrisjsewell chrisjsewell left a comment

Choose a reason for hiding this comment

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

Yep that looks good to me

@sphuber sphuber merged commit e5eae67 into aiidateam:develop Apr 9, 2020
@sphuber sphuber deleted the feature/3902/group-query-subclassing branch April 9, 2020 09:27
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.

Add support for querying of subclasses of Group in the QueryBuilder
2 participants