1313import sys
1414import time
1515import traceback
16+ from typing import TYPE_CHECKING , Any
1617import weakref
1718
1819import yaml
1920
2021from homeassistant .const import SERVICE_RELOAD
22+ from homeassistant .core import SupportsResponse
2123from homeassistant .helpers .service import async_set_service_schema
2224
2325from .const import (
2729 DOMAIN ,
2830 LOGGER_PATH ,
2931 SERVICE_JUPYTER_KERNEL_START ,
30- SERVICE_RESPONSE_NONE ,
3132)
3233from .function import Function
3334from .state import State
3435
36+ if TYPE_CHECKING :
37+ from .global_ctx import GlobalContext
38+
39+ type SymTable = dict [str , Any ]
40+
3541_LOGGER = logging .getLogger (LOGGER_PATH + ".eval" )
3642
3743#
@@ -315,7 +321,14 @@ def getattr(self):
315321class EvalFunc :
316322 """Class for a callable pyscript function."""
317323
318- def __init__ (self , func_def , code_list , code_str , global_ctx , async_func = False ):
324+ def __init__ (
325+ self ,
326+ func_def : ast .FunctionDef ,
327+ code_list : list [str ],
328+ code_str : str ,
329+ global_ctx : "GlobalContext" ,
330+ async_func : bool = False ,
331+ ) -> None :
319332 """Initialize a function calling context."""
320333 self .func_def = func_def
321334 self .name = func_def .name
@@ -533,7 +546,7 @@ async def do_service_call(func, ast_ctx, data):
533546 domain ,
534547 name ,
535548 pyscript_service_factory (func_name , self ),
536- dec_kwargs .get ("supports_response" , SERVICE_RESPONSE_NONE ),
549+ dec_kwargs .get ("supports_response" , SupportsResponse . NONE ),
537550 )
538551 async_set_service_schema (Function .hass , domain , name , service_desc )
539552 self .trigger_service .add (srv_name )
@@ -838,12 +851,12 @@ async def check_for_closure(self, arg):
838851class EvalFuncVar :
839852 """Class for a callable pyscript function."""
840853
841- def __init__ (self , func ) :
854+ def __init__ (self , func : EvalFunc ) -> None :
842855 """Initialize instance with given EvalFunc function."""
843856 self .func = func
844- self .ast_ctx = None
857+ self .ast_ctx : AstEval | None = None
845858
846- def get_func (self ):
859+ def get_func (self ) -> EvalFunc :
847860 """Return the EvalFunc function."""
848861 return self .func
849862
@@ -895,7 +908,7 @@ async def __call__(self, *args, **kwargs):
895908class EvalFuncVarClassInst (EvalFuncVar ):
896909 """Class for a callable pyscript class instance function."""
897910
898- def __init__ (self , func , ast_ctx , class_inst_weak ) :
911+ def __init__ (self , func : EvalFunc , ast_ctx : "AstEval" , class_inst_weak : weakref . ReferenceType ) -> None :
899912 """Initialize instance with given EvalFunc function."""
900913 super ().__init__ (func )
901914 self .ast_ctx = ast_ctx
@@ -913,25 +926,25 @@ async def __call__(self, *args, **kwargs):
913926class AstEval :
914927 """Python interpreter AST object evaluator."""
915928
916- def __init__ (self , name , global_ctx , logger_name = None ):
929+ def __init__ (self , name : str , global_ctx : "GlobalContext" , logger_name : str | None = None ) -> None :
917930 """Initialize an interpreter execution context."""
918931 self .name = name
919932 self .str = None
920933 self .ast = None
921934 self .global_ctx = global_ctx
922- self .global_sym_table = global_ctx .get_global_sym_table () if global_ctx else {}
923- self .sym_table_stack = []
935+ self .global_sym_table : SymTable = global_ctx .get_global_sym_table () if global_ctx else {}
936+ self .sym_table_stack : list [ SymTable ] = []
924937 self .sym_table = self .global_sym_table
925- self .local_sym_table = {}
926- self .user_locals = {}
927- self .curr_func = None
938+ self .local_sym_table : SymTable = {}
939+ self .user_locals : SymTable = {}
940+ self .curr_func : EvalFunc | None = None
928941 self .filename = name
929- self .code_str = None
930- self .code_list = None
931- self .exception = None
932- self .exception_obj = None
933- self .exception_long = None
934- self .exception_curr = None
942+ self .code_str : str | None = None
943+ self .code_list : list [ str ] | None = None
944+ self .exception : str | None = None
945+ self .exception_obj : Exception | None = None
946+ self .exception_long : str | None = None
947+ self .exception_curr : Exception | None = None
935948 self .lineno = 1
936949 self .col_offset = 0
937950 self .logger_handlers = set ()
@@ -2159,7 +2172,7 @@ async def get_names(self, this_ast=None, nonlocal_names=None, global_names=None,
21592172 await self .get_names_set (this_ast , names , nonlocal_names , global_names , local_names )
21602173 return names
21612174
2162- def parse (self , code_str , filename = None , mode = "exec" ):
2175+ def parse (self , code_str : str , filename : str | None = None , mode : str = "exec" ) -> bool :
21632176 """Parse the code_str source code into an AST tree."""
21642177 self .exception = None
21652178 self .exception_obj = None
@@ -2298,7 +2311,7 @@ def completions(self, root):
22982311 for attr in var .__dict__ :
22992312 if attr .lower ().startswith (attr_root ) and (attr_root != "" or attr [0 :1 ] != "_" ):
23002313 words .add (f"{ name } .{ attr } " )
2301- except Exception :
2314+ except Exception : # noqa: S110
23022315 pass
23032316 for keyw in set (keyword .kwlist ) - {"yield" }:
23042317 if keyw .lower ().startswith (root ):
@@ -2313,7 +2326,7 @@ def completions(self, root):
23132326 words .add (name )
23142327 return words
23152328
2316- async def eval (self , new_state_vars = None , merge_local = False ):
2329+ async def eval (self , new_state_vars : dict [ str , Any ] | None = None , merge_local : bool = False ) -> None :
23172330 """Execute parsed code, with the optional state variables added to the scope."""
23182331 self .exception = None
23192332 self .exception_obj = None
0 commit comments