Skip to content

Commit 85e6ca8

Browse files
Copiloteleanorjboyd
andcommitted
Fix Python type checking errors and format code
- Added NotRequired to imports for optional TypedDict fields - Made lineno an optional field in TestNode TypedDict - Formatted Python files with ruff Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
1 parent 72fd373 commit 85e6ca8

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

python_files/tests/pytestadapter/expected_discovery_test_output.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@
100100
"unittest_pytest_same_file.py::TestExample",
101101
unit_pytest_same_file_path,
102102
),
103-
"lineno": find_class_line_number(
104-
"TestExample", unit_pytest_same_file_path
105-
),
103+
"lineno": find_class_line_number("TestExample", unit_pytest_same_file_path),
106104
},
107105
{
108106
"name": "test_true_pytest",
@@ -215,9 +213,7 @@
215213
"unittest_folder/test_add.py::TestAddFunction",
216214
test_add_path,
217215
),
218-
"lineno": find_class_line_number(
219-
"TestAddFunction", test_add_path
220-
),
216+
"lineno": find_class_line_number("TestAddFunction", test_add_path),
221217
},
222218
{
223219
"name": "TestDuplicateFunction",
@@ -573,9 +569,7 @@
573569
"parametrize_tests.py::TestClass",
574570
parameterize_tests_path,
575571
),
576-
"lineno": find_class_line_number(
577-
"TestClass", parameterize_tests_path
578-
),
572+
"lineno": find_class_line_number("TestClass", parameterize_tests_path),
579573
"children": [
580574
{
581575
"name": "test_adding",
@@ -952,9 +946,7 @@
952946
"test_multi_class_nest.py::TestFirstClass",
953947
TEST_MULTI_CLASS_NEST_PATH,
954948
),
955-
"lineno": find_class_line_number(
956-
"TestFirstClass", TEST_MULTI_CLASS_NEST_PATH
957-
),
949+
"lineno": find_class_line_number("TestFirstClass", TEST_MULTI_CLASS_NEST_PATH),
958950
"children": [
959951
{
960952
"name": "TestSecondClass",
@@ -1547,9 +1539,7 @@
15471539
"pytest_describe_plugin/describe_only.py::describe_A",
15481540
describe_only_path,
15491541
),
1550-
"lineno": find_class_line_number(
1551-
"describe_A", describe_only_path
1552-
),
1542+
"lineno": find_class_line_number("describe_A", describe_only_path),
15531543
}
15541544
],
15551545
}
@@ -1673,9 +1663,7 @@
16731663
"pytest_describe_plugin/nested_describe.py::describe_list",
16741664
nested_describe_path,
16751665
),
1676-
"lineno": find_class_line_number(
1677-
"describe_list", nested_describe_path
1678-
),
1666+
"lineno": find_class_line_number("describe_list", nested_describe_path),
16791667
}
16801668
],
16811669
}

python_files/tests/pytestadapter/helpers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,11 @@ def find_class_line_number(class_name: str, test_file_path) -> str:
382382
for i, line in enumerate(f):
383383
# Match "class ClassName" or "class ClassName(" or "class ClassName:"
384384
# Also match "def ClassName(" for pytest-describe blocks
385-
if line.strip().startswith(f"class {class_name}") or line.strip().startswith(
386-
f"class {class_name}("
387-
) or line.strip().startswith(f"def {class_name}("):
385+
if (
386+
line.strip().startswith(f"class {class_name}")
387+
or line.strip().startswith(f"class {class_name}(")
388+
or line.strip().startswith(f"def {class_name}(")
389+
):
388390
return str(i + 1)
389391
error_str: str = f"Class {class_name!r} not found on any line in {test_file_path}"
390392
raise ValueError(error_str)

python_files/vscode_pytest/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@
1010
import pathlib
1111
import sys
1212
import traceback
13-
from typing import TYPE_CHECKING, Any, Dict, Generator, Literal, Protocol, TypedDict, cast
13+
from typing import (
14+
TYPE_CHECKING,
15+
Any,
16+
Dict,
17+
Generator,
18+
Literal,
19+
NotRequired,
20+
Protocol,
21+
TypedDict,
22+
cast,
23+
)
1424

1525
import pytest
1626

@@ -52,6 +62,7 @@ class TestNode(TestData):
5262
"""A general class that handles all test data which contains children."""
5363

5464
children: list[TestNode | TestItem | None]
65+
lineno: NotRequired[str] # Optional field for class/function nodes
5566

5667

5768
class VSCodePytestError(Exception):

0 commit comments

Comments
 (0)