Skip to content

Commit e56c32b

Browse files
committed
Check static typing
1 parent e32484c commit e56c32b

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

Diff for: .github/workflows/python-package.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ jobs:
2222
- name: Install dependencies
2323
run: |
2424
python -m pip install --upgrade pip
25-
python -m pip install black
25+
python -m pip install black mypy
2626
- name: Check code formatting
2727
run: |
2828
black --check .
29+
- name: Check static typing
30+
run: |
31+
mypy --strict diffplus/
2932
- name: Run unit tests
3033
run: |
3134
python -m unittest tests/test.py

Diff for: diffplus/incremental_diff.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
from .indented_config import IndentedConfig
1+
from .indented_config import IndentedConfig, IndentedConfigDict
22
from re import sub, M
3+
from typing import Dict
4+
5+
# Recursive type alias
6+
IncrementalDiffDict = Dict[str, "IncrementalDiffDict"]
37

48

59
class IncrementalDiff:
@@ -18,8 +22,12 @@ class IncrementalDiff:
1822
# Coloring is useful to better visualize the changes in the diff.
1923
#
2024
def __init__(
21-
self, a: IndentedConfig, b: IndentedConfig, merge=False, colored=False
22-
):
25+
self,
26+
a: IndentedConfig,
27+
b: IndentedConfig,
28+
merge: bool = False,
29+
colored: bool = False,
30+
) -> None:
2331
self.a = a
2432
self.b = b
2533
self.merge = merge
@@ -28,13 +36,13 @@ def __init__(
2836
#
2937
# Returns the incremental diff as a dict.
3038
#
31-
def to_dict(self):
39+
def to_dict(self) -> IncrementalDiffDict:
3240
return IncrementalDiff._to_dict(self.a.to_dict(), self.b.to_dict(), self.merge)
3341

3442
#
3543
# Returns the incremental diff as an str.
3644
#
37-
def __str__(self):
45+
def __str__(self) -> str:
3846
str_ = IncrementalDiff._to_str(self.to_dict(), indent_char=self.a.indent_char)
3947
if self.colored:
4048
str_ = sub(
@@ -47,8 +55,10 @@ def __str__(self):
4755
# New items from A are prepended by "+" to tell them from existing ones from B.
4856
#
4957
@staticmethod
50-
def _to_dict(a, b, merge=False):
51-
new_items = {}
58+
def _to_dict(
59+
a: IndentedConfigDict, b: IndentedConfigDict, merge: bool = False
60+
) -> IncrementalDiffDict:
61+
new_items: IncrementalDiffDict = {}
5262

5363
for item in a.keys():
5464
if item not in b.keys():
@@ -71,7 +81,12 @@ def _to_dict(a, b, merge=False):
7181
# It is pretty as it indents lines and adds a "+" in front of new ones.
7282
#
7383
@staticmethod
74-
def _to_str(incrdiff, indent_char=" ", indent_level=0, is_new=False):
84+
def _to_str(
85+
incrdiff: IncrementalDiffDict,
86+
indent_char: str = " ",
87+
indent_level: int = 0,
88+
is_new: bool = False,
89+
) -> str:
7590
str_ = ""
7691

7792
for item in incrdiff:

Diff for: diffplus/indented_config.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from typing import Dict, List
2+
3+
# Recursive type alias
4+
IndentedConfigDict = Dict[str, "IndentedConfigDict"]
5+
6+
17
class IndentedConfig:
28
#
39
# Initializes a given config:
@@ -6,11 +12,17 @@ class IndentedConfig:
612
#
713
# The config may be sanitized at init via the "sanitize" parameter.
814
#
9-
def __init__(self, config: str, indent_char=" ", comment_char="#", sanitize=False):
15+
def __init__(
16+
self,
17+
config: str,
18+
indent_char: str = " ",
19+
comment_char: str = "#",
20+
sanitize: bool = False,
21+
) -> None:
1022
if not len(indent_char) == 1:
11-
raise ValueError(f'"indent_char" must be a char, not an str')
23+
raise ValueError(f'"indent_char" must be a char')
1224
if not len(comment_char) == 1:
13-
raise ValueError(f'"comment_char" must be a char, not an str')
25+
raise ValueError(f'"comment_char" must be a char')
1426
self.config = config
1527
self.indent_char = indent_char
1628
self.comment_char = comment_char
@@ -24,8 +36,8 @@ def __init__(self, config: str, indent_char=" ", comment_char="#", sanitize=Fals
2436
#
2537
# A KeyError may be raised if the config is not correctly indented.
2638
#
27-
def to_dict(self):
28-
tree = {}
39+
def to_dict(self) -> IndentedConfigDict:
40+
tree: IndentedConfigDict = {}
2941
last_parent = {0: tree} # last parents encountered by indent level
3042

3143
for line in self.config.splitlines():
@@ -42,8 +54,8 @@ def to_dict(self):
4254
# - ignore blank lines and comments
4355
# - fix lines not correctly indented
4456
#
45-
def sanitize(self):
46-
sanitized_indented_config = []
57+
def sanitize(self) -> None:
58+
sanitized_indented_config: List[str] = []
4759
max_indent_level = 0
4860

4961
for line in self.config.splitlines():
@@ -73,5 +85,5 @@ def sanitize(self):
7385
#
7486
# Returns the indented config as an str.
7587
#
76-
def __str__(self):
88+
def __str__(self) -> str:
7789
return self.config

0 commit comments

Comments
 (0)