kiminuo
commented at 7:15 pm on February 25, 2020:
contributor
This PR adds initial support for type hints checking in python scripts.
Support for type hints was introduced in Python 3.5. Type hints make it easier to read and review code in my opinion. Also an IDE may discover a potential bug sooner. Yet, as PEP 484 says: “It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.”
Mypy is used in lint-python.sh to do the type checking. The package is standard so there is little chance that it will be abandoned. Mypy checks that type hints in source code are correct when they are not, it fails with an error.
Notes:
–ignore-missing-imports switch is passed on to mypy checker for now. The effect of this is that one does not need # type: ignore for import zmq. More information about import processing can be found here. This can be changed in a follow-up PR, if it is deemed useful.
MarcoFalke
commented at 7:23 pm on February 25, 2020:
member
Is there a way to make python crash if the type hint is incorrect? In that case they seem useful, otherwise they might get outdated and cause more confusion.
DrahtBot added the label
Tests
on Feb 25, 2020
kiminuo
commented at 7:48 pm on February 25, 2020:
contributor
There is Mypy - a static type checker for Python 3 and Python 2.7.
Example
0PS C:\temp> cat test.py
1#!/usr/bin/env python323s: str
4s =15PS C:\temp> python -m mypy test.py
6test.py:4: error: Incompatible types in assignment (expression has type "int", variable has type "str")
7Found 1 error in1 file (checked 1 source file)
DrahtBot
commented at 10:23 pm on February 25, 2020:
member
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Conflicts
No conflicts as of last run.
in
test/functional/test_framework/test_framework.py:93
in
79410242b5outdated
89@@ -90,6 +90,9 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
9091 This class also contains various public and private helper methods."""
9293+ chain: str
fanquake
commented at 2:34 am on February 26, 2020:
We can’t use these annotations while we are still supporting Python 3.5.
kiminuo
commented at 6:20 am on February 26, 2020:
Yes, that’s unfortunately true. However, Python 3.5 EOL is 09/2020. Is that a sufficient reason to bump Python version, or what is the policy for upgrade here?
fanquake
commented at 2:41 am on February 26, 2020:
member
Concept ACK - assuming the types are actually checked/enforced in some way, documentation is written for contributors etc. As Marco mentioned, shotgunning type annotations all over the code base without anything actually using them is likely to just lead them them being out of date and/or confusing.
I did look at doing this for our test framework at one point, but think I came to the conclusion it wasn’t quite worth it while we are still supporting 3.5+, given you are basically limited to function param and return type annotations. However maybe we can require 3.6+ at some point in the near future?
0 $ mypy test/functional/feature_cltv.py
1test/functional/test_framework/script.py:19: error: Need type annotation for 'OPCODE_NAMES' (hint: "OPCODE_NAMES: Dict[<type>, <type>] = ...")
2test/functional/test_framework/script.py:25: error: Need type annotation for '_opcode_instances' (hint: "_opcode_instances: List[<type>] = ...")
3test/functional/test_framework/test_framework.py:599: error: No library stub file for module 'zmq'
4test/functional/test_framework/test_framework.py:599: note: (Stub files are from https://github.com/python/typeshed)
5test/functional/feature_cltv.py:41: error: "int" has no attribute "vin"
6test/functional/feature_cltv.py:42: error: "int" has no attribute "nLockTime"
7Found 5 errors in 3 files (checked 1 source file)
kiminuo
commented at 7:56 pm on March 1, 2020:
contributor
@elichai I’m slightly more in favor of not using that ignore flag because:
https://github.com/python/typeshed seems very active so somebody may actually add typings for zmq to typeshed which would be ideal I guess.
It does not seem that number of # type: ignores needed for module imports will be high - correct me if I’m wrong.
EDIT: I have changed my mind and I have added --ignore-missing-imports mypy flag to make the commit easier to review and less obtrusive for developers at the beginning.
DrahtBot added the label
Needs rebase
on Mar 11, 2020
kiminuo force-pushed
on Mar 12, 2020
DrahtBot removed the label
Needs rebase
on Mar 12, 2020
DrahtBot added the label
Needs rebase
on Mar 17, 2020
kiminuo force-pushed
on Mar 17, 2020
kiminuo force-pushed
on May 4, 2020
kiminuo force-pushed
on May 4, 2020
DrahtBot removed the label
Needs rebase
on May 4, 2020
kiminuo force-pushed
on May 29, 2020
troygiorshev
commented at 1:10 pm on June 1, 2020:
contributor
Concept ACK. The combination of Type Hinting and mypy is the single best improvement to ever occur to the python ecosystem IMO. However we’ll need to go about this gradually. This may be a useful guide: https://mypy.readthedocs.io/en/stable/existing_code.html
in
test/functional/data/invalid_txs.py:24
in
b466ecb697outdated
Can this be set to a specific version or is this assumed to be a stable program? We had trillion of issues with flake8 and codespell being updated from down under, so I’d like to avoid any such mishaps in the future.
Looks like there is a new release every month or so: https://github.com/python/mypy/releases (it should be stable enough though as they seem to fix bugs and improve it, they don’t redesign it every month)
So it makes sense to set: travis_retry pip3 install mypy==0.770
This PR adds initial support for type hints checking in python scripts.
Support for type hints was introduced in Python 3.5. Type hints make it easier to read and review code in my opinion. Also an IDE may discover a potential bug sooner. Yet, as PEP 484 says: "It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention."
Mypy is used in lint-python.sh to do the type checking. The package is standard so there is little chance that it will be abandoned. Mypy checks that type hints in source code are correct when they are not, it fails with an error.
Useful resources:
* https://docs.python.org/3.5/library/typing.html
* https://www.python.org/dev/peps/pep-0484/
bd7e530f01
kiminuo force-pushed
on Jun 2, 2020
MarcoFalke
commented at 10:27 am on June 2, 2020:
member
ACKbd7e530f010d43816bb05d6f1590d1cd36cdaa2c fine with me
fanquake approved
fanquake
commented at 9:28 am on June 3, 2020:
member
ACKbd7e530f010d43816bb05d6f1590d1cd36cdaa2c - the type checking is not the most robust (there are things it fails to detect), but I think this is worth adopting (in a limited capacity while we maintain 3.5 compat).
Sanity checked that the linter complains when you change things:
0test/functional/test_framework/script.py:251: error: Dict entry 0 has incompatible type "CScriptOp": "str"; expected "CScriptOp": "int"1test/functional/test_framework/script.py:252: error: Dict entry 1 has incompatible type "CScriptOp": "str"; expected "CScriptOp": "int"2test/functional/test_framework/script.py:253: error: Dict entry 2 has incompatible type "CScriptOp": "str"; expected "CScriptOp": "int"
0diff --git a/test/functional/data/invalid_txs.py b/test/functional/data/invalid_txs.py
1index 6e72db1d9..14dffcee4 100644
2--- a/test/functional/data/invalid_txs.py
3+++ b/test/functional/data/invalid_txs.py
4@@ -57,7 +57,7 @@ class BadTxTemplate:
5 __metaclass__ = abc.ABCMeta
67 # The expected error code given by bitcoind upon submission of the tx.
8- reject_reason = "" # type: Optional[str]
9+ reject_reason = "" # type: Optional[bool]
0test/functional/data/invalid_txs.py:60: error: Incompatible types in assignment (expression has type "str", variable has type "Optional[bool]")1test/functional/data/invalid_txs.py:84: error: Incompatible types in assignment (expression has type "str", base class "BadTxTemplate" defined the type as "Optional[bool]")
fanquake merged this
on Jun 3, 2020
fanquake closed this
on Jun 3, 2020
sidhujag referenced this in commit
08b5728354
on Jun 3, 2020
hebasto
commented at 5:23 am on June 5, 2020:
member
I have linter warnings on master (4ede05d421e7799e631bb83ed16799e8bae79eeb):
0$ python3 --version
1Python 3.6.9
2$ ./test/lint/lint-python.sh
3test/functional/data/invalid_txs.py:24:1: F401 'typing.Optional' imported but unused
4test/functional/test_framework/script.py:12:1: F401 'typing.List' imported but unused
5test/functional/test_framework/script.py:12:1: F401 'typing.Dict' imported but unused
6Success: no issues found in 168 source files
UPDATE: False positive warnings due to the outdated local flake8 version:
0$ pip3 show flake8 | grep Version
1Version: 3.5.0
fanquake referenced this in commit
17cfa52d38
on Jun 6, 2020
This is a metadata mirror of the GitHub repository
bitcoin/bitcoin.
This site is not affiliated with GitHub.
Content is generated from a GitHub metadata backup.
generated: 2024-12-18 18:12 UTC
This site is hosted by @0xB10C More mirrored repositories can be found on mirror.b10c.me