Skip to content

Commit 7f6f2ac

Browse files
committed
Adding docstrings and tidying up
1 parent 9d76407 commit 7f6f2ac

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

coverage_pyver_pragma/__init__.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@
2727
# stdlib
2828
import re
2929
import sys
30-
from typing import List, NamedTuple, Pattern
30+
from typing import TYPE_CHECKING, Any, List, NamedTuple, Pattern, Union
3131

3232
# 3rd party
3333
import coverage # type: ignore
3434

35+
if TYPE_CHECKING:
36+
37+
# stdlib
38+
from sys import _version_info as VersionInfo
39+
3540
__author__: str = "Dominic Davis-Foster"
3641
__copyright__: str = "2020 Dominic Davis-Foster"
3742

@@ -44,12 +49,31 @@
4449

4550

4651
class Version(NamedTuple):
52+
"""
53+
:class:`~typing.NamedTuple` with the same elements as :func:`sys.version_info`.
54+
55+
:type major: int
56+
:type minor: int
57+
:type micro: int
58+
59+
"""
60+
4761
major: int
4862
minor: int
4963
micro: int
64+
releaselevel: str
65+
serial: str
66+
5067

68+
def make_regexes(version_tuple: Union["Version", "VersionInfo"]) -> List[Pattern]:
69+
"""
70+
Generate a list of regular expressions to match all valid ignores for the given Python version.
5171
52-
def make_regexes(version_tuple: Version) -> List[Pattern]:
72+
:param version_tuple: The Python version.
73+
:type version_tuple: :class:`~typing.NamedTuple` with the attributes ``major`` and ``minor``.
74+
75+
:return: List of regular expressions.
76+
"""
5377
if version_tuple.major == 3:
5478
# Python 3.X
5579

@@ -66,17 +90,28 @@ def make_regexes(version_tuple: Version) -> List[Pattern]:
6690
re.compile(fr"{regex_main}\s*\(<(py|PY|Py)3({'|'.join(less_than_versions)})\)"),
6791
re.compile(fr"{regex_main}\s*\(<=(py|PY|Py)3({'|'.join(less_equal_versions)})\)"),
6892
re.compile(fr"{regex_main}\s*\(>(py|PY|Py)3({'|'.join(greater_than_versions)})\)"),
69-
re.compile(fr"{regex_main}\s*\((py|PY|Py)3({'|'.join(greater_than_versions)})+\)"),
93+
re.compile(fr"{regex_main}\s*\((py|PY|Py)3({'|'.join(greater_than_versions)})\+\)"),
7094
re.compile(fr"{regex_main}\s*\(>=(py|PY|Py)3({'|'.join(greater_equal_versions)})\)"),
7195
re.compile(fr"{regex_main}\s*\((py|PY|Py)3({'|'.join(exact_versions)})\)"),
7296
]
7397

7498
return excludes
7599

100+
else:
101+
raise ValueError("Unknown Python version.")
102+
76103

77104
class PyVerPragmaPlugin(coverage.CoveragePlugin):
105+
"""
106+
Plugin for Coverage.py to selectively ignore branches depending on the Python version.
107+
"""
108+
109+
def configure(self, config: Any) -> None:
110+
"""
111+
Configure the plugin.
78112
79-
def configure(self, config):
113+
:param config:
114+
"""
80115

81116
# Coverage.py gives either a Coverage() object, or a CoverageConfig() object.
82117
if isinstance(config, coverage.Coverage):
@@ -98,4 +133,16 @@ def configure(self, config):
98133

99134

100135
def coverage_init(reg, options):
136+
"""
137+
Initialise the plugin.
138+
139+
:param reg:
140+
:type reg:
141+
:param options:
142+
:type options:
143+
144+
:return:
145+
:rtype:
146+
"""
147+
101148
reg.add_configurer(PyVerPragmaPlugin()) # pragma: no cover

tests/test_regex.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616
def test_not_version_regex():
1717
counter = 1
1818

19-
for comment_string in ["#", "# ", "# ", "#\t", "# \t", "# \t ", "#\t "]:
19+
for comment_string in ['#', "# ", "# ", "#\t", "# \t", "# \t ", "#\t "]:
2020
for pragma_string in ["pragma", "PRAGMA"]:
2121
for post_pragma_space in ['', ':', ": ", ":\t", " "]:
2222
for no_string in ["no", "NO"]:
23-
for post_no_space in ['', " ", "\t", " "]:
23+
for post_no_space in ['', ' ', "\t", " "]:
2424
for cover_string in ["cover", "COVER"]:
25-
for post_cover_space in ['', " ", "\t", " "]:
26-
for pre_version_sign in [">", "<", ">=", "<=", ""]:
25+
for post_cover_space in ['', ' ', "\t", " "]:
26+
for pre_version_sign in ['>', '<', ">=", "<=", '']:
2727
for py_string in ["Py", "PY", "py"]:
2828
for version in [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]:
29-
for post_version_sign in ["+", ""]:
29+
for post_version_sign in ['+', '']:
3030
test_string = f"{comment_string}{pragma_string}{post_pragma_space}{no_string}{post_no_space}{cover_string}{post_cover_space}({pre_version_sign}{py_string}{version}{post_version_sign})"
3131
# print(f"[{counter} TESTING: {test_string}]")
3232

3333
if re.match(not_version_regex, test_string):
3434
raise AssertionError(f"[{counter} FAIL: {test_string}]")
3535
counter += 1
3636

37-
for comment_string in ["#", "# ", "# ", "#\t", "# \t", "# \t ", "#\t "]:
37+
for comment_string in ['#', "# ", "# ", "#\t", "# \t", "# \t ", "#\t "]:
3838
for pragma_string in ["pragma", "PRAGMA"]:
3939
for post_pragma_space in ['', ':', ": ", ":\t", " "]:
4040
for no_string in ["no", "NO"]:
41-
for post_no_space in ['', " ", "\t", " "]:
41+
for post_no_space in ['', ' ', "\t", " "]:
4242
for cover_string in ["cover", "COVER"]:
43-
for post_cover_space in ['', " ", "\t", " "]:
44-
for post_cover_text in ["", "abcdefg", "hello world"]:
43+
for post_cover_space in ['', ' ', "\t", " "]:
44+
for post_cover_text in ['', "abcdefg", "hello world"]:
4545
test_string = f"{comment_string}{pragma_string}{post_pragma_space}{no_string}{post_no_space}{cover_string}{post_cover_space}{post_cover_text}"
4646
# print(f"[{counter} TESTING: {test_string}]")
4747

@@ -83,21 +83,21 @@ def test_correct_version_regex():
8383

8484
# TODO: Plus
8585
for pre_version_sign, minor_versions in zip(
86-
[">", "<", ">=", "<=", ""],
86+
['>', '<', ">=", "<=", ''],
8787
[range(0, python_version.minor), range(python_version.minor + 1, 10), range(0, python_version.minor + 1), range(python_version.minor, 10), [python_version.minor], [python_version.minor]]
8888
):
8989
for minor_version in minor_versions:
9090
version = 30 + minor_version
9191

92-
for comment_string in ["#", "# ", "# ", "#\t", "# \t", "# \t ", "#\t "]:
92+
for comment_string in ['#', "# ", "# ", "#\t", "# \t", "# \t ", "#\t "]:
9393
for pragma_string in ["pragma", "PRAGMA"]:
9494
for post_pragma_space in ['', ':', ": ", ":\t", " "]:
9595
for no_string in ["no", "NO"]:
96-
for post_no_space in ['', " ", "\t", " "]:
96+
for post_no_space in ['', ' ', '\t', " "]:
9797
for cover_string in ["cover", "COVER"]:
98-
for post_cover_space in ['', " ", "\t", " "]:
98+
for post_cover_space in ['', ' ', '\t', " "]:
9999
for py_string in ["Py", "PY", "py"]:
100-
# for post_version_sign in ["+", ""]:
100+
# for post_version_sign in ['+', '']:
101101

102102
test_string = f"{comment_string}{pragma_string}{post_pragma_space}{no_string}{post_no_space}{cover_string}{post_cover_space}({pre_version_sign}{py_string}{version}{post_version_sign})" # print(f"[{counter} TESTING: {test_string}]")
103103

0 commit comments

Comments
 (0)