Skip to content

Commit

Permalink
fix several deprecation warnings
Browse files Browse the repository at this point in the history
adding -Werror flag to pytest configuration
so we can flush all of those with the test we have
so we won't have user of the driver running into those
and get them fixed address as soon as we support new
python versions
  • Loading branch information
fruch committed Jul 4, 2024
1 parent c53ff5c commit d498fa9
Show file tree
Hide file tree
Showing 26 changed files with 100 additions and 50 deletions.
2 changes: 1 addition & 1 deletion cassandra/cqlengine/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def setup(
:param int consistency: The global default :class:`~.ConsistencyLevel` - default is the same as :attr:`.Session.default_consistency_level`
:param bool lazy_connect: True if should not connect until first use
:param bool retry_connect: True if we should retry to connect even if there was a connection failure initially
:param \*\*kwargs: Pass-through keyword arguments for :class:`cassandra.cluster.Cluster`
:param kwargs: Pass-through keyword arguments for :class:`cassandra.cluster.Cluster`
"""

from cassandra.cqlengine import models
Expand Down
8 changes: 4 additions & 4 deletions cassandra/cqlengine/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def add_callback(self, fn, *args, **kwargs):
:param fn: Callable object
:type fn: callable
:param \*args: Positional arguments to be passed to the callback at the time of execution
:param \*\*kwargs: Named arguments to be passed to the callback at the time of execution
:param args: Positional arguments to be passed to the callback at the time of execution
:param kwargs: Named arguments to be passed to the callback at the time of execution
"""
if not callable(fn):
raise ValueError("Value for argument 'fn' is {0} and is not a callable object.".format(type(fn)))
Expand Down Expand Up @@ -277,8 +277,8 @@ class ContextQuery(object):
A Context manager to allow a Model to switch context easily. Presently, the context only
specifies a keyspace for model IO.
:param \*args: One or more models. A model should be a class type, not an instance.
:param \*\*kwargs: (optional) Context parameters: can be *keyspace* or *connection*
:param args: One or more models. A model should be a class type, not an instance.
:param kwargs: (optional) Context parameters: can be *keyspace* or *connection*
For example:
Expand Down
4 changes: 2 additions & 2 deletions cassandra/datastax/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

_HAS_SSL = True
try:
from ssl import SSLContext, PROTOCOL_TLS, CERT_REQUIRED
from ssl import SSLContext, PROTOCOL_TLS_CLIENT, CERT_REQUIRED
except:
_HAS_SSL = False

Expand Down Expand Up @@ -170,7 +170,7 @@ def parse_metadata_info(config, http_data):


def _ssl_context_from_cert(ca_cert_location, cert_location, key_location):
ssl_context = SSLContext(PROTOCOL_TLS)
ssl_context = SSLContext(PROTOCOL_TLS_CLIENT)
ssl_context.load_verify_locations(ca_cert_location)
ssl_context.verify_mode = CERT_REQUIRED
ssl_context.load_cert_chain(certfile=cert_location, keyfile=key_location)
Expand Down
2 changes: 1 addition & 1 deletion cassandra/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from cassandra import DriverException

DATETIME_EPOC = datetime.datetime(1970, 1, 1)
UTC_DATETIME_EPOC = datetime.datetime.utcfromtimestamp(0)
UTC_DATETIME_EPOC = datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc)

_nan = float('nan')

Expand Down
14 changes: 14 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@ log_format = %(asctime)s.%(msecs)03d %(levelname)s [%(module)s:%(lineno)s]: %(me
log_level = DEBUG
log_date_format = %Y-%m-%d %H:%M:%S
xfail_strict=true

filterwarnings =
error
ignore::pytest.PytestCollectionWarning
ignore::ResourceWarning
ignore:distutils Version classes are deprecated:DeprecationWarning:eventlet.support.greenlets
ignore:X509Extension support in pyOpenSSL is deprecated.:DeprecationWarning
ignore:CRL support in pyOpenSSL is deprecated:DeprecationWarning
ignore:sign\(\) is deprecated:DeprecationWarning
ignore:verify\(\) is deprecated:DeprecationWarning
ignore:pkg_resources is deprecated as an API:DeprecationWarning:gevent.events
ignore:.*pkg_resources.declare_namespace.*:DeprecationWarning
ignore:"@coroutine" decorator is deprecated since Python 3.8:DeprecationWarning:asynctest.*
ignore:The asyncore module is deprecated and will be removed in Python 3.12:DeprecationWarning:asyncore.*
2 changes: 1 addition & 1 deletion tests/integration/cqlengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def wrapped_function(*args, **kwargs):
# DeMonkey Patch our code
cassandra.cqlengine.connection.execute = original_function
# Check to see if we have a pre-existing test case to work from.
if len(args) is 0:
if len(args) == 0:
test_case = unittest.TestCase("__init__")
else:
test_case = args[0]
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/cqlengine/query/test_queryoperators.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ def test_named_table_pk_token_function(self):
query = named.all().limit(1)
first_page = list(query)
last = first_page[-1]
self.assertTrue(len(first_page) is 1)
self.assertTrue(len(first_page) == 1)
next_page = list(query.filter(pk__token__gt=functions.Token(last.key)))
self.assertTrue(len(next_page) is 1)
self.assertTrue(len(next_page) == 1)
3 changes: 2 additions & 1 deletion tests/integration/standard/test_cluster.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -150,7 +151,7 @@ def test_raise_error_on_control_connection_timeout(self):
get_node(1).pause()
cluster = TestCluster(contact_points=['127.0.0.1'], connect_timeout=1)

with self.assertRaisesRegex(NoHostAvailable, "OperationTimedOut\('errors=Timed out creating connection \(1 seconds\)"):
with self.assertRaisesRegex(NoHostAvailable, r"OperationTimedOut\('errors=Timed out creating connection \(1 seconds\)"):
cluster.connect()
cluster.shutdown()

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/standard/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def wait_for_connections(self, host, cluster):
while(retry < 300):
retry += 1
connections = self.fetch_connections(host, cluster)
if len(connections) is not 0:
if len(connections) != 0:
return connections
time.sleep(.1)
self.fail("No new connections found")
Expand All @@ -191,7 +191,7 @@ def wait_for_no_connections(self, host, cluster):
while(retry < 100):
retry += 1
connections = self.fetch_connections(host, cluster)
if len(connections) is 0:
if len(connections) == 0:
return
time.sleep(.5)
self.fail("Connections never cleared")
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/standard/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ def test_function_no_parameters(self):

with self.VerifiedFunction(self, **kwargs) as vf:
fn_meta = self.keyspace_function_meta[vf.signature]
self.assertRegex(fn_meta.as_cql_query(), "CREATE FUNCTION.*%s\(\) .*" % kwargs['name'])
self.assertRegex(fn_meta.as_cql_query(), r"CREATE FUNCTION.*%s\(\) .*" % kwargs['name'])

def test_functions_follow_keyspace_alter(self):
"""
Expand Down Expand Up @@ -1689,12 +1689,12 @@ def test_function_cql_called_on_null(self):
kwargs['called_on_null_input'] = True
with self.VerifiedFunction(self, **kwargs) as vf:
fn_meta = self.keyspace_function_meta[vf.signature]
self.assertRegex(fn_meta.as_cql_query(), "CREATE FUNCTION.*\) CALLED ON NULL INPUT RETURNS .*")
self.assertRegex(fn_meta.as_cql_query(), r"CREATE FUNCTION.*\) CALLED ON NULL INPUT RETURNS .*")

kwargs['called_on_null_input'] = False
with self.VerifiedFunction(self, **kwargs) as vf:
fn_meta = self.keyspace_function_meta[vf.signature]
self.assertRegex(fn_meta.as_cql_query(), "CREATE FUNCTION.*\) RETURNS NULL ON NULL INPUT RETURNS .*")
self.assertRegex(fn_meta.as_cql_query(), r"CREATE FUNCTION.*\) RETURNS NULL ON NULL INPUT RETURNS .*")


@requires_java_udf
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/standard/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_client_ip_in_trace(self):
client_ip = trace.client

# Ip address should be in the local_host range
pat = re.compile("127.0.0.\d{1,3}")
pat = re.compile(r"127.0.0.\d{1,3}")

# Ensure that ip is set
self.assertIsNotNone(client_ip, "Client IP was not set in trace with C* >= 2.2")
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/standard/test_scylla_cloud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import warnings
import os.path
from unittest import TestCase
from ccmlib.utils.ssl_utils import generate_ssl_stores
Expand All @@ -11,7 +12,9 @@
from cassandra.io.libevreactor import LibevConnection
supported_connection_classes = [LibevConnection, TwistedConnection]
try:
from cassandra.io.asyncorereactor import AsyncoreConnection
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning, message="The asyncore module is deprecated")
from cassandra.io.asyncorereactor import AsyncoreConnection
supported_connection_classes += [AsyncoreConnection]
except ImportError:
pass
Expand Down
1 change: 1 addition & 0 deletions tests/unit/advanced/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def test_with_graph_protocol(self):

def test_init_unknown_kwargs(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
GraphOptions(unknown_param=42)
self.assertEqual(len(w), 1)
self.assertRegex(str(w[0].message), r"^Unknown keyword.*GraphOptions.*")
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/advanced/test_insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


import unittest
import pytest

import logging
from mock import sentinel
Expand Down Expand Up @@ -103,6 +104,7 @@ def superclass_sentinel_serializer(obj):
class TestConfigAsDict(unittest.TestCase):

# graph/query.py
@pytest.mark.filterwarnings("ignore:Unknown keyword argument received for GraphOptions:UserWarning")
def test_graph_options(self):
self.maxDiff = None

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/advanced/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import pytest

from mock import Mock

Expand All @@ -29,6 +30,7 @@ def get_host(self, addr):
return self.hosts.get(addr)


@pytest.mark.filterwarnings("ignore:DSELoadBalancingPolicy will be removed:DeprecationWarning")
class DSELoadBalancingPolicyTest(unittest.TestCase):

def test_no_target(self):
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/cython/bytesio_testhelper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# cython: language_level=3

from cassandra.bytesio cimport BytesIOReader

def test_read1(assert_equal, assert_raises):
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/cython/types_testhelper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# cython: language_level=3

import calendar
import datetime
import time
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/cython/utils_testhelper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# cython: language_level=3

import datetime

from cassandra.cython_utils cimport datetime_from_timestamp
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def submit_and_wait_for_completion(unit_test, create_timer, start, end, incremen
pending_callbacks.append(callback)

# wait for all the callbacks associated with the timers to be invoked
while len(pending_callbacks) is not 0:
while len(pending_callbacks) != 0:
for callback in pending_callbacks:
if callback.was_invoked():
pending_callbacks.remove(callback)
Expand Down Expand Up @@ -233,7 +233,7 @@ def make_error_body(self, code, msg):
def make_msg(self, header, body=binary_type()):
return header + uint32_pack(len(body)) + body

def test_successful_connection(self):
def _test_successful_connection(self):
c = self.make_connection()

# let it write the OptionsMessage
Expand All @@ -255,6 +255,9 @@ def test_successful_connection(self):
self.assertTrue(c.connected_event.is_set())
return c

def test_successful_connection(self):
self._test_successful_connection()

def test_eagain_on_buffer_size(self):
self._check_error_recovery_on_buffer_size(errno.EAGAIN)

Expand All @@ -272,7 +275,7 @@ def test_sslwantwrite_on_buffer_size(self):
error_class=ssl.SSLError)

def _check_error_recovery_on_buffer_size(self, error_code, error_class=socket_error):
c = self.test_successful_connection()
c = self._test_successful_connection()

# current data, used by the recv side_effect
message_chunks = None
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import pytest

import logging
import six
Expand Down Expand Up @@ -275,6 +276,9 @@ def test_default_exec_parameters(self):
self.assertEqual(cluster.profile_manager.default.row_factory, named_tuple_factory)

@mock_session_pools
@pytest.mark.filterwarnings("ignore:DowngradingConsistencyRetryPolicy:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:Legacy execution parameters will be removed in 4.0:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:Setting the consistency level at the session level will be removed in 4.0:DeprecationWarning")
def test_default_legacy(self):
cluster = Cluster(load_balancing_policy=RoundRobinPolicy(), default_retry_policy=DowngradingConsistencyRetryPolicy())
self.assertEqual(cluster._config_mode, _ConfigMode.LEGACY)
Expand Down Expand Up @@ -322,6 +326,8 @@ def test_serial_consistency_level_validation(self):
ep = ExecutionProfile(RoundRobinPolicy(), serial_consistency_level=42)

@mock_session_pools
@pytest.mark.filterwarnings("ignore:DowngradingConsistencyRetryPolicy:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:Legacy execution parameters will be removed in 4.0:DeprecationWarning")
def test_statement_params_override_legacy(self):
cluster = Cluster(load_balancing_policy=RoundRobinPolicy(), default_retry_policy=DowngradingConsistencyRetryPolicy())
self.assertEqual(cluster._config_mode, _ConfigMode.LEGACY)
Expand All @@ -343,6 +349,7 @@ def test_statement_params_override_legacy(self):
self._verify_response_future_profile(rf, expected_profile)

@mock_session_pools
@pytest.mark.filterwarnings("ignore:DowngradingConsistencyRetryPolicy:DeprecationWarning")
def test_statement_params_override_profile(self):
non_default_profile = ExecutionProfile(RoundRobinPolicy(), *[object() for _ in range(2)])
cluster = Cluster(execution_profiles={'non-default': non_default_profile})
Expand All @@ -367,6 +374,9 @@ def test_statement_params_override_profile(self):
self._verify_response_future_profile(rf, expected_profile)

@mock_session_pools
@pytest.mark.filterwarnings("ignore:DowngradingConsistencyRetryPolicy:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:Legacy execution parameters will be removed in 4.0:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:Setting the consistency level at the session level will be removed in 4.0:DeprecationWarning")
def test_no_profile_with_legacy(self):
# don't construct with both
self.assertRaises(ValueError, Cluster, load_balancing_policy=RoundRobinPolicy(), execution_profiles={'a': ExecutionProfile()})
Expand All @@ -393,6 +403,7 @@ def test_no_profile_with_legacy(self):
self.assertRaises(ValueError, session.execute_async, "query", execution_profile='some name here')

@mock_session_pools
@pytest.mark.filterwarnings("ignore:Setting the consistency level at the session level will be removed in 4.0:DeprecationWarning")
def test_no_legacy_with_profile(self):
cluster_init = Cluster(execution_profiles={'name': ExecutionProfile()})
cluster_add = Cluster()
Expand Down Expand Up @@ -513,6 +524,7 @@ def _check_warning_on_no_lbp_with_contact_points(self, cluster_kwargs):
self.assertIn('please specify a load-balancing policy', warning_message)
self.assertIn("contact_points = ['127.0.0.1']", warning_message)

@pytest.mark.filterwarnings("ignore:Legacy execution parameters will be removed in 4.0:DeprecationWarning")
def test_no_warning_on_contact_points_with_lbp_legacy_mode(self):
"""
Test that users aren't warned when they instantiate a Cluster object
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def extract_consistency(self, msg):
:param msg: message with consistency value
:return: String representing consistency value
"""
match = re.search("'consistency':\s+'([\w\s]+)'", msg)
match = re.search(r"'consistency':\s+'([\w\s]+)'", msg)
return match and match.group(1)

def test_timeout_consistency(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,9 +849,9 @@ def test_strip_frozen(self):
argument_to_expected_results = [
('int', 'int'),
('tuple<text>', 'tuple<text>'),
(r'map<"!@#$%^&*()[]\ frozen >>>", int>', r'map<"!@#$%^&*()[]\ frozen >>>", int>'), # A valid UDT name
(r'map<"!@#$%^&*()[]\\ frozen >>>", int>', r'map<"!@#$%^&*()[]\ frozen >>>", int>'), # A valid UDT name
('frozen<tuple<text>>', 'tuple<text>'),
(r'frozen<map<"!@#$%^&*()[]\ frozen >>>", int>>', r'map<"!@#$%^&*()[]\ frozen >>>", int>'),
(r'frozen<map<"!@#$%^&*()[]\\ frozen >>>", int>>', r'map<"!@#$%^&*()[]\ frozen >>>", int>'),
('frozen<map<frozen<tuple<int, frozen<list<text>>, int>>, frozen<map<int, frozen<tuple<int>>>>>>',
'map<tuple<int, list<text>, int>, map<int, tuple<int>>>'),
]
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import unittest
import pytest

from itertools import islice, cycle
from mock import Mock, patch, call
Expand Down Expand Up @@ -1134,6 +1135,7 @@ def test_unavailable(self):
self.assertEqual(consistency, None)


@pytest.mark.filterwarnings("ignore:DowngradingConsistencyRetryPolicy:DeprecationWarning")
class DowngradingConsistencyRetryPolicyTest(unittest.TestCase):

def test_read_timeout(self):
Expand Down
Loading

0 comments on commit d498fa9

Please sign in to comment.