From da0538b6a09df85ed350ca1855dd8e5654a3324b Mon Sep 17 00:00:00 2001 From: Lasse Yledahl <ylle@sunet.se> Date: Wed, 18 Sep 2024 08:29:24 +0000 Subject: [PATCH 1/2] remove duplicated code --- src/eduid/userdb/deprecation.py | 68 --------------------------------- src/eduid/userdb/security/db.py | 2 +- 2 files changed, 1 insertion(+), 69 deletions(-) delete mode 100644 src/eduid/userdb/deprecation.py diff --git a/src/eduid/userdb/deprecation.py b/src/eduid/userdb/deprecation.py deleted file mode 100644 index 2c0e62e66..000000000 --- a/src/eduid/userdb/deprecation.py +++ /dev/null @@ -1,68 +0,0 @@ -import inspect -import warnings -from functools import wraps - - -# https://stackoverflow.com/questions/2536307/how-do-i-deprecate-python-functions/40301488#40301488 -def deprecated(reason): - """ - This is a decorator which can be used to mark functions - as deprecated. It will result in a warning being emitted - when the function is used. - """ - - if isinstance(reason, str): - # The @deprecated is used with a 'reason'. - # - # .. code-block:: python - # - # @deprecated("please, use another function") - # def old_function(x, y): - # pass - - def decorator(func1): - if inspect.isclass(func1): - fmt1 = "Call to deprecated class {name} ({reason})." - else: - fmt1 = "Call to deprecated function {name} ({reason})." - - @wraps(func1) - def new_func1(*args, **kwargs): - warnings.simplefilter("always", DeprecationWarning) - warnings.warn( - fmt1.format(name=func1.__name__, reason=reason), category=DeprecationWarning, stacklevel=2 - ) - warnings.simplefilter("default", DeprecationWarning) - return func1(*args, **kwargs) - - return new_func1 - - return decorator - - elif inspect.isclass(reason) or inspect.isfunction(reason): - # The @deprecated is used without any 'reason'. - # - # .. code-block:: python - # - # @deprecated - # def old_function(x, y): - # pass - - func2 = reason - - if inspect.isclass(func2): - fmt2 = "Call to deprecated class {name}." - else: - fmt2 = "Call to deprecated function {name}." - - @wraps(func2) - def new_func2(*args, **kwargs): - warnings.simplefilter("always", DeprecationWarning) - warnings.warn(fmt2.format(name=func2.__name__), category=DeprecationWarning, stacklevel=2) - warnings.simplefilter("default", DeprecationWarning) - return func2(*args, **kwargs) - - return new_func2 - - else: - raise TypeError(repr(type(reason))) diff --git a/src/eduid/userdb/security/db.py b/src/eduid/userdb/security/db.py index d1017a893..9b5dee43d 100644 --- a/src/eduid/userdb/security/db.py +++ b/src/eduid/userdb/security/db.py @@ -4,7 +4,7 @@ from typing import Any from eduid.userdb.db import BaseDB, SaveResult, TUserDbDocument -from eduid.userdb.deprecation import deprecated +from eduid.common.decorators import deprecated from eduid.userdb.exceptions import MultipleDocumentsReturned from eduid.userdb.security.state import PasswordResetEmailAndPhoneState, PasswordResetEmailState, PasswordResetState from eduid.userdb.security.user import SecurityUser From 62a6e7cf8d6896148fa7947e83f2a1a04ad5b1f0 Mon Sep 17 00:00:00 2001 From: Lasse Yledahl <ylle@sunet.se> Date: Wed, 18 Sep 2024 08:35:15 +0000 Subject: [PATCH 2/2] make reformat --- src/eduid/userdb/security/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eduid/userdb/security/db.py b/src/eduid/userdb/security/db.py index 9b5dee43d..e05488a93 100644 --- a/src/eduid/userdb/security/db.py +++ b/src/eduid/userdb/security/db.py @@ -3,8 +3,8 @@ from collections.abc import Mapping from typing import Any -from eduid.userdb.db import BaseDB, SaveResult, TUserDbDocument from eduid.common.decorators import deprecated +from eduid.userdb.db import BaseDB, SaveResult, TUserDbDocument from eduid.userdb.exceptions import MultipleDocumentsReturned from eduid.userdb.security.state import PasswordResetEmailAndPhoneState, PasswordResetEmailState, PasswordResetState from eduid.userdb.security.user import SecurityUser