Skip to content

BIP352: complete return type annotations in bitcoin_utils#2154

Merged
jonatack merged 1 commit into
bitcoin:masterfrom
omipheo:bip-0352-typing-return-annotations
May 12, 2026
Merged

BIP352: complete return type annotations in bitcoin_utils#2154
jonatack merged 1 commit into
bitcoin:masterfrom
omipheo:bip-0352-typing-return-annotations

Conversation

@omipheo
Copy link
Copy Markdown
Contributor

@omipheo omipheo commented May 8, 2026

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

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.
@jonatack
Copy link
Copy Markdown
Member

jonatack commented May 8, 2026

Approach ACK

@edilmedeiros
Copy link
Copy Markdown

utACK 3b76c84

Copy link
Copy Markdown
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 3b76c84

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?

@jonatack jonatack merged commit fd250df into bitcoin:master May 12, 2026
4 checks passed
Copy link
Copy Markdown
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems the "Verification" section in the PR description is incomplete?

@omipheo mind fixing the PR description? (thanks!)

@omipheo
Copy link
Copy Markdown
Contributor Author

omipheo commented May 12, 2026

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants