Skip to content

Commit f4aa051

Browse files
committed
add typing information
1 parent 32f0e5e commit f4aa051

4 files changed

Lines changed: 25 additions & 19 deletions

File tree

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def get_version(filename):
5050
include_package_data=True,
5151
keywords='doi',
5252
name='python-doi',
53+
package_data={"doi": ["py.typed"]},
5354
packages=find_packages(where="src"),
5455
package_dir={"": "src"},
5556
url='https://github.com/papis/python-doi',

src/doi/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
import sys
33
import logging
44

5+
from typing import Optional
6+
7+
58
__version__ = '0.1.1'
6-
logger = logging.getLogger("doi")
9+
logger = logging.getLogger("doi") # type: logging.Logger
710

811

9-
def pdf_to_doi(filepath, maxlines=None):
12+
def pdf_to_doi(filepath: str, maxlines: Optional[int] = None) -> Optional[str]:
1013
"""Try to get DOI from a filepath. It looks for a regex in the binary
1114
data and returns the first DOI found, in the hopes that this DOI
1215
is the correct one.
@@ -31,7 +34,7 @@ def pdf_to_doi(filepath, maxlines=None):
3134
return None
3235

3336

34-
def validate_doi(doi):
37+
def validate_doi(doi: str) -> Optional[str]:
3538
"""We check that the DOI can be resolved by
3639
`official means <http://www.doi.org/factsheets/DOIProxy.html>`_. If so, we
3740
return the resolved URL, otherwise, we return ``None`` (which means the
@@ -71,7 +74,7 @@ def validate_doi(doi):
7174
raise ValueError('Something unexpected happened')
7275

7376

74-
def get_clean_doi(doi):
77+
def get_clean_doi(doi: str) -> str:
7578
"""Check if the DOI is actually a URL and in that case just get
7679
the exact DOI.
7780
@@ -87,7 +90,7 @@ def get_clean_doi(doi):
8790
return doi
8891

8992

90-
def find_doi_in_text(text):
93+
def find_doi_in_text(text: str) -> Optional[str]:
9194
"""Try to find a DOI in a text.
9295
9396
:param text: Text in which to look for DOI.
@@ -119,7 +122,7 @@ def find_doi_in_text(text):
119122
return None
120123

121124

122-
def get_real_url_from_doi(doi):
125+
def get_real_url_from_doi(doi: str) -> Optional[str]:
123126
"""Get a URL corresponding to a DOI.
124127
125128
:param doi: Identifier.

src/doi/py.typed

Whitespace-only changes.

tests/test_doi.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
)
1010

1111

12-
def test_valid_version():
12+
def test_valid_version() -> None:
1313
"""Check that the package defines a valid __version__"""
1414
assert parse_version(__version__) >= parse_version("0.1.0")
1515

1616

17-
def test_validate_doi():
17+
def test_validate_doi() -> None:
1818
data = [
1919
('10.1063/1.5081715',
2020
'http://aip.scitation.org/doi/10.1063/1.5081715'),
@@ -29,25 +29,26 @@ def test_validate_doi():
2929
'https://linkinghub.elsevier.com/retrieve/pii/S0009261497040141'),
3030
]
3131
for doi, url in data:
32-
assert(url == validate_doi(doi))
32+
assert url == validate_doi(doi)
3333

3434
for doi in ['', 'asdf']:
3535
try:
3636
validate_doi(doi)
3737
except ValueError as e:
38-
assert(str(e) == 'HTTP 404: DOI not found')
38+
assert str(e) == 'HTTP 404: DOI not found'
3939

40-
def test_get_real_url_from_doi():
40+
41+
def test_get_real_url_from_doi() -> None:
4142
data = [
4243
('10.1016/S0009-2614(97)04014-1',
4344
'https://www.sciencedirect.com/science/'
4445
'article/abs/pii/S0009261497040141'),
4546
]
4647
for doi, url in data:
47-
assert(url == get_real_url_from_doi(doi))
48+
assert url == get_real_url_from_doi(doi)
4849

4950

50-
def test_find_doi_in_line():
51+
def test_find_doi_in_line() -> None:
5152
test_data = [
5253
('http://dx.doi.org/10.1063/1.881498', '10.1063/1.881498'),
5354
('http://dx.doi.org/10.1063%2F1.881498', '10.1063/1.881498'),
@@ -61,8 +62,8 @@ def test_find_doi_in_line():
6162
('/scitation.org/doi/10.1063/1.88149 8?234saf=34', '10.1063/1.88149'),
6263
('/scitation.org/doi/10.1063/1.uniau12?as=234',
6364
'10.1063/1.uniau12'),
64-
('https://doi.org/10.1093/analys/anw053' , '10.1093/analys/anw053'),
65-
('http://.scitation.org/doi/10.1063/1.mart(88)1498?asdfwer' ,
65+
('https://doi.org/10.1093/analys/anw053', '10.1093/analys/anw053'),
66+
('http://.scitation.org/doi/10.1063/1.mart(88)1498?asdfwer',
6667
'10.1063/1.mart(88)1498'),
6768
('@ibook{doi:10.1002/9780470125915.ch2,', '10.1002/9780470125915.ch2'),
6869
('<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1'
@@ -78,10 +79,11 @@ def test_find_doi_in_line():
7879
('doi(10.1038/s41535-018-0103-6;)', '10.1038/s41535-018-0103-6'),
7980
]
8081
for url, doi in test_data:
81-
assert(find_doi_in_text(url) == doi)
82+
assert find_doi_in_text(url) == doi
8283

8384

84-
def test_doi_from_pdf():
85+
def test_doi_from_pdf() -> None:
8586
f = os.path.join(os.path.dirname(__file__), 'resources', 'doc.pdf')
86-
assert(os.path.exists(f))
87-
assert(pdf_to_doi(f) == '10.1103/PhysRevLett.50.1998')
87+
88+
assert os.path.exists(f)
89+
assert pdf_to_doi(f) == '10.1103/PhysRevLett.50.1998'

0 commit comments

Comments
 (0)