1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
4
+ from __future__ import annotations
5
+
4
6
import atexit
5
7
import json
6
8
import os
7
9
import pathlib
8
10
import sys
9
11
import traceback
10
12
from typing import (
13
+ TYPE_CHECKING ,
11
14
Any ,
12
15
Dict ,
13
16
Generator ,
14
- List ,
15
17
Literal ,
16
- Optional ,
17
18
TypedDict ,
18
- Union ,
19
19
)
20
20
21
21
import pytest
22
- from pluggy import Result
23
22
24
23
script_dir = pathlib .Path (__file__ ).parent .parent
25
24
sys .path .append (os .fspath (script_dir ))
26
25
sys .path .append (os .fspath (script_dir / "lib" / "python" ))
27
26
from testing_tools import socket_manager # noqa: E402
28
27
28
+ if TYPE_CHECKING :
29
+ from pluggy import Result
30
+
29
31
30
32
class TestData (TypedDict ):
31
33
"""A general class that all test objects inherit from."""
@@ -46,7 +48,7 @@ class TestItem(TestData):
46
48
class TestNode (TestData ):
47
49
"""A general class that handles all test data which contains children."""
48
50
49
- children : " list[Union[ TestNode, TestItem, None]]"
51
+ children : list [TestNode | TestItem | None ]
50
52
51
53
52
54
class VSCodePytestError (Exception ):
@@ -209,17 +211,17 @@ class TestOutcome(Dict):
209
211
210
212
test : str
211
213
outcome : Literal ["success" , "failure" , "skipped" , "error" ]
212
- message : Union [ str , None ]
213
- traceback : Union [ str , None ]
214
- subtest : Optional [ str ]
214
+ message : str | None
215
+ traceback : str | None
216
+ subtest : str | None
215
217
216
218
217
219
def create_test_outcome (
218
220
testid : str ,
219
221
outcome : str ,
220
- message : Union [ str , None ] ,
221
- traceback : Union [ str , None ] ,
222
- subtype : Optional [ str ] = None , # noqa: ARG001
222
+ message : str | None ,
223
+ traceback : str | None ,
224
+ subtype : str | None = None , # noqa: ARG001
223
225
) -> TestOutcome :
224
226
"""A function that creates a TestOutcome object."""
225
227
return TestOutcome (
@@ -235,7 +237,7 @@ class TestRunResultDict(Dict[str, Dict[str, TestOutcome]]):
235
237
"""A class that stores all test run results."""
236
238
237
239
outcome : str
238
- tests : Dict [str , TestOutcome ]
240
+ tests : dict [str , TestOutcome ]
239
241
240
242
241
243
@pytest .hookimpl (hookwrapper = True , trylast = True )
@@ -384,7 +386,7 @@ def pytest_sessionfinish(session, exitstatus):
384
386
}
385
387
post_response (os .fsdecode (cwd ), error_node )
386
388
try :
387
- session_node : Union [ TestNode , None ] = build_test_tree (session )
389
+ session_node : TestNode | None = build_test_tree (session )
388
390
if not session_node :
389
391
raise VSCodePytestError (
390
392
"Something went wrong following pytest finish, \
@@ -430,10 +432,10 @@ def build_test_tree(session: pytest.Session) -> TestNode:
430
432
session -- the pytest session object.
431
433
"""
432
434
session_node = create_session_node (session )
433
- session_children_dict : Dict [str , TestNode ] = {}
434
- file_nodes_dict : Dict [Any , TestNode ] = {}
435
- class_nodes_dict : Dict [str , TestNode ] = {}
436
- function_nodes_dict : Dict [str , TestNode ] = {}
435
+ session_children_dict : dict [str , TestNode ] = {}
436
+ file_nodes_dict : dict [Any , TestNode ] = {}
437
+ class_nodes_dict : dict [str , TestNode ] = {}
438
+ function_nodes_dict : dict [str , TestNode ] = {}
437
439
438
440
# Check to see if the global variable for symlink path is set
439
441
if SYMLINK_PATH :
@@ -492,7 +494,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
492
494
if isinstance (test_case .parent , pytest .Class ):
493
495
case_iter = test_case .parent
494
496
node_child_iter = test_node
495
- test_class_node : Union [ TestNode , None ] = None
497
+ test_class_node : TestNode | None = None
496
498
while isinstance (case_iter , pytest .Class ):
497
499
# While the given node is a class, create a class and nest the previous node as a child.
498
500
try :
@@ -529,7 +531,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
529
531
parent_test_case = create_file_node (test_case .parent )
530
532
file_nodes_dict [test_case .parent ] = parent_test_case
531
533
parent_test_case ["children" ].append (test_node )
532
- created_files_folders_dict : Dict [str , TestNode ] = {}
534
+ created_files_folders_dict : dict [str , TestNode ] = {}
533
535
for file_node in file_nodes_dict .values ():
534
536
# Iterate through all the files that exist and construct them into nested folders.
535
537
root_folder_node : TestNode
@@ -562,7 +564,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
562
564
563
565
def build_nested_folders (
564
566
file_node : TestNode ,
565
- created_files_folders_dict : Dict [str , TestNode ],
567
+ created_files_folders_dict : dict [str , TestNode ],
566
568
session_node : TestNode ,
567
569
) -> TestNode :
568
570
"""Takes a file or folder and builds the nested folder structure for it.
@@ -722,18 +724,18 @@ class DiscoveryPayloadDict(TypedDict):
722
724
723
725
cwd : str
724
726
status : Literal ["success" , "error" ]
725
- tests : Optional [ TestNode ]
726
- error : Optional [ List [ str ]]
727
+ tests : TestNode | None
728
+ error : list [ str ] | None
727
729
728
730
729
731
class ExecutionPayloadDict (Dict ):
730
732
"""A dictionary that is used to send a execution post request to the server."""
731
733
732
734
cwd : str
733
735
status : Literal ["success" , "error" ]
734
- result : Union [ TestRunResultDict , None ]
735
- not_found : Union [ List [ str ], None ] # Currently unused need to check
736
- error : Union [ str , None ] # Currently unused need to check
736
+ result : TestRunResultDict | None
737
+ not_found : list [ str ] | None # Currently unused need to check
738
+ error : str | None # Currently unused need to check
737
739
738
740
739
741
class EOTPayloadDict (TypedDict ):
@@ -782,9 +784,7 @@ def get_node_path(node: Any) -> pathlib.Path:
782
784
atexit .register (lambda : __writer .close () if __writer else None )
783
785
784
786
785
- def execution_post (
786
- cwd : str , status : Literal ["success" , "error" ], tests : Union [TestRunResultDict , None ]
787
- ):
787
+ def execution_post (cwd : str , status : Literal ["success" , "error" ], tests : TestRunResultDict | None ):
788
788
"""Sends a POST request with execution payload details.
789
789
790
790
Args:
@@ -829,7 +829,7 @@ def default(self, obj):
829
829
830
830
831
831
def send_post_request (
832
- payload : Union [ ExecutionPayloadDict , DiscoveryPayloadDict , EOTPayloadDict ] ,
832
+ payload : ExecutionPayloadDict | DiscoveryPayloadDict | EOTPayloadDict ,
833
833
cls_encoder = None ,
834
834
):
835
835
"""
0 commit comments