Skip to content

Commit 09db398

Browse files
committed
BUG#37541353: (Contribution) Fix typing annotation of MySQLConnectionAbstract's close function
As part of the connection API, improved the type hint of `close()` which is an alias of the instance method `disconnect()`. Thanks, Parul Gupta, for the contribution! Additionally, since `close()` is the preferred term (see [PEP 249 – Python Database API Specification v2.0]), with this patch, we also changed roles by letting `disconnect()` now be an alias of `close()`. From a functional perspective, nothing changes, this modification is oriented to provide consistency and clarity to the interface declaration. Change-Id: I61d407186fcc9fa5496c617f05593c4c69bdfac8
1 parent a0e6975 commit 09db398

File tree

6 files changed

+8
-18
lines changed

6 files changed

+8
-18
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ v9.3.0
1212
======
1313

1414
- WL#16327: Remove Cursors Prepared Raw and Named Tuple
15+
- BUG#37541353: (Contribution) Fix typing annotation of MySQLConnectionAbstract's close function
1516
- BUG#37453587: Github links in PyPI project's pages do not work
1617
- BUG#37418436: Arbitrary File Read in MySQL Python Client library
1718

mysql-connector-python/lib/mysql/connector/abstracts.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
Any,
4747
BinaryIO,
4848
Callable,
49+
ClassVar,
4950
Deque,
5051
Dict,
5152
Generator,
@@ -1494,7 +1495,7 @@ def _post_connection(self) -> None:
14941495
self._execute_query(self._init_command)
14951496

14961497
@abstractmethod
1497-
def disconnect(self) -> None:
1498+
def close(self) -> None:
14981499
"""Disconnects from the MySQL server.
14991500
15001501
This method tries to send a `QUIT` command and close the socket. It raises
@@ -1507,7 +1508,7 @@ def disconnect(self) -> None:
15071508
use `shutdown()`.
15081509
"""
15091510

1510-
close: Callable[[], Any] = disconnect
1511+
disconnect: ClassVar[Callable[["MySQLConnectionAbstract"], None]] = close
15111512

15121513
def connect(self, **kwargs: Any) -> None:
15131514
"""Connects to the MySQL server.

mysql-connector-python/lib/mysql/connector/aio/abstracts.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@
4848
TYPE_CHECKING,
4949
Any,
5050
AsyncGenerator,
51+
Awaitable,
5152
BinaryIO,
5253
Callable,
54+
ClassVar,
5355
Deque,
5456
Dict,
5557
Generator,
@@ -1445,7 +1447,7 @@ async def close(self) -> None:
14451447
no exceptions.
14461448
"""
14471449

1448-
disconnect: Callable[[], Any] = close
1450+
disconnect: ClassVar[Callable[["MySQLConnectionAbstract"], Awaitable[None]]] = close
14491451

14501452
@abstractmethod
14511453
async def cursor(

mysql-connector-python/lib/mysql/connector/aio/connection.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
Any,
4848
AsyncGenerator,
4949
BinaryIO,
50-
Callable,
5150
Dict,
5251
List,
5352
Mapping,
@@ -750,17 +749,6 @@ async def shutdown(self) -> None:
750749
pass # Getting an exception would mean we are disconnected.
751750

752751
async def close(self) -> None:
753-
"""Close the connection.
754-
755-
It closes any opened cursor associated to this connection, and closes the
756-
underling socket connection.
757-
758-
`MySQLConnection.close()` is a synonymous for `MySQLConnection.disconnect()`
759-
method name and more commonly used.
760-
761-
This method tries to send a `QUIT` command and close the socket. It raises
762-
no exceptions.
763-
"""
764752
with contextlib.suppress(Error):
765753
for cursor in tuple(self._cursors):
766754
await cursor.close()
@@ -773,7 +761,7 @@ async def close(self) -> None:
773761
await self._socket.close_connection()
774762
self._socket = None
775763

776-
disconnect: Callable[[], Any] = close
764+
disconnect = close
777765

778766
async def cursor(
779767
self,

mysql-connector-python/lib/mysql/connector/connection.py

-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ def shutdown(self) -> None:
449449
pass # Getting an exception would mean we are disconnected.
450450

451451
def close(self) -> None:
452-
"""Disconnect from the MySQL server"""
453452
if self._span and self._span.is_recording():
454453
# pylint: disable=possibly-used-before-assignment
455454
record_exception_event(self._span, sys.exc_info()[1])

mysql-connector-python/lib/mysql/connector/connection_cext.py

-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ def _open_connection(self) -> None:
377377
warn_ciphersuites_deprecated(cipher, tls_version)
378378

379379
def close(self) -> None:
380-
"""Disconnect from the MySQL server"""
381380
if self._span and self._span.is_recording():
382381
# pylint: disable=possibly-used-before-assignment
383382
record_exception_event(self._span, sys.exc_info()[1])

0 commit comments

Comments
 (0)