Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Synapse passing type check #1725

Merged
merged 5 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*] uncomment went mypy passes
; ignore_errors = False

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