Skip to content

Commit dd88b60

Browse files
committed
Fix linter issue introduced by WL#15523
Change-Id: I220662769bb7bbcf63c4e2587af79d8c29a4254a
1 parent 6b9d0dc commit dd88b60

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

lib/mysql/connector/aio/abstracts.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
from abc import ABC, abstractmethod
4545
from dataclasses import dataclass, field
46+
from inspect import signature
4647
from types import TracebackType
4748
from typing import (
4849
TYPE_CHECKING,
@@ -102,7 +103,7 @@
102103
StrOrBytes,
103104
WarningType,
104105
)
105-
from ..utils import GenericWrapper
106+
from ..utils import GenericWrapper, import_object
106107
from .authentication import MySQLAuthenticator
107108
from .charsets import Charset, charsets
108109
from .protocol import MySQLProtocol
@@ -181,6 +182,7 @@ def __init__(
181182
raw: bool = False,
182183
kerberos_auth_mode: Optional[str] = None,
183184
krb_service_principal: Optional[str] = None,
185+
fido_callback: Optional[Union[str, Callable[[str], None]]] = None,
184186
webauthn_callback: Optional[Union[str, Callable[[str], None]]] = None,
185187
allow_local_infile: bool = DEFAULT_CONFIGURATION["allow_local_infile"],
186188
allow_local_infile_in_path: Optional[str] = DEFAULT_CONFIGURATION[
@@ -259,8 +261,10 @@ def __init__(
259261
self._in_transaction: bool = False
260262
self._oci_config_file: Optional[str] = None
261263
self._oci_config_profile: Optional[str] = None
262-
self._fido_callback: Optional[Union[str, Callable[[str], None]]] = None
263-
self._webauthn_callback: Optional[Union[str, Callable[[str], None]]] = None
264+
self._fido_callback: Optional[Union[str, Callable[[str], None]]] = fido_callback
265+
self._webauthn_callback: Optional[
266+
Union[str, Callable[[str], None]]
267+
] = webauthn_callback
264268

265269
self.converter: Optional[MySQLConverter] = None
266270

@@ -588,6 +592,41 @@ def _validate_tls_versions(self) -> None:
588592
elif invalid_tls_versions:
589593
raise AttributeError(TLS_VERSION_ERROR.format(tls_ver, TLS_VERSIONS))
590594

595+
@staticmethod
596+
def _validate_callable(
597+
option_name: str, callback: Union[str, Callable], num_args: int = 0
598+
) -> None:
599+
"""Validates if it's a Python callable.
600+
601+
Args:
602+
option_name (str): Connection option name.
603+
callback (str or callable): The fully qualified path to the callable or
604+
a callable.
605+
num_args (int): Number of positional arguments allowed.
606+
607+
Raises:
608+
ProgrammingError: If `callback` is not valid or wrong number of positional
609+
arguments.
610+
611+
.. versionadded:: 8.2.0
612+
"""
613+
if isinstance(callback, str):
614+
try:
615+
callback = import_object(callback)
616+
except ValueError as err:
617+
raise ProgrammingError(f"{err}") from err
618+
619+
if not callable(callback):
620+
raise ProgrammingError(f"Expected a callable for '{option_name}'")
621+
622+
# Check if the callable signature has <num_args> positional arguments
623+
num_params = len(signature(callback).parameters)
624+
if num_params != num_args:
625+
raise ProgrammingError(
626+
f"'{option_name}' requires {num_args} positional argument, but the "
627+
f"callback provided has {num_params}"
628+
)
629+
591630
@property
592631
@abstractmethod
593632
def connection_id(self) -> Optional[int]:

tests/test_connection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3968,7 +3968,10 @@ def setUpClass(cls):
39683968
test_table = "wl14237"
39693969
with connection.MySQLConnection(**tests.get_mysql_config()) as cnx:
39703970
with cnx.cursor() as cur:
3971-
cur.execute('INSTALL COMPONENT "file://component_query_attributes"')
3971+
try:
3972+
cur.execute('INSTALL COMPONENT "file://component_query_attributes"')
3973+
except errors.Error:
3974+
pass # The component is already installed
39723975
cur.execute(f"DROP TABLE IF EXISTS {test_table}")
39733976
cur.execute(
39743977
f"""

0 commit comments

Comments
 (0)