Bug #106632 Connector/Python >= 8.0.20 no longer accepts tuple as "failover" argument
Submitted: 3 Mar 2022 16:29 Modified: 29 Apr 2022 20:18
Reporter: Ville Skyttä Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:8.0.20, 8.0.28 OS:Any
Assigned to: CPU Architecture:Any

[3 Mar 2022 16:29] Ville Skyttä
Description:
Per the documentation at https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html for Server Failover:

"The argument value is a tuple or list of dictionaries (tuple is preferred because it is nonmutable)."

Since version 8.0.20, tuple no longer works:

  File ".../python3.6/site-packages/mysql/connector/__init__.py", line 159, in _get_failover_connection
    failover.sort(key=lambda x: x['priority'], reverse=True)
AttributeError: 'tuple' object has no attribute 'sort'

Connecting using a list instead of tuple serves as a workaround.

How to repeat:
Try to connect using a tuple of dicts in the failover parameter, i.e. failover=({...}, {...})

Workaround is to use a list of dicts instead: failover=[{...}, {...}]

Suggested fix:
For 8.0.28

--- __init__.py~	2022-03-03 18:12:28.343959331 +0200
+++ __init__.py	2022-03-03 18:24:19.648623767 +0200
@@ -158,7 +158,5 @@
 
-    failover.sort(key=lambda x: x['priority'], reverse=True)
-
     server_directory = {}
     server_priority_list = []
-    for server in failover:
+    for server in sorted(failover, key=lambda x: x['priority'], reverse=True):
         if server["priority"] not in server_directory:
[4 Mar 2022 9:09] MySQL Verification Team
Hello Ville Skyttä,

Thank you for the report and feedback.
Verified as described.

regards,
Umesh
[21 Apr 2022 15:13] Nuno Mariz
Posted by developer:
 
This patch fixes this issue by applying the sorted() function instead of using the sort() method in the failover argument, for sorting the servers by priority.

Thank you for your contribution.
[29 Apr 2022 20:18] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 8.0.30 release, and here's the proposed changelog entry from the documentation team:
---
The connect()'s failover argument now accepts a tuple, as was documented,
when previously it only accepted a list of dictionaries. 

Thanks to Ville Skyttä for the patch.
---

Thank you for the bug report.