From 59f07a3698c01d8b6bb36690e6736874b9d8a599 Mon Sep 17 00:00:00 2001 From: Privat33r-dev Date: Sat, 10 Aug 2024 18:36:07 +0200 Subject: [PATCH] Improve typing suggestions for MyPy compliance --- clairvoyance/client.py | 4 ++-- clairvoyance/graphql.py | 14 +++++++------- clairvoyance/oracle.py | 8 ++++---- tests/server/graphql.py | 3 ++- tests/server/unstable.py | 3 ++- tests/system.py | 4 ++-- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/clairvoyance/client.py b/clairvoyance/client.py index a4f02d0..73f8ac6 100644 --- a/clairvoyance/client.py +++ b/clairvoyance/client.py @@ -68,8 +68,8 @@ async def post( return await response.json(content_type=None) except ( - aiohttp.client_exceptions.ClientConnectionError, - aiohttp.client_exceptions.ClientPayloadError, + aiohttp.ClientConnectionError, + aiohttp.ClientPayloadError, asyncio.TimeoutError, json.decoder.JSONDecodeError, ) as e: diff --git a/clairvoyance/graphql.py b/clairvoyance/graphql.py index 9057221..5041d59 100644 --- a/clairvoyance/graphql.py +++ b/clairvoyance/graphql.py @@ -11,10 +11,10 @@ class Schema: def __init__( self, - query_type: str = None, - mutation_type: str = None, - subscription_type: str = None, - schema: Dict[str, Any] = None, + query_type: Optional[str] = None, + mutation_type: Optional[str] = None, + subscription_type: Optional[str] = None, + schema: Optional[Dict[str, Any]] = None, ): if schema: self._schema = { @@ -142,7 +142,7 @@ def get_path_from_root( def get_type_without_fields( self, - ignored: Set[str] = None, + ignored: Optional[Set[str]] = None, ) -> str: """Gets the type without a field.""" ignored = ignored or set() @@ -331,7 +331,7 @@ def __init__( self, name: str, typeref: Optional[TypeRef], - args: List[InputValue] = None, + args: Optional[List[InputValue]] = None, ): if not typeref: raise ValueError(f"Can't create {name} Field from {typeref} TypeRef.") @@ -367,7 +367,7 @@ def __init__( self, name: str = "", kind: str = "", - fields: List[Field] = None, + fields: Optional[List[Field]] = None, ): self.name = name self.kind = kind diff --git a/clairvoyance/oracle.py b/clairvoyance/oracle.py index 3d2c8a6..a09ea46 100644 --- a/clairvoyance/oracle.py +++ b/clairvoyance/oracle.py @@ -422,9 +422,9 @@ async def __probation(document: str) -> Optional[graphql.TypeRef]: error["message"], context, ) - - log().debug(f'get_typeref("{error["message"]}", "{context}") -> {typeref}') - if typeref: + log().debug( + f'get_typeref("{error["message"]}", "{context}") -> {typeref}' + ) return typeref return None @@ -582,7 +582,7 @@ async def explore_field( async def clairvoyance( wordlist: List[str], input_document: str, - input_schema: Dict[str, Any] = None, + input_schema: Optional[Dict[str, Any]] = None, ) -> str: log().debug(f"input_document = {input_document}") diff --git a/tests/server/graphql.py b/tests/server/graphql.py index 70349ae..1cb4cb5 100644 --- a/tests/server/graphql.py +++ b/tests/server/graphql.py @@ -1,5 +1,6 @@ import http.server import json +from typing import Optional class UnstableHTTPRequestHandler(http.server.BaseHTTPRequestHandler): @@ -24,7 +25,7 @@ def log_message(self, format, *args) -> None: # type: ignore[no-untyped-def] # pass -def main(port: int = None) -> None: +def main(port: Optional[int] = None) -> None: port = port or 8081 with http.server.HTTPServer(("", port), UnstableHTTPRequestHandler) as httpd: httpd.serve_forever() diff --git a/tests/server/unstable.py b/tests/server/unstable.py index b0bfd32..4c66b96 100644 --- a/tests/server/unstable.py +++ b/tests/server/unstable.py @@ -1,6 +1,7 @@ import http.server import json import os +from typing import Optional class UnstableHTTPRequestHandler(http.server.BaseHTTPRequestHandler): @@ -23,7 +24,7 @@ def log_message(self, format, *args) -> None: # type: ignore[no-untyped-def] # pass -def main(port: int = None) -> None: +def main(port: Optional[int] = None) -> None: port = port or 8000 with http.server.HTTPServer(("", port), UnstableHTTPRequestHandler) as httpd: httpd.serve_forever() diff --git a/tests/system.py b/tests/system.py index 1037c8d..b71db1e 100644 --- a/tests/system.py +++ b/tests/system.py @@ -45,7 +45,7 @@ def setUpClass(cls) -> None: def query_type(self) -> Any: query_type = self.get_type(self.schema["queryType"]["name"]) if not query_type: - raise Exception("Schema don't contain query type") + raise RuntimeError("Schema don't contain query type") return query_type @@ -56,7 +56,7 @@ def get_type(self, name: str) -> Optional[Dict[str, Any]]: return None - def test_validate_wordlist(self): + def test_validate_wordlist(self) -> None: self.assertIn(b"Removed 1 items from wordlist", self.clairvoyance.stderr) def test_found_root_type_names(self) -> None: