Skip to content

Commit

Permalink
Format with ruff (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasschwab authored Oct 25, 2023
1 parent 53d59a2 commit ece3022
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 58 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check format with black
uses: psf/black@stable
- name: Check format with ruff
uses: chartboost/ruff-action@v1
with:
options: "--check --verbose"
src: "."
args: --verbose
lint:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tests := ${wildcard tests/*.py}
all: lint test docs

format: $(source) $(tests)
black .
ruff format .

lint: $(source) $(tests)
ruff check .
Expand Down
28 changes: 7 additions & 21 deletions arxiv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ def _from_feed_entry(entry: feedparser.FeedParserDict) -> Result:
if hasattr(entry, "title"):
title = entry.title
else:
logger.warning(
"Result %s is missing title attribute; defaulting to '0'", entry.id
)
logger.warning("Result %s is missing title attribute; defaulting to '0'", entry.id)
return Result(
entry_id=entry.id,
updated=Result._to_datetime(entry.updated_parsed),
Expand Down Expand Up @@ -459,9 +457,7 @@ def __str__(self) -> str:
return repr(self)

def __repr__(self) -> str:
return (
"{}(query={}, id_list={}, max_results={}, sort_by={}, " "sort_order={})"
).format(
return ("{}(query={}, id_list={}, max_results={}, sort_by={}, " "sort_order={})").format(
_classname(self),
repr(self.query),
repr(self.id_list),
Expand Down Expand Up @@ -531,9 +527,7 @@ class Client(object):
_last_request_dt: datetime
_session: requests.Session

def __init__(
self, page_size: int = 100, delay_seconds: float = 3.0, num_retries: int = 3
):
def __init__(self, page_size: int = 100, delay_seconds: float = 3.0, num_retries: int = 3):
"""
Constructs an arXiv API client with the specified options.
Expand Down Expand Up @@ -580,9 +574,7 @@ def results(self, search: Search, offset: int = 0) -> Generator[Result, None, No
return iter(())
return itertools.islice(self._results(search, offset), limit)

def _results(
self, search: Search, offset: int = 0
) -> Generator[Result, None, None]:
def _results(self, search: Search, offset: int = 0) -> Generator[Result, None, None]:
page_url = self._format_url(search, offset, self.page_size)
feed = self._parse_feed(page_url, first_page=True)
if not feed.entries:
Expand Down Expand Up @@ -631,19 +623,15 @@ def _parse_feed(
`self.num_retries` times.
"""
try:
return self.__try_parse_feed(
url, first_page=first_page, try_index=_try_index
)
return self.__try_parse_feed(url, first_page=first_page, try_index=_try_index)
except (
HTTPError,
UnexpectedEmptyPageError,
requests.exceptions.ConnectionError,
) as err:
if _try_index < self.num_retries:
logger.debug("Got error (try %d): %s", _try_index, err)
return self._parse_feed(
url, first_page=first_page, _try_index=_try_index + 1
)
return self._parse_feed(url, first_page=first_page, _try_index=_try_index + 1)
logger.debug("Giving up (try %d): %s", _try_index, err)
raise err

Expand All @@ -667,9 +655,7 @@ def __try_parse_feed(
logger.info("Sleeping: %f seconds", to_sleep)
time.sleep(to_sleep)

logger.info(
"Requesting page (first: %r, try: %d): %s", first_page, try_index, url
)
logger.info("Requesting page (first: %r, try: %d): %s", first_page, try_index, url)

resp = self._session.get(url, headers={"user-agent": "arxiv.py/2.1.0"})
self._last_request_dt = datetime.now()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ requests==2.31.0

# Development dependencies
pytest>=6.2.2
ruff>=0.0.261
ruff>=0.1.2
pdoc==13.1.0
pip-audit>=1.1.2
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ignore = []
ignore = ["E501", "W191"]
select = [
"E",
"F",
Expand Down
24 changes: 6 additions & 18 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def test_nonexistent_id_in_list(self):
results = list(client.results(arxiv.Search(id_list=["0808.05394"])))
self.assertEqual(len(results), 0)
# Generator should still yield valid entries.
results = list(
client.results(arxiv.Search(id_list=["0808.05394", "1707.08567"]))
)
results = list(client.results(arxiv.Search(id_list=["0808.05394", "1707.08567"])))
self.assertEqual(len(results), 1)

def test_max_results(self):
Expand Down Expand Up @@ -83,9 +81,7 @@ def test_search_results_offset(self):
client_results = list(client.results(search, offset=offset))
self.assertEqual(len(client_results), max(0, search.max_results - offset))
if client_results:
self.assertEqual(
all_results[offset].entry_id, client_results[0].entry_id
)
self.assertEqual(all_results[offset].entry_id, client_results[0].entry_id)

def test_no_duplicates(self):
search = arxiv.Search("testing", max_results=100)
Expand Down Expand Up @@ -124,9 +120,7 @@ def test_sleep_standard(self, patched_time_sleep):
# environments will have different page fetch times.
client._last_request_dt = datetime.now()
client._parse_feed(url)
patched_time_sleep.assert_called_once_with(
approx(client.delay_seconds, rel=1e-3)
)
patched_time_sleep.assert_called_once_with(approx(client.delay_seconds, rel=1e-3))

@patch("time.sleep", return_value=None)
def test_sleep_multiple_requests(self, patched_time_sleep):
Expand All @@ -139,25 +133,19 @@ def test_sleep_multiple_requests(self, patched_time_sleep):
patched_time_sleep.assert_not_called()
client._last_request_dt = datetime.now()
client._parse_feed(url2)
patched_time_sleep.assert_called_once_with(
approx(client.delay_seconds, rel=1e-3)
)
patched_time_sleep.assert_called_once_with(approx(client.delay_seconds, rel=1e-3))

@patch("time.sleep", return_value=None)
def test_sleep_elapsed(self, patched_time_sleep):
client = TestClient.get_code_client(200)
url = client._format_url(arxiv.Search(query="quantum"), 0, 1)
# If _last_request_dt is less than delay_seconds ago, sleep.
client._last_request_dt = datetime.now() - timedelta(
seconds=client.delay_seconds - 1
)
client._last_request_dt = datetime.now() - timedelta(seconds=client.delay_seconds - 1)
client._parse_feed(url)
patched_time_sleep.assert_called_once()
patched_time_sleep.reset_mock()
# If _last_request_dt is at least delay_seconds ago, don't sleep.
client._last_request_dt = datetime.now() - timedelta(
seconds=client.delay_seconds
)
client._last_request_dt = datetime.now() - timedelta(seconds=client.delay_seconds)
client._parse_feed(url)
patched_time_sleep.assert_not_called()

Expand Down
4 changes: 1 addition & 3 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class TestDownload(unittest.TestCase):
@classmethod
def setUpClass(self):
self.fetched_result = next(arxiv.Search(id_list=["1605.08386"]).results())
self.fetched_result_with_slash = next(
arxiv.Search(id_list=["hep-ex/0406020v1"]).results()
)
self.fetched_result_with_slash = next(arxiv.Search(id_list=["hep-ex/0406020v1"]).results())

@classmethod
def setUp(self):
Expand Down
4 changes: 1 addition & 3 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def test_deprecated_import_pattern(self):
import arxiv as nondeprecated

expected = TestPackage.get_public_classes(nondeprecated)
self.assertTrue(
expected, "should export non-empty set of classes; check the helper"
)
self.assertTrue(expected, "should export non-empty set of classes; check the helper")

from arxiv import arxiv as deprecated_from

Expand Down
8 changes: 2 additions & 6 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def test_result_shape(self):
self.assert_valid_result(result)

def test_from_feed_entry(self):
feed = self.client._parse_feed(
"https://export.arxiv.org/api/query?search_query=testing"
)
feed = self.client._parse_feed("https://export.arxiv.org/api/query?search_query=testing")
feed_entry = feed.entries[0]
result = arxiv.Result._from_feed_entry(feed_entry)
self.assert_valid_result(result)
Expand All @@ -66,9 +64,7 @@ def test_to_datetime(self):
# critical to the test that they remain equivalent.
paper_published = "2016-05-26T17:59:46Z"
paper_published_parsed = time.struct_time((2016, 5, 26, 17, 59, 46, 3, 147, 0))
expected = datetime(
2016, 5, 26, hour=17, minute=59, second=46, tzinfo=timezone.utc
)
expected = datetime(2016, 5, 26, hour=17, minute=59, second=46, tzinfo=timezone.utc)
actual = arxiv.Result._to_datetime(paper_published_parsed)
self.assertEqual(actual, expected)
self.assertEqual(actual.strftime("%Y-%m-%dT%H:%M:%SZ"), paper_published)
Expand Down

0 comments on commit ece3022

Please sign in to comment.