Skip to content

Commit

Permalink
chore: Use f-strings and add more type hints in ElementList() (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler authored Feb 14, 2024
1 parent b824a46 commit bc5d582
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
26 changes: 10 additions & 16 deletions splinter/element_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class ElementList:

def __init__(
self,
list,
elements: list,
driver=None,
find_by=None,
query=None,
) -> None: # NOQA: A002
) -> None:
self._container = []
self._container.extend(list)
self._container.extend(elements)

self.driver = driver
self.find_by = find_by
Expand All @@ -41,13 +41,10 @@ def __getitem__(self, index):
return self.first[index]
try:
return self._container[index]
except IndexError:
except IndexError as err:
raise ElementDoesNotExist(
'no elements could be found with {} "{}"'.format(
self.find_by,
self.query,
),
)
f'No elements were found with {self.find_by} "{self.query}"',
) from err

@property
def first(self):
Expand Down Expand Up @@ -77,19 +74,16 @@ def is_empty(self) -> bool:
"""
return len(self) == 0

def __getattr__(self, name):
def __getattr__(self, name: str):
try:
return getattr(self.first, name)
except AttributeError:
try:
return getattr(self._container, name)
except AttributeError:
except AttributeError as err:
raise AttributeError(
"'{}' object has no attribute '{}'".format(
self.__class__.__name__,
name,
),
)
f"'{self.__class__.__name__}' object has no attribute '{name}'",
) from err

def __iter__(self):
yield from self._container
Expand Down
4 changes: 2 additions & 2 deletions tests/element_does_not_exist.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_element_list_contains_right_information_and_raises_right_exception(self
self.assertEqual(".element-that-dont-exists", element_list.query)
element_list.first

expected_message = 'no elements could be found with css ".element-that-dont-exists"'
expected_message = 'No elements were found with css ".element-that-dont-exists"'

e = cm.exception
self.assertEqual(expected_message, e.args[0])
Expand All @@ -46,7 +46,7 @@ def test_element_list_raises_when_element_first_doesnt_exists_in_element_context
self.assertEqual(".inner-element-that-dont-exists", element_list.query)
element_list.first

expected_message = 'no elements could be found with css ".inner-element-that-dont-exists"'
expected_message = 'No elements were found with css ".inner-element-that-dont-exists"'

e = cm.exception
self.assertEqual(expected_message, e.args[0])
2 changes: 1 addition & 1 deletion tests/test_element_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_not_found_exception_with_query_and_method():
the_list = ElementList([], find_by="id", query="menu")
the_list.first

expected_message = 'no elements could be found with id "menu"'
expected_message = 'No elements were found with id "menu"'
assert expected_message == str(e.value)


Expand Down

0 comments on commit bc5d582

Please sign in to comment.