Skip to content

Commit 7a33634

Browse files
committed
Use TypeError for bad prepared stmt param types
1 parent 9d17030 commit 7a33634

3 files changed

Lines changed: 13 additions & 39 deletions

File tree

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Other
2121
* Remove ignored ``tracing_enabled`` parameter for ``SimpleStatement``. The
2222
correct way to trace a query is by setting the ``trace`` argument to ``True``
2323
in ``Session.execute()`` and ``Session.execute_async()``.
24+
* Raise TypeError instead of cassandra.query.InvalidParameterTypeError when
25+
a parameter for a prepared statement has the wrong type; remove
26+
cassandra.query.InvalidParameterTypeError.
2427

2528
1.0.1
2629
=====

cassandra/query.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ def bind(self, values):
238238
expected_type = col_type
239239
actual_type = type(value)
240240

241-
err = InvalidParameterTypeError(col_name=col_name,
242-
expected_type=expected_type,
243-
actual_type=actual_type)
244-
raise err
241+
message = ('Received an argument of invalid type for column "%s". '
242+
'Expected: %s, Got: %s' % (col_name, expected_type, actual_type))
243+
raise TypeError(message)
245244

246245
return self
247246

@@ -319,24 +318,6 @@ class TraceUnavailable(Exception):
319318
pass
320319

321320

322-
class InvalidParameterTypeError(TypeError):
323-
"""
324-
Raised when a used tries to bind a prepared statement with an argument of an
325-
invalid type.
326-
"""
327-
328-
def __init__(self, col_name, expected_type, actual_type):
329-
self.col_name = col_name
330-
self.expected_type = expected_type
331-
self.actual_type = actual_type
332-
333-
values = (self.col_name, self.expected_type, self.actual_type)
334-
message = ('Received an argument of invalid type for column "%s". '
335-
'Expected: %s, Got: %s' % values)
336-
337-
super(InvalidParameterTypeError, self).__init__(message)
338-
339-
340321
class QueryTrace(object):
341322
"""
342323
A trace of the duration and events that occurred when executing

tests/unit/test_parameter_binding.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from cassandra.query import bind_params, ValueSequence
77
from cassandra.query import PreparedStatement, BoundStatement
8-
from cassandra.query import InvalidParameterTypeError
98
from cassandra.cqltypes import Int32Type
109

1110
try:
@@ -77,31 +76,22 @@ def test_invalid_argument_type(self):
7776

7877
values = ['nonint', 1]
7978

80-
try:
81-
bound_statement.bind(values)
82-
except InvalidParameterTypeError as e:
83-
self.assertEqual(e.col_name, 'foo1')
84-
self.assertEqual(e.expected_type, Int32Type)
85-
self.assertEqual(e.actual_type, str)
86-
else:
87-
self.fail('Passed invalid type but exception was not thrown')
88-
8979
try:
9080
bound_statement.bind(values)
9181
except TypeError as e:
92-
self.assertEqual(e.col_name, 'foo1')
93-
self.assertEqual(e.expected_type, Int32Type)
94-
self.assertEqual(e.actual_type, str)
82+
self.assertIn('foo1', str(e))
83+
self.assertIn('Int32Type', str(e))
84+
self.assertIn('str', str(e))
9585
else:
9686
self.fail('Passed invalid type but exception was not thrown')
9787

9888
values = [1, ['1', '2']]
9989

10090
try:
10191
bound_statement.bind(values)
102-
except InvalidParameterTypeError as e:
103-
self.assertEqual(e.col_name, 'foo2')
104-
self.assertEqual(e.expected_type, Int32Type)
105-
self.assertEqual(e.actual_type, list)
92+
except TypeError as e:
93+
self.assertIn('foo2', str(e))
94+
self.assertIn('Int32Type', str(e))
95+
self.assertIn('list', str(e))
10696
else:
10797
self.fail('Passed invalid type but exception was not thrown')

0 commit comments

Comments
 (0)