Skip to content

Commit

Permalink
Merge pull request #1725 from opentensor/feature/gus/51818215/expand-…
Browse files Browse the repository at this point in the history
…type-checking-synapse

Feature: Synapse passing type check
  • Loading branch information
ifrit98 authored Mar 6, 2024
2 parents 0c05882 + fdcab3c commit fed333b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
55 changes: 30 additions & 25 deletions bittensor/synapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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 ))
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
4 changes: 2 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ ignore_errors = False
[mypy-*.subtensor.*]
ignore_errors = False

; [mypy-*.synapse.*] uncomment went mypy passes
; ignore_errors = False
[mypy-*.synapse.*]
ignore_errors = False

0 comments on commit fed333b

Please sign in to comment.