Skip to content

Commit

Permalink
Use Optional type hints where appropriate (#168)
Browse files Browse the repository at this point in the history
+ Belatedly `make format`
+ Convenience: bump version to 2.1.3 for release
  • Loading branch information
lukasschwab authored Jun 25, 2024
1 parent c67e92e commit c993d11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
14 changes: 7 additions & 7 deletions arxiv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from calendar import timegm

from enum import Enum
from typing import Dict, Generator, List
from typing import Dict, Generator, List, Optional

logger = logging.getLogger(__name__)

Expand All @@ -44,11 +44,11 @@ class Result(object):
"""The result's authors."""
summary: str
"""The result abstract."""
comment: str
comment: Optional[str]
"""The authors' comment if present."""
journal_ref: str
journal_ref: Optional[str]
"""A journal reference if present."""
doi: str
doi: Optional[str]
"""A URL for the resolved DOI to an external resource if present."""
primary_category: str
"""
Expand All @@ -62,7 +62,7 @@ class Result(object):
"""
links: List[Link]
"""Up to three URLs associated with this result."""
pdf_url: str
pdf_url: Optional[str]
"""The URL of a PDF version of this result if present among links."""
_raw: feedparser.FeedParserDict
"""
Expand Down Expand Up @@ -294,7 +294,7 @@ class Link(object):

href: str
"""The link's `href` attribute."""
title: str
title: Optional[str]
"""The link's title."""
rel: str
"""The link's relationship to the `Result`."""
Expand Down Expand Up @@ -657,7 +657,7 @@ def __try_parse_feed(

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.2"})
resp = self._session.get(url, headers={"user-agent": "arxiv.py/2.1.3"})
self._last_request_dt = datetime.now()
if resp.status_code != requests.codes.OK:
raise HTTPError(url, try_index, resp.status_code)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

version = "2.1.2"
version = "2.1.3"

with open("README.md", "r") as fh:
long_description = fh.read()
Expand Down
17 changes: 10 additions & 7 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from pytest import approx
from requests import Response


def empty_response(code: int) -> Response:
r = Response()
r.status_code = code
r._content = b''
r._content = b""
return r


class TestClient(unittest.TestCase):
def test_invalid_format_id(self):
with self.assertRaises(arxiv.HTTPError):
Expand Down Expand Up @@ -96,10 +98,11 @@ def test_no_duplicates(self):
self.assertFalse(r.entry_id in ids)
ids.add(r.entry_id)

@patch('requests.Session.get', return_value=empty_response(500))
@patch("requests.Session.get", return_value=empty_response(500))
@patch("time.sleep", return_value=None)
def test_retry(self, mock_sleep, mock_get):
broken_client = arxiv.Client()

def broken_get():
search = arxiv.Search(query="quantum")
return next(broken_client.results(search))
Expand All @@ -115,7 +118,7 @@ def broken_get():
self.assertEqual(e.status, 500)
self.assertEqual(e.retry, broken_client.num_retries)

@patch('requests.Session.get', return_value=empty_response(200))
@patch("requests.Session.get", return_value=empty_response(200))
@patch("time.sleep", return_value=None)
def test_sleep_standard(self, mock_sleep, mock_get):
client = arxiv.Client()
Expand All @@ -129,7 +132,7 @@ def test_sleep_standard(self, mock_sleep, mock_get):
client._parse_feed(url)
mock_sleep.assert_called_once_with(approx(client.delay_seconds, rel=1e-3))

@patch('requests.Session.get', return_value=empty_response(200))
@patch("requests.Session.get", return_value=empty_response(200))
@patch("time.sleep", return_value=None)
def test_sleep_multiple_requests(self, mock_sleep, mock_get):
client = arxiv.Client()
Expand All @@ -143,7 +146,7 @@ def test_sleep_multiple_requests(self, mock_sleep, mock_get):
client._parse_feed(url2)
mock_sleep.assert_called_once_with(approx(client.delay_seconds, rel=1e-3))

@patch('requests.Session.get', return_value=empty_response(200))
@patch("requests.Session.get", return_value=empty_response(200))
@patch("time.sleep", return_value=None)
def test_sleep_elapsed(self, mock_sleep, mock_get):
client = arxiv.Client()
Expand All @@ -158,7 +161,7 @@ def test_sleep_elapsed(self, mock_sleep, mock_get):
client._parse_feed(url)
mock_sleep.assert_not_called()

@patch('requests.Session.get', return_value=empty_response(200))
@patch("requests.Session.get", return_value=empty_response(200))
@patch("time.sleep", return_value=None)
def test_sleep_zero_delay(self, mock_sleep, mock_get):
client = arxiv.Client(delay_seconds=0)
Expand All @@ -167,7 +170,7 @@ def test_sleep_zero_delay(self, mock_sleep, mock_get):
client._parse_feed(url)
mock_sleep.assert_not_called()

@patch('requests.Session.get', return_value=empty_response(500))
@patch("requests.Session.get", return_value=empty_response(500))
@patch("time.sleep", return_value=None)
def test_sleep_between_errors(self, mock_sleep, mock_get):
client = arxiv.Client()
Expand Down

0 comments on commit c993d11

Please sign in to comment.