@@ -694,7 +694,7 @@ def _fn_decl(
694694 is_builtin : bool ,
695695 ruleset : Ruleset | None = None ,
696696 unextractable : bool = False ,
697- ) -> tuple [FunctionRef | MethodRef | PropertyRef | ClassMethodRef | InitRef , Callable [[], None ]]:
697+ ) -> tuple [CallableRef , Callable [[], None ]]:
698698 """
699699 Sets the function decl for the function object and returns the ref as well as a thunk that sets the default callable.
700700 """
@@ -707,10 +707,7 @@ def _fn_decl(
707707 if "Callable" not in hint_globals :
708708 hint_globals ["Callable" ] = Callable
709709
710- try :
711- hints = get_type_hints (fn , hint_globals , hint_locals )
712- except Exception as e :
713- raise TypeError (f"Failed to get type hints for { fn } " ) from e
710+ hints = get_type_hints (fn , hint_globals , hint_locals )
714711
715712 params = list (signature (fn ).parameters .values ())
716713
@@ -771,33 +768,41 @@ def _fn_decl(
771768 )
772769 )
773770 decls .update (* merge_action )
774-
775- signature_ = FunctionSignature (
776- return_type = None if mutates_first_arg else return_type ,
777- var_arg_type = var_arg_type ,
778- arg_types = arg_types ,
779- arg_names = arg_names ,
780- arg_defaults = tuple (a .__egg_typed_expr__ .expr if a is not None else None for a in arg_defaults ),
781- )
782- decl = FunctionDecl (
783- signature = signature_ ,
784- cost = cost ,
785- egg_name = egg_name ,
786- merge = merged .__egg_typed_expr__ .expr if merged is not None else None ,
787- unextractable = unextractable ,
788- builtin = is_builtin ,
789- default = None if default is None else default .__egg_typed_expr__ .expr ,
790- on_merge = tuple (a .action for a in merge_action ),
791- )
792- res = Thunk .fn (_create_default_value , decls , ref , fn , signature_ , ruleset )
771+ # defer this in generator so it doesnt resolve for builtins eagerly
772+ args = (TypedExprDecl (tp .to_just (), VarDecl (name , False )) for name , tp in zip (arg_names , arg_types , strict = True ))
773+ res_ref : FunctionRef | MethodRef | ClassMethodRef | PropertyRef | InitRef | UnnamedFunctionRef
774+ res_thunk : Callable [[], object ]
793775 # If we were not passed in a ref, this is an unnamed funciton, so eagerly compute the value and use that to refer to it
794776 if not ref :
795- res_value = res ()
796- assert isinstance (res_value , RuntimeExpr )
797- just_arg_types = tuple (tp .to_just () for tp in arg_types )
798- ref = FunctionRef (UnnamedFunctionRef (just_arg_types , arg_names , res_value .__egg_typed_expr__ ))
799- decls .set_function_decl (ref , decl )
800- return ref , Thunk .fn (_add_default_rewrite_function , decls , ref , signature_ .semantic_return_type , ruleset , res )
777+ tuple_args = tuple (args )
778+ res = _create_default_value (decls , ref , fn , tuple_args , ruleset )
779+ assert isinstance (res , RuntimeExpr )
780+ res_ref = UnnamedFunctionRef (tuple_args , res .__egg_typed_expr__ )
781+ decls ._unnamed_functions .add (res_ref )
782+ res_thunk = Thunk .value (res )
783+
784+ else :
785+ signature_ = FunctionSignature (
786+ return_type = None if mutates_first_arg else return_type ,
787+ var_arg_type = var_arg_type ,
788+ arg_types = arg_types ,
789+ arg_names = arg_names ,
790+ arg_defaults = tuple (a .__egg_typed_expr__ .expr if a is not None else None for a in arg_defaults ),
791+ )
792+ decl = FunctionDecl (
793+ signature = signature_ ,
794+ cost = cost ,
795+ egg_name = egg_name ,
796+ merge = merged .__egg_typed_expr__ .expr if merged is not None else None ,
797+ unextractable = unextractable ,
798+ builtin = is_builtin ,
799+ default = None if default is None else default .__egg_typed_expr__ .expr ,
800+ on_merge = tuple (a .action for a in merge_action ),
801+ )
802+ res_ref = ref
803+ decls .set_function_decl (ref , decl )
804+ res_thunk = Thunk .fn (_create_default_value , decls , ref , fn , args , ruleset )
805+ return res_ref , Thunk .fn (_add_default_rewrite_function , decls , res_ref , return_type , ruleset , res_thunk )
801806
802807
803808# Overload to support aritys 0-4 until variadic generic support map, so we can map from type to value
@@ -872,38 +877,25 @@ def _create_default_value(
872877 decls : Declarations ,
873878 ref : CallableRef | None ,
874879 fn : Callable ,
875- signature : FunctionSignature ,
880+ args : Iterable [ TypedExprDecl ] ,
876881 ruleset : Ruleset | None ,
877882) -> object :
878- args : list [object ] = [
879- RuntimeExpr .__from_values__ (
880- decls ,
881- TypedExprDecl (
882- tp .to_just (),
883- VarDecl (name , False ),
884- ),
885- )
886- for name , tp in zip (signature .arg_names , signature .arg_types , strict = False )
887- ]
883+ args : list [object ] = [RuntimeExpr .__from_values__ (decls , a ) for a in args ]
888884
889885 # If this is a classmethod, add the class as the first arg
890886 if isinstance (ref , ClassMethodRef ):
891887 tp = decls .get_paramaterized_class (ref .class_name )
892888 args .insert (0 , RuntimeClass (Thunk .value (decls ), tp ))
893889 with set_current_ruleset (ruleset ):
894- try :
895- return fn (* args )
896- except Exception as err :
897- msg = f"Error when calling { fn } "
898- raise ValueError (msg ) from err
890+ return fn (* args )
899891
900892
901893def _add_default_rewrite_function (
902894 decls : Declarations ,
903895 ref : CallableRef ,
904896 res_type : TypeOrVarRef ,
905897 ruleset : Ruleset | None ,
906- value_thunk : Thunk ,
898+ value_thunk : Callable [[], object ] ,
907899) -> None :
908900 """
909901 Helper functions that resolves a value thunk to create the default value.
0 commit comments