1313 SymbolTable , Statement , MypyFile , Var , Expression , Lvalue , Node ,
1414 OverloadedFuncDef , FuncDef , FuncItem , FuncBase , TypeInfo ,
1515 ClassDef , Block , AssignmentStmt , NameExpr , MemberExpr , IndexExpr ,
16- TupleExpr , ListExpr , ExpressionStmt , ReturnStmt , IfStmt ,
16+ TupleExpr , ListExpr , SetExpr , ExpressionStmt , ReturnStmt , IfStmt ,
1717 WhileStmt , OperatorAssignmentStmt , WithStmt , AssertStmt ,
1818 RaiseStmt , TryStmt , ForStmt , DelStmt , CallExpr , IntExpr , StrExpr ,
1919 UnicodeExpr , OpExpr , UnaryExpr , LambdaExpr , TempNode , SymbolTableNode ,
@@ -2733,6 +2733,27 @@ def visit_decorator(self, e: Decorator) -> None:
27332733 self .fail ('Single overload definition, multiple required' , e )
27342734 continue
27352735 dec = self .expr_checker .accept (d )
2736+
2737+ if (isinstance (d , CallExpr )
2738+ and getattr (d .callee , 'fullname' , '' ) == 'mypy_extensions.delegate'
2739+ and d .args
2740+ and isinstance (sig , CallableType ) # TODO allow Overloaded?
2741+ and sig .is_kw_arg ):
2742+ # TODO how should this combine with other decorators?
2743+ delegate_sig = self .expr_checker .accept (d .args [0 ])
2744+ if not isinstance (delegate_sig , CallableType ):
2745+ continue # TODO error message?
2746+ exclude = []
2747+ if d .arg_names [1 :2 ] == ['exclude' ]:
2748+ exclude = d .args [1 ]
2749+ if not (isinstance (exclude , (ListExpr , TupleExpr , SetExpr ))
2750+ and all (isinstance (ex , StrExpr )
2751+ for ex in exclude .items )):
2752+ continue # TODO error message?
2753+ exclude = [s .value for s in cast (List [StrExpr ], exclude .items )]
2754+ sig = self ._delegated_sig (delegate_sig , sig , exclude )
2755+ continue
2756+
27362757 temp = self .temp_node (sig )
27372758 fullname = None
27382759 if isinstance (d , RefExpr ):
@@ -2751,6 +2772,27 @@ def visit_decorator(self, e: Decorator) -> None:
27512772 if e .func .info and not e .func .is_dynamic ():
27522773 self .check_method_override (e )
27532774
2775+ def _delegated_sig (self , delegate_sig , sig , exclude ):
2776+ # TODO: also delegate *args (currently only does **kwargs)
2777+ exclude += sig .arg_names
2778+ args = [(name ,
2779+ kind if kind != nodes .ARG_OPT else nodes .ARG_NAMED_OPT ,
2780+ typ )
2781+ for (name , kind , typ ) in
2782+ zip (delegate_sig .arg_names ,
2783+ delegate_sig .arg_kinds ,
2784+ delegate_sig .arg_types )
2785+ if kind not in (nodes .ARG_POS , nodes .ARG_STAR )
2786+ and name not in exclude ]
2787+ names , kinds , types = map (list , zip (* args ))
2788+ # **kwargs are always last in the signature, so we remove them with [:-1]
2789+ sig = sig .copy_modified (
2790+ arg_names = sig .arg_names [:- 1 ] + names ,
2791+ arg_kinds = sig .arg_kinds [:- 1 ] + kinds ,
2792+ arg_types = sig .arg_types [:- 1 ] + types ,
2793+ )
2794+ return sig
2795+
27542796 def check_for_untyped_decorator (self ,
27552797 func : FuncDef ,
27562798 dec_type : Type ,
0 commit comments