Browse Source

fix type hints; add mypy to test pipeline

https://github.com/fphammerle/switchbot-mqtt/commit/c6bc41e1c3bdb22139ca5b0ac9c3ec952b48544a
Fabian Peter Hammerle 2 years ago
parent
commit
8327983371

+ 1 - 0
.github/workflows/python.yml

@@ -58,6 +58,7 @@ jobs:
     # > tests/resources/__init__.py:1:0: F0010: error while code parsing: Unable to load file tests/resources/__init__.py:
     # > [Errno 2] No such file or directory: 'tests/resources/__init__.py' (parse-error)
     - run: pipenv run pylint --disable=parse-error tests/*
+    - run: pipenv run mypy "$(cat *.egg-info/top_level.txt)" tests
     # >=2.1.0 to support GITHUB_TOKEN
     # COVERALLS_REPO_TOKEN required manual configuration of secret
     # https://github.com/TheKevJames/coveralls-python/commit/f597109b62fadaf900af79d4f08a7debee5229e2

+ 3 - 0
CHANGELOG.md

@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Removed
 - compatibility with `python3.5`
 
+### Fixed
+- return type hint of function `bibtex_entry_from_pmid`
+
 ## [0.1.0] - 2019-05-16
 
 [Unreleased]: https://github.com/fphammerle/pubmed-bibtex/compare/0.1.0...HEAD

+ 4 - 0
Pipfile

@@ -8,15 +8,19 @@ pubmed-bibtex = {editable = true, path = "."}
 
 [dev-packages]
 black = "==21.12b0"
+mypy = "*"
 pylint = "*"
 pytest = "*"
 pytest-cov = "*"
+types-requests = "*"
 
 # python3.6 compatibility
 importlib-metadata = "<4.9"
 platformdirs = "<2.4.1"
 tomli = "<2"
 zipp = "<3.7"
+# mypy on python<3.8
+typed-ast = {markers = "python_version < '3.8'"}
 
 [requires]
 python_version = "3"

+ 68 - 1
Pipfile.lock

@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "f0943efdde478fec1e14549ab744c51ece9b41f685d35e64256aa413345f95ed"
+            "sha256": "f5c7075aa94a8c796667be00d4c6f0f00254d787603f09f96e30b9e3011e70c9"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -211,6 +211,32 @@
             ],
             "version": "==0.6.1"
         },
+        "mypy": {
+            "hashes": [
+                "sha256:0038b21890867793581e4cb0d810829f5fd4441aa75796b53033af3aa30430ce",
+                "sha256:1171f2e0859cfff2d366da2c7092b06130f232c636a3f7301e3feb8b41f6377d",
+                "sha256:1b06268df7eb53a8feea99cbfff77a6e2b205e70bf31743e786678ef87ee8069",
+                "sha256:1b65714dc296a7991000b6ee59a35b3f550e0073411ac9d3202f6516621ba66c",
+                "sha256:1bf752559797c897cdd2c65f7b60c2b6969ffe458417b8d947b8340cc9cec08d",
+                "sha256:300717a07ad09525401a508ef5d105e6b56646f7942eb92715a1c8d610149714",
+                "sha256:3c5b42d0815e15518b1f0990cff7a705805961613e701db60387e6fb663fe78a",
+                "sha256:4365c60266b95a3f216a3047f1d8e3f895da6c7402e9e1ddfab96393122cc58d",
+                "sha256:50c7346a46dc76a4ed88f3277d4959de8a2bd0a0fa47fa87a4cde36fe247ac05",
+                "sha256:5b56154f8c09427bae082b32275a21f500b24d93c88d69a5e82f3978018a0266",
+                "sha256:74f7eccbfd436abe9c352ad9fb65872cc0f1f0a868e9d9c44db0893440f0c697",
+                "sha256:7b3f6f557ba4afc7f2ce6d3215d5db279bcf120b3cfd0add20a5d4f4abdae5bc",
+                "sha256:8c11003aaeaf7cc2d0f1bc101c1cc9454ec4cc9cb825aef3cafff8a5fdf4c799",
+                "sha256:8ca7f8c4b1584d63c9a0f827c37ba7a47226c19a23a753d52e5b5eddb201afcd",
+                "sha256:c89702cac5b302f0c5d33b172d2b55b5df2bede3344a2fbed99ff96bddb2cf00",
+                "sha256:d8f1ff62f7a879c9fe5917b3f9eb93a79b78aad47b533911b853a757223f72e7",
+                "sha256:d9d2b84b2007cea426e327d2483238f040c49405a6bf4074f605f0156c91a47a",
+                "sha256:e839191b8da5b4e5d805f940537efcaa13ea5dd98418f06dc585d2891d228cf0",
+                "sha256:f9fe20d0872b26c4bba1c1be02c5340de1019530302cf2dcc85c7f9fc3252ae0",
+                "sha256:ff3bf387c14c805ab1388185dd22d6b210824e164d4bb324b195ff34e322d166"
+            ],
+            "index": "pypi",
+            "version": "==0.931"
+        },
         "mypy-extensions": {
             "hashes": [
                 "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d",
@@ -300,6 +326,47 @@
             "index": "pypi",
             "version": "==1.2.3"
         },
+        "typed-ast": {
+            "hashes": [
+                "sha256:24058827d8f5d633f97223f5148a7d22628099a3d2efe06654ce872f46f07cdb",
+                "sha256:256115a5bc7ea9e665c6314ed6671ee2c08ca380f9d5f130bd4d2c1f5848d695",
+                "sha256:38cf5c642fa808300bae1281460d4f9b7617cf864d4e383054a5ef336e344d32",
+                "sha256:484137cab8ecf47e137260daa20bafbba5f4e3ec7fda1c1e69ab299b75fa81c5",
+                "sha256:4f30a2bcd8e68adbb791ce1567fdb897357506f7ea6716f6bbdd3053ac4d9471",
+                "sha256:591bc04e507595887160ed7aa8d6785867fb86c5793911be79ccede61ae96f4d",
+                "sha256:5b6ab14c56bc9c7e3c30228a0a0b54b915b1579613f6e463ba6f4eb1382e7fd4",
+                "sha256:5d8314c92414ce7481eee7ad42b353943679cf6f30237b5ecbf7d835519e1212",
+                "sha256:71dcda943a471d826ea930dd449ac7e76db7be778fcd722deb63642bab32ea3f",
+                "sha256:7c42707ab981b6cf4b73490c16e9d17fcd5227039720ca14abe415d39a173a30",
+                "sha256:9caaf2b440efb39ecbc45e2fabde809cbe56272719131a6318fd9bf08b58e2cb",
+                "sha256:a2b8d7007f6280e36fa42652df47087ac7b0a7d7f09f9468f07792ba646aac2d",
+                "sha256:a6d495c1ef572519a7bac9534dbf6d94c40e5b6a608ef41136133377bba4aa08",
+                "sha256:a80d84f535642420dd17e16ae25bb46c7f4c16ee231105e7f3eb43976a89670a",
+                "sha256:b53ae5de5500529c76225d18eeb060efbcec90ad5e030713fe8dab0fb4531631",
+                "sha256:b6d17f37f6edd879141e64a5db17b67488cfeffeedad8c5cec0392305e9bc775",
+                "sha256:c9bcad65d66d594bffab8575f39420fe0ee96f66e23c4d927ebb4e24354ec1af",
+                "sha256:ca9e8300d8ba0b66d140820cf463438c8e7b4cdc6fd710c059bfcfb1531d03fb",
+                "sha256:de4ecae89c7d8b56169473e08f6bfd2df7f95015591f43126e4ea7865928677e"
+            ],
+            "index": "pypi",
+            "markers": "python_version < '3.8'",
+            "version": "==1.5.1"
+        },
+        "types-requests": {
+            "hashes": [
+                "sha256:2e0e100dd489f83870d4f61949d3a7eae4821e7bfbf46c57e463c38f92d473d4",
+                "sha256:f38bd488528cdcbce5b01dc953972f3cead0d060cfd9ee35b363066c25bab13c"
+            ],
+            "index": "pypi",
+            "version": "==2.27.7"
+        },
+        "types-urllib3": {
+            "hashes": [
+                "sha256:3adcf2cb5981809091dbff456e6999fe55f201652d8c360f99997de5ac2f556e",
+                "sha256:cfd1fbbe4ba9a605ed148294008aac8a7b8b7472651d1cc357d507ae5962e3d2"
+            ],
+            "version": "==1.26.7"
+        },
         "typing-extensions": {
             "hashes": [
                 "sha256:4ca091dea149f945ec56afb48dae714f21e8692ef22a395223bcd328961b6a0e",

+ 2 - 0
mypy.ini

@@ -0,0 +1,2 @@
+[mypy]
+ignore_missing_imports = True

+ 5 - 3
pubmed_bibtex/__init__.py

@@ -44,13 +44,15 @@ import requests
 
 from pubmed_bibtex.version import __version__
 
+__all__ = ["__version__", "bibtex_entry_from_pmid"]
+
 _TEXMED_URL_PATTERN = (
     "https://www.bioinformatics.org/texmed/cgi-bin/list.cgi?PMID={pmid}&linkOut"
 )
 
 
 class _TeXMedHtmlParser(html.parser.HTMLParser):
-    def __init__(self):
+    def __init__(self) -> None:
         self.bibtex_entry: typing.Optional[str] = None
         super().__init__()
 
@@ -62,11 +64,11 @@ class _TeXMedHtmlParser(html.parser.HTMLParser):
         if "Author" in data:
             self.bibtex_entry = self._strip_bibtex_entry(data)
 
-    def error(self, message) -> None:
+    def error(self, message: str) -> None:
         raise Exception(message)  # pragma: no cover
 
 
-def bibtex_entry_from_pmid(pmid: str) -> str:
+def bibtex_entry_from_pmid(pmid: str) -> typing.Optional[str]:
     assert pmid.isdigit(), pmid
     resp = requests.get(_TEXMED_URL_PATTERN.format(pmid=pmid))
     resp.raise_for_status()

+ 2 - 2
pubmed_bibtex/__main__.py

@@ -23,7 +23,7 @@ import pubmed_bibtex
 __version__ = pubmed_bibtex.__version__
 
 
-def main():
+def _main() -> None:
     argparser = argparse.ArgumentParser(
         description=pubmed_bibtex.__doc__.strip(),
         formatter_class=argparse.RawDescriptionHelpFormatter,
@@ -37,4 +37,4 @@ def main():
 
 
 if __name__ == "__main__":
-    main()
+    _main()

+ 1 - 1
setup.py

@@ -46,7 +46,7 @@ setuptools.setup(
     packages=setuptools.find_packages(),
     entry_points={
         "console_scripts": [
-            "pubmed-bibtex = pubmed_bibtex.__main__:main",
+            "pubmed-bibtex = pubmed_bibtex.__main__:_main",
         ],
     },
     # >=3.6 for variable type hints

+ 2 - 2
tests/test_bibtex_entry_from_pmid.py

@@ -1,8 +1,8 @@
 from pubmed_bibtex import bibtex_entry_from_pmid
 
 # pylint: disable=wrong-import-order; false positive
-from conftest import TEST_PMID, TEST_BIBTEX_ENTRY
+from conftest import TEST_BIBTEX_ENTRY, TEST_PMID
 
 
-def test_bibtex_entry_from_pmid():
+def test_bibtex_entry_from_pmid() -> None:
     assert bibtex_entry_from_pmid(pmid=TEST_PMID) == TEST_BIBTEX_ENTRY

+ 9 - 7
tests/test_main.py

@@ -1,22 +1,24 @@
 import subprocess
 import unittest.mock
 
+import _pytest.capture
+
 import pubmed_bibtex
-from pubmed_bibtex.__main__ import main
+from pubmed_bibtex.__main__ import _main
 
 # pylint: disable=wrong-import-order; false positive
-from conftest import TEST_PMID, TEST_BIBTEX_ENTRY
+from conftest import TEST_BIBTEX_ENTRY, TEST_PMID
 
 
-def test_main(capsys):
+def test__main(capsys: _pytest.capture.CaptureFixture) -> None:
     with unittest.mock.patch("sys.argv", ["", TEST_PMID]):
-        main()
+        _main()
     out, err = capsys.readouterr()
     assert not err
     assert out == TEST_BIBTEX_ENTRY
 
 
-def test_script_module():
+def test_script_module() -> None:
     proc_info = subprocess.run(
         ["python", "-m", "pubmed_bibtex", TEST_PMID],
         check=True,
@@ -27,7 +29,7 @@ def test_script_module():
     assert proc_info.stdout == TEST_BIBTEX_ENTRY.encode()
 
 
-def test_script():
+def test_script() -> None:
     proc_info = subprocess.run(
         ["pubmed-bibtex", TEST_PMID],
         check=True,
@@ -38,7 +40,7 @@ def test_script():
     assert proc_info.stdout == TEST_BIBTEX_ENTRY.encode()
 
 
-def test_version():
+def test_version() -> None:
     proc_info = subprocess.run(
         ["pubmed-bibtex", "--version"],
         check=True,

+ 1 - 1
tests/test_version.py

@@ -3,5 +3,5 @@ import re
 import pubmed_bibtex
 
 
-def test_version():
+def test_version() -> None:
     assert re.match(r"^\d+\.\d+\.", pubmed_bibtex.__version__)