From 94a6dc6121cd1ae734c113c610948754472b39a4 Mon Sep 17 00:00:00 2001 From: Vasily Zakharov Date: Thu, 17 Jun 2021 21:05:03 +0300 Subject: [PATCH 1/7] Addded workaround for OpenSSL.crypto that has submodules wrapped into cryptography.utils._ModuleWithDeprecations --- mypy/stubtest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 7915e14f1551b..1f6361aa65da2 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -202,6 +202,10 @@ def verify_mypyfile( if isinstance(runtime, Missing): yield Error(object_path, "is not present at runtime", stub, runtime) return + if not isinstance(runtime, types.ModuleType) and hasattr(runtime, "_module"): + # Workaround for OpenSSL.crypto that has submodules wrapped into cryptography.utils._ModuleWithDeprecations. + # See https://github.com/python/typeshed/pull/5657 for details. + runtime = runtime._module if not isinstance(runtime, types.ModuleType): yield Error(object_path, "is not a module", stub, runtime) return From 11e1018b9d8c0296216387f869338c747138917f Mon Sep 17 00:00:00 2001 From: Vasily Zakharov Date: Thu, 17 Jun 2021 21:11:55 +0300 Subject: [PATCH 2/7] Comment text improved --- mypy/stubtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 1f6361aa65da2..b9212ab387bc5 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -203,7 +203,7 @@ def verify_mypyfile( yield Error(object_path, "is not present at runtime", stub, runtime) return if not isinstance(runtime, types.ModuleType) and hasattr(runtime, "_module"): - # Workaround for OpenSSL.crypto that has submodules wrapped into cryptography.utils._ModuleWithDeprecations. + # Workaround for pyOpenSSL that has its submodules wrapped into cryptography.utils._ModuleWithDeprecations instances. # See https://github.com/python/typeshed/pull/5657 for details. runtime = runtime._module if not isinstance(runtime, types.ModuleType): From a356d214dd4b741687f18e1d9b2c702a1143285b Mon Sep 17 00:00:00 2001 From: Vasily Zakharov Date: Thu, 17 Jun 2021 21:34:45 +0300 Subject: [PATCH 3/7] Additional fix for "is not a function" errors on deprecated methods --- mypy/stubtest.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index b9212ab387bc5..8e5c6e6b9db06 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -656,6 +656,16 @@ def verify_funcitem( yield Error(object_path, "is not present at runtime", stub, runtime) return + if ( + not isinstance(runtime, (types.FunctionType, types.BuiltinFunctionType)) + and not isinstance(runtime, (types.MethodType, types.BuiltinMethodType)) + and not inspect.ismethoddescriptor(runtime) + and hasattr(runtime, "value") + ): + # Workaround for pyOpenSSL that has some of its function wrapped into cryptography.utils._DeprecatedValue instances. + # See https://github.com/python/typeshed/pull/5657 for details. + runtime = runtime.value + if ( not isinstance(runtime, (types.FunctionType, types.BuiltinFunctionType)) and not isinstance(runtime, (types.MethodType, types.BuiltinMethodType)) From 2038cbd49ac172fb960a8e2b27834a4304f230aa Mon Sep 17 00:00:00 2001 From: Vasily Zakharov Date: Thu, 17 Jun 2021 22:05:27 +0300 Subject: [PATCH 4/7] Formatting fixed --- mypy/stubtest.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 8e5c6e6b9db06..3dc585f4a3f8a 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -203,7 +203,8 @@ def verify_mypyfile( yield Error(object_path, "is not present at runtime", stub, runtime) return if not isinstance(runtime, types.ModuleType) and hasattr(runtime, "_module"): - # Workaround for pyOpenSSL that has its submodules wrapped into cryptography.utils._ModuleWithDeprecations instances. + # Workaround for pyOpenSSL that has its submodules + # wrapped into cryptography.utils._ModuleWithDeprecations instances. # See https://github.com/python/typeshed/pull/5657 for details. runtime = runtime._module if not isinstance(runtime, types.ModuleType): @@ -662,7 +663,8 @@ def verify_funcitem( and not inspect.ismethoddescriptor(runtime) and hasattr(runtime, "value") ): - # Workaround for pyOpenSSL that has some of its function wrapped into cryptography.utils._DeprecatedValue instances. + # Workaround for pyOpenSSL that has some of its function + # wrapped into cryptography.utils._DeprecatedValue instances. # See https://github.com/python/typeshed/pull/5657 for details. runtime = runtime.value From 50a7106374f66004d961ff90463a5bcf8d44764f Mon Sep 17 00:00:00 2001 From: Vasily Zakharov Date: Thu, 17 Jun 2021 23:31:41 +0300 Subject: [PATCH 5/7] More explicit version of the checks --- mypy/stubtest.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 3dc585f4a3f8a..077141db9d1b7 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -202,11 +202,13 @@ def verify_mypyfile( if isinstance(runtime, Missing): yield Error(object_path, "is not present at runtime", stub, runtime) return - if not isinstance(runtime, types.ModuleType) and hasattr(runtime, "_module"): - # Workaround for pyOpenSSL that has its submodules - # wrapped into cryptography.utils._ModuleWithDeprecations instances. - # See https://github.com/python/typeshed/pull/5657 for details. + + # Workaround for pyOpenSSL that has its module objects wrapped. + # See https://github.com/python/typeshed/pull/5657 for details. + if f"{type(runtime).__module__}.{type(runtime).__name__}" \ + == "cryptography.utils._ModuleWithDeprecations": runtime = runtime._module + if not isinstance(runtime, types.ModuleType): yield Error(object_path, "is not a module", stub, runtime) return @@ -657,15 +659,10 @@ def verify_funcitem( yield Error(object_path, "is not present at runtime", stub, runtime) return - if ( - not isinstance(runtime, (types.FunctionType, types.BuiltinFunctionType)) - and not isinstance(runtime, (types.MethodType, types.BuiltinMethodType)) - and not inspect.ismethoddescriptor(runtime) - and hasattr(runtime, "value") - ): - # Workaround for pyOpenSSL that has some of its function - # wrapped into cryptography.utils._DeprecatedValue instances. - # See https://github.com/python/typeshed/pull/5657 for details. + # Workaround for pyOpenSSL that has some of its function objects wrapped. + # See https://github.com/python/typeshed/pull/5657 for details. + if f"{type(runtime).__module__}.{type(runtime).__name__}" \ + == "cryptography.utils._DeprecatedValue": runtime = runtime.value if ( From 350b7c4200b9f6b010f8989d2d359e969e8c3f13 Mon Sep 17 00:00:00 2001 From: Vasily Zakharov Date: Thu, 17 Jun 2021 23:33:15 +0300 Subject: [PATCH 6/7] More explicit version of the checks --- mypy/stubtest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 077141db9d1b7..615bcafec7624 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -205,7 +205,7 @@ def verify_mypyfile( # Workaround for pyOpenSSL that has its module objects wrapped. # See https://github.com/python/typeshed/pull/5657 for details. - if f"{type(runtime).__module__}.{type(runtime).__name__}" \ + if f"{type(runtime).__module__}.{type(runtime).__qualname__}" \ == "cryptography.utils._ModuleWithDeprecations": runtime = runtime._module @@ -661,7 +661,7 @@ def verify_funcitem( # Workaround for pyOpenSSL that has some of its function objects wrapped. # See https://github.com/python/typeshed/pull/5657 for details. - if f"{type(runtime).__module__}.{type(runtime).__name__}" \ + if f"{type(runtime).__module__}.{type(runtime).__qualname__}" \ == "cryptography.utils._DeprecatedValue": runtime = runtime.value From c8e1a2dfccc201c3dd9b6978c11430f6cc70f9df Mon Sep 17 00:00:00 2001 From: Vasily Zakharov Date: Fri, 18 Jun 2021 00:20:40 +0300 Subject: [PATCH 7/7] typing ignore added --- mypy/stubtest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 615bcafec7624..f0fb16900bde0 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -207,7 +207,7 @@ def verify_mypyfile( # See https://github.com/python/typeshed/pull/5657 for details. if f"{type(runtime).__module__}.{type(runtime).__qualname__}" \ == "cryptography.utils._ModuleWithDeprecations": - runtime = runtime._module + runtime = runtime._module # type: ignore[attr-defined] if not isinstance(runtime, types.ModuleType): yield Error(object_path, "is not a module", stub, runtime)