Skip to content

Commit 4c11ab0

Browse files
authored
adopted mypy --strict (#87)
1 parent 9478bf5 commit 4c11ab0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+653
-615
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ jobs:
2929
python-version: "3.11"
3030
- name: Check isort and black
3131
run: |
32-
python -m pip install -U pip isort black
32+
python -m pip install --upgrade pip isort black
3333
isort -c .
3434
black --check .

.github/workflows/test-pydev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ jobs:
2727
python-version: 3.12-dev
2828
- name: Run pytest
2929
run: |
30-
python -m pip install -U -rrequirements.txt -rrequirements-dev.txt
30+
python -m pip install --upgrade -rrequirements.txt -rrequirements-dev.txt
3131
pytest

.pre-commit-config.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ repos:
3838
hooks:
3939
- id: copyright-notice
4040
args: [--notice=COPYRIGHT]
41+
files: python
42+
- repo: https://github.com/asottile/pyupgrade
43+
rev: v3.3.1
44+
hooks:
45+
- id: pyupgrade
46+
args: [--py37-plus]
47+
# exclude: *fixtures
4148
- repo: https://github.com/PyCQA/autoflake
4249
rev: v2.0.0
4350
hooks:
@@ -50,12 +57,6 @@ repos:
5057
- --remove-duplicate-keys
5158
- --remove-unused-variables
5259
- --remove-rhs-for-unused-variables
53-
- repo: https://github.com/asottile/pyupgrade
54-
rev: v3.3.1
55-
hooks:
56-
- id: pyupgrade
57-
args: [--py37-plus]
58-
# exclude: *fixtures
5960
- repo: https://github.com/pycqa/isort
6061
rev: 5.11.4
6162
hooks:
@@ -97,16 +98,15 @@ repos:
9798
hooks:
9899
- id: bandit
99100
name: bandit (tests)
100-
# disable B101 (Test for use of assert) on tests folder
101-
args: ["-s", "B101"]
101+
# disable B101 (Test for use of assert) for the tests folder
102+
args: ["--skip", "B101"]
102103
exclude: btclib
103104
- repo: https://github.com/pycqa/pylint
104105
rev: v2.15.9
105106
hooks:
106107
- id: pylint
107108
args: [
108-
"-rn", # Only display messages
109-
"-sn", # Don't display the score
109+
"--score=false",
110110
"--disable=E0401", # import-error
111111
]
112112
- repo: https://github.com/pre-commit/mirrors-mypy

HISTORY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Notable changes to the codebase are documented here.
44

5-
Release names follow [*calendar versioning*](https://calver.org/):
5+
Release names follow [_calendar versioning_](https://calver.org/):
66
full year, short month, short day (YYYY-M-D)
77

88
## v2023.0.0 (unreleased yet)
@@ -13,6 +13,7 @@ Major changes include:
1313
- improved typing
1414
- added SECURITY, CONTRIBUTING, bug report and feature request templates
1515
- added pre-commit hooks
16+
- adopted _mypy --strict_ and _from \_\_future\_\_ import annotations_
1617

1718
## v2022.12.31
1819

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ recursive-include btclib *.md
2222
recursive-include btclib *.txt
2323
recursive-include btclib *.bin
2424
recursive-include btclib *.typed
25+
recursive-include tests *.typed
2526

2627
recursive-include tests *.bin
2728
recursive-include tests *.csv

btclib/alias.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
1313
mypy aliases, documenting also coding input conventions.
1414
"""
15+
from __future__ import annotations
1516

1617
from io import BytesIO
1718
from typing import Any, Callable, Tuple, Union
1819

19-
from typing_extensions import TypeAlias
20-
2120
# Octets are a sequence of eight-bit bytes or a hex-string (not text string)
2221
#
2322
# hex-strings are strings that can be converted to bytes using bytes.fromhex,
@@ -35,7 +34,7 @@
3534
# dsa.Sig (DER serialization of ECDSA signature),
3635
# ssa.Sig (BIP340 serialization of Schnorr signature)
3736
# etc.
38-
Octets: TypeAlias = Union[bytes, str]
37+
Octets = Union[bytes, str]
3938

4039
# bytes or text string (not hex-string)
4140
#
@@ -61,23 +60,23 @@
6160
#
6261
# In those cases often there is no need to encode() to bytes
6362
# as b58decode/b32decode/etc. will take care of that
64-
String: TypeAlias = Union[bytes, str]
63+
String = Union[bytes, str]
6564

6665
# binary data, usually to be cosumed as byte stream,
6766
# but possibily provided as Octets too
68-
BinaryData: TypeAlias = Union[BytesIO, Octets]
67+
BinaryData = Union[BytesIO, Octets]
6968

7069
# hex-string or bytes representation of an int
7170
# Integer = Union[Octets, int]
72-
Integer: TypeAlias = Union[bytes, str, int]
71+
Integer = Union[bytes, str, int]
7372

7473
# Hash digest constructor: it may be any name suitable to hashlib.new()
75-
HashF: TypeAlias = Callable[[], Any]
74+
HashF = Callable[[], Any]
7675
# HashF = Callable[[Any], Any]
7776

7877
# Elliptic curve point in affine coordinates.
7978
# Warning: to make Point a NamedTuple would slow down the code
80-
Point: TypeAlias = Tuple[int, int]
79+
Point = Tuple[int, int]
8180

8281
# Note that the infinity point in affine coordinates is INF = (int, 0)
8382
# (no affine point has y=0 coordinate in a group of prime order).
@@ -88,7 +87,7 @@
8887
INF = 5, 0
8988

9089
# Elliptic curve point in Jacobian coordinates.
91-
JacPoint: TypeAlias = Tuple[int, int, int]
90+
JacPoint = Tuple[int, int, int]
9291

9392
# Infinity point in Jacobian coordinates is INF = (int, int, 0).
9493
# It can be checked with 'INF[2] == 0'

btclib/amount.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
(in a backward compatible way) in the future if such a need arises.
2828
"""
2929

30+
from __future__ import annotations
31+
3032
from decimal import Decimal, FloatOperation, getcontext
3133
from typing import Any
3234

btclib/b32.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
(as this is not enforced by bech32.b32decode anymore)
4343
"""
4444

45+
from __future__ import annotations
4546

46-
from typing import Iterable, List, Optional, Tuple
47+
from typing import Iterable
4748

4849
from btclib.alias import Octets, String
4950
from btclib.bech32 import decode, encode
@@ -65,7 +66,7 @@ def has_segwit_prefix(addr: String) -> bool:
6566

6667
def power_of_2_base_conversion(
6768
data: Iterable[int], from_bits: int, to_bits: int, pad: bool = True
68-
) -> List[int]:
69+
) -> list[int]:
6970
"""Convert a power-of-two digit sequence to another power-of-two base."""
7071
acc = 0
7172
bits = 0
@@ -130,7 +131,7 @@ def address_from_witness(
130131
return _address_from_witness(wit_ver, wit_prg, hrp)
131132

132133

133-
def witness_from_address(b32addr: String) -> Tuple[int, bytes, str]:
134+
def witness_from_address(b32addr: String) -> tuple[int, bytes, str]:
134135
"""Return the witness from a bech32 native SegWit address.
135136
136137
The returned data structure is: version, program, network.
@@ -161,7 +162,7 @@ def witness_from_address(b32addr: String) -> Tuple[int, bytes, str]:
161162
# 1.+2. = 3. bech32 address from pub_key/script_pub_key
162163

163164

164-
def p2wpkh(key: Key, network: Optional[str] = None) -> str:
165+
def p2wpkh(key: Key, network: str | None = None) -> str:
165166
"""Return the p2wpkh bech32 address corresponding to a public key."""
166167
pub_key, network = pub_keyinfo_from_key(key, network, compressed=True)
167168
return address_from_witness(0, hash160(pub_key), network)
@@ -174,8 +175,8 @@ def p2wsh(script_pub_key: Octets, network: str = "mainnet") -> str:
174175

175176

176177
def p2tr(
177-
internal_key: Optional[Key] = None,
178-
script_path: Optional[TaprootScriptTree] = None,
178+
internal_key: Key | None = None,
179+
script_path: TaprootScriptTree | None = None,
179180
network: str = "mainnet",
180181
) -> str:
181182
"""Return the p2tr bech32 address corresponding to a taproot output key."""

btclib/b58.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
Base58 encoding of public keys and scripts as addresses,
1414
private keys as WIFs
1515
"""
16-
17-
from typing import Optional, Tuple
16+
from __future__ import annotations
1817

1918
from btclib import b32
2019
from btclib.alias import Octets, String
@@ -29,7 +28,7 @@
2928

3029

3130
def wif_from_prv_key(
32-
prv_key: PrvKey, network: Optional[str] = None, compressed: Optional[bool] = None
31+
prv_key: PrvKey, network: str | None = None, compressed: bool | None = None
3332
) -> str:
3433
"""Return the WIF encoding of a private key."""
3534

@@ -71,7 +70,7 @@ def address_from_h160(script_type: str, h160: Octets, network: str = "mainnet")
7170
return b58encode(payload).decode("ascii")
7271

7372

74-
def h160_from_address(b58addr: String) -> Tuple[str, bytes, str]:
73+
def h160_from_address(b58addr: String) -> tuple[str, bytes, str]:
7574
"""Return the payload from a base58 address."""
7675

7776
if isinstance(b58addr, str):
@@ -93,9 +92,7 @@ def h160_from_address(b58addr: String) -> Tuple[str, bytes, str]:
9392
# 1.+2. = 3. base58 address from pub_key/script_pub_key
9493

9594

96-
def p2pkh(
97-
key: Key, network: Optional[str] = None, compressed: Optional[bool] = None
98-
) -> str:
95+
def p2pkh(key: Key, network: str | None = None, compressed: bool | None = None) -> str:
9996
"""Return the p2pkh base58 address corresponding to a public key."""
10097
pub_key, network = pub_keyinfo_from_key(key, network, compressed=compressed)
10198
return address_from_h160("p2pkh", hash160(pub_key), network)
@@ -123,7 +120,7 @@ def _address_from_v0_witness(wit_prg: Octets, network: str = "mainnet") -> str:
123120
# 1.+2b. = 3b. base58 (p2sh-wrapped) SegWit addresses from pub_key/script_pub_key
124121

125122

126-
def p2wpkh_p2sh(key: Key, network: Optional[str] = None) -> str:
123+
def p2wpkh_p2sh(key: Key, network: str | None = None) -> str:
127124
"""Return the p2wpkh-p2sh base58 address corresponding to a public key."""
128125
pub_key, network = pub_keyinfo_from_key(key, network, compressed=True)
129126
witness_program = hash160(pub_key)

btclib/base58.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
it supports encoding bytes-like objects to ASCII bytes,
3636
and decoding ASCII bytes-like objects or ASCII strings to bytes.
3737
"""
38-
39-
from typing import Optional
38+
from __future__ import annotations
4039

4140
from btclib.alias import Octets, String
4241
from btclib.exceptions import BTClibValueError
@@ -74,7 +73,7 @@ def _b58encode(v: bytes) -> bytes:
7473
return result
7574

7675

77-
def b58encode(v: Octets, in_size: Optional[int] = None) -> bytes:
76+
def b58encode(v: Octets, in_size: int | None = None) -> bytes:
7877
"""Encode a bytes-like object using Base58Check."""
7978

8079
v = bytes_from_octets(v, in_size)
@@ -113,7 +112,7 @@ def _b58decode(v: bytes) -> bytes:
113112
return result
114113

115114

116-
def b58decode(v: String, out_size: Optional[int] = None) -> bytes:
115+
def b58decode(v: String, out_size: int | None = None) -> bytes:
117116
"""Decode a Base58Check encoded bytes-like object or ASCII string.
118117
119118
Optionally, it also ensures required output size.

0 commit comments

Comments
 (0)