refactor utils.deprecated to be more mypy friendly (#6923)

* refactor utils.deprecated to be more mypy friendly

* Poke for CI
This commit is contained in:
Alex Gaynor 2022-03-02 14:46:30 -05:00 committed by GitHub
parent 1cc4a6e786
commit 7274228685
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 6 deletions

View file

@ -143,11 +143,16 @@ def deprecated(
module_name: str,
message: str,
warning_class: typing.Type[Warning],
name: typing.Optional[str] = None,
) -> _DeprecatedValue:
module = sys.modules[module_name]
if not isinstance(module, _ModuleWithDeprecations):
sys.modules[module_name] = _ModuleWithDeprecations(module)
return _DeprecatedValue(value, message, warning_class)
sys.modules[module_name] = module = _ModuleWithDeprecations(module)
dv = _DeprecatedValue(value, message, warning_class)
# Maintain backwards compatibility with `name is None` for pyOpenSSL.
if name is not None:
setattr(module, name, dv)
return dv
def cached_property(func: typing.Callable) -> property:

View file

@ -6,8 +6,13 @@ from cryptography import utils
# This module exists to test `cryptography.utils.deprecated`
DEPRECATED = utils.deprecated(
3, __name__, "Test Deprecated Object", DeprecationWarning
DEPRECATED = 3
utils.deprecated(
DEPRECATED,
__name__,
"Test Deprecated Object",
DeprecationWarning,
name="DEPRECATED",
)
NOT_DEPRECATED = 12

View file

@ -18,7 +18,8 @@ class TestDeprecated:
def test_deprecated(self, monkeypatch):
mod = types.ModuleType("TestDeprecated/test_deprecated")
monkeypatch.setitem(sys.modules, mod.__name__, mod)
mod.X = deprecated(
deprecated(
name="X",
value=1,
module_name=mod.__name__,
message="deprecated message text",
@ -53,7 +54,8 @@ class TestDeprecated:
def test_deleting_deprecated_members(self, monkeypatch):
mod = types.ModuleType("TestDeprecated/test_deprecated")
monkeypatch.setitem(sys.modules, mod.__name__, mod)
mod.X = deprecated(
deprecated(
name="X",
value=1,
module_name=mod.__name__,
message="deprecated message text",