Skip to content

Commit d108e7d

Browse files
authored
Check tests/ with basedpyright in CI in "standard" mode (#50)
And fix reported errors in the process.
1 parent 66643bf commit d108e7d

5 files changed

Lines changed: 28 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,7 @@ jobs:
9090
- name: Check tests/ with mypy
9191
run: |
9292
python -m mypy tests/
93+
94+
- name: Check tests/ with basedpyright
95+
run: |
96+
basedpyright tests/

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "docstub"
77
authors = [
88
{name = "Lars Grüter"},
99
]
10-
description = "Generate Python stub files (PYI) from docstrings"
10+
description = "Generate Python stub files from docstrings"
1111
readme = "README.md"
1212
license.file = "LICENSE"
1313
requires-python = ">=3.12"
@@ -46,6 +46,7 @@ test = [
4646
"pytest >=5.0.0",
4747
"pytest-cov >= 5.0.0",
4848
"mypy",
49+
"basedpyright",
4950
]
5051

5152
[project.urls]
@@ -129,3 +130,8 @@ disable_error_code = ["var-annotated", "union-attr"]
129130
[[tool.mypy.overrides]]
130131
module = ["numpydoc.*"]
131132
ignore_missing_imports = true
133+
134+
135+
[tool.basedpyright]
136+
stubPath = "stubs/"
137+
typeCheckingMode = "standard"

tests/test_analysis.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def test_query_types(self, search_name, expected_name, expected_origin):
148148
assert expected_name is type_name
149149
assert expected_origin is type_origin
150150
else:
151+
assert type_name is not None
152+
assert type_origin is not None
151153
assert str(type_origin) == expected_origin
152154
assert type_name.startswith(type_origin.target)
153155
assert type_name == expected_name
@@ -174,6 +176,8 @@ def test_query_prefix(self, search_name, expected_name, expected_origin):
174176
assert expected_name is type_name
175177
assert expected_origin is type_origin
176178
else:
179+
assert type_name is not None
180+
assert type_origin is not None
177181
assert str(type_origin) == expected_origin
178182
assert type_name.startswith(type_origin.target)
179183
assert type_name == expected_name

tests/test_docstrings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def test_natlang_array_invalid_shape(self, shape):
207207
doctype = f"array of shape {shape}"
208208
transformer = DoctypeTransformer()
209209
with pytest.raises(lark.exceptions.UnexpectedInput):
210-
transformer.doctype_to_annotation(doctype)
210+
_ = transformer.doctype_to_annotation(doctype)
211211

212212
def test_unknown_name(self):
213213
# Simple unknown name is aliased to typing.Any
@@ -302,6 +302,7 @@ def test_returns(self, doctypes, expected):
302302
).format(*doctypes)
303303
transformer = DoctypeTransformer()
304304
annotations = DocstringAnnotations(docstring, transformer=transformer)
305+
assert annotations.returns is not None
305306
assert annotations.returns.value == expected
306307

307308
def test_yields(self, caplog):
@@ -315,6 +316,7 @@ def test_yields(self, caplog):
315316
)
316317
transformer = DoctypeTransformer()
317318
annotations = DocstringAnnotations(docstring, transformer=transformer)
319+
assert annotations.returns is not None
318320
assert annotations.returns.value == "Generator[tuple[int, str]]"
319321
assert annotations.returns.imports == {
320322
KnownImport(import_path="collections.abc", import_name="Generator")
@@ -336,6 +338,7 @@ def test_receives(self, caplog):
336338
)
337339
transformer = DoctypeTransformer()
338340
annotations = DocstringAnnotations(docstring, transformer=transformer)
341+
assert annotations.returns is not None
339342
assert (
340343
annotations.returns.value
341344
== "Generator[tuple[int, str], tuple[float, bytes]]"
@@ -364,6 +367,7 @@ def test_full_generator(self, caplog):
364367
)
365368
transformer = DoctypeTransformer()
366369
annotations = DocstringAnnotations(docstring, transformer=transformer)
370+
assert annotations.returns is not None
367371
assert annotations.returns.value == (
368372
"Generator[tuple[int, str], tuple[float, bytes], bool]"
369373
)
@@ -386,6 +390,7 @@ def test_yields_and_returns(self, caplog):
386390
)
387391
transformer = DoctypeTransformer()
388392
annotations = DocstringAnnotations(docstring, transformer=transformer)
393+
assert annotations.returns is not None
389394
assert annotations.returns.value == ("Generator[tuple[int, str], None, bool]")
390395
assert annotations.returns.imports == {
391396
KnownImport(import_path="collections.abc", import_name="Generator")
@@ -416,6 +421,8 @@ def test_duplicate_returns(self, caplog):
416421
)
417422
transformer = DoctypeTransformer()
418423
annotations = DocstringAnnotations(docstring, transformer=transformer)
424+
assert annotations.returns is not None
425+
assert annotations.returns is not None
419426
assert annotations.returns.value == "int"
420427

421428
def test_args_kwargs(self):

tests/test_stubs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def foo(a, b=None):
5252
assert isinstance(func_def, cst.FunctionDef)
5353
docstring_node = _get_docstring_node(func_def)
5454

55+
assert isinstance(docstring_node, cst.SimpleString)
5556
assert docstring_node.value == docstring
5657

5758
def test_func_without_docstring(self):
@@ -196,7 +197,8 @@ def test_attributes_no_doctype(self, assign, expected, scope):
196197
src = MODULE_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype="")
197198
elif scope == "class":
198199
src = CLASS_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype="")
199-
elif scope == "nested class":
200+
else:
201+
assert scope == "nested class"
200202
src = NESTED_CLASS_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype="")
201203

202204
transformer = Py2StubTransformer()
@@ -234,7 +236,8 @@ def test_attributes_with_doctype(self, assign, doctype, expected, scope):
234236
src = MODULE_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype=doctype)
235237
elif scope == "class":
236238
src = CLASS_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype=doctype)
237-
elif scope == "nested class":
239+
else:
240+
assert scope == "nested class"
238241
src = NESTED_CLASS_ATTRIBUTE_TEMPLATE.format(assign=assign, doctype=doctype)
239242

240243
transformer = Py2StubTransformer()

0 commit comments

Comments
 (0)