def _handle_import(self, node: Union[Import, ImportFrom]) -> None: node_start = self.get_metadata(PositionProvider, node).start.line if node_start in self._ignored_lines: return names = node.names if isinstance(names, ImportStar): return for alias in names: position = self.get_metadata(PositionProvider, alias) lines = set(range(position.start.line, position.end.line + 1)) if lines.isdisjoint(self._ignored_lines): if isinstance(node, Import): RemoveImportsVisitor.remove_unused_import( self.context, module=alias.evaluated_name, asname=alias.evaluated_alias, ) else: module_name = get_absolute_module_for_import( self.context.full_module_name, node) if module_name is None: raise ValueError( f"Couldn't get absolute module name for {alias.evaluated_name}" ) RemoveImportsVisitor.remove_unused_import( self.context, module=module_name, obj=alias.evaluated_name, asname=alias.evaluated_alias, )
def _update_imports(self): RemoveImportsVisitor.remove_unused_import(self.context, "pytz") RemoveImportsVisitor.remove_unused_import(self.context, "pytz", "utc") RemoveImportsVisitor.remove_unused_import(self.context, "pytz", "UTC") RemoveImportsVisitor.remove_unused_import( self.context, "datetime", "timezone" ) AddImportsVisitor.add_needed_import( self.context, "bulb.platform.common.timezones", "UTC" )
def visit_Module(self, node: cst.Module) -> None: AddImportsVisitor.add_needed_import(self.context, "foo", "quux") RemoveImportsVisitor.remove_unused_import( self.context, "foo", "bar")