Skip to content

Commit b374640

Browse files
authored
Add type hints (#349)
1 parent c6b3ebd commit b374640

File tree

8 files changed

+32
-6
lines changed

8 files changed

+32
-6
lines changed

.pre-commit-config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ repos:
4242
hooks:
4343
- id: check-manifest
4444
args: [--no-build-isolation]
45+
- repo: https://github.com/pre-commit/mirrors-mypy
46+
rev: v0.910
47+
hooks:
48+
- id: mypy

HISTORY.rst

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
History
33
=======
44

5+
* Add type hints.
6+
57
3.5.0 (2021-05-10)
68
------------------
79

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ prune requirements
55
include HISTORY.rst
66
include LICENSE
77
include README.rst
8+
include src/flake8_comprehensions/py.typed
89
exclude .editorconfig
910
exclude .pre-commit-config.yaml
1011
include pyproject.toml

pyproject.toml

+15
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,18 @@ target-version = ['py36']
77

88
[tool.isort]
99
profile = "black"
10+
11+
[tool.mypy]
12+
check_untyped_defs = true
13+
disallow_any_generics = true
14+
disallow_incomplete_defs = true
15+
disallow_untyped_defs = true
16+
mypy_path = "src/"
17+
no_implicit_optional = true
18+
show_error_codes = true
19+
warn_unreachable = true
20+
warn_unused_ignores = true
21+
22+
[[tool.mypy.overrides]]
23+
module = "tests.*"
24+
allow_untyped_defs = true

setup.cfg

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ license_file = LICENSE
3030
[options]
3131
package_dir=
3232
=src
33-
py_modules = flake8_comprehensions
33+
packages = find:
3434
include_package_data = True
3535
install_requires =
3636
flake8>=3.0,!=3.2.0,<4
3737
importlib-metadata ; python_version < "3.8"
3838
python_requires = >=3.6
3939
zip_safe = False
4040

41+
[options.packages.find]
42+
where = src
43+
4144
[options.entry_points]
4245
flake8.extension =
4346
C4 = flake8_comprehensions:ComprehensionChecker

src/flake8_comprehensions.py renamed to src/flake8_comprehensions/__init__.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ast
22
import sys
3+
from typing import Any, Generator, Optional, Tuple, Type
34

45
if sys.version_info >= (3, 8):
56
from importlib.metadata import version
@@ -17,7 +18,7 @@ class ComprehensionChecker:
1718

1819
__slots__ = ("tree",)
1920

20-
def __init__(self, tree, *args, **kwargs):
21+
def __init__(self, tree: ast.Module) -> None:
2122
self.tree = tree
2223

2324
messages = {
@@ -40,7 +41,7 @@ def __init__(self, tree, *args, **kwargs):
4041
"C416": "C416 Unnecessary {type} comprehension - rewrite using {type}().",
4142
}
4243

43-
def run(self):
44+
def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]:
4445
for node in ast.walk(self.tree):
4546
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
4647
num_positional_args = len(node.args)
@@ -164,7 +165,7 @@ def run(self):
164165
):
165166
remediation = ""
166167
if node.func.id == "reversed":
167-
reverse_flag_value = False
168+
reverse_flag_value: Optional[bool] = False
168169
for keyword in node.args[0].keywords:
169170
if keyword.arg != "reverse":
170171
continue
@@ -260,11 +261,11 @@ def run(self):
260261
)
261262

262263

263-
def has_star_args(call_node):
264+
def has_star_args(call_node: ast.Call) -> bool:
264265
return any(isinstance(a, ast.Starred) for a in call_node.args)
265266

266267

267-
def has_double_star_args(call_node):
268+
def has_double_star_args(call_node: ast.Call) -> bool:
268269
return any(k.arg is None for k in call_node.keywords)
269270

270271

src/flake8_comprehensions/py.typed

Whitespace-only changes.

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)