From 58417526cb1f871b5a1dcf0a7e03c46cf3064eca Mon Sep 17 00:00:00 2001 From: discopatrick Date: Thu, 20 Apr 2017 14:28:36 +0100 Subject: [PATCH 1/3] Rename `InvalidQueryArgumentTypeError` to `InvalidQueryArgumentValueError` The way we use `InvalidQueryArgumentTypeError` is more akin to a `ValueError` than a `TypeError`. For example, we try to parse a string as an int, float, or date, but the parsing fails - there was nothing wrong with the type of the variable (string), but its contents were not parseable into the type we wanted - there was a problem with the value of the string. --- beets/dbcore/query.py | 24 ++++++++++++------------ beets/library.py | 2 +- test/test_datequery.py | 8 ++++---- test/test_query.py | 6 +++--- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index aa8aa4af88..a6040c7d10 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -47,7 +47,7 @@ def __init__(self, query, explanation): super(InvalidQueryError, self).__init__(message) -class InvalidQueryArgumentTypeError(ParsingError): +class InvalidQueryArgumentValueError(ParsingError): """Represent a query argument that could not be converted as expected. It exists to be caught in upper stack levels so a meaningful (i.e. with the @@ -57,7 +57,7 @@ def __init__(self, what, expected, detail=None): message = u"'{0}' is not {1}".format(what, expected) if detail: message = u"{0}: {1}".format(message, detail) - super(InvalidQueryArgumentTypeError, self).__init__(message) + super(InvalidQueryArgumentValueError, self).__init__(message) class Query(object): @@ -211,9 +211,9 @@ def __init__(self, field, pattern, fast=True): self.pattern = re.compile(self.pattern) except re.error as exc: # Invalid regular expression. - raise InvalidQueryArgumentTypeError(pattern, - u"a regular expression", - format(exc)) + raise InvalidQueryArgumentValueError(pattern, + u"a regular expression", + format(exc)) @staticmethod def _normalize(s): @@ -285,7 +285,7 @@ def _convert(self, s): try: return float(s) except ValueError: - raise InvalidQueryArgumentTypeError(s, u"an int or a float") + raise InvalidQueryArgumentValueError(s, u"an int or a float") def __init__(self, field, pattern, fast=True): super(NumericQuery, self).__init__(field, pattern, fast) @@ -548,7 +548,7 @@ def __init__(self, date, precision): @classmethod def parse(cls, string): """Parse a date and return a `Period` object, or `None` if the - string is empty, or raise an InvalidQueryArgumentTypeError if + string is empty, or raise an InvalidQueryArgumentValueError if the string could not be parsed to a date. """ if not string: @@ -556,15 +556,15 @@ def parse(cls, string): ordinal = string.count('-') if ordinal >= len(cls.date_formats): # Too many components. - raise InvalidQueryArgumentTypeError(string, - 'a valid datetime string') + raise InvalidQueryArgumentValueError(string, + 'a valid datetime string') date_format = cls.date_formats[ordinal] try: date = datetime.strptime(string, date_format) except ValueError: # Parsing failed. - raise InvalidQueryArgumentTypeError(string, - 'a valid datetime string') + raise InvalidQueryArgumentValueError(string, + 'a valid datetime string') precision = cls.precisions[ordinal] return cls(date, precision) @@ -686,7 +686,7 @@ def _convert(self, s): try: return float(s) except ValueError: - raise InvalidQueryArgumentTypeError( + raise InvalidQueryArgumentValueError( s, u"a M:SS string or a float") diff --git a/beets/library.py b/beets/library.py index b263ecd646..56fd8f65b7 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1306,7 +1306,7 @@ def _fetch(self, model_cls, query, sort=None): query, parsed_sort = parse_query_string(query, model_cls) elif isinstance(query, (list, tuple)): query, parsed_sort = parse_query_parts(query, model_cls) - except dbcore.query.InvalidQueryArgumentTypeError as exc: + except dbcore.query.InvalidQueryArgumentValueError as exc: raise dbcore.InvalidQueryError(query, exc) # Any non-null sort specified by the parsed query overrides the diff --git a/test/test_datequery.py b/test/test_datequery.py index e81544aaad..8ca5680c68 100644 --- a/test/test_datequery.py +++ b/test/test_datequery.py @@ -22,7 +22,7 @@ import unittest import time from beets.dbcore.query import _parse_periods, DateInterval, DateQuery,\ - InvalidQueryArgumentTypeError + InvalidQueryArgumentValueError def _date(string): @@ -118,11 +118,11 @@ def test_single_day_nonmatch_fast(self): class DateQueryConstructTest(unittest.TestCase): def test_long_numbers(self): - with self.assertRaises(InvalidQueryArgumentTypeError): + with self.assertRaises(InvalidQueryArgumentValueError): DateQuery('added', '1409830085..1412422089') def test_too_many_components(self): - with self.assertRaises(InvalidQueryArgumentTypeError): + with self.assertRaises(InvalidQueryArgumentValueError): DateQuery('added', '12-34-56-78') def test_invalid_date_query(self): @@ -137,7 +137,7 @@ def test_invalid_date_query(self): '..2aa' ] for q in q_list: - with self.assertRaises(InvalidQueryArgumentTypeError): + with self.assertRaises(InvalidQueryArgumentValueError): DateQuery('added', q) diff --git a/test/test_query.py b/test/test_query.py index 3538c15a88..61df3ca101 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -30,7 +30,7 @@ from beets import dbcore from beets.dbcore import types from beets.dbcore.query import (NoneQuery, ParsingError, - InvalidQueryArgumentTypeError) + InvalidQueryArgumentValueError) from beets.library import Library, Item from beets import util import platform @@ -301,11 +301,11 @@ def test_numeric_search_negative(self): self.assertFalse(results) def test_invalid_query(self): - with self.assertRaises(InvalidQueryArgumentTypeError) as raised: + with self.assertRaises(InvalidQueryArgumentValueError) as raised: dbcore.query.NumericQuery('year', u'199a') self.assertIn(u'not an int', six.text_type(raised.exception)) - with self.assertRaises(InvalidQueryArgumentTypeError) as raised: + with self.assertRaises(InvalidQueryArgumentValueError) as raised: dbcore.query.RegexpQuery('year', u'199(') exception_text = six.text_type(raised.exception) self.assertIn(u'not a regular expression', exception_text) From 63cd799e8d4e7ecb43878562d4ff2007931b6282 Mon Sep 17 00:00:00 2001 From: discopatrick Date: Sat, 22 Apr 2017 00:56:52 +0100 Subject: [PATCH 2/3] Raise the correct error type The incorrect error type was reintroduced in the previous merge commit. --- beets/dbcore/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index c1150c9c4b..558a434bc2 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -562,7 +562,7 @@ def parse(cls, string): # Parsing failed. pass if date is None: - raise InvalidQueryArgumentTypeError(string, + raise InvalidQueryArgumentValueError(string, 'a valid datetime string') precision = cls.precisions[ordinal] return cls(date, precision) From d24b373c8715d54e12b54334ce062a4b2bc44287 Mon Sep 17 00:00:00 2001 From: discopatrick Date: Sat, 22 Apr 2017 01:11:48 +0100 Subject: [PATCH 3/3] Adjust indentation to pass flake8 tests --- beets/dbcore/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 558a434bc2..e532ed4195 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -563,7 +563,7 @@ def parse(cls, string): pass if date is None: raise InvalidQueryArgumentValueError(string, - 'a valid datetime string') + 'a valid datetime string') precision = cls.precisions[ordinal] return cls(date, precision)