Skip to content

Commit

Permalink
fix: when calling representation of Selector on json type (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laerte authored Jun 16, 2023
1 parent 26ee0c2 commit 8c39b2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 8 additions & 5 deletions parsel/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,10 @@ def re_first(

def get(self) -> Any:
"""
Serialize and return the matched nodes in a single string.
Percent encoded content is unquoted.
Serialize and return the matched nodes.
For HTML and XML, the result is always a string, and percent-encoded
content is unquoted.
"""
if self.type in ("text", "json"):
return self.root
Expand Down Expand Up @@ -873,7 +875,8 @@ def __bool__(self) -> bool:
__nonzero__ = __bool__

def __str__(self) -> str:
data = repr(shorten(self.get(), width=40))
return f"<{type(self).__name__} query={self._expr!r} data={data}>"
return str(self.get())

__repr__ = __str__
def __repr__(self) -> str:
data = repr(shorten(str(self.get()), width=40))
return f"<{type(self).__name__} query={self._expr!r} data={data}>"
8 changes: 8 additions & 0 deletions tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,14 @@ def test_etree_root_invalid_type(self) -> None:
type="json",
)

def test_json_selector_representation(self) -> None:
selector = Selector(text="true")
assert repr(selector) == "<Selector query=None data='True'>"
assert str(selector) == "True"
selector = Selector(text="1")
assert repr(selector) == "<Selector query=None data='1'>"
assert str(selector) == "1"


class ExsltTestCase(unittest.TestCase):

Expand Down

0 comments on commit 8c39b2f

Please sign in to comment.