-
Notifications
You must be signed in to change notification settings - Fork 192
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
QueryBuilder
: add flat
keyword to first
method
#5410
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
An instance of one of the implementation classes becomes a member of the :func:`QueryBuilder` instance | ||
when instantiated by the user. | ||
""" | ||
from __future__ import annotations | ||
|
||
from copy import deepcopy | ||
from inspect import isclass as inspect_isclass | ||
from typing import ( | ||
|
@@ -27,6 +29,7 @@ | |
Dict, | ||
Iterable, | ||
List, | ||
Literal, | ||
NamedTuple, | ||
Optional, | ||
Sequence, | ||
|
@@ -35,6 +38,7 @@ | |
Type, | ||
Union, | ||
cast, | ||
overload, | ||
) | ||
import warnings | ||
|
||
|
@@ -989,20 +993,36 @@ def _get_aiida_entity_res(value) -> Any: | |
except TypeError: | ||
return value | ||
|
||
def first(self) -> Optional[List[Any]]: | ||
"""Executes the query, asking for the first row of results. | ||
@overload | ||
def first(self, flat: Literal[False]) -> Optional[list[Any]]: | ||
... | ||
|
||
@overload | ||
def first(self, flat: Literal[True]) -> Optional[Any]: | ||
... | ||
|
||
def first(self, flat: bool = False) -> Optional[list[Any] | Any]: | ||
"""Return the first result of the query. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think it is important to indicate this actually executes a query, i.e. connects to the storage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a comment in docstring in vein of SQLA docs |
||
|
||
Note, this may change if several rows are valid for the query, | ||
as persistent ordering is not guaranteed unless explicitly specified. | ||
Calling ``first`` results in an execution of the underlying query. | ||
|
||
Note, this may change if several rows are valid for the query, as persistent ordering is not guaranteed unless | ||
explicitly specified. | ||
|
||
:param flat: if True, return just the projected quantity if there is just a single projection. | ||
:returns: One row of results as a list, or None if no result returned. | ||
""" | ||
result = self._impl.first(self.as_dict()) | ||
|
||
if result is None: | ||
return None | ||
Comment on lines
1017
to
1018
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sphuber it can return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I saw that, but take the first hit existing_upf = builder.first()
if existing_upf is None:
...
else:
existing_upf = existing_upf[0] That looks like a false positive to me. If anything this would be the perfect candidate for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same goes for the next "hits"
Also false positives. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair, then IMO, I would just disable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eurgh could also be overload, could be future annotations: pylint-dev/pylint#5189, pylint-dev/pylint#3979, pylint-dev/pylint#4369 |
||
|
||
return [self._get_aiida_entity_res(rowitem) for rowitem in result] | ||
result = [self._get_aiida_entity_res(rowitem) for rowitem in result] | ||
|
||
if flat and len(result) == 1: | ||
return result[0] | ||
|
||
return result | ||
|
||
def count(self) -> int: | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will approve anyway, but you are kind of mixing old and new type annotations in these changes