python-driver icon indicating copy to clipboard operation
python-driver copied to clipboard

Fix code quality issues in test_cluster.py

Open Copilot opened this issue 2 months ago • 0 comments

Description

Applied code quality improvements to tests/unit/test_cluster.py:

  • Fixed spelling: tringstring in test_port_str docstring
  • Removed typo: Extra t character at end of comment in test_event_delay_timing
  • Refactored nested list comprehension in test_tuple_for_contact_points: Broke down complex one-liner into multiple lines with descriptive variable names (addr_info, sockaddr_tuples, localhost_addr)
  • Fixed None comparisons: Changed == None to is None (2 instances) following PEP 8

Pre-review checklist

  • [x] I have split my patch into logically separate commits.
  • [x] All commit messages clearly explain what they change and why.
  • [x] I added relevant tests for new features and bug fixes.
  • [x] All commits compile, pass static checks and pass test.
  • [x] PR description sums up the changes and reasons why they should be introduced.
  • [ ] I have provided docstrings for the public items that I want to introduce.
  • [ ] I have adjusted the documentation in ./docs/source/.
Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"The docstring contains a spelling error: 'tring' should be 'string'.","fixFiles":[{"filePath":"tests/unit/test_cluster.py","diff":"diff --git a/tests/unit/test_cluster.py b/tests/unit/test_cluster.py\n--- a/tests/unit/test_cluster.py\n+++ b/tests/unit/test_cluster.py\n@@ -107,7 +107,7 @@\n             Cluster(contact_points=\"not a sequence\", protocol_version=4, connect_timeout=1)\n \n     def test_port_str(self):\n-        \"\"\"Check port passed as tring is converted and checked properly\"\"\"\n+        \"\"\"Check port passed as string is converted and checked properly\"\"\"\n         cluster = Cluster(contact_points=['127.0.0.1'], port='1111')\n         for cp in cluster.endpoints_resolved:\n             if cp.address in ('::1', '127.0.0.1'):\n"}]},{"message":"There's an extra 't' character at the end of the comment that should be removed.","fixFiles":[{"filePath":"tests/unit/test_cluster.py","diff":"diff --git a/tests/unit/test_cluster.py b/tests/unit/test_cluster.py\n--- a/tests/unit/test_cluster.py\n+++ b/tests/unit/test_cluster.py\n@@ -181,7 +181,7 @@\n         \"\"\"\n         sched = _Scheduler(None)\n         sched.schedule(0, lambda: None)\n-        sched.schedule(0, lambda: None)  # pre-473: \"TypeError: unorderable types: function() < function()\"t\n+        sched.schedule(0, lambda: None)  # pre-473: \"TypeError: unorderable types: function() < function()\"\n \n \n class SessionTest(unittest.TestCase):\n"}]},{"message":"The variable name 't' is not descriptive. Consider renaming to 'sockaddr_tuple' or similar to clarify what it represents.","fixFiles":[{"filePath":"tests/unit/test_cluster.py","diff":"diff --git a/tests/unit/test_cluster.py b/tests/unit/test_cluster.py\n--- a/tests/unit/test_cluster.py\n+++ b/tests/unit/test_cluster.py\n@@ -90,7 +90,7 @@\n \n     def test_tuple_for_contact_points(self):\n         cluster = Cluster(contact_points=[('localhost', 9045), ('127.0.0.2', 9046), '127.0.0.3'], port=9999)\n-        localhost_addr = set([addr[0] for addr in [t for (_,_,_,_,t) in socket.getaddrinfo(\"localhost\",80)]])\n+        localhost_addr = set([addr[0] for addr in [sockaddr_tuple for (_,_,_,_,sockaddr_tuple) in socket.getaddrinfo(\"localhost\",80)]])\n         for cp in cluster.endpoints_resolved:\n             if cp.address in localhost_addr:\n                 assert cp.port == 9045\n"}]},{"message":"[nitpick] The complex nested list comprehension with underscore placeholders reduces readability. Consider breaking this into multiple lines or using more descriptive variable names for the socket.getaddrinfo() fields.","fixFiles":[{"filePath":"tests/unit/test_cluster.py","diff":"diff --git a/tests/unit/test_cluster.py b/tests/unit/test_cluster.py\n--- a/tests/unit/test_cluster.py\n+++ b/tests/unit/test_cluster.py\n@@ -90,7 +90,10 @@\n \n     def test_tuple_for_contact_points(self):\n         cluster = Cluster(contact_points=[('localhost', 9045), ('127.0.0.2', 9046), '127.0.0.3'], port=9999)\n-        localhost_addr = set([addr[0] for addr in [t for (_,_,_,_,t) in socket.getaddrinfo(\"localhost\",80)]])\n+        # Refactored for clarity\n+        addr_info = socket.getaddrinfo(\"localhost\", 80)\n+        sockaddr_tuples = [info[4] for info in addr_info]  # info[4] is sockaddr\n+        localhost_addr = set([sockaddr[0] for sockaddr in sockaddr_tuples])\n         for cp in cluster.endpoints_resolved:\n             if cp.address in localhost_addr:\n                 assert cp.port == 9045\n"}]},{"message":"Use 'is None' instead of '== None' for None comparisons to follow Python best practices.","fixFiles":[{"filePath":"tests/unit/test_cluster.py","diff":"diff --git a/tests/unit/test_cluster.py b/tests/unit/test_cluster.py\n--- a/tests/unit/test_cluster.py\n+++ b/tests/unit/test_cluster.py\n@@ -291,7 +291,7 @@\n         assert cluster.profile_manager.default.request_timeout == 10.0\n         assert session.default_consistency_level == ConsistencyLevel.LOCAL_ONE\n         assert cluster.profile_manager.default.consistency_level == ConsistencyLevel.LOCAL_ONE\n-        assert session.default_serial_consistency_level == None\n+        assert session.default_serial_consistency_level is None\n         assert cluster.profile_manager.default.serial_consistency_level == None\n         assert session.row_factory == named_tuple_factory\n         assert cluster.profile_manager.default.row_factory == named_tuple_factory\n"}]}]

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Dec 23 '25 09:12 Copilot