BIP352: complete return type annotations in bitcoin_utils #2154

pull omipheo wants to merge 1 commits into bitcoin:master from omipheo:bip-0352-typing-return-annotations changing 1 files +8 −8
  1. omipheo commented at 6:10 AM on May 8, 2026: contributor

    Summary

    The serialization helpers in bip-0352/bitcoin_utils.py are partially typed. ser_uint32, hash160, and the is_p2* helpers already declare both argument and return types, but the surrounding from_hex, ser_uint256, deser_uint256, deser_txid, deser_compact_size, deser_string, and deser_string_vector helpers omit them.

    Fill in the missing return types (and the obvious argument types) so the file is consistent and so static analysis can flow types through the callers in reference.py. Follow-up to 2f7117c ("BIP352: fix Any typing").

    No behavior changes.

    Diff

    Function Before After
    from_hex def from_hex(hex_string) def from_hex(hex_string: str) -> BytesIO
    ser_uint256 def ser_uint256(u) def ser_uint256(u: int) -> bytes
    deser_uint256 def deser_uint256(f) def deser_uint256(f: BytesIO) -> int
    deser_txid def deser_txid(txid: str) def deser_txid(txid: str) -> bytes
    deser_compact_size def deser_compact_size(f: BytesIO) def deser_compact_size(f: BytesIO) -> int
    deser_string def deser_string(f: BytesIO) def deser_string(f: BytesIO) -> bytes
    deser_string_vector def deser_string_vector(f: BytesIO) def deser_string_vector(f: BytesIO) -> List[bytes]

    List is added to the existing typing import. The ser_uint32 and hash160 annotations on adjacent lines were the model.

    Verification

    Imported the module and round-tripped each helper to confirm runtime behavior is unchanged:

    $ python3 -c "
    import sys
    sys.path.insert(0, 'bip-0352')
    sys.path.insert(0, 'bip-0352/secp256k1lab/src')
    import bitcoin_utils
    print('from_hex   ', bitcoin_utils.from_hex('deadbeef').read(2).hex())
    print('ser_uint32 ', bitcoin_utils.ser_uint32(1).hex())
    print('ser_uint256', bitcoin_utils.ser_uint256(1).hex()[:16])
    print('deser_txid ', bitcoin_utils.deser_txid('00112233' * 8).hex()[:16])
    "
    from_hex    dead
    ser_uint32  00000001
    ser_uint256 0100000000000000
    deser_txid  3322110033221100
    

    Type-checker is happy across the whole file (matching @theStack's verification on the PR):

    $ mypy --ignore-missing-imports ./bip-0352/bitcoin_utils.py
    Success: no issues found in 1 source file
    
  2. BIP352: complete return type annotations in bitcoin_utils
    The serialization helpers in bip-0352/bitcoin_utils.py were partially
    typed: ser_uint32, hash160, is_p2tr, is_p2wpkh, is_p2sh and is_p2pkh
    already declare argument and return types, but the surrounding
    from_hex / ser_uint256 / deser_uint256 / deser_txid / deser_compact_size
    / deser_string / deser_string_vector helpers omit them.
    
    Annotate the missing return types (and fill in the obvious argument
    types) so the file is consistent and so static analysis can flow types
    through callers in reference.py. No behavior changes.
    3b76c84d6f
  3. jonatack commented at 7:34 PM on May 8, 2026: member

    Approach ACK

  4. edilmedeiros commented at 8:57 PM on May 9, 2026: none

    utACK 3b76c84d6f0273460c2e62b84252d7b7166bd5c5

  5. theStack approved
  6. theStack commented at 2:47 PM on May 12, 2026: contributor

    ACK 3b76c84d6f0273460c2e62b84252d7b7166bd5c5

    LGTM, and mypy seems to be happy as well:

    $ mypy --ignore-missing-imports ./bip-0352/bitcoin_utils.py 
    Success: no issues found in 1 source file
    

    non-blocking nit: seems the "Verification" section in the PR description is incomplete?

  7. jonatack merged this on May 12, 2026
  8. jonatack closed this on May 12, 2026

  9. jonatack commented at 4:21 PM on May 12, 2026: member

    seems the "Verification" section in the PR description is incomplete? @omipheo mind fixing the PR description? (thanks!)

  10. omipheo commented at 5:13 PM on May 12, 2026: contributor

    Done — Verification section in the PR description now has the actual round-trip script + output, and a mypy block mirroring @theStack's check. Thanks for both reviews!

  11. omipheo referenced this in commit 1259a23acc on May 15, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bips. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-19 06:50 UTC