diff --git a/mypy.ini b/mypy.ini index 5e2e483..6e76e80 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,4 +1,8 @@ [mypy] +strict = True +warn_return_any = False +show_error_context = True +pretty = True [mypy-docs.source.*] ignore_errors = True diff --git a/pyvulnerabilitylookup/__init__.py b/pyvulnerabilitylookup/__init__.py index 8d3a243..df886a0 100644 --- a/pyvulnerabilitylookup/__init__.py +++ b/pyvulnerabilitylookup/__init__.py @@ -1,11 +1,17 @@ +from __future__ import annotations + import argparse import json import sys +from typing import Any + from .api import PyVulnerabilityLookup +__all__ = ['PyVulnerabilityLookup'] -def main(): + +def main() -> None: parser = argparse.ArgumentParser(description='Query a thing.') parser.add_argument('--url', type=str, required=True, help='URL of the instance.') group = parser.add_mutually_exclusive_group(required=True) @@ -15,6 +21,8 @@ def main(): client = PyVulnerabilityLookup(args.url) + response: bool | dict[str, Any] + if not client.is_up: print(f'Unable to reach {client.root_url}. Is the server up?') sys.exit(1) diff --git a/pyvulnerabilitylookup/api.py b/pyvulnerabilitylookup/api.py index 1bc6003..2d447f6 100644 --- a/pyvulnerabilitylookup/api.py +++ b/pyvulnerabilitylookup/api.py @@ -1,9 +1,10 @@ #!/usr/bin/env python3 -# -*- coding: utf-8 -*- + +from __future__ import annotations from importlib.metadata import version from pathlib import Path -from typing import Dict, Optional, Any +from typing import Any from urllib.parse import urljoin, urlparse import requests @@ -11,8 +12,8 @@ import requests class PyVulnerabilityLookup(): - def __init__(self, root_url: str, useragent: Optional[str]=None, - *, proxies: Optional[Dict[str, str]]=None): + def __init__(self, root_url: str, useragent: str | None=None, + *, proxies: dict[str, str] | None=None) -> None: '''Query a specific instance. :param root_url: URL of the instance to query. @@ -39,11 +40,11 @@ class PyVulnerabilityLookup(): return False return r.status_code == 200 - def redis_up(self) -> Dict: + def redis_up(self) -> bool: '''Check if redis is up and running''' r = self.session.get(urljoin(self.root_url, 'redis_up')) return r.json() - def get_vulnerability(self, vulnerability_id: str) -> Dict[str, Any]: + def get_vulnerability(self, vulnerability_id: str) -> dict[str, Any]: r = self.session.get(urljoin(self.root_url, str(Path('vulnerability', vulnerability_id)))) return r.json() diff --git a/tests/test_web.py b/tests/test_web.py index 1f762fb..6062672 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -# -*- coding: utf-8 -*- import unittest @@ -8,13 +7,13 @@ from pyvulnerabilitylookup import PyVulnerabilityLookup class TestBasic(unittest.TestCase): - def setUp(self): + def setUp(self) -> None: self.client = PyVulnerabilityLookup(root_url="http://127.0.0.1:10001") - def test_up(self): + def test_up(self) -> None: self.assertTrue(self.client.is_up) self.assertTrue(self.client.redis_up()) - def test_get_vulnerability(self): + def test_get_vulnerability(self) -> None: vuln = self.client.get_vulnerability('CVE-2023-23059') self.assertEqual(vuln['cve']['id'], 'CVE-2023-23059')