2828 Pattern ,
2929 Set ,
3030 Tuple ,
31+ TypeVar ,
3132 Union ,
3233 cast ,
3334)
@@ -647,7 +648,12 @@ def get_meta_prop(o: Union[IMeta, Var]) -> Any:
647648_tag_meta = _meta_getter (SYM_TAG_META_KEY )
648649
649650
650- def _loc (form : Union [LispForm , ISeq ]) -> Optional [Tuple [int , int , int , int ]]:
651+ T_form = TypeVar ("T_form" , bound = ReaderForm )
652+ T_node = TypeVar ("T_node" , bound = Node )
653+ LispAnalyzer = Callable [[T_form , AnalyzerContext ], T_node ]
654+
655+
656+ def _loc (form : T_form ) -> Optional [Tuple [int , int , int , int ]]:
651657 """Fetch the location of the form in the original filename from the
652658 input form, if it has metadata."""
653659 # Technically, IMeta is sufficient for fetching `form.meta` but the
@@ -669,17 +675,17 @@ def _loc(form: Union[LispForm, ISeq]) -> Optional[Tuple[int, int, int, int]]:
669675 return None
670676
671677
672- def _with_loc (f ) :
678+ def _with_loc (f : LispAnalyzer [ T_form , T_node ]) -> LispAnalyzer [ T_form , T_node ] :
673679 """Attach any available location information from the input form to
674680 the node environment returned from the parsing function."""
675681
676682 @wraps (f )
677- def _analyze_form (form : Union [ LispForm , ISeq ], ctx : AnalyzerContext ) -> Node :
683+ def _analyze_form (form : T_form , ctx : AnalyzerContext ) -> T_node :
678684 form_loc = _loc (form )
679685 if form_loc is None :
680686 return f (form , ctx )
681687 else :
682- return f (form , ctx ).fix_missing_locations (form_loc )
688+ return cast ( T_node , f (form , ctx ).fix_missing_locations (form_loc ) )
683689
684690 return _analyze_form
685691
@@ -795,24 +801,15 @@ def _tag_ast(form: Optional[LispForm], ctx: AnalyzerContext) -> Optional[Node]:
795801 return _analyze_form (form , ctx )
796802
797803
798- def _with_meta (gen_node ) :
804+ def _with_meta (gen_node : LispAnalyzer [ T_form , T_node ]) -> LispAnalyzer [ T_form , T_node ] :
799805 """Wraps the node generated by gen_node in a :with-meta AST node if the
800806 original form has meta.
801807
802808 :with-meta AST nodes are used for non-quoted collection literals and for
803809 function expressions."""
804810
805811 @wraps (gen_node )
806- def with_meta (
807- form : Union [
808- llist .PersistentList ,
809- lmap .PersistentMap ,
810- ISeq ,
811- lset .PersistentSet ,
812- vec .PersistentVector ,
813- ],
814- ctx : AnalyzerContext ,
815- ) -> Node :
812+ def with_meta (form : T_form , ctx : AnalyzerContext ) -> T_node :
816813 assert not ctx .is_quoted , "with-meta nodes are not used in quoted expressions"
817814
818815 descriptor = gen_node (form , ctx )
@@ -825,11 +822,14 @@ def with_meta(
825822 assert isinstance (meta_ast , MapNode ) or (
826823 isinstance (meta_ast , Const ) and meta_ast .type == ConstType .MAP
827824 )
828- return WithMeta (
829- form = form ,
830- meta = meta_ast ,
831- expr = descriptor ,
832- env = ctx .get_node_env (pos = ctx .syntax_position ),
825+ return cast (
826+ T_node ,
827+ WithMeta (
828+ form = cast (LispForm , form ),
829+ meta = meta_ast ,
830+ expr = descriptor ,
831+ env = ctx .get_node_env (pos = ctx .syntax_position ),
832+ ),
833833 )
834834
835835 return descriptor
@@ -3113,7 +3113,7 @@ def _yield_ast(form: ISeq, ctx: AnalyzerContext) -> Yield:
31133113 return Yield .expressionless (form , ctx .get_node_env (pos = ctx .syntax_position ))
31143114
31153115
3116- SpecialFormHandler = Callable [[ISeq , AnalyzerContext ], SpecialFormNode ]
3116+ SpecialFormHandler = Callable [[T_form , AnalyzerContext ], SpecialFormNode ]
31173117_SPECIAL_FORM_HANDLERS : Mapping [sym .Symbol , SpecialFormHandler ] = {
31183118 SpecialForm .AWAIT : _await_ast ,
31193119 SpecialForm .DEF : _def_ast ,
0 commit comments