11import asyncio
2+ import functools
23import json
34import os
45import pathlib
56import types
67
78import requests
89from collections import namedtuple
9- from typing import List , Dict , Optional , Callable , Awaitable , Tuple , cast
10+ from typing import List , Dict , Optional , Callable , Awaitable , Tuple , cast , Union
1011
1112from .install_methods import InstallMethod
1213from .log import log
1819Attr = namedtuple ('Attr' , ['filter' , 'name' , 'value' ])
1920
2021
22+ async def _to_thread (func , / , * args , ** kwargs ): # type: ignore
23+ loop = asyncio .get_running_loop ()
24+ func_call = functools .partial (func , * args , ** kwargs )
25+ return await loop .run_in_executor (None , func_call )
26+
27+
2128class _Options :
2229 def __init__ (self ) -> None :
2330 self .custom_attributes = None
2431
2532
2633class ConfWrapper :
27- def __init__ (self , dict : Dict [str , str | int | bool | None ]):
34+ def __init__ (self , dict : Dict [str , Union [ str , int , bool , None ] ]):
2835 self .dict = dict
2936 self .option = _Options ()
3037 dict ['run_id' ] = 'x'
3138 dict ['save_results' ] = False
3239 dict ['upload_results' ] = True
3340 dict ['branch_name_override' ] = None
3441
35- def getoption (self , name : str ) -> str | int | bool | None :
42+ def getoption (self , name : str ) -> Union [ str , int , bool , None ] :
3643 return self .dict [name ]
3744
3845
@@ -43,16 +50,16 @@ def __init__(self, conftest: types.ModuleType, ci_runner: types.ModuleType):
4350 log .write ('Conf: ' + str (self .conf ) + '\n ' )
4451 log .write (str (self .env ) + '\n ' )
4552 self .disable_install = False
46- self .install_method : InstallMethod | None = None
47- self .active_panel : int | None = None
48- self .scheduler : str | None = None
53+ self .install_method : Optional [ InstallMethod ] = None
54+ self .active_panel : Optional [ int ] = None
55+ self .scheduler : Optional [ str ] = None
4956 self .attrs = self ._parse_attributes ()
5057 self .run_test_job = True
5158 self .has_key = KEY_PATH .exists ()
5259 if self .has_key :
5360 with open (KEY_PATH , 'r' ) as f :
5461 self .key = f .read ().strip ()
55- self .key_is_valid : bool | None = None
62+ self .key_is_valid : Optional [ bool ] = None
5663 log .write (f'has key: { self .has_key } \n ' )
5764
5865 def _parse_attributes (self ) -> List [Attr ]:
@@ -89,15 +96,15 @@ def update_conf(self, name: str, value: str) -> None:
8996 self ._write_conf_value (name , value )
9097
9198 async def request (self , query : str , data : Dict [str , object ], title : str ,
92- error_cb : Callable [[str , str ], Awaitable [Dict [str , object ] | None ]]) \
93- -> Dict [str , object ] | None :
99+ error_cb : Callable [[str , str ], Awaitable [Optional [ Dict [str , object ]] ]]) \
100+ -> Optional [ Dict [str , object ]] :
94101 baseUrl = self .conf ['server_url' ]
95- response = await asyncio . to_thread (requests .post , baseUrl + query , data = data )
102+ response = await _to_thread (requests .post , baseUrl + query , data = data ) # type: ignore
96103 return await self ._check_error (response , title , error_cb )
97104
98105 async def _check_error (self , response : requests .Response , title : str ,
99- error_cb : Callable [[str , str ], Awaitable [Dict [str , object ] | None ]]) \
100- -> Dict [str , object ] | None :
106+ error_cb : Callable [[str , str ], Awaitable [Optional [ Dict [str , object ]] ]]) \
107+ -> Optional [ Dict [str , object ]] :
101108 log .write (f'Response: { response .text } \n ' )
102109 if response .status_code != 200 :
103110 msg = self ._extract_response_message (response .text )
0 commit comments