11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
33
4+ from __future__ import annotations
5+
46import atexit
57import json
68import os
79import pathlib
810import sys
911import traceback
1012from typing import (
13+ TYPE_CHECKING ,
1114 Any ,
1215 Dict ,
1316 Generator ,
14- List ,
1517 Literal ,
16- Optional ,
1718 TypedDict ,
18- Union ,
1919)
2020
2121import pytest
22- from pluggy import Result
2322
2423script_dir = pathlib .Path (__file__ ).parent .parent
2524sys .path .append (os .fspath (script_dir ))
2625sys .path .append (os .fspath (script_dir / "lib" / "python" ))
2726from testing_tools import socket_manager # noqa: E402
2827
28+ if TYPE_CHECKING :
29+ from pluggy import Result
30+
2931
3032class TestData (TypedDict ):
3133 """A general class that all test objects inherit from."""
@@ -46,7 +48,7 @@ class TestItem(TestData):
4648class TestNode (TestData ):
4749 """A general class that handles all test data which contains children."""
4850
49- children : " list[Union[ TestNode, TestItem, None]]"
51+ children : list [TestNode | TestItem | None ]
5052
5153
5254class VSCodePytestError (Exception ):
@@ -209,17 +211,17 @@ class TestOutcome(Dict):
209211
210212 test : str
211213 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
215217
216218
217219def create_test_outcome (
218220 testid : str ,
219221 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
223225) -> TestOutcome :
224226 """A function that creates a TestOutcome object."""
225227 return TestOutcome (
@@ -235,7 +237,7 @@ class TestRunResultDict(Dict[str, Dict[str, TestOutcome]]):
235237 """A class that stores all test run results."""
236238
237239 outcome : str
238- tests : Dict [str , TestOutcome ]
240+ tests : dict [str , TestOutcome ]
239241
240242
241243@pytest .hookimpl (hookwrapper = True , trylast = True )
@@ -384,7 +386,7 @@ def pytest_sessionfinish(session, exitstatus):
384386 }
385387 post_response (os .fsdecode (cwd ), error_node )
386388 try :
387- session_node : Union [ TestNode , None ] = build_test_tree (session )
389+ session_node : TestNode | None = build_test_tree (session )
388390 if not session_node :
389391 raise VSCodePytestError (
390392 "Something went wrong following pytest finish, \
@@ -430,10 +432,10 @@ def build_test_tree(session: pytest.Session) -> TestNode:
430432 session -- the pytest session object.
431433 """
432434 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 ] = {}
437439
438440 # Check to see if the global variable for symlink path is set
439441 if SYMLINK_PATH :
@@ -492,7 +494,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
492494 if isinstance (test_case .parent , pytest .Class ):
493495 case_iter = test_case .parent
494496 node_child_iter = test_node
495- test_class_node : Union [ TestNode , None ] = None
497+ test_class_node : TestNode | None = None
496498 while isinstance (case_iter , pytest .Class ):
497499 # While the given node is a class, create a class and nest the previous node as a child.
498500 try :
@@ -529,7 +531,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
529531 parent_test_case = create_file_node (test_case .parent )
530532 file_nodes_dict [test_case .parent ] = parent_test_case
531533 parent_test_case ["children" ].append (test_node )
532- created_files_folders_dict : Dict [str , TestNode ] = {}
534+ created_files_folders_dict : dict [str , TestNode ] = {}
533535 for file_node in file_nodes_dict .values ():
534536 # Iterate through all the files that exist and construct them into nested folders.
535537 root_folder_node : TestNode
@@ -562,7 +564,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
562564
563565def build_nested_folders (
564566 file_node : TestNode ,
565- created_files_folders_dict : Dict [str , TestNode ],
567+ created_files_folders_dict : dict [str , TestNode ],
566568 session_node : TestNode ,
567569) -> TestNode :
568570 """Takes a file or folder and builds the nested folder structure for it.
@@ -722,18 +724,18 @@ class DiscoveryPayloadDict(TypedDict):
722724
723725 cwd : str
724726 status : Literal ["success" , "error" ]
725- tests : Optional [ TestNode ]
726- error : Optional [ List [ str ]]
727+ tests : TestNode | None
728+ error : list [ str ] | None
727729
728730
729731class ExecutionPayloadDict (Dict ):
730732 """A dictionary that is used to send a execution post request to the server."""
731733
732734 cwd : str
733735 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
737739
738740
739741class EOTPayloadDict (TypedDict ):
@@ -782,9 +784,7 @@ def get_node_path(node: Any) -> pathlib.Path:
782784atexit .register (lambda : __writer .close () if __writer else None )
783785
784786
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 ):
788788 """Sends a POST request with execution payload details.
789789
790790 Args:
@@ -829,7 +829,7 @@ def default(self, obj):
829829
830830
831831def send_post_request (
832- payload : Union [ ExecutionPayloadDict , DiscoveryPayloadDict , EOTPayloadDict ] ,
832+ payload : ExecutionPayloadDict | DiscoveryPayloadDict | EOTPayloadDict ,
833833 cls_encoder = None ,
834834):
835835 """
0 commit comments