summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2022-12-27 09:56:02 -0500
committerNed Batchelder <ned@nedbatchelder.com>2022-12-27 10:00:19 -0500
commit00f681d6a63c66740a2a93ee138f351b531430b0 (patch)
treebddae0eb97ddb6679b212c85c7d592013bb648c9
parentcf3fe635dd63217c0999c372aadae5a14741b1c5 (diff)
downloadpython-coveragepy-git-00f681d6a63c66740a2a93ee138f351b531430b0.tar.gz
test: add mypy just for one file at first
-rw-r--r--coverage/numbits.py23
-rw-r--r--pyproject.toml28
-rw-r--r--tox.ini2
3 files changed, 39 insertions, 14 deletions
diff --git a/coverage/numbits.py b/coverage/numbits.py
index da8e724b..99d53878 100644
--- a/coverage/numbits.py
+++ b/coverage/numbits.py
@@ -16,12 +16,12 @@ the future. Use these functions to work with those binary blobs of data.
import json
from itertools import zip_longest
+from typing import Iterable, List
-from coverage.misc import contract
+import sqlite3
-@contract(nums='Iterable', returns='blob')
-def nums_to_numbits(nums):
+def nums_to_numbits(nums: Iterable[int]) -> bytes:
"""Convert `nums` into a numbits.
Arguments:
@@ -41,8 +41,7 @@ def nums_to_numbits(nums):
return bytes(b)
-@contract(numbits='blob', returns='list[int]')
-def numbits_to_nums(numbits):
+def numbits_to_nums(numbits: bytes) -> List[int]:
"""Convert a numbits into a list of numbers.
Arguments:
@@ -63,8 +62,7 @@ def numbits_to_nums(numbits):
return nums
-@contract(numbits1='blob', numbits2='blob', returns='blob')
-def numbits_union(numbits1, numbits2):
+def numbits_union(numbits1: bytes, numbits2: bytes) -> bytes:
"""Compute the union of two numbits.
Returns:
@@ -74,8 +72,7 @@ def numbits_union(numbits1, numbits2):
return bytes(b1 | b2 for b1, b2 in byte_pairs)
-@contract(numbits1='blob', numbits2='blob', returns='blob')
-def numbits_intersection(numbits1, numbits2):
+def numbits_intersection(numbits1: bytes, numbits2: bytes) -> bytes:
"""Compute the intersection of two numbits.
Returns:
@@ -86,8 +83,7 @@ def numbits_intersection(numbits1, numbits2):
return intersection_bytes.rstrip(b'\0')
-@contract(numbits1='blob', numbits2='blob', returns='bool')
-def numbits_any_intersection(numbits1, numbits2):
+def numbits_any_intersection(numbits1: bytes, numbits2: bytes) -> bool:
"""Is there any number that appears in both numbits?
Determine whether two number sets have a non-empty intersection. This is
@@ -100,8 +96,7 @@ def numbits_any_intersection(numbits1, numbits2):
return any(b1 & b2 for b1, b2 in byte_pairs)
-@contract(num='int', numbits='blob', returns='bool')
-def num_in_numbits(num, numbits):
+def num_in_numbits(num: int, numbits: bytes) -> bool:
"""Does the integer `num` appear in `numbits`?
Returns:
@@ -113,7 +108,7 @@ def num_in_numbits(num, numbits):
return bool(numbits[nbyte] & (1 << nbit))
-def register_sqlite_functions(connection):
+def register_sqlite_functions(connection: sqlite3.Connection) -> None:
"""
Define numbits functions in a SQLite connection.
diff --git a/pyproject.toml b/pyproject.toml
index 4b13c41f..a9c7db12 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,3 +4,31 @@
[build-system]
requires = ['setuptools']
build-backend = 'setuptools.build_meta'
+
+[tool.mypy]
+# mypy settings started from https://quantlane.com/blog/type-checking-large-codebase/
+# Ensure full coverage
+disallow_untyped_calls = true
+disallow_untyped_defs = true
+disallow_incomplete_defs = true
+disallow_untyped_decorators = true
+check_untyped_defs = true
+
+# Restrict dynamic typing
+disallow_any_generics = true
+disallow_subclassing_any = true
+warn_return_any = true
+
+# Know exactly what you're doing
+warn_redundant_casts = true
+warn_unused_ignores = true
+warn_unused_configs = true
+warn_unreachable = true
+show_error_codes = true
+
+# Explicit is better than implicit
+no_implicit_optional = true
+
+# Don't follow imports
+ignore_missing_imports = true
+follow_imports = "silent"
diff --git a/tox.ini b/tox.ini
index 4a410aa7..481c04b0 100644
--- a/tox.ini
+++ b/tox.ini
@@ -76,6 +76,7 @@ deps =
setenv =
{[testenv]setenv}
LINTABLE=coverage tests doc ci igor.py setup.py __main__.py
+ TYPEABLE=coverage/numbits.py
commands =
python -m tabnanny {env:LINTABLE}
@@ -88,6 +89,7 @@ commands =
# this can be simplifed:
python igor.py quietly "python -m build"
twine check dist/*
+ mypy {env:TYPEABLE}
python -m pylint --notes= {env:LINTABLE}
check-manifest --ignore 'doc/sample_html/*,.treerc'