Skip to content

Commit

Permalink
Deprecate using too many positional args
Browse files Browse the repository at this point in the history
  • Loading branch information
amochin committed Sep 25, 2024
1 parent b0e6b26 commit 2645ed5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/DatabaseLibrary/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from robot.libraries.BuiltIn import BuiltIn
from robot.utils import timestr_to_secs

from .params_decorator import renamed_args
from .params_decorator import deprecated_positional_args, renamed_args


class Assertion:
Expand Down Expand Up @@ -266,6 +266,7 @@ def row_count_is_less_than_x(
)

@renamed_args(mapping={"selectStatement": "select_statement", "sansTran": "no_transaction"})
@deprecated_positional_args(4)
def check_row_count(
self,
select_statement: str,
Expand Down Expand Up @@ -326,6 +327,7 @@ def check_row_count(
time_counter += timestr_to_secs(retry_pause)

@renamed_args(mapping={"selectStatement": "select_statement", "sansTran": "no_transaction"})
@deprecated_positional_args(3)
def check_query_result(
self,
select_statement: str,
Expand Down Expand Up @@ -407,6 +409,7 @@ def check_query_result(
time_counter += timestr_to_secs(retry_pause)

@renamed_args(mapping={"tableName": "table_name", "sansTran": "no_transaction"})
@deprecated_positional_args(1)
def table_must_exist(
self,
table_name: str,
Expand Down
49 changes: 44 additions & 5 deletions src/DatabaseLibrary/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from robot.api import logger

from .params_decorator import renamed_args
from .params_decorator import deprecated_positional_args, renamed_args


@dataclass
Expand Down Expand Up @@ -172,6 +172,7 @@ def _hide_password_values(string_with_pass, params_separator=","):
"driverMode": "oracle_driver_mode",
}
)
@deprecated_positional_args(1)
def connect_to_database(
self,
python_module: Optional[str] = None,
Expand All @@ -185,6 +186,17 @@ def connect_to_database(
config_file: Optional[str] = None,
oracle_driver_mode: Optional[str] = None,
alias: str = "default",
*,
dbapiModuleName: Optional[str] = None,
dbName: Optional[str] = None,
dbUsername: Optional[str] = None,
dbPassword: Optional[str] = None,
dbHost: Optional[str] = None,
dbPort: Optional[int] = None,
dbCharset: Optional[str] = None,
dbDriver: Optional[str] = None,
dbConfigFile: Optional[str] = None,
driverMode: Optional[str] = None,
**custom_connection_params,
):
"""
Expand Down Expand Up @@ -454,8 +466,14 @@ def _arg_or_config(arg_value, param_name, mandatory=False):

self.connection_store.register_connection(db_connection, db_api_module_name, alias)

@renamed_args(mapping={"dbapiModuleName": "python_module"})
def connect_to_database_using_custom_params(
self, python_module: Optional[str] = None, db_connect_string: str = "", alias: str = "default"
self,
python_module: Optional[str] = None,
db_connect_string: str = "",
alias: str = "default",
*,
dbapiModuleName: Optional[str] = None,
):
"""
*DEPRECATED* Use new `Connect To Database` keyword with custom parameters instead.
Expand All @@ -468,6 +486,9 @@ def connect_to_database_using_custom_params(
Use `connect_to_database_using_custom_connection_string` for passing
all params in a single connection string or URI.
The old ``dbapiModuleName`` param duplicates new ``python_module``.
The old naming is deprecated and will be removed in future versions.
Example usage:
| Connect To Database Using Custom Params | psycopg2 | database='my_db_test', user='postgres', password='s3cr3t', host='tiger.foobar.com', port=5432 |
| Connect To Database Using Custom Params | jaydebeapi | 'oracle.jdbc.driver.OracleDriver', 'my_db_test', 'system', 's3cr3t' |
Expand All @@ -485,8 +506,15 @@ def connect_to_database_using_custom_params(
db_connection = eval(db_connect_string)
self.connection_store.register_connection(db_connection, db_api_module_name, alias)

@renamed_args(mapping={"dbapiModuleName": "python_module"})
@deprecated_positional_args(2)
def connect_to_database_using_custom_connection_string(
self, python_module: Optional[str] = None, db_connect_string: str = "", alias: str = "default"
self,
python_module: Optional[str] = None,
db_connect_string: str = "",
alias: str = "default",
*,
dbapiModuleName: Optional[str] = None,
):
"""
Loads the DB API 2.0 module given `python_module` then uses it to
Expand All @@ -496,6 +524,9 @@ def connect_to_database_using_custom_connection_string(
Use `connect_to_database_using_custom_params` for passing
connection params as named arguments.
The old ``dbapiModuleName`` param duplicates new ``python_module``.
The old naming is deprecated and will be removed in future versions.
Example usage:
| Connect To Database Using Custom Connection String | psycopg2 | postgresql://postgres:[email protected]:5432/my_db_test |
| Connect To Database Using Custom Connection String | oracledb | username/pass@localhost:1521/orclpdb |
Expand All @@ -509,6 +540,7 @@ def connect_to_database_using_custom_connection_string(
db_connection = db_api_2.connect(db_connect_string)
self.connection_store.register_connection(db_connection, db_api_module_name, alias)

@deprecated_positional_args(0)
def disconnect_from_database(self, error_if_no_connection: bool = False, alias: Optional[str] = None):
"""
Disconnects from the database.
Expand Down Expand Up @@ -542,7 +574,11 @@ def disconnect_from_all_databases(self):
db_connection.client.close()
self.connection_store.clear()

def set_auto_commit(self, autoCommit: bool = True, alias: Optional[str] = None):
@renamed_args(mapping={"autoCommit": "auto_commit"})
@deprecated_positional_args(1)
def set_auto_commit(
self, auto_commit: bool = True, alias: Optional[str] = None, *, autoCommit: Optional[bool] = None
):
"""
Turn the autocommit on the database connection ON or OFF.
Expand All @@ -552,6 +588,9 @@ def set_auto_commit(self, autoCommit: bool = True, alias: Optional[str] = None):
or database snapshot. By turning on auto commit on the database connection these actions
can be performed.
The old ``autoCommit`` param duplicates new ``auto_commit``.
The old naming is deprecated and will be removed in future versions.
Example usage:
| # Default behaviour, sets auto commit to true
| Set Auto Commit
Expand All @@ -560,7 +599,7 @@ def set_auto_commit(self, autoCommit: bool = True, alias: Optional[str] = None):
| Set Auto Commit | False
"""
db_connection = self.connection_store.get_connection(alias)
db_connection.client.autocommit = autoCommit
db_connection.client.autocommit = auto_commit

def switch_database(self, alias: str):
"""
Expand Down
26 changes: 26 additions & 0 deletions src/DatabaseLibrary/params_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
These decorators are introduced for the transition from old argument naming / positioning to the new one.
"""
from functools import wraps
from inspect import signature

from robot.api import logger

Expand Down Expand Up @@ -31,3 +32,28 @@ def wrapper(*args, **kwargs):
return wrapper

return decorator


def deprecated_positional_args(allowed_pos_args_count):
"""
Warn about using too many positional arguments
"""

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
sig = signature(func)
bound_args = sig.bind_partial(*args, **kwargs)

# Issue a warning if not allowed positional arguments are used
if len(bound_args.args) > allowed_pos_args_count + 1:
logger.warn(
f"Using more than {allowed_pos_args_count} positional arguments in this keyword "
"is deprecated, use named arguments instead"
)

return func(*args, **kwargs)

return wrapper

return decorator

0 comments on commit 2645ed5

Please sign in to comment.