Merge bitcoin/bitcoin#22092: test: convert documentation into type annotations

68ace23fa3 test: convert docs into type annotations in test_framework/test_node.py (fanquake)
8bfcba36db test: convert docs into type annotations in test_framework/wallet.py (fanquake)
b043ca8e8b test: convert docs into type annotations in test_framework/util.py (fanquake)

Pull request description:

  Rather than having function types exist as documentation, make them type annotations, which enables more `mypy` checking.

ACKs for top commit:
  instagibbs:
    utACK 68ace23fa3

Tree-SHA512: b705f26b48baabde07b9b2c0a8d547b4dcca291b16eaf5ac70462bb3a1f9e9c2783d93a9d4290889d0cbb3f7db3671446754411a1f498b265d336f6ff33478df
This commit is contained in:
fanquake 2021-06-07 13:05:05 +08:00
commit 260b1d74fe
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1
3 changed files with 16 additions and 14 deletions

View File

@ -400,14 +400,14 @@ class TestNode():
self._raise_assertion_error('Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(str(expected_msgs), print_log)) self._raise_assertion_error('Expected messages "{}" does not partially match log:\n\n{}\n\n'.format(str(expected_msgs), print_log))
@contextlib.contextmanager @contextlib.contextmanager
def profile_with_perf(self, profile_name): def profile_with_perf(self, profile_name: str):
""" """
Context manager that allows easy profiling of node activity using `perf`. Context manager that allows easy profiling of node activity using `perf`.
See `test/functional/README.md` for details on perf usage. See `test/functional/README.md` for details on perf usage.
Args: Args:
profile_name (str): This string will be appended to the profile_name: This string will be appended to the
profile data filename generated by perf. profile data filename generated by perf.
""" """
subp = self._start_perf(profile_name) subp = self._start_perf(profile_name)

View File

@ -20,6 +20,7 @@ import unittest
from . import coverage from . import coverage
from .authproxy import AuthServiceProxy, JSONRPCException from .authproxy import AuthServiceProxy, JSONRPCException
from io import BytesIO from io import BytesIO
from typing import Callable, Optional
logger = logging.getLogger("TestFramework.utils") logger = logging.getLogger("TestFramework.utils")
@ -80,7 +81,7 @@ def assert_raises_message(exc, message, fun, *args, **kwds):
raise AssertionError("No exception raised") raise AssertionError("No exception raised")
def assert_raises_process_error(returncode, output, fun, *args, **kwds): def assert_raises_process_error(returncode: int, output: str, fun: Callable, *args, **kwds):
"""Execute a process and asserts the process return code and output. """Execute a process and asserts the process return code and output.
Calls function `fun` with arguments `args` and `kwds`. Catches a CalledProcessError Calls function `fun` with arguments `args` and `kwds`. Catches a CalledProcessError
@ -88,9 +89,9 @@ def assert_raises_process_error(returncode, output, fun, *args, **kwds):
no CalledProcessError was raised or if the return code and output are not as expected. no CalledProcessError was raised or if the return code and output are not as expected.
Args: Args:
returncode (int): the process return code. returncode: the process return code.
output (string): [a substring of] the process output. output: [a substring of] the process output.
fun (function): the function to call. This should execute a process. fun: the function to call. This should execute a process.
args*: positional arguments for the function. args*: positional arguments for the function.
kwds**: named arguments for the function. kwds**: named arguments for the function.
""" """
@ -105,7 +106,7 @@ def assert_raises_process_error(returncode, output, fun, *args, **kwds):
raise AssertionError("No exception raised") raise AssertionError("No exception raised")
def assert_raises_rpc_error(code, message, fun, *args, **kwds): def assert_raises_rpc_error(code: Optional[int], message: Optional[str], fun: Callable, *args, **kwds):
"""Run an RPC and verify that a specific JSONRPC exception code and message is raised. """Run an RPC and verify that a specific JSONRPC exception code and message is raised.
Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException
@ -113,11 +114,11 @@ def assert_raises_rpc_error(code, message, fun, *args, **kwds):
no JSONRPCException was raised or if the error code/message are not as expected. no JSONRPCException was raised or if the error code/message are not as expected.
Args: Args:
code (int), optional: the error code returned by the RPC call (defined code: the error code returned by the RPC call (defined in src/rpc/protocol.h).
in src/rpc/protocol.h). Set to None if checking the error code is not required. Set to None if checking the error code is not required.
message (string), optional: [a substring of] the error string returned by the message: [a substring of] the error string returned by the RPC call.
RPC call. Set to None if checking the error string is not required. Set to None if checking the error string is not required.
fun (function): the function to call. This should be the name of an RPC. fun: the function to call. This should be the name of an RPC.
args*: positional arguments for the function. args*: positional arguments for the function.
kwds**: named arguments for the function. kwds**: named arguments for the function.
""" """

View File

@ -6,6 +6,7 @@
from decimal import Decimal from decimal import Decimal
from enum import Enum from enum import Enum
from typing import Optional
from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
from test_framework.key import ECKey from test_framework.key import ECKey
from test_framework.messages import ( from test_framework.messages import (
@ -105,12 +106,12 @@ class MiniWallet:
def get_address(self): def get_address(self):
return self._address return self._address
def get_utxo(self, *, txid='', mark_as_spent=True): def get_utxo(self, *, txid: Optional[str]='', mark_as_spent=True):
""" """
Returns a utxo and marks it as spent (pops it from the internal list) Returns a utxo and marks it as spent (pops it from the internal list)
Args: Args:
txid (string), optional: get the first utxo we find from a specific transaction txid: get the first utxo we find from a specific transaction
Note: Can be used to get the change output immediately after a send_self_transfer Note: Can be used to get the change output immediately after a send_self_transfer
""" """