def leave_Name(self, original_node: cst.Name, updated_node: cst.Name): def extract_module_var_type(module_type_annot: Dict[Tuple, Tuple[str, str]]): if (None, None, original_node.value ) in module_type_annot: # Skips imported module names if module_type_annot[(None, None, original_node.value)][0] != '': return module_type_annot[(None, None, original_node.value)][0] name_type = None # Adds types of class variables, function parameters and function variables if len(self.cls_stack) > 0: if (self.cls_stack[-1], self.fn_stack[-1] if len(self.fn_stack) > 0 else None, original_node.value) in \ self.module_type_annot: # skips classes' identifiers if self.module_type_annot[(self.cls_stack[-1], self.fn_stack[-1] if len(self.fn_stack) > 0 else None, original_node.value)][0] != '': name_type = self.module_type_annot[( self.cls_stack[-1], self.fn_stack[-1] if len(self.fn_stack) > 0 else None, original_node.value)][0] else: # module-level variables (constants) in a function name_type = extract_module_var_type(self.module_type_annot) else: # module-level variables (constants) name_type = extract_module_var_type(self.module_type_annot) return updated_node.with_changes( value=f"${name_type}$") if name_type is not None else updated_node
def visit_Name(self, node: libcst.Name) -> None: # Avoid a false-positive in this scenario: # # ``` # from typing import List as list # from graphene import List # ``` qualified_names = self.get_metadata(QualifiedNameProvider, node, set()) is_builtin_type = node.value in BUILTINS_TO_REPLACE and all( qualified_name.name in QUALIFIED_BUILTINS_TO_REPLACE for qualified_name in qualified_names ) if self.annotation_counter > 0 and is_builtin_type: correct_type = node.value.title() scope = self.get_metadata(ScopeProvider, node) replacement = None if scope is not None and correct_type in scope: replacement = node.with_changes(value=correct_type) self.report( node, REPLACE_BUILTIN_TYPE_ANNOTATION.format( builtin_type=node.value, correct_type=correct_type ), replacement=replacement, )
def leave_Name(self, original_node: cst.Name, updated_node: cst.Name) -> cst.Name: # Yes this is linear search. We could potentially create a set out of the list, # but in most cases there are so few references to the renamed variable that the # overhead of creating a set + computing hashes on lookup would likely outweigh # any savings. Did not actually benchmark. This code runs extremely rarely IRL. if original_node in self.names: return original_node.with_changes(value=self.new_name) return updated_node
def leave_Name(self, original_node: Name, updated_node: Name) -> BaseExpression: if ( self.is_imported_with_old_name and not self.entity_is_name_called and m.matches(updated_node, m.Name(value=self.old_name)) ): return updated_node.with_changes(value=self.new_name) return super().leave_Name(original_node, updated_node)
def leave_Name(self, original_node: Name, updated_node: Name) -> BaseExpression: """Rename reference to the imported name.""" matcher = self.name_matcher if (matcher and m.matches(updated_node, matcher) and not self.is_wrapped_in_call(original_node) and self.matches_import_scope(original_node)): return updated_node.with_changes(value=self.new_name) return super().leave_Name(original_node, updated_node)
def leave_Name( self, original_node: cst.Name, updated_node: cst.Name) -> Union[cst.Name, cst.SimpleString]: value = updated_node.value if value == "NoneType": # This is special-cased in typing, un-special case it. return updated_node.with_changes(value="None") if value in CST_DIR and not value.endswith("Sentinel"): # If this isn't a typing define and it isn't a builtin, convert it to # a forward ref string. return cst.SimpleString(repr(value)) return updated_node
def visit_Name(self, node: libcst.Name) -> None: if self.annotation_counter > 0 and node.value in BUILTINS_TO_REPLACE: correct_type = node.value.title() scope = self.get_metadata(ScopeProvider, node) replacement = None if scope is not None and correct_type in scope: replacement = node.with_changes(value=correct_type) self.report( node, REPLACE_BUILTIN_TYPE_ANNOTATION.format( builtin_type=node.value, correct_type=correct_type ), replacement=replacement, )
def fix_unicode(self, original_node: Name, updated_node: Name) -> BaseExpression: value = "text_type" if value not in self.future_utils_imports: self.future_utils_new_imports.add(value) return updated_node.with_changes(value=value)
def leave_Name(self, original_node: cst.Name, updated_node: cst.Name): if re.match(r"^\$.+\$$", original_node.value): return updated_node.with_changes( value=original_node.value.replace(" ", "")) else: return original_node