From 7a33634090d86c11b7a5bb92fd0133088e91d30d Mon Sep 17 00:00:00 2001 From: Tyler Hobbs Date: Wed, 26 Feb 2014 17:09:23 -0600 Subject: [PATCH] Use TypeError for bad prepared stmt param types --- CHANGELOG.rst | 3 +++ cassandra/query.py | 25 +++---------------------- tests/unit/test_parameter_binding.py | 24 +++++++----------------- 3 files changed, 13 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 535f5900..10aded84 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -21,6 +21,9 @@ Other * Remove ignored ``tracing_enabled`` parameter for ``SimpleStatement``. The correct way to trace a query is by setting the ``trace`` argument to ``True`` in ``Session.execute()`` and ``Session.execute_async()``. +* Raise TypeError instead of cassandra.query.InvalidParameterTypeError when + a parameter for a prepared statement has the wrong type; remove + cassandra.query.InvalidParameterTypeError. 1.0.1 ===== diff --git a/cassandra/query.py b/cassandra/query.py index c68ecd35..130c1437 100644 --- a/cassandra/query.py +++ b/cassandra/query.py @@ -238,10 +238,9 @@ class BoundStatement(Statement): expected_type = col_type actual_type = type(value) - err = InvalidParameterTypeError(col_name=col_name, - expected_type=expected_type, - actual_type=actual_type) - raise err + message = ('Received an argument of invalid type for column "%s". ' + 'Expected: %s, Got: %s' % (col_name, expected_type, actual_type)) + raise TypeError(message) return self @@ -319,24 +318,6 @@ class TraceUnavailable(Exception): pass -class InvalidParameterTypeError(TypeError): - """ - Raised when a used tries to bind a prepared statement with an argument of an - invalid type. - """ - - def __init__(self, col_name, expected_type, actual_type): - self.col_name = col_name - self.expected_type = expected_type - self.actual_type = actual_type - - values = (self.col_name, self.expected_type, self.actual_type) - message = ('Received an argument of invalid type for column "%s". ' - 'Expected: %s, Got: %s' % values) - - super(InvalidParameterTypeError, self).__init__(message) - - class QueryTrace(object): """ A trace of the duration and events that occurred when executing diff --git a/tests/unit/test_parameter_binding.py b/tests/unit/test_parameter_binding.py index 7706e6bc..3ed66647 100644 --- a/tests/unit/test_parameter_binding.py +++ b/tests/unit/test_parameter_binding.py @@ -5,7 +5,6 @@ except ImportError: from cassandra.query import bind_params, ValueSequence from cassandra.query import PreparedStatement, BoundStatement -from cassandra.query import InvalidParameterTypeError from cassandra.cqltypes import Int32Type try: @@ -77,21 +76,12 @@ class BoundStatementTestCase(unittest.TestCase): values = ['nonint', 1] - try: - bound_statement.bind(values) - except InvalidParameterTypeError as e: - self.assertEqual(e.col_name, 'foo1') - self.assertEqual(e.expected_type, Int32Type) - self.assertEqual(e.actual_type, str) - else: - self.fail('Passed invalid type but exception was not thrown') - try: bound_statement.bind(values) except TypeError as e: - self.assertEqual(e.col_name, 'foo1') - self.assertEqual(e.expected_type, Int32Type) - self.assertEqual(e.actual_type, str) + self.assertIn('foo1', str(e)) + self.assertIn('Int32Type', str(e)) + self.assertIn('str', str(e)) else: self.fail('Passed invalid type but exception was not thrown') @@ -99,9 +89,9 @@ class BoundStatementTestCase(unittest.TestCase): try: bound_statement.bind(values) - except InvalidParameterTypeError as e: - self.assertEqual(e.col_name, 'foo2') - self.assertEqual(e.expected_type, Int32Type) - self.assertEqual(e.actual_type, list) + except TypeError as e: + self.assertIn('foo2', str(e)) + self.assertIn('Int32Type', str(e)) + self.assertIn('list', str(e)) else: self.fail('Passed invalid type but exception was not thrown')