22from enum import Enum
33from functools import wraps
44from inspect import get_annotations , signature , Parameter
5- from typing import Optional , Callable , Final , cast , Any , TypeVar
5+ from typing import Optional , Callable , Final , cast , Any , TypeVar , overload
66from types import FunctionType
77from algosdk .abi import Method
88from pyteal import (
3232from beaker .state import AccountStateValue , ApplicationStateValue
3333
3434HandlerFunc = Callable [..., Expr ]
35+ DecoratorFunc = Callable [[HandlerFunc ], HandlerFunc ]
3536
3637_handler_config_attr : Final [str ] = "__handler_config__"
3738
@@ -394,9 +395,23 @@ def _remove_self(fn: HandlerFunc) -> HandlerFunc:
394395 return fn
395396
396397
398+ @overload
397399def internal (
398- return_type_or_handler : TealType | HandlerFunc ,
400+ return_type_or_handler : HandlerFunc ,
399401) -> HandlerFunc :
402+ ...
403+
404+
405+ @overload
406+ def internal (
407+ return_type_or_handler : TealType ,
408+ ) -> DecoratorFunc :
409+ ...
410+
411+
412+ def internal (
413+ return_type_or_handler : TealType | HandlerFunc ,
414+ ) -> HandlerFunc | DecoratorFunc :
400415 """creates a subroutine to be called by logic internally
401416
402417 Args:
@@ -433,24 +448,48 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
433448 if fn is not None :
434449 return _impl (fn )
435450
436- return _impl # type: ignore
451+ return _impl
437452
438453
454+ @overload
439455def external (
440- fn : HandlerFunc | None = None ,
456+ func : HandlerFunc ,
441457 / ,
442458 * ,
443459 name : str | None = None ,
444460 authorize : SubroutineFnWrapper | None = None ,
445461 method_config : MethodConfig | None = None ,
446462 read_only : bool = False ,
447463) -> HandlerFunc :
464+ ...
465+
466+
467+ @overload
468+ def external (
469+ * ,
470+ name : str | None = None ,
471+ authorize : SubroutineFnWrapper | None = None ,
472+ method_config : MethodConfig | None = None ,
473+ read_only : bool = False ,
474+ ) -> DecoratorFunc :
475+ ...
476+
477+
478+ def external (
479+ func : HandlerFunc | None = None ,
480+ / ,
481+ * ,
482+ name : str | None = None ,
483+ authorize : SubroutineFnWrapper | None = None ,
484+ method_config : MethodConfig | None = None ,
485+ read_only : bool = False ,
486+ ) -> HandlerFunc | DecoratorFunc :
448487
449488 """
450489 Add the method decorated to be handled as an ABI method for the Application
451490
452491 Args:
453- fn : The function being wrapped.
492+ func : The function being wrapped.
454493 name: Name of ABI method. If not set, name of the python method will be used.
455494 Useful for method overriding.
456495 authorize: a subroutine with input of ``Txn.sender()`` and output uint64
@@ -482,10 +521,10 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
482521
483522 return fn
484523
485- if fn is None :
486- return _impl # type: ignore
524+ if func is None :
525+ return _impl
487526
488- return _impl (fn )
527+ return _impl (func )
489528
490529
491530def bare_external (
@@ -554,13 +593,33 @@ def is_bare(fn: HandlerFunc) -> bool:
554593 )
555594
556595
596+ @overload
557597def create (
558- fn : HandlerFunc | None = None ,
598+ fn : HandlerFunc ,
559599 / ,
560600 * ,
561601 authorize : SubroutineFnWrapper | None = None ,
562602 method_config : Optional [MethodConfig ] | None = None ,
563603) -> HandlerFunc :
604+ ...
605+
606+
607+ @overload
608+ def create (
609+ * ,
610+ authorize : SubroutineFnWrapper | None = None ,
611+ method_config : Optional [MethodConfig ] | None = None ,
612+ ) -> DecoratorFunc :
613+ ...
614+
615+
616+ def create (
617+ fn : HandlerFunc | None = None ,
618+ / ,
619+ * ,
620+ authorize : SubroutineFnWrapper | None = None ,
621+ method_config : Optional [MethodConfig ] | None = None ,
622+ ) -> HandlerFunc | DecoratorFunc :
564623 """set method to be handled by an application call with its :code:`OnComplete`
565624 set to :code:`NoOp` call and ApplicationId == 0
566625
@@ -590,17 +649,29 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
590649 else :
591650 return external (method_config = MethodConfig (** mconfig ), authorize = authorize )(
592651 fn
593- ) # type: ignore
652+ )
594653
595654 if fn is None :
596- return _impl # type: ignore
655+ return _impl
597656
598657 return _impl (fn )
599658
600659
660+ @overload
601661def delete (
602- fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
662+ fn : HandlerFunc , / , * , authorize : SubroutineFnWrapper | None = None
603663) -> HandlerFunc :
664+ ...
665+
666+
667+ @overload
668+ def delete (* , authorize : SubroutineFnWrapper | None = None ) -> DecoratorFunc :
669+ ...
670+
671+
672+ def delete (
673+ fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
674+ ) -> HandlerFunc | DecoratorFunc :
604675 """set method to be handled by an application call with it's
605676 :code:`OnComplete` set to :code:`DeleteApplication` call
606677
@@ -622,19 +693,29 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
622693 return external (
623694 method_config = MethodConfig (delete_application = CallConfig .CALL ),
624695 authorize = authorize ,
625- )(
626- fn
627- ) # type: ignore
696+ )(fn )
628697
629698 if fn is None :
630- return _impl # type: ignore
699+ return _impl
631700
632701 return _impl (fn )
633702
634703
704+ @overload
635705def update (
636- fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
706+ fn : HandlerFunc , / , * , authorize : SubroutineFnWrapper | None = None
637707) -> HandlerFunc :
708+ ...
709+
710+
711+ @overload
712+ def update (* , authorize : SubroutineFnWrapper | None = None ) -> DecoratorFunc :
713+ ...
714+
715+
716+ def update (
717+ fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
718+ ) -> HandlerFunc | DecoratorFunc :
638719 """set method to be handled by an application call with it's
639720 :code:`OnComplete` set to :code:`UpdateApplication` call
640721
@@ -656,19 +737,29 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
656737 return external (
657738 method_config = MethodConfig (update_application = CallConfig .CALL ),
658739 authorize = authorize ,
659- )(
660- fn
661- ) # type: ignore
740+ )(fn )
662741
663742 if fn is None :
664- return _impl # type: ignore
743+ return _impl
665744
666745 return _impl (fn )
667746
668747
748+ @overload
669749def opt_in (
670- fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
750+ fn : HandlerFunc , / , * , authorize : SubroutineFnWrapper | None = None
671751) -> HandlerFunc :
752+ ...
753+
754+
755+ @overload
756+ def opt_in (* , authorize : SubroutineFnWrapper | None = None ) -> DecoratorFunc :
757+ ...
758+
759+
760+ def opt_in (
761+ fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
762+ ) -> HandlerFunc | DecoratorFunc :
672763 """set method to be handled by an application call with it's
673764 :code:`OnComplete` set to :code:`OptIn` call
674765
@@ -689,19 +780,29 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
689780 else :
690781 return external (
691782 method_config = MethodConfig (opt_in = CallConfig .CALL ), authorize = authorize
692- )(
693- fn
694- ) # type: ignore
783+ )(fn )
695784
696785 if fn is None :
697- return _impl # type: ignore
786+ return _impl
698787
699788 return _impl (fn )
700789
701790
791+ @overload
702792def clear_state (
703- fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
793+ fn : HandlerFunc , / , * , authorize : SubroutineFnWrapper | None = None
704794) -> HandlerFunc :
795+ ...
796+
797+
798+ @overload
799+ def clear_state (* , authorize : SubroutineFnWrapper | None = None ) -> DecoratorFunc :
800+ ...
801+
802+
803+ def clear_state (
804+ fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
805+ ) -> HandlerFunc | DecoratorFunc :
705806 """set method to be handled by an application call with it'ws
706807 :code:`OnComplete` set to :code:`ClearState` call
707808
@@ -723,19 +824,29 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
723824 return external (
724825 method_config = MethodConfig (clear_state = CallConfig .CALL ),
725826 authorize = authorize ,
726- )(
727- fn
728- ) # type: ignore
827+ )(fn )
729828
730829 if fn is None :
731- return _impl # type: ignore
830+ return _impl
732831
733832 return _impl (fn )
734833
735834
835+ @overload
736836def close_out (
737- fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
837+ fn : HandlerFunc , / , * , authorize : SubroutineFnWrapper | None = None
738838) -> HandlerFunc :
839+ ...
840+
841+
842+ @overload
843+ def close_out (* , authorize : SubroutineFnWrapper | None = None ) -> DecoratorFunc :
844+ ...
845+
846+
847+ def close_out (
848+ fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
849+ ) -> HandlerFunc | DecoratorFunc :
739850 """set method to be handled by an application call with it's
740851 :code:`OnComplete` set to :code:`CloseOut` call
741852
@@ -757,19 +868,29 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
757868 return external (
758869 method_config = MethodConfig (close_out = CallConfig .CALL ),
759870 authorize = authorize ,
760- )(
761- fn
762- ) # type: ignore
871+ )(fn )
763872
764873 if fn is None :
765- return _impl # type: ignore
874+ return _impl
766875
767876 return _impl (fn )
768877
769878
879+ @overload
770880def no_op (
771- fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
881+ fn : HandlerFunc , / , * , authorize : SubroutineFnWrapper | None = None
772882) -> HandlerFunc :
883+ ...
884+
885+
886+ @overload
887+ def no_op (* , authorize : SubroutineFnWrapper | None = None ) -> DecoratorFunc :
888+ ...
889+
890+
891+ def no_op (
892+ fn : HandlerFunc | None = None , / , * , authorize : SubroutineFnWrapper | None = None
893+ ) -> HandlerFunc | DecoratorFunc :
773894 """set method to be handled by an application call with
774895 it's :code:`OnComplete` set to :code:`NoOp` call
775896
@@ -790,11 +911,9 @@ def _impl(fn: HandlerFunc) -> HandlerFunc:
790911 else :
791912 return external (
792913 method_config = MethodConfig (no_op = CallConfig .CALL ), authorize = authorize
793- )(
794- fn
795- ) # type: ignore
914+ )(fn )
796915
797916 if fn is None :
798- return _impl # type: ignore
917+ return _impl
799918
800919 return _impl (fn )
0 commit comments