From d81e07a842ee4bead30c015e0e4b30addffd57b6 Mon Sep 17 00:00:00 2001 From: Gus Date: Mon, 26 Feb 2024 18:30:13 -0500 Subject: [PATCH 1/2] feature: synapse.py now passing type check --- bittensor/synapse.py | 53 ++++++++++++++++++++++++-------------------- mypy.ini | 4 ++-- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/bittensor/synapse.py b/bittensor/synapse.py index 2d5aacfdb8..a69182e376 100644 --- a/bittensor/synapse.py +++ b/bittensor/synapse.py @@ -73,7 +73,7 @@ def cast_int(raw: str) -> int: int or None: The converted integer, or ``None`` if the input was ``None``. """ - return int(raw) if raw != None else raw + return int(raw) if raw != None else raw # type: ignore def cast_float(raw: str) -> float: @@ -89,7 +89,7 @@ def cast_float(raw: str) -> float: float or None: The converted float, or ``None`` if the input was ``None``. """ - return float(raw) if raw != None else raw + return float(raw) if raw != None else raw # type: ignore class TerminalInfo(pydantic.BaseModel): @@ -399,7 +399,7 @@ def deserialize(self) -> "CustomSynapse": @pydantic.root_validator(pre=True) def set_name_type(cls, values) -> dict: - values["name"] = cls.__name__ + values["name"] = cls.__name__ # type: ignore return values # Defines the http route name which is set on axon.attach( callable( request: RequestName )) @@ -526,7 +526,7 @@ def is_success(self) -> bool: Returns: bool: ``True`` if dendrite's status code is ``200``, ``False`` otherwise. """ - return self.dendrite.status_code == 200 + return self.dendrite is not None and self.dendrite.status_code == 200 @property def is_failure(self) -> bool: @@ -539,7 +539,7 @@ def is_failure(self) -> bool: Returns: bool: ``True`` if dendrite's status code is not ``200``, ``False`` otherwise. """ - return self.dendrite.status_code != 200 + return self.dendrite is not None and self.dendrite.status_code != 200 @property def is_timeout(self) -> bool: @@ -552,7 +552,7 @@ def is_timeout(self) -> bool: Returns: bool: ``True`` if dendrite's status code is ``408``, ``False`` otherwise. """ - return self.dendrite.status_code == 408 + return self.dendrite is not None and self.dendrite.status_code == 408 @property def is_blacklist(self) -> bool: @@ -565,7 +565,7 @@ def is_blacklist(self) -> bool: Returns: bool: ``True`` if dendrite's status code is ``403``, ``False`` otherwise. """ - return self.dendrite.status_code == 403 + return self.dendrite is not None and self.dendrite.status_code == 403 @property def failed_verification(self) -> bool: @@ -578,7 +578,7 @@ def failed_verification(self) -> bool: Returns: bool: ``True`` if dendrite's status code is ``401``, ``False`` otherwise. """ - return self.dendrite.status_code == 401 + return self.dendrite is not None and self.dendrite.status_code == 401 def to_headers(self) -> dict: """ @@ -608,20 +608,22 @@ def to_headers(self) -> dict: headers = {"name": self.name, "timeout": str(self.timeout)} # Adding headers for 'axon' and 'dendrite' if they are not None - headers.update( - { - f"bt_header_axon_{k}": str(v) - for k, v in self.axon.dict().items() - if v is not None - } - ) - headers.update( - { - f"bt_header_dendrite_{k}": str(v) - for k, v in self.dendrite.dict().items() - if v is not None - } - ) + if self.axon: + headers.update( + { + f"bt_header_axon_{k}": str(v) + for k, v in self.axon.dict().items() + if v is not None + } + ) + if self.dendrite: + headers.update( + { + f"bt_header_dendrite_{k}": str(v) + for k, v in self.dendrite.dict().items() + if v is not None + } + ) # Getting the fields of the instance instance_fields = self.dict() @@ -689,7 +691,10 @@ def body_hash(self) -> str: for field, value in instance_fields.items(): # If the field is required in the subclass schema, hash and add it. - if field in self.required_hash_fields: + if ( + self.required_hash_fields is not None + and field in self.required_hash_fields + ): hashes.append(bittensor.utils.hash(str(value))) # Hash and return the hashes that have been concatenated @@ -730,7 +735,7 @@ def parse_headers_to_inputs(cls, headers: dict) -> dict: """ # Initialize the input dictionary with empty sub-dictionaries for 'axon' and 'dendrite' - inputs_dict = {"axon": {}, "dendrite": {}} + inputs_dict: dict[str, dict[str, str]] = {"axon": {}, "dendrite": {}} # Iterate over each item in the headers for key, value in headers.items(): diff --git a/mypy.ini b/mypy.ini index 98ff5c0ed1..1846e77a13 100644 --- a/mypy.ini +++ b/mypy.ini @@ -14,5 +14,5 @@ ignore_errors = False ; [mypy-*.subtensor.*] uncomment went mypy passes ; ignore_errors = False -; [mypy-*.synapse.*] uncomment went mypy passes -; ignore_errors = False +[mypy-*.synapse.*] +ignore_errors = False From fb63c8e888d45905de91a888df3d35cdfa528cdf Mon Sep 17 00:00:00 2001 From: Gus Date: Tue, 27 Feb 2024 12:03:50 -0500 Subject: [PATCH 2/2] dict -> Dict, fixes error: "dict" is not subscriptable, use "typing.Dict" instead --- bittensor/synapse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bittensor/synapse.py b/bittensor/synapse.py index a69182e376..d4f210670e 100644 --- a/bittensor/synapse.py +++ b/bittensor/synapse.py @@ -24,7 +24,7 @@ import pydantic from pydantic.schema import schema import bittensor -from typing import Optional, List, Any +from typing import Optional, List, Any, Dict def get_size(obj, seen=None) -> int: @@ -735,7 +735,7 @@ def parse_headers_to_inputs(cls, headers: dict) -> dict: """ # Initialize the input dictionary with empty sub-dictionaries for 'axon' and 'dendrite' - inputs_dict: dict[str, dict[str, str]] = {"axon": {}, "dendrite": {}} + inputs_dict: Dict[str, Dict[str, str]] = {"axon": {}, "dendrite": {}} # Iterate over each item in the headers for key, value in headers.items():