chg: More new annotations

This commit is contained in:
Raphaël Vinot 2024-01-17 12:41:24 +01:00
parent 6736ede6be
commit f278bccfb5
4 changed files with 23 additions and 11 deletions

View file

@ -1,4 +1,8 @@
[mypy] [mypy]
strict = True
warn_return_any = False
show_error_context = True
pretty = True
[mypy-docs.source.*] [mypy-docs.source.*]
ignore_errors = True ignore_errors = True

View file

@ -1,11 +1,17 @@
from __future__ import annotations
import argparse import argparse
import json import json
import sys import sys
from typing import Any
from .api import PyVulnerabilityLookup from .api import PyVulnerabilityLookup
__all__ = ['PyVulnerabilityLookup']
def main():
def main() -> None:
parser = argparse.ArgumentParser(description='Query a thing.') parser = argparse.ArgumentParser(description='Query a thing.')
parser.add_argument('--url', type=str, required=True, help='URL of the instance.') parser.add_argument('--url', type=str, required=True, help='URL of the instance.')
group = parser.add_mutually_exclusive_group(required=True) group = parser.add_mutually_exclusive_group(required=True)
@ -15,6 +21,8 @@ def main():
client = PyVulnerabilityLookup(args.url) client = PyVulnerabilityLookup(args.url)
response: bool | dict[str, Any]
if not client.is_up: if not client.is_up:
print(f'Unable to reach {client.root_url}. Is the server up?') print(f'Unable to reach {client.root_url}. Is the server up?')
sys.exit(1) sys.exit(1)

View file

@ -1,9 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
from importlib.metadata import version from importlib.metadata import version
from pathlib import Path from pathlib import Path
from typing import Dict, Optional, Any from typing import Any
from urllib.parse import urljoin, urlparse from urllib.parse import urljoin, urlparse
import requests import requests
@ -11,8 +12,8 @@ import requests
class PyVulnerabilityLookup(): class PyVulnerabilityLookup():
def __init__(self, root_url: str, useragent: Optional[str]=None, def __init__(self, root_url: str, useragent: str | None=None,
*, proxies: Optional[Dict[str, str]]=None): *, proxies: dict[str, str] | None=None) -> None:
'''Query a specific instance. '''Query a specific instance.
:param root_url: URL of the instance to query. :param root_url: URL of the instance to query.
@ -39,11 +40,11 @@ class PyVulnerabilityLookup():
return False return False
return r.status_code == 200 return r.status_code == 200
def redis_up(self) -> Dict: def redis_up(self) -> bool:
'''Check if redis is up and running''' '''Check if redis is up and running'''
r = self.session.get(urljoin(self.root_url, 'redis_up')) r = self.session.get(urljoin(self.root_url, 'redis_up'))
return r.json() 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)))) r = self.session.get(urljoin(self.root_url, str(Path('vulnerability', vulnerability_id))))
return r.json() return r.json()

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest import unittest
@ -8,13 +7,13 @@ from pyvulnerabilitylookup import PyVulnerabilityLookup
class TestBasic(unittest.TestCase): class TestBasic(unittest.TestCase):
def setUp(self): def setUp(self) -> None:
self.client = PyVulnerabilityLookup(root_url="http://127.0.0.1:10001") 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.is_up)
self.assertTrue(self.client.redis_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') vuln = self.client.get_vulnerability('CVE-2023-23059')
self.assertEqual(vuln['cve']['id'], 'CVE-2023-23059') self.assertEqual(vuln['cve']['id'], 'CVE-2023-23059')