3636 "RuntimeClass" ,
3737 "RuntimeExpr" ,
3838 "RuntimeFunction" ,
39+ "create_callable" ,
3940 "define_expr_method" ,
4041 "resolve_callable" ,
4142 "resolve_type_annotation" ,
@@ -340,7 +341,7 @@ def __repr__(self) -> str:
340341
341342 # Make hashable so can go in Union
342343 def __hash__ (self ) -> int :
343- return hash (( id ( self .__egg_decls_thunk__ ), self . __egg_tp__ ) )
344+ return hash (self .__egg_tp__ )
344345
345346 def __eq__ (self , other : object ) -> bool :
346347 """
@@ -478,6 +479,9 @@ def __str__(self) -> str:
478479 bound_tp_params = args
479480 return pretty_callable_ref (self .__egg_decls__ , self .__egg_ref__ , first_arg , bound_tp_params )
480481
482+ def __repr__ (self ) -> str :
483+ return str (self )
484+
481485
482486def to_py_signature (sig : FunctionSignature , decls : Declarations , optional_args : bool ) -> Signature :
483487 """
@@ -670,11 +674,12 @@ def resolve_callable(callable: object) -> tuple[CallableRef, Declarations]:
670674 """
671675 Resolves a runtime callable into a ref
672676 """
677+ # TODO: Make runtime class work with __match_args__
678+ if isinstance (callable , RuntimeClass ):
679+ return InitRef (callable .__egg_tp__ .name ), callable .__egg_decls__
673680 match callable :
674681 case RuntimeFunction (decls , ref , _):
675682 return ref (), decls ()
676- case RuntimeClass (thunk , tp ):
677- return InitRef (tp .name ), thunk ()
678683 case RuntimeExpr (decl_thunk , expr_thunk ):
679684 if not isinstance ((expr := expr_thunk ().expr ), CallDecl ) or not isinstance (
680685 expr .callable , ConstantRef | ClassVariableRef
@@ -683,3 +688,21 @@ def resolve_callable(callable: object) -> tuple[CallableRef, Declarations]:
683688 return expr .callable , decl_thunk ()
684689 case _:
685690 raise NotImplementedError (f"Cannot turn { callable } of type { type (callable )} into a callable ref" )
691+
692+
693+ def create_callable (decls : Declarations , ref : CallableRef ) -> RuntimeFunction | RuntimeExpr :
694+ """
695+ Creates a callable object from a callable ref. This might not actually be callable, if the ref is a constant
696+ or classvar then it is a value
697+ """
698+ match ref :
699+ case InitRef (name ):
700+ return RuntimeClass (Thunk .value (decls ), TypeRefWithVars (name ))
701+ case FunctionRef () | MethodRef () | ClassMethodRef () | PropertyRef () | UnnamedFunctionRef ():
702+ bound = JustTypeRef (ref .class_name ) if isinstance (ref , ClassMethodRef ) else None
703+ return RuntimeFunction (Thunk .value (decls ), Thunk .value (ref ), bound )
704+ case ConstantRef (name ):
705+ tp = decls ._constants [name ].type_ref
706+ case ClassVariableRef (cls_name , var_name ):
707+ tp = decls ._classes [cls_name ].class_variables [var_name ].type_ref
708+ return RuntimeExpr .__from_values__ (decls , TypedExprDecl (tp , CallDecl (ref )))
0 commit comments