Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use f-strings and add more type hints in ElementList() #1239

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading