From badba97ff20a961153ddd86490c9646e042df172 Mon Sep 17 00:00:00 2001 From: dlee Date: Fri, 17 Feb 2023 16:20:29 -0600 Subject: [PATCH 01/14] Type annotations added for wormholetesting.py --- src/allmydata/test/cli/wormholetesting.py | 35 +++++++++++------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index 744f9d75a..7cf9d7eff 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -32,8 +32,7 @@ For example:: from __future__ import annotations -from typing import Iterator, Optional, List, Tuple -from collections.abc import Awaitable +from typing import Iterator, Optional, List, Tuple, Any, TextIO from inspect import getargspec from itertools import count from sys import stderr @@ -66,18 +65,18 @@ class MemoryWormholeServer(object): def create( self, - appid, - relay_url, - reactor, - versions={}, - delegate=None, - journal=None, - tor=None, - timing=None, - stderr=stderr, - _eventual_queue=None, - _enable_dilate=False, - ): + appid: str, + relay_url: str, + reactor: Any, + versions: Any={}, + delegate: Optional[Any]=None, + journal: Optional[Any]=None, + tor: Optional[Any]=None, + timing: Optional[Any]=None, + stderr: TextIO=stderr, + _eventual_queue: Optional[Any]=None, + _enable_dilate: bool=False, + )-> _MemoryWormhole: """ Create a wormhole. It will be able to connect to other wormholes created by this instance (and constrained by the normal appid/relay_url @@ -184,7 +183,7 @@ class _WormholeApp(object): return code - def wait_for_wormhole(self, code: WormholeCode) -> Awaitable[_MemoryWormhole]: + def wait_for_wormhole(self, code: WormholeCode) -> Deferred[_MemoryWormhole]: """ Return a ``Deferred`` which fires with the next wormhole to be associated with the given code. This is used to let the first end of a wormhole @@ -262,7 +261,7 @@ class _MemoryWormhole(object): return d return succeed(self._code) - def get_welcome(self): + def get_welcome(self) -> Deferred[str]: return succeed("welcome") def send_message(self, payload: WormholeMessage) -> None: @@ -276,8 +275,8 @@ class _MemoryWormhole(object): ) d = self._view.wormhole_by_code(self._code, exclude=self) - def got_wormhole(wormhole): - msg = wormhole._payload.get() + def got_wormhole(wormhole: _MemoryWormhole) -> Deferred[_MemoryWormhole]: + msg: Deferred[_MemoryWormhole] = wormhole._payload.get() return msg d.addCallback(got_wormhole) From 86dbcb21ce4f27e779b3d8febc633c1cbf3fd97e Mon Sep 17 00:00:00 2001 From: dlee Date: Fri, 17 Feb 2023 16:24:32 -0600 Subject: [PATCH 02/14] Refactored verify function to update deprecated getargspec function with getfullargspec and maintained strictness --- src/allmydata/test/cli/wormholetesting.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index 7cf9d7eff..9ce199545 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -33,7 +33,7 @@ For example:: from __future__ import annotations from typing import Iterator, Optional, List, Tuple, Any, TextIO -from inspect import getargspec +from inspect import getfullargspec from itertools import count from sys import stderr @@ -133,18 +133,24 @@ class TestingHelper(object): return wormhole -def _verify(): +def _verify() -> None: """ Roughly confirm that the in-memory wormhole creation function matches the interface of the real implementation. """ # Poor man's interface verification. - a = getargspec(create) - b = getargspec(MemoryWormholeServer.create) + a = getfullargspec(create) + b = getfullargspec(MemoryWormholeServer.create) + # I know it has a `self` argument at the beginning. That's okay. b = b._replace(args=b.args[1:]) - assert a == b, "{} != {}".format(a, b) + + # Just compare the same information to check function signature + assert a.varkw == b.varkw + assert a.args == b.args + assert a.varargs == b.varargs + assert a.kwonlydefaults == b.kwonlydefaults _verify() From be9d76e2b8cffda206afe066fe00be2db8dd6759 Mon Sep 17 00:00:00 2001 From: dlee Date: Fri, 17 Feb 2023 16:24:52 -0600 Subject: [PATCH 03/14] Added newsfragment --- newsfragments/3970.minor | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 newsfragments/3970.minor diff --git a/newsfragments/3970.minor b/newsfragments/3970.minor new file mode 100644 index 000000000..e69de29bb From af51b022284f2b7284a806de7beb2223f5ad9961 Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 27 Feb 2023 15:05:52 -0600 Subject: [PATCH 04/14] Revert wait_for_wormhole function return type back to Awaitable for forward compatibility when we move to async def --- src/allmydata/test/cli/wormholetesting.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index 9ce199545..b30b92fe1 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -38,7 +38,7 @@ from itertools import count from sys import stderr from attrs import frozen, define, field, Factory -from twisted.internet.defer import Deferred, DeferredQueue, succeed +from twisted.internet.defer import Deferred, DeferredQueue, succeed, Awaitable from wormhole._interfaces import IWormhole from wormhole.wormhole import create from zope.interface import implementer @@ -189,7 +189,7 @@ class _WormholeApp(object): return code - def wait_for_wormhole(self, code: WormholeCode) -> Deferred[_MemoryWormhole]: + def wait_for_wormhole(self, code: WormholeCode) -> Awaitable[_MemoryWormhole]: """ Return a ``Deferred`` which fires with the next wormhole to be associated with the given code. This is used to let the first end of a wormhole @@ -281,8 +281,8 @@ class _MemoryWormhole(object): ) d = self._view.wormhole_by_code(self._code, exclude=self) - def got_wormhole(wormhole: _MemoryWormhole) -> Deferred[_MemoryWormhole]: - msg: Deferred[_MemoryWormhole] = wormhole._payload.get() + def got_wormhole(wormhole: _MemoryWormhole) -> Deferred[WormholeMessage]: + msg: Deferred[WormholeMessage] = wormhole._payload.get() return msg d.addCallback(got_wormhole) From 582876197a724dd8c24b06160345d122832e03b6 Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 27 Feb 2023 15:14:58 -0600 Subject: [PATCH 05/14] Added default check to verify to ensure strictness --- src/allmydata/test/cli/wormholetesting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index b30b92fe1..d4e53a342 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -151,6 +151,7 @@ def _verify() -> None: assert a.args == b.args assert a.varargs == b.varargs assert a.kwonlydefaults == b.kwonlydefaults + assert a.defaults == b.defaults _verify() From 8ccbd37d29906cef62d8db22573878534a783fdd Mon Sep 17 00:00:00 2001 From: dlee Date: Wed, 8 Mar 2023 15:16:03 -0600 Subject: [PATCH 06/14] Fix implicit re-export error by importing IWormhole from wormhole library directly --- src/allmydata/test/cli/test_invite.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/allmydata/test/cli/test_invite.py b/src/allmydata/test/cli/test_invite.py index 07756eeed..94d4395ff 100644 --- a/src/allmydata/test/cli/test_invite.py +++ b/src/allmydata/test/cli/test_invite.py @@ -19,7 +19,8 @@ from ...util.jsonbytes import dumps_bytes from ..common_util import run_cli from ..no_network import GridTestMixin from .common import CLITestMixin -from .wormholetesting import IWormhole, MemoryWormholeServer, TestingHelper, memory_server +from .wormholetesting import MemoryWormholeServer, TestingHelper, memory_server +from wormhole._interfaces import IWormhole # Logically: # JSONable = dict[str, Union[JSONable, None, int, float, str, list[JSONable]]] From 10b3eabed41baedd47e3b4f9ce55aec92699003a Mon Sep 17 00:00:00 2001 From: dlee Date: Wed, 8 Mar 2023 15:19:08 -0600 Subject: [PATCH 07/14] Apply per file flags corresponding to --strict to wormholetesting.py --- mypy.ini | 2 +- src/allmydata/test/cli/wormholetesting.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index e6e7d16ff..27e9f6154 100644 --- a/mypy.ini +++ b/mypy.ini @@ -7,4 +7,4 @@ show_error_codes = True warn_unused_configs =True no_implicit_optional = True warn_redundant_casts = True -strict_equality = True \ No newline at end of file +strict_equality = True diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index d4e53a342..a0050a75b 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -29,6 +29,7 @@ For example:: import wormhole run(peerA(wormhole)) """ +# mypy: warn-unused-configs, disallow-any-generics, disallow-subclassing-any, disallow-untyped-calls, disallow-untyped-defs, disallow-incomplete-defs, check-untyped-defs, disallow-untyped-decorators, warn-redundant-casts, warn-unused-ignores, warn-return-any, no-implicit-reexport, strict-equality, strict-concatenate from __future__ import annotations From 4f47a18c6af89e92c81641c9bcc96bb30398c355 Mon Sep 17 00:00:00 2001 From: dlee Date: Wed, 8 Mar 2023 15:29:50 -0600 Subject: [PATCH 08/14] Comments added for inline mypy config. Individual flags used as --strict flag can only be used on a per-module basis. --- src/allmydata/test/cli/wormholetesting.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index a0050a75b..6fb2b791c 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -30,6 +30,10 @@ For example:: run(peerA(wormhole)) """ # mypy: warn-unused-configs, disallow-any-generics, disallow-subclassing-any, disallow-untyped-calls, disallow-untyped-defs, disallow-incomplete-defs, check-untyped-defs, disallow-untyped-decorators, warn-redundant-casts, warn-unused-ignores, warn-return-any, no-implicit-reexport, strict-equality, strict-concatenate +# This inline mypy config applies a per-file mypy config for this file. +# It applies the '--strict' list of flags to this file. +# If you want to test using CLI run the command remove the inline config above and run: +# "mypy --follow-imports silent --strict src/allmydata/test/cli/wormholetesting.py" from __future__ import annotations From 74ff8cd08041a1107b05771778310449bf4d99f8 Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 13 Mar 2023 11:04:52 -0500 Subject: [PATCH 09/14] Per-file configuration for wormholetesting.py moved from inline mypy configuration moved to mypy.ini file --- mypy.ini | 16 ++++++++++++++++ src/allmydata/test/cli/wormholetesting.py | 5 ----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/mypy.ini b/mypy.ini index 27e9f6154..c391c5594 100644 --- a/mypy.ini +++ b/mypy.ini @@ -8,3 +8,19 @@ warn_unused_configs =True no_implicit_optional = True warn_redundant_casts = True strict_equality = True + +[mypy-allmydata.test.cli.wormholetesting] +warn_unused_configs = True +disallow_any_generics = True +disallow_subclassing_any = True +disallow_untyped_calls = True +disallow_untyped_defs = True +disallow_incomplete_defs = True +check_untyped_defs = True +disallow_untyped_decorators = True +warn_redundant_casts = True +warn_unused_ignores = True +warn_return_any = True +no_implicit_reexport = True +strict_equality = True +strict_concatenate = True diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index 6fb2b791c..d4e53a342 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -29,11 +29,6 @@ For example:: import wormhole run(peerA(wormhole)) """ -# mypy: warn-unused-configs, disallow-any-generics, disallow-subclassing-any, disallow-untyped-calls, disallow-untyped-defs, disallow-incomplete-defs, check-untyped-defs, disallow-untyped-decorators, warn-redundant-casts, warn-unused-ignores, warn-return-any, no-implicit-reexport, strict-equality, strict-concatenate -# This inline mypy config applies a per-file mypy config for this file. -# It applies the '--strict' list of flags to this file. -# If you want to test using CLI run the command remove the inline config above and run: -# "mypy --follow-imports silent --strict src/allmydata/test/cli/wormholetesting.py" from __future__ import annotations From 61c835c8a05c15b7eabe29b453633b7c4da022e8 Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 13 Mar 2023 11:17:01 -0500 Subject: [PATCH 10/14] Added missing space between return type --- src/allmydata/test/cli/wormholetesting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index d4e53a342..be94a7981 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -76,7 +76,7 @@ class MemoryWormholeServer(object): stderr: TextIO=stderr, _eventual_queue: Optional[Any]=None, _enable_dilate: bool=False, - )-> _MemoryWormhole: + ) -> _MemoryWormhole: """ Create a wormhole. It will be able to connect to other wormholes created by this instance (and constrained by the normal appid/relay_url From b58dd2bb3bed375258f611eb0af39f6c08f64684 Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 13 Mar 2023 12:27:53 -0500 Subject: [PATCH 11/14] Remove flags that are unused --- mypy.ini | 2 -- 1 file changed, 2 deletions(-) diff --git a/mypy.ini b/mypy.ini index c391c5594..7acc0ddc5 100644 --- a/mypy.ini +++ b/mypy.ini @@ -10,7 +10,6 @@ warn_redundant_casts = True strict_equality = True [mypy-allmydata.test.cli.wormholetesting] -warn_unused_configs = True disallow_any_generics = True disallow_subclassing_any = True disallow_untyped_calls = True @@ -18,7 +17,6 @@ disallow_untyped_defs = True disallow_incomplete_defs = True check_untyped_defs = True disallow_untyped_decorators = True -warn_redundant_casts = True warn_unused_ignores = True warn_return_any = True no_implicit_reexport = True From 041a634d27f1f2adfdc82471e60192aaecb1fbfc Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 13 Mar 2023 13:08:32 -0500 Subject: [PATCH 12/14] Fix private interface import to test_invite --- src/allmydata/test/cli/test_invite.py | 4 ++-- src/allmydata/test/cli/wormholetesting.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/allmydata/test/cli/test_invite.py b/src/allmydata/test/cli/test_invite.py index 94d4395ff..31992a54d 100644 --- a/src/allmydata/test/cli/test_invite.py +++ b/src/allmydata/test/cli/test_invite.py @@ -19,8 +19,8 @@ from ...util.jsonbytes import dumps_bytes from ..common_util import run_cli from ..no_network import GridTestMixin from .common import CLITestMixin -from .wormholetesting import MemoryWormholeServer, TestingHelper, memory_server -from wormhole._interfaces import IWormhole +from .wormholetesting import MemoryWormholeServer, TestingHelper, memory_server, IWormhole + # Logically: # JSONable = dict[str, Union[JSONable, None, int, float, str, list[JSONable]]] diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index be94a7981..9fbe8b63e 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -32,6 +32,8 @@ For example:: from __future__ import annotations +__all__ = ['IWormhole'] + from typing import Iterator, Optional, List, Tuple, Any, TextIO from inspect import getfullargspec from itertools import count @@ -76,7 +78,7 @@ class MemoryWormholeServer(object): stderr: TextIO=stderr, _eventual_queue: Optional[Any]=None, _enable_dilate: bool=False, - ) -> _MemoryWormhole: + )-> _MemoryWormhole: """ Create a wormhole. It will be able to connect to other wormholes created by this instance (and constrained by the normal appid/relay_url From f1be1ca1de497f4b3ebb5d5795803a5a331dcba9 Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 13 Mar 2023 14:53:25 -0500 Subject: [PATCH 13/14] Added more elements to export list in wormholetesting.py --- src/allmydata/test/cli/wormholetesting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index 9fbe8b63e..99e26e64b 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -32,7 +32,7 @@ For example:: from __future__ import annotations -__all__ = ['IWormhole'] +__all__ = ['MemoryWormholeServer', 'TestingHelper', 'memory_server', 'IWormhole'] from typing import Iterator, Optional, List, Tuple, Any, TextIO from inspect import getfullargspec From 1c926aeb869817c0a2aaa76786075b5459c396a2 Mon Sep 17 00:00:00 2001 From: dlee Date: Mon, 13 Mar 2023 16:23:28 -0500 Subject: [PATCH 14/14] Add space to return type --- src/allmydata/test/cli/wormholetesting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/allmydata/test/cli/wormholetesting.py b/src/allmydata/test/cli/wormholetesting.py index 99e26e64b..eb6249a0d 100644 --- a/src/allmydata/test/cli/wormholetesting.py +++ b/src/allmydata/test/cli/wormholetesting.py @@ -78,7 +78,7 @@ class MemoryWormholeServer(object): stderr: TextIO=stderr, _eventual_queue: Optional[Any]=None, _enable_dilate: bool=False, - )-> _MemoryWormhole: + ) -> _MemoryWormhole: """ Create a wormhole. It will be able to connect to other wormholes created by this instance (and constrained by the normal appid/relay_url