typing: type beets.dbcore.query

This commit is contained in:
Šarūnas Nejus
2026-07-06 15:22:02 +01:00
parent a000232509
commit b1701c2e12

View File

@@ -47,7 +47,9 @@ class InvalidQueryError(ParsingError):
The query should be a unicode string or a list, which will be space-joined.
"""
def __init__(self, query, explanation):
def __init__(
self, query: str | Sequence[str] | Query, explanation: Exception
) -> None:
if isinstance(query, list):
query = " ".join(query)
message = f"'{query}': {explanation}"
@@ -61,7 +63,9 @@ class InvalidQueryArgumentValueError(ParsingError):
query) InvalidQueryError can be raised.
"""
def __init__(self, what, expected, detail=None):
def __init__(
self, what: str, expected: str, detail: str | None = None
) -> None:
message = f"'{what}' is not {expected}"
if detail:
message = f"{message}: {detail}"
@@ -89,7 +93,7 @@ class Query(ABC):
"""
@abstractmethod
def match(self, obj: Model):
def match(self, obj: Model) -> bool:
"""Check whether this query matches a given Model. Can be used to
perform queries on arbitrary sets of Model.
"""
@@ -100,7 +104,7 @@ class Query(ABC):
def __repr__(self) -> str:
return f"{self.__class__.__name__}()"
def __eq__(self, other) -> bool:
def __eq__(self, other: object) -> bool:
return type(self) is type(other)
def __hash__(self) -> int:
@@ -136,7 +140,7 @@ class FieldQuery(Query, Generic[P]):
"""Return a set with field names that this query operates on."""
return {self.field_name}
def __init__(self, field_name: str, pattern: P, fast: bool = True):
def __init__(self, field_name: str, pattern: P, fast: bool = True) -> None:
self.table, _, self.field_name = field_name.rpartition(".")
self.pattern = pattern
self.fast = fast
@@ -151,7 +155,7 @@ class FieldQuery(Query, Generic[P]):
return None, ()
@classmethod
def value_match(cls, pattern: P, value: Any):
def value_match(cls, pattern: P, value: Any) -> bool:
"""Determine whether the value matches the pattern."""
raise NotImplementedError
@@ -164,7 +168,7 @@ class FieldQuery(Query, Generic[P]):
f"fast={self.fast})"
)
def __eq__(self, other) -> bool:
def __eq__(self, other: object) -> bool:
return (
super().__eq__(other)
and self.field_name == other.field_name
@@ -191,7 +195,7 @@ class MatchQuery(FieldQuery[AnySQLiteType]):
class NoneQuery(FieldQuery[None]):
"""A query that checks whether a field is null."""
def __init__(self, field, fast: bool = True):
def __init__(self, field: str, fast: bool = True) -> None:
super().__init__(field, None, fast)
def col_clause(self) -> tuple[str, Sequence[SQLiteType]]:
@@ -210,7 +214,7 @@ class StringFieldQuery(FieldQuery[P]):
"""
@classmethod
def value_match(cls, pattern: P, value: Any):
def value_match(cls, pattern: P, value: Any) -> bool:
"""Determine whether the value matches the pattern. The value
may have any type.
"""
@@ -371,7 +375,9 @@ class RegexpQuery(StringFieldQuery[Pattern[str]]):
expression.
"""
def __init__(self, field_name: str, pattern: str, fast: bool = True):
def __init__(
self, field_name: str, pattern: str, fast: bool = True
) -> None:
pattern = self._normalize(pattern)
try:
pattern_re = re.compile(pattern)
@@ -403,7 +409,9 @@ class BooleanQuery(MatchQuery[int]):
string reflecting a boolean.
"""
def __init__(self, field_name: str, pattern: bool, fast: bool = True):
def __init__(
self, field_name: str, pattern: bool, fast: bool = True
) -> None:
if isinstance(pattern, str):
pattern = util.str2bool(pattern)
@@ -438,7 +446,9 @@ class NumericQuery(FieldQuery[str]):
except ValueError:
raise InvalidQueryArgumentValueError(s, "an int or a float")
def __init__(self, field_name: str, pattern: str, fast: bool = True):
def __init__(
self, field_name: str, pattern: str, fast: bool = True
) -> None:
super().__init__(field_name, pattern, fast)
parts = pattern.split("..", 1)
@@ -520,7 +530,7 @@ class CollectionQuery(Query):
"""Return a set with field names that this query operates on."""
return reduce(or_, (sq.field_names for sq in self.subqueries))
def __init__(self, subqueries: Sequence[Query] = ()):
def __init__(self, subqueries: Sequence[Query] = ()) -> None:
self.subqueries = subqueries
# Act like a sequence.
@@ -528,13 +538,13 @@ class CollectionQuery(Query):
def __len__(self) -> int:
return len(self.subqueries)
def __getitem__(self, key):
def __getitem__(self, key: int) -> Query:
return self.subqueries[key]
def __iter__(self) -> Iterator[Query]:
return iter(self.subqueries)
def __contains__(self, subq) -> bool:
def __contains__(self, subq: Query) -> bool:
return subq in self.subqueries
def clause_with_joiner(
@@ -558,7 +568,7 @@ class CollectionQuery(Query):
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.subqueries!r})"
def __eq__(self, other) -> bool:
def __eq__(self, other: object) -> bool:
return super().__eq__(other) and self.subqueries == other.subqueries
def __hash__(self) -> int:
@@ -575,10 +585,10 @@ class MutableCollectionQuery(CollectionQuery):
subqueries: MutableSequence[Query]
def __setitem__(self, key, value):
def __setitem__(self, key: int, value: Query) -> None:
self.subqueries[key] = value
def __delitem__(self, key):
def __delitem__(self, key: int) -> None:
del self.subqueries[key]
@@ -612,7 +622,7 @@ class NotQuery(Query):
"""Return a set with field names that this query operates on."""
return self.subquery.field_names
def __init__(self, subquery):
def __init__(self, subquery: Query) -> None:
self.subquery = subquery
def clause(self) -> tuple[str | None, Sequence[SQLiteType]]:
@@ -629,7 +639,7 @@ class NotQuery(Query):
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.subquery!r})"
def __eq__(self, other) -> bool:
def __eq__(self, other: object) -> bool:
return super().__eq__(other) and self.subquery == other.subquery
def __hash__(self) -> int:
@@ -696,7 +706,7 @@ class Period:
}
relative_re = "(?P<sign>[+|-]?)(?P<quantity>[0-9]+)(?P<timespan>[y|m|w|d])"
def __init__(self, date: datetime, precision: str):
def __init__(self, date: datetime, precision: str) -> None:
"""Create a period with the given date (a `datetime` object) and
precision (a string, one of "year", "month", "day", "hour", "minute",
or "second").
@@ -798,7 +808,7 @@ class DateInterval:
A right endpoint of None means towards infinity.
"""
def __init__(self, start: datetime | None, end: datetime | None):
def __init__(self, start: datetime | None, end: datetime | None) -> None:
if start is not None and end is not None and not start < end:
raise ValueError(f"start date {start} is not before end date {end}")
self.start = start
@@ -834,7 +844,9 @@ class DateQuery(FieldQuery[str]):
using an ellipsis interval syntax similar to that of NumericQuery.
"""
def __init__(self, field_name: str, pattern: str, fast: bool = True):
def __init__(
self, field_name: str, pattern: str, fast: bool = True
) -> None:
super().__init__(field_name, pattern, fast)
start, end = _parse_periods(pattern)
self.interval = DateInterval.from_periods(start, end)
@@ -908,7 +920,9 @@ class SingletonQuery(FieldQuery[str]):
and singleton:false, singleton:0 are handled consistently.
"""
def __new__(cls, field: str, value: str, *args, **kwargs):
def __new__(
cls, field: str, value: str, *args: Any, **kwargs: Any
) -> Query:
query = NoneQuery("album_id")
if util.str2bool(value):
return query