Skip to content

Commit de4257a

Browse files
committed
check type annotations
1 parent 9ff032b commit de4257a

File tree

8 files changed

+41
-22
lines changed

8 files changed

+41
-22
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ matrix:
4242

4343
- python: "3.7"
4444
env: ENV=flake8
45+
46+
- python: "3.7"
47+
env: ENV=typing

dephell_setuptools/_base.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# built-in
22
from pathlib import Path
3-
from typing import Union
3+
from typing import Any, Dict, Union
44

55
# app
6+
from ._cached_property import cached_property
67
from ._constants import FIELDS
78

89

@@ -21,7 +22,7 @@ def _normalize_path(path: Union[str, Path], default_name: str) -> Path:
2122
return path
2223

2324
@staticmethod
24-
def _clean(data: dict):
25+
def _clean(data: Dict[str, Any]) -> Dict[str, Any]:
2526
result = dict()
2627
for k, v in data.items():
2728
if k not in FIELDS:
@@ -37,3 +38,7 @@ def _clean(data: dict):
3738
result['keywords'] = sum((kw.split() for kw in result['keywords']), [])
3839

3940
return result
41+
42+
@cached_property
43+
def content(self) -> Dict[str, Any]:
44+
raise NotImplementedError

dephell_setuptools/_cfg.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
from configparser import ConfigParser
33
from copy import deepcopy
44
from pathlib import Path
5-
from typing import Dict, List, Optional, Union
5+
from typing import Any, Dict, Union
66

77
# external
88
from setuptools.config import ConfigMetadataHandler, ConfigOptionsHandler
99

1010
# app
1111
from ._base import BaseReader
12+
from ._cached_property import cached_property
1213
from ._constants import FIELDS
1314

1415

1516
class CfgReader(BaseReader):
1617
def __init__(self, path: Union[str, Path]):
1718
self.path = self._normalize_path(path, default_name='setup.cfg')
1819

19-
@property
20-
def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
20+
@cached_property
21+
def content(self) -> Dict[str, Any]:
2122
path = self.path
2223
if path.name == 'setup.py':
2324
path = path.parent / 'setup.cfg'
@@ -27,7 +28,7 @@ def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
2728
parser = ConfigParser()
2829
parser.read(str(path))
2930

30-
options = deepcopy(parser._sections)
31+
options = deepcopy(parser._sections) # type: ignore
3132
for section, content in options.items():
3233
for k, v in content.items():
3334
options[section][k] = ('', v)

dephell_setuptools/_cmd.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
from distutils.core import Command
88
from pathlib import Path
99
from tempfile import NamedTemporaryFile
10+
from typing import Any, Dict
1011

1112
# app
1213
from ._base import BaseReader
14+
from ._cached_property import cached_property
1315
from ._constants import FIELDS
1416

1517

@@ -24,8 +26,8 @@ def cd(path: Path):
2426

2527

2628
class CommandReader(BaseReader):
27-
@property
28-
def content(self):
29+
@cached_property
30+
def content(self) -> Dict[str, Any]:
2931
# generate a temporary json file which contains the metadata
3032
output_json = NamedTemporaryFile()
3133
cmd = [
@@ -44,11 +46,11 @@ def content(self):
4446
env={'PYTHONPATH': str(Path(__file__).parent.parent)},
4547
)
4648
if result.returncode != 0:
47-
return None
49+
raise RuntimeError(result.stderr.decode().split('\n')[-1])
4850

4951
with open(output_json.name) as stream:
50-
result = json.load(stream)
51-
return self._clean(result)
52+
content = json.load(stream)
53+
return self._clean(content)
5254

5355

5456
class JSONCommand(Command):

dephell_setuptools/_manager.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# built-in
22
from logging import getLogger
33
from pathlib import Path
4-
from typing import Any, Callable, Iterable, Union
4+
from typing import Any, Callable, Dict, Iterable, Union, Type
55

66
# app
7+
from ._base import BaseReader
78
from ._cfg import CfgReader
89
from ._cmd import CommandReader
910
from ._pkginfo import PkgInfoReader
@@ -22,8 +23,8 @@
2223
def read_setup(*,
2324
path: Union[str, Path],
2425
error_handler: Callable[[Exception], Any] = logger.exception,
25-
readers: Iterable = ALL_READERS):
26-
result = dict()
26+
readers: Iterable[Type[BaseReader]] = ALL_READERS) -> Dict[str, Any]:
27+
result = dict() # type: Dict[str, Any]
2728
for reader in readers:
2829
try:
2930
content = reader(path=path).content

dephell_setuptools/_pkginfo.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# built-in
22
import json
33
import subprocess
4+
from typing import Any, Dict
45

56
# app
67
from ._base import BaseReader
8+
from ._cached_property import cached_property
79

810

911
class PkgInfoReader(BaseReader):
10-
@property
11-
def content(self):
12+
@cached_property
13+
def content(self) -> Dict[str, Any]:
1214
if self.path.is_file() and self.path.name != 'setup.py':
13-
return None
15+
raise NameError('cannot parse non setup.py named files')
1416

1517
cmd = ['pkginfo', '--json', str(self.path)]
1618
result = subprocess.run(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
1719
if result.returncode != 0:
18-
return None
20+
raise RuntimeError(result.stderr.decode().split('\n')[-1])
1921
content = json.loads(result.stdout.decode())
2022
return self._clean(content)

dephell_setuptools/_static.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# built-in
22
import ast
3-
from typing import Any, Dict, List, Optional, Union
3+
from typing import Any, Dict, Optional
44

55
# app
66
from ._base import BaseReader
@@ -9,9 +9,9 @@
99

1010
class StaticReader(BaseReader):
1111
@cached_property
12-
def content(self) -> Optional[Dict[str, Union[List, Dict]]]:
12+
def content(self) -> Dict[str, Any]:
1313
if not self.call:
14-
return None
14+
raise LookupError('cannot find setup()')
1515
result = self._get_call_kwargs(self.call)
1616
return self._clean(result)
1717

pyproject.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ from = {format = "pip", path = "requirements-flake.txt"}
1313
python = ">=3.6"
1414
command = "flake8"
1515

16-
# -- FLIT -- #
16+
[tool.dephell.typing]
17+
from = {format = "flit", path = "pyproject.toml"}
18+
envs = ["main", "dev"]
19+
command = "mypy --ignore-missing-imports --allow-redefinition dephell_setuptools"
20+
1721

1822
[tool.flit.metadata]
1923
module="dephell_setuptools"
@@ -40,6 +44,7 @@ classifiers=[
4044

4145
[tool.flit.metadata.requires-extra]
4246
dev = [
47+
"mypy",
4348
"pkginfo",
4449
"pytest",
4550
]

0 commit comments

Comments
 (0)