Skip to content

Commit 5e2814d

Browse files
authored
fix: Fix issues with typing annotation and mypy (#18)
1 parent 4532182 commit 5e2814d

14 files changed

+142
-128
lines changed

.github/workflows/main.yaml

+11-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
shell: bash -l {0}
5454

5555
steps:
56-
- uses: actions/checkout@v3
56+
- uses: actions/checkout@v4
5757

5858
- uses: conda-incubator/setup-miniconda@v3
5959
with:
@@ -69,9 +69,19 @@ jobs:
6969
run: |
7070
poetry install
7171
72+
- name: Create .env file
73+
env:
74+
NCBI_API_KEY: ${{ secrets.NCBI_API_KEY }}
75+
SERVICE_EMAIL: ${{ secrets.SERVICE_EMAIL }}
76+
run: |
77+
pushd tests
78+
envsubst < .env.tpl > .env
79+
popd
80+
7281
- name: Run tests
7382
env:
7483
NCBI_API_KEY: ${{ secrets.NCBI_API_KEY }}
84+
SERVICE_EMAIL: ${{ secrets.SERVICE_EMAIL }}
7585
run: makim tests.unittest
7686

7787
linter-and-docs:

.pre-commit-config.yaml

+7-7
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ repos:
4646
types:
4747
- python
4848

49-
# - id: mypy
50-
# name: mypy
51-
# entry: mypy .
52-
# language: system
53-
# pass_filenames: false
54-
# types:
55-
# - python
49+
- id: mypy
50+
name: mypy
51+
entry: mypy .
52+
language: system
53+
pass_filenames: false
54+
types:
55+
- python
5656

5757
- id: shellcheck
5858
name: shellcheck

pyproject.toml

+6-7
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,12 @@ quote-style = "double"
108108

109109
[tool.mypy]
110110
python_version = "3.9"
111-
no_strict_optional = false
112-
# check_untyped_defs = true
113-
# strict = true
114-
# ignore_missing_imports = true
115-
# warn_unused_ignores = true
116-
# warn_redundant_casts = true
117-
# warn_unused_configs = true
111+
check_untyped_defs = true
112+
strict = true
113+
ignore_missing_imports = true
114+
warn_unused_ignores = true
115+
warn_redundant_casts = true
116+
warn_unused_configs = true
118117
exclude = [
119118
'^docs/$',
120119
'^examples/$',

src/pymedx/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .api import PubMed, PubMedCentral
77

88

9-
def get_version():
9+
def get_version() -> str:
1010
"""Return the program version."""
1111
try:
1212
return importlib_metadata.version(__name__)

src/pymedx/api.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
import time
99

1010
from copy import copy
11-
from typing import Any, Dict, Generator, Iterable, Iterator, cast
11+
from typing import Any, Dict, Generator, Iterator, cast
1212

1313
import requests
1414

1515
from lxml import etree as xml
1616
from typeguard import typechecked
1717

18-
from .article import PubMedArticle, PubMedCentralArticle
19-
from .book import PubMedBookArticle
20-
from .helpers import (
18+
from pymedx.article import PubMedArticle, PubMedCentralArticle
19+
from pymedx.book import PubMedBookArticle
20+
from pymedx.helpers import (
2121
arrange_query,
2222
batches,
2323
get_range_date_from_query,
@@ -248,7 +248,7 @@ def _get(
248248

249249
# Return the response
250250
if output == "json":
251-
return response.json()
251+
return cast(Dict[str, Any], response.json())
252252
else:
253253
return response.text
254254

@@ -262,7 +262,10 @@ def _get(
262262
)
263263

264264
def _getArticleIdsMonth(
265-
self, search_term, range_begin_date, range_end_date
265+
self,
266+
search_term: str,
267+
range_begin_date: datetime.date,
268+
range_end_date: datetime.date,
266269
) -> list[str]:
267270
article_ids = []
268271
range_dates_month = get_range_months(range_begin_date, range_end_date)
@@ -500,7 +503,7 @@ def query(
500503
self,
501504
query: str,
502505
max_results: int = 100,
503-
) -> Iterable[PubMedArticle | PubMedBookArticle | PubMedCentralArticle]:
506+
) -> Iterator[PubMedArticle | PubMedBookArticle | PubMedCentralArticle]:
504507
"""
505508
Execute a query agains the GraphQL schema.
506509
@@ -631,16 +634,20 @@ def _getArticleIds(
631634

632635
def _getArticles(
633636
self, article_ids: list[str]
634-
) -> Iterable[PubMedArticle | PubMedBookArticle | PubMedCentralArticle]:
637+
) -> Generator[
638+
PubMedArticle | PubMedBookArticle | PubMedCentralArticle, None, None
639+
]:
635640
"""Batch a list of article IDs and retrieves the content.
636641
637642
Parameters
638643
----------
639-
- article_ids List, article IDs.
644+
article_ids: List
645+
article IDs.
640646
641647
Returns
642648
-------
643-
- articles List, article objects.
649+
articles: List
650+
article objects.
644651
"""
645652
# Get the default parameters
646653
parameters = self.parameters.copy()

src/pymedx/article.py

+31-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import datetime
66
import json
77

8-
from typing import Any, Dict, List, Optional, Union, cast
8+
from typing import Any, Dict, List, Optional
99

1010
from lxml.etree import _Element
1111
from typeguard import typechecked
@@ -52,11 +52,11 @@ def __init__(
5252
for field in self.__slots__:
5353
self.__setattr__(field, kwargs.get(field, None))
5454

55-
def _extractPubMedId(self, xml_element: _Element) -> Union[str, None, int]:
55+
def _extractPubMedId(self, xml_element: _Element) -> Optional[str]:
5656
path = ".//PMID"
5757
return getContentUnique(element=xml_element, path=path)
5858

59-
def _extractTitle(self, xml_element: _Element) -> Union[str, None, int]:
59+
def _extractTitle(self, xml_element: _Element) -> Optional[str]:
6060
path = ".//ArticleTitle"
6161
return getAllContent(element=xml_element, path=path)
6262

@@ -68,35 +68,32 @@ def _extractKeywords(self, xml_element: _Element) -> List[Any]:
6868
if keyword is not None
6969
]
7070

71-
def _extractJournal(self, xml_element: _Element) -> Union[str, None, int]:
71+
def _extractJournal(self, xml_element: _Element) -> Optional[str]:
7272
path = ".//Journal/Title"
7373
return getContent(element=xml_element, path=path)
7474

75-
def _extractAbstract(self, xml_element: _Element) -> Union[str, None, int]:
75+
def _extractAbstract(self, xml_element: _Element) -> Optional[str]:
7676
path = ".//AbstractText"
77-
return getAbstract(element=xml_element, path=path)
77+
abstract = getAbstract(element=xml_element, path=path)
78+
return abstract
7879

79-
def _extractConclusions(
80-
self, xml_element: _Element
81-
) -> Union[str, None, int]:
80+
def _extractConclusions(self, xml_element: _Element) -> Optional[str]:
8281
path = ".//AbstractText[@Label='CONCLUSION']"
8382
return getContent(element=xml_element, path=path)
8483

85-
def _extractMethods(self, xml_element: _Element) -> Union[str, None, int]:
84+
def _extractMethods(self, xml_element: _Element) -> Optional[str]:
8685
path = ".//AbstractText[@Label='METHOD']"
8786
return getContent(element=xml_element, path=path)
8887

89-
def _extractResults(self, xml_element: _Element) -> Union[str, None, int]:
88+
def _extractResults(self, xml_element: _Element) -> Optional[str]:
9089
path = ".//AbstractText[@Label='RESULTS']"
9190
return getContent(element=xml_element, path=path)
9291

93-
def _extractCopyrights(
94-
self, xml_element: _Element
95-
) -> Union[str, None, int]:
92+
def _extractCopyrights(self, xml_element: _Element) -> Optional[str]:
9693
path = ".//CopyrightInformation"
9794
return getContent(element=xml_element, path=path)
9895

99-
def _extractDoi(self, xml_element: _Element) -> Union[str, None, int]:
96+
def _extractDoi(self, xml_element: _Element) -> Optional[str]:
10097
path = ".//ArticleId[@IdType='doi']"
10198
return getContentUnique(element=xml_element, path=path)
10299

@@ -128,7 +125,7 @@ def _extractPublicationDate(
128125

129126
def _extractAuthors(
130127
self, xml_element: _Element
131-
) -> List[dict[str, Union[str, None, int]]]:
128+
) -> List[dict[str, Optional[str]]]:
132129
return [
133130
{
134131
"lastname": getContent(author, ".//LastName", None),
@@ -145,15 +142,15 @@ def _initializeFromXML(self, xml_element: _Element) -> None:
145142
"""Parse an XML element into an article object."""
146143
# Parse the different fields of the article
147144
self.pubmed_id = self._extractPubMedId(xml_element)
148-
self.title = cast(str, self._extractTitle(xml_element))
145+
self.title = self._extractTitle(xml_element)
149146
self.keywords = self._extractKeywords(xml_element)
150-
self.journal = cast(str, self._extractJournal(xml_element))
151-
self.abstract = cast(str, self._extractAbstract(xml_element) or "")
152-
self.conclusions = cast(str, self._extractConclusions(xml_element))
153-
self.methods = cast(str, self._extractMethods(xml_element))
154-
self.results = cast(str, self._extractResults(xml_element))
155-
self.copyrights = cast(str, self._extractCopyrights(xml_element))
156-
self.doi = cast(str, self._extractDoi(xml_element))
147+
self.journal = self._extractJournal(xml_element)
148+
self.abstract = self._extractAbstract(xml_element) or ""
149+
self.conclusions = self._extractConclusions(xml_element)
150+
self.methods = self._extractMethods(xml_element)
151+
self.results = self._extractResults(xml_element)
152+
self.copyrights = self._extractCopyrights(xml_element)
153+
self.doi = self._extractDoi(xml_element)
157154
self.publication_date = self._extractPublicationDate(xml_element)
158155
self.authors = self._extractAuthors(xml_element)
159156
self.xml = xml_element
@@ -230,11 +227,11 @@ def __init__(
230227
for field in self.__slots__:
231228
self.__setattr__(field, kwargs.get(field, None))
232229

233-
def _extractPMCId(self, xml_element: _Element) -> Union[str, None, int]:
230+
def _extractPMCId(self, xml_element: _Element) -> Optional[str]:
234231
path = ".//article-meta/article-id[@pub-id-type='pmc']"
235232
return getContentUnique(element=xml_element, path=path)
236233

237-
def _extractTitle(self, xml_element: _Element) -> Union[str, None, int]:
234+
def _extractTitle(self, xml_element: _Element) -> Optional[str]:
238235
path = ".//title-group"
239236
return getAllContent(element=xml_element, path=path)
240237

@@ -248,38 +245,38 @@ def _extractTitle(self, xml_element: _Element) -> Union[str, None, int]:
248245
# ]
249246
# TODO: adapt the function for PubMed Central
250247
# def _extractJournal
251-
# (self, xml_element: _Element) -> Union[str, None, int]:
248+
# (self, xml_element: _Element) -> Optional[str]:
252249
# path = ".//Journal/Title"
253250
# return getContent(element=xml_element, path=path)
254251

255-
def _extractAbstract(self, xml_element: _Element) -> Union[str, None, int]:
252+
def _extractAbstract(self, xml_element: _Element) -> Optional[str]:
256253
path = ".//abstract"
257254
return getAllContent(element=xml_element, path=path)
258255

259256
# TODO: adapt the function for PubMed Central
260257
# def _extractConclusions(
261258
# self, xml_element: _Element
262-
# ) -> Union[str, None, int]:
259+
# ) -> Optional[str]:
263260
# path = ".//AbstractText[@Label='CONCLUSION']"
264261
# return getContent(element=xml_element, path=path)
265262
# TODO: adapt the function for PubMed Central
266263
# def _extractMethods(self, xml_element: _Element)
267-
# -> Union[str, None, int]:
264+
# -> Optional[str]:
268265
# path = ".//AbstractText[@Label='METHOD']"
269266
# return getContent(element=xml_element, path=path)
270267
# TODO: adapt the function for PubMed Central
271268
# def _extractResults(self, xml_element: _Element)
272-
# -> Union[str, None, int]:
269+
# -> Optional[str]:
273270
# path = ".//AbstractText[@Label='RESULTS']"
274271
# return getContent(element=xml_element, path=path)
275272
# TODO: adapt the function for PubMed Central
276273
# def _extractCopyrights(
277274
# self, xml_element: _Element
278-
# ) -> Union[str, None, int]:
275+
# ) -> Optional[str]:
279276
# path = ".//CopyrightInformation"
280277
# return getContent(element=xml_element, path=path)
281278

282-
def _extractDoi(self, xml_element: _Element) -> Union[str, None, int]:
279+
def _extractDoi(self, xml_element: _Element) -> Optional[str]:
283280
path = ".//article-meta/article-id[@pub-id-type='doi']"
284281
return getContentUnique(element=xml_element, path=path)
285282

@@ -318,7 +315,7 @@ def _extractPublicationDate(
318315

319316
def _extractAuthors(
320317
self, xml_element: _Element
321-
) -> List[dict[str, Union[str, None, int]]]:
318+
) -> List[dict[str, Optional[str]]]:
322319
contrib_group = xml_element.findall(".//contrib-group")
323320
if contrib_group:
324321
return [

0 commit comments

Comments
 (0)