Skip to content

BIP-327: fix reference.py type errors failing tests.sh - #2261

Merged
jonatack merged 1 commit into
bitcoin:masterfrom
SatsAndSports:fix-bip327-tests-mypy
Aug 19, 2026
Merged

BIP-327: fix reference.py type errors failing tests.sh#2261
jonatack merged 1 commit into
bitcoin:masterfrom
SatsAndSports:fix-bip327-tests-mypy

Conversation

@SatsAndSports

@SatsAndSports SatsAndSports commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Found during a review of BIP327's reference implementation and its shipped test gate.

bip-0327/tests.sh (the BIP's own set -e quality gate: mypy --no-error-summary reference.py && python3 reference.py && python3 gen_vectors_helper.py) currently fails at its first step under the current mypy (2.3.1), before the vector suite ever runs:

reference.py:354: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]
reference.py:355: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]
reference.py:672: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]
reference.py:673: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]

Cause: sign() and the tweak-test driver slice secnonce, which is a bytearray by design (mutable, for in-place zeroization); recent mypy/typeshed no longer accepts bytearray for a bytes parameter.

Fix: convert to immutable bytes at the four call sites (int_from_bytes(bytes(secnonce[...]))). This keeps the int_from_bytes helper byte-identical to the BIP-340 reference implementation it was copied from, and is behavior-identical at runtime (int.from_bytes already accepts both types).

Verification: full tests.sh gate now passes end-to-end in a clean container with mypy 2.3.1 (mypy clean + python3 reference.py — all vector suites and the randomized self-test — + gen_vectors_helper.py).

History: this was accepted in older versions of mypy because it was more relaxed then. But from mypy 2.0, it is stricter per PEP 688, mypy no longer treats bytearray and memoryview values as assignable to the bytes type. python/mypy#18371

@jonatack jonatack added the Fixups Minor fixups not worth bothering the BIP author(s) for label Aug 18, 2026
@jonatack

jonatack commented Aug 19, 2026

Copy link
Copy Markdown
Member

Issue reproduced.

~/bitcoin/bips/bip-0327$ ./tests.sh 
reference.py:354: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]
reference.py:355: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]
reference.py:672: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]
reference.py:673: error: Argument 1 to "int_from_bytes" has incompatible type "bytearray"; expected "bytes"  [arg-type]
~/bitcoin/bips/bip-0327$ mypy --version
mypy 2.3.1 (compiled: yes)

@jonatack jonatack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK

@jonatack
jonatack merged commit 9934351 into bitcoin:master Aug 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Fixups Minor fixups not worth bothering the BIP author(s) for

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants