def _import_parameter_annotations( self, parameters: cst.Parameters ) -> cst.Parameters: def update_annotations(parameters: Sequence[cst.Param]) -> List[cst.Param]: updated_parameters = [] for parameter in list(parameters): annotation = parameter.annotation if annotation is not None: parameter = parameter.with_changes( annotation=self._create_import_from_annotation(annotation) ) updated_parameters.append(parameter) return updated_parameters return parameters.with_changes(params=update_annotations(parameters.params))
def _handle_Parameters( self, parameters: cst.Parameters, ) -> cst.Parameters: def update_annotations( parameters: Sequence[cst.Param], ) -> List[cst.Param]: updated_parameters = [] for parameter in list(parameters): annotation = parameter.annotation if annotation is not None: parameter = parameter.with_changes( annotation=self._handle_Annotation( annotation=annotation)) updated_parameters.append(parameter) return updated_parameters return parameters.with_changes( params=update_annotations(parameters.params))
def leave_Parameters( self, original_node: cst.Parameters, updated_node: cst.Parameters, ) -> cst.Parameters: skip = ( # self.parameter_count is None or len(updated_node.params) < self.parameter_count or (len(updated_node.params) == 1 and updated_node.params[0].name.value in {"self", "cls"})) if skip: return updated_node else: last_param = updated_node.params[-1] return updated_node.with_changes(params=( *updated_node.params[:-1], last_param.with_changes(comma=cst.Comma()), ), )
def update_parameters( params: cst.Parameters, get_annotation, overwrite_existing_annotations: bool, ) -> cst.Parameters: def update_annotation( parameters: Sequence[cst.Param], ) -> List[cst.Param]: annotated_parameters = [] for parameter in parameters: # key = parameter.name.value anno = get_annotation(parameter) if anno and (overwrite_existing_annotations or not parameter.annotation): parameter = parameter.with_changes(annotation=anno) annotated_parameters.append(parameter) return annotated_parameters return params.with_changes( params=update_annotation(params.params, ), kwonly_params=update_annotation(params.kwonly_params, ), posonly_params=update_annotation(params.posonly_params, ), )