Skip to content

Commit fe60a4a

Browse files
authored
Switch to inbuilt types (#10)
* switch to using inbuilt types * add poetry to container * add conditional imports for typing
1 parent 4145c4f commit fe60a4a

File tree

6 files changed

+53
-20
lines changed

6 files changed

+53
-20
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"ghcr.io/devcontainers/features/python:1": {},
66
"ghcr.io/devcontainers-contrib/features/black:1": {},
77
"ghcr.io/devcontainers-contrib/features/tox:1": {},
8-
"ghcr.io/devcontainers-contrib/features/isort:1": {}
8+
"ghcr.io/devcontainers-contrib/features/isort:1": {},
9+
"ghcr.io/devcontainers-contrib/features/poetry:1": {}
910
}
1011
}

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased]
44

5+
## [v1.0.6] - 2022-12-28
6+
7+
### Fixed
8+
9+
- Switch to using inbuilt types rather than from `typing`
10+
511
## [v1.0.5] - 2022-12-23
612

713
### Fixed
@@ -23,3 +29,4 @@
2329
[//]: # "Release links"
2430
[v1.0.0]: https://github.com/jdkandersson/flake8-test-docs/releases/v1.0.0
2531
[v1.0.5]: https://github.com/jdkandersson/flake8-test-docs/releases/v1.0.5
32+
[v1.0.6]: https://github.com/jdkandersson/flake8-test-docs/releases/v1.0.6

flake8_test_docs.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
"""A linter that checks test docstrings for the arrange/act/assert structure."""
22

3+
from __future__ import annotations
4+
35
import argparse
46
import ast
57
import re
8+
import sys
69
from functools import wraps
710
from pathlib import Path
8-
from typing import Callable, Iterable, List, NamedTuple, Optional, Tuple, Type
11+
from typing import Callable, Iterator, NamedTuple
12+
13+
# Can't cover both paths of a conditional import
14+
if sys.version_info < (3, 10):
15+
from typing_extensions import ParamSpec # pragma: nocover
16+
else:
17+
from typing import ParamSpec # pragma: nocover
918

1019
from flake8.options.manager import OptionManager
1120

@@ -71,12 +80,15 @@ class Section(NamedTuple):
7180
index_: int
7281
name: str
7382
description: str
74-
next_section_name: Optional[str]
83+
next_section_name: str | None
84+
85+
86+
AIMPPParamSpec = ParamSpec("AIMPPParamSpec")
7587

7688

7789
def _append_invalid_msg_prefix_postfix(
78-
func: Callable[..., Optional[str]]
79-
) -> Callable[..., Optional[str]]:
90+
func: Callable[AIMPPParamSpec, str | None]
91+
) -> Callable[AIMPPParamSpec, str | None]:
8092
"""Add the code prefix and invalid message postfix to the return value.
8193
8294
Args:
@@ -87,7 +99,7 @@ def _append_invalid_msg_prefix_postfix(
8799
"""
88100

89101
@wraps(func)
90-
def wrapper(*args, **kwargs):
102+
def wrapper(*args: AIMPPParamSpec.args, **kwargs: AIMPPParamSpec.kwargs) -> str | None:
91103
"""Wrap the function."""
92104
if (return_value := func(*args, **kwargs)) is None:
93105
return None
@@ -98,7 +110,7 @@ def wrapper(*args, **kwargs):
98110

99111
def _section_start_problem_message(
100112
line: str, section: Section, col_offset: int, section_prefix: str
101-
) -> Optional[str]:
113+
) -> str | None:
102114
"""Check the first line of a section.
103115
104116
Args:
@@ -136,7 +148,7 @@ def _section_start_problem_message(
136148
return None
137149

138150

139-
def _next_section_start(line: str, next_section_name: Optional[str], section_prefix: str) -> bool:
151+
def _next_section_start(line: str, next_section_name: str | None, section_prefix: str) -> bool:
140152
"""Detect whether the line is the start of the next section.
141153
142154
The next section is defined to be either that the line starts with the next section name after
@@ -165,11 +177,11 @@ def _next_section_start(line: str, next_section_name: Optional[str], section_pre
165177

166178
def _remaining_description_problem_message(
167179
section: Section,
168-
docstring_lines: List[str],
180+
docstring_lines: list[str],
169181
section_prefix: str,
170182
description_prefix: str,
171183
indent_size: int,
172-
) -> Tuple[Optional[str], int]:
184+
) -> tuple[str | None, int]:
173185
"""Check the remaining description of a section after the first line.
174186
175187
Args:
@@ -213,7 +225,7 @@ def _remaining_description_problem_message(
213225
@_append_invalid_msg_prefix_postfix
214226
def _docstring_problem_message(
215227
docstring: str, col_offset: int, docs_pattern: DocsPattern, indent_size: int
216-
) -> Optional[str]:
228+
) -> str | None:
217229
"""Get the problem message for a docstring.
218230
219231
Args:
@@ -296,7 +308,7 @@ class Visitor(ast.NodeVisitor):
296308
problems: All the problems that were encountered.
297309
"""
298310

299-
problems: List[Problem]
311+
problems: list[Problem]
300312
_test_docs_pattern: DocsPattern
301313
_test_function_pattern: str
302314
_indent_size: int
@@ -456,7 +468,7 @@ def parse_options(cls, options: argparse.Namespace) -> None: # pragma: nocover
456468
getattr(options, _cli_arg_name_to_attr(INDENT_SIZE_ARN_NAME), None) or cls._indent_size
457469
)
458470

459-
def run(self) -> Iterable[Tuple[int, int, str, Type["Plugin"]]]:
471+
def run(self) -> Iterator[tuple[int, int, str, type["Plugin"]]]:
460472
"""Lint a file.
461473
462474
Yields:

poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "flake8-test-docs"
3-
version = "1.0.5"
3+
version = "1.0.6"
44
description = "A linter that checks test docstrings for the arrange/act/assert structure"
55
authors = ["David Andersson <[email protected]>"]
66
license = "Apache 2.0"
@@ -22,6 +22,7 @@ classifiers = [
2222
[tool.poetry.dependencies]
2323
python = "^3.8.1"
2424
flake8 = "^6"
25+
typing_extensions = { version = "^4", python = "<3.10" }
2526

2627
[tool.poetry.group.dev.dependencies]
2728
pytest = "^7"
@@ -65,3 +66,14 @@ show_missing = true
6566

6667
[tool.mypy]
6768
ignore_missing_imports = true
69+
check_untyped_defs = true
70+
disallow_untyped_defs = true
71+
72+
[[tool.mypy.overrides]]
73+
module = "tests.*"
74+
disallow_untyped_defs = false
75+
76+
[tool.pylint.messages_control]
77+
disable = [
78+
"wrong-import-position"
79+
]

tests/test_flake8_test_docs_unit.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Unit tests for plugin."""
22

3+
from __future__ import annotations
4+
35
import ast
4-
from typing import Tuple
56

67
import hypothesis
78
import pytest
@@ -18,7 +19,7 @@
1819
)
1920

2021

21-
def _result(code: str, filename: str = "test_.py") -> Tuple[str, ...]:
22+
def _result(code: str, filename: str = "test_.py") -> tuple[str, ...]:
2223
"""Generate linting results.
2324
2425
Args:
@@ -796,7 +797,7 @@ def function_1():
796797
),
797798
],
798799
)
799-
def test_plugin_invalid(code: str, expected_result: Tuple[str, ...]):
800+
def test_plugin_invalid(code: str, expected_result: tuple[str, ...]):
800801
"""
801802
given: code
802803
when: linting is run on the code
@@ -812,7 +813,7 @@ def test_plugin_invalid(code: str, expected_result: Tuple[str, ...]):
812813
pytest.param("file.py", (), id="not test file"),
813814
],
814815
)
815-
def test_plugin_filename(filename: str, expected_result: Tuple[str, ...]):
816+
def test_plugin_filename(filename: str, expected_result: tuple[str, ...]):
816817
"""
817818
given: code and filename
818819
when: linting is run on the code

0 commit comments

Comments
 (0)