Skip to content

Commit

Permalink
Add ImportInfo.__repr__()
Browse files Browse the repository at this point in the history
  • Loading branch information
lieryan committed Apr 4, 2024
1 parent 3be7530 commit ba54cab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rope/refactor/importutils/importinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def get_imported_names(self, context):
primary.split(".")[0] for primary in self.get_imported_primaries(context)
]

def __repr__(self):
return f'<{self.__class__.__name__} "{self.get_import_statement()}">'

def get_import_statement(self):
pass

Expand Down
22 changes: 22 additions & 0 deletions ropetest/reprtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rope.contrib import findit
from rope.contrib.autoimport import models
from rope.refactor import occurrences
from rope.refactor.importutils import importinfo
from ropetest import testutils


Expand Down Expand Up @@ -165,3 +166,24 @@ def test_autoimport_models_finalquery(project, mod1):
obj = models.Package.delete_by_package_name
assert isinstance(obj, models.FinalQuery)
assert repr(obj) == expected_repr


def test_repr_normal_import(project):
obj = importinfo.NormalImport([("abc", None), ("ghi", "jkl")])
expected_repr = '<NormalImport "import abc, ghi as jkl">'
assert isinstance(obj, importinfo.NormalImport)
assert repr(obj) == expected_repr


def test_repr_from_import(project):
obj = importinfo.FromImport("pkg1.pkg2", 0, [("abc", None), ("ghi", "jkl")])
expected_repr = '<FromImport "from pkg1.pkg2 import abc, ghi as jkl">'
assert isinstance(obj, importinfo.FromImport)
assert repr(obj) == expected_repr


def test_repr_from_import_with_level(project):
obj = importinfo.FromImport("pkg1.pkg2", 3, [("abc", None), ("ghi", "jkl")])
expected_repr = '<FromImport "from pkg1.pkg2 import abc, ghi as jkl">'
assert isinstance(obj, importinfo.FromImport)
assert repr(obj) == expected_repr

0 comments on commit ba54cab

Please sign in to comment.