Skip to content

Conversation

fanquake
Copy link
Member

TLDR: This add type hints to base58.py, as well as some basic tests and a Travis job that runs mypy.

This adds some simple base58 tests (copied from the Bitcoin Core repo), type hints for base58.py and a Travis job that runs mypy on base58.py. I started with base58.py to keep everything self contained.

mypy was added as a dev dependency using poetry add mypy --dev.

Given that the project is using > Python 3.6, we can also take advantage variable type annotations.

Just opening as a draft for some concept level discussion. If there is interested then more anntotations can be added, and the Travis mypy scope increased. If we are happy to add type annotations here, then I'll PR the same changes to any upstream libraries, i.e bech32.py.

Another good reference for type hints is: https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html

@jb55
Copy link
Contributor

jb55 commented Jul 1, 2019

Approach ACK!

Can't go wrong with more type annotations.

@IlyasRidhuan
Copy link

Concept Ack

@fanquake fanquake marked this pull request as ready for review August 21, 2019 00:10
@Sjors
Copy link
Member

Sjors commented Aug 21, 2019

Concept ACK on type safety!

@fanquake can you force push a trivial change when you're released from Travis prison? git commit --amend --date "$(date)" should do the trick.

@fanquake
Copy link
Member Author

@Sjors I've force pushed a change.

@achow101
Copy link
Member

achow101 commented Sep 5, 2019

The project is actually listed as allowing Python 3.5.6, so I believe this would break that. However I wouldn't be opposed to bumping the minimum to 3.6.

Also needs rebase.

@fanquake
Copy link
Member Author

fanquake commented Sep 6, 2019

Will rebase once #246 and #247 are in.

@practicalswift
Copy link

Concept ACK: Python without type hints is like driving without a safety belt :-)

(Like C++ without the sanitisers or static analysis!)

@achow101
Copy link
Member

Tried using Python 3.5.6, our current minimum, and got:

Traceback (most recent call last):
  File "./run_tests.py", line 7, in <module>
    from test_base58 import TestBase58
  File "/home/andy/bitcoin/HWI6/test/test_base58.py", line 12
    TEST_VECTORS: List[Tuple[str, str]] = [
                ^
SyntaxError: invalid syntax

.travis.yml Outdated
@@ -54,6 +54,8 @@ jobs:
- name: lint
stage: lint
script: flake8
- name: Type annotation checking
script: poetry run mypy hwilib/base58.py
Copy link
Member

@achow101 achow101 Sep 23, 2019

Choose a reason for hiding this comment

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

Could you add an install phase that just installs mypy and whatever else is needed to run the check?

We don't need to do the full install phase since that does a bunch of things unneeded for linters. See #261

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated to use an install phase, and dropped the mypy dev dependency.

@fanquake
Copy link
Member Author

Tried using Python 3.5.6, our current minimum, and got:

If you're still happy to bump the minimum Python version to 3.6.x, I can do that here. Otherwise I can propose it in a separate PR?

achow101 added a commit that referenced this pull request Sep 24, 2019
d6b1d09 Bump minimum python version to 3.6 (Andrew Chow)

Pull request description:

  Needed for #203

  This is not expected to impact existing projects using HWI. AFAIK there are only two projects that use HWI, [Wasabi Wallet](https://github.com/zkSNACKs/WalletWasabi) and [Junction](https://github.com/justinmoon/junction). Wasabi use the binary distribution so it is not effected. Junction also requires 3.6+.

ACKs for top commit:
  fanquake:
    ACK d6b1d09 - will rebase #203 afterwards.

Tree-SHA512: 147e2a24962b3bb85566468836b99ae25719d2ff4208711d0ca26b27cd7301efec050a44234184911bbe9e703a0b2a661d36b69e098ce4a3dc642e9b53027251
@fanquake
Copy link
Member Author

Rebased for #261 & #262.

@laanwj
Copy link
Member

laanwj commented Sep 25, 2019

ACK b739f38

1 similar comment
@achow101
Copy link
Member

ACK b739f38

achow101 added a commit that referenced this pull request Sep 25, 2019
b739f38 travis: add mypy lint job (fanquake)
06fa997 base58: add type hints (fanquake)
394dab3 base58: remove Python3 check (fanquake)
a2edbf1 test: Add base58 tests (fanquake)

Pull request description:

  TLDR: This add type hints to base58.py, as well as some basic tests and a Travis job that runs [`mypy`](http://mypy-lang.org).

  This adds some simple base58 tests (copied from the Bitcoin Core repo), [type hints](https://docs.python.org/3.6/library/typing.html) for base58.py and a Travis job that runs `mypy` on base58.py. I started with base58.py to keep everything self contained.

  `mypy` was added as a dev dependency using `poetry add mypy --dev`.

  Given that the project is using > Python 3.6, we can also take advantage [variable type annotations](https://www.python.org/dev/peps/pep-0526/).

  Just opening as a draft for some concept level discussion. If there is interested then more anntotations can be added, and the Travis `mypy` scope increased. If we are happy to add type annotations here, then I'll PR the same changes to any upstream libraries, i.e [`bech32.py`](https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py).

  Another good reference for type hints is: https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html

ACKs for top commit:
  laanwj:
    ACK b739f38
  achow101:
    ACK b739f38

Tree-SHA512: 839822b75756a2cd6d3595988d8bbe2cce962a344b71d2abc4d5b410a4161f3ad98c8aa35881fd148c0c0646e146d8942fa4a5f69f4ddf990da9d00599d7df39
@achow101 achow101 merged commit b739f38 into bitcoin-core:master Sep 25, 2019
@fanquake fanquake deleted the base58_type_hints branch December 21, 2019 19:31
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.

7 participants