Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ jobs:
key: v2-pypi-py-ruff-<< parameters.python-version >>

- run:
name: Ruff format check
name: Ruff linter check
command: |
. .venv/bin/activate
ruff check --diff --unsafe-fixes .

- run:
name: Ruff format
command: |
. .venv/bin/activate
ruff format --diff .
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ Subtensor: Interfaces with bittensor's blockchain and can perform operations lik
```python
import bittensor
# Bittensor's chain interface.
subtensor = bittensor.subtensor()
subtensor = bittensor.Subtensor()
# Get the chain block
subtensor.get_current_block()
# Transfer Tao to a destination address.
Expand Down
9 changes: 4 additions & 5 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from rich.console import Console
from rich.traceback import install

# Install and apply nest asyncio to allow the async functions
# to run in a .ipynb
import nest_asyncio
import nest_asyncio # noqa: F401

nest_asyncio.apply()

Expand All @@ -37,6 +34,9 @@
)
__new_signature_version__ = 360

from rich.console import Console # noqa: F401, E402
from rich.traceback import install # noqa: F401, E402

# Rich console.
__console__ = Console()
__use_console__ = True
Expand Down Expand Up @@ -261,7 +261,6 @@ def debug(on: bool = True):
strtobool,
strtobool_with_default,
get_explorer_root_url_by_network_from_map,
get_explorer_root_url_by_network_from_map,
get_explorer_url_for_network,
ss58_address_to_bytes,
U16_NORMALIZED_FLOAT,
Expand Down
53 changes: 24 additions & 29 deletions bittensor/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
import traceback
import typing
import uuid
from inspect import signature, Signature, Parameter
from typing import List, Optional, Tuple, Callable, Any, Dict, Awaitable
from collections.abc import Awaitable
from inspect import Parameter, Signature, signature
from typing import Any, Callable, Optional

import uvicorn
from fastapi import FastAPI, APIRouter, Depends
from fastapi import APIRouter, Depends, FastAPI
from fastapi.responses import JSONResponse
from fastapi.routing import serialize_response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
Expand All @@ -45,15 +46,15 @@

import bittensor
from bittensor.errors import (
BlacklistedException,
InvalidRequestNameError,
SynapseDendriteNoneException,
SynapseParsingError,
UnknownSynapseError,
NotVerifiedException,
BlacklistedException,
PriorityException,
PostProcessException,
PriorityException,
SynapseDendriteNoneException,
SynapseException,
SynapseParsingError,
UnknownSynapseError,
)
from bittensor.threadpool import PriorityThreadPoolExecutor
from bittensor.utils import networking
Expand Down Expand Up @@ -342,12 +343,12 @@ def __init__(
self.port = self.config.axon.port
self.external_ip = (
self.config.axon.external_ip
if self.config.axon.external_ip != None
if self.config.axon.external_ip is not None
else bittensor.utils.networking.get_external_ip()
)
self.external_port = (
self.config.axon.external_port
if self.config.axon.external_port != None
if self.config.axon.external_port is not None
else self.config.axon.port
)
self.full_address = str(self.config.axon.ip) + ":" + str(self.config.axon.port)
Expand All @@ -357,14 +358,14 @@ def __init__(
self.thread_pool = bittensor.PriorityThreadPoolExecutor(
max_workers=self.config.axon.max_workers
)
self.nonces: Dict[str, int] = {}
self.nonces: dict[str, int] = {}

# Request default functions.
self.forward_class_types: Dict[str, List[Signature]] = {}
self.blacklist_fns: Dict[str, Optional[Callable]] = {}
self.priority_fns: Dict[str, Optional[Callable]] = {}
self.forward_fns: Dict[str, Optional[Callable]] = {}
self.verify_fns: Dict[str, Optional[Callable]] = {}
self.forward_class_types: dict[str, list[Signature]] = {}
self.blacklist_fns: dict[str, Optional[Callable]] = {}
self.priority_fns: dict[str, Optional[Callable]] = {}
self.forward_fns: dict[str, Optional[Callable]] = {}
self.verify_fns: dict[str, Optional[Callable]] = {}

# Instantiate FastAPI
self.app = FastAPI()
Expand Down Expand Up @@ -517,27 +518,21 @@ async def endpoint(*args, **kwargs):
]
if blacklist_fn:
blacklist_sig = Signature(
expected_params, return_annotation=Tuple[bool, str]
expected_params, return_annotation=tuple[bool, str]
)
assert (
signature(blacklist_fn) == blacklist_sig
), "The blacklist_fn function must have the signature: blacklist( synapse: {} ) -> Tuple[bool, str]".format(
request_name
)
), f"The blacklist_fn function must have the signature: blacklist( synapse: {request_name} ) -> Tuple[bool, str]"
if priority_fn:
priority_sig = Signature(expected_params, return_annotation=float)
assert (
signature(priority_fn) == priority_sig
), "The priority_fn function must have the signature: priority( synapse: {} ) -> float".format(
request_name
)
), f"The priority_fn function must have the signature: priority( synapse: {request_name} ) -> float"
if verify_fn:
verify_sig = Signature(expected_params, return_annotation=None)
assert (
signature(verify_fn) == verify_sig
), "The verify_fn function must have the signature: verify( synapse: {} ) -> None".format(
request_name
)
), f"The verify_fn function must have the signature: verify( synapse: {request_name} ) -> None"

# Store functions in appropriate attribute dictionaries
self.forward_class_types[request_name] = param_class
Expand Down Expand Up @@ -1149,7 +1144,7 @@ async def preprocess(self, request: Request) -> bittensor.Synapse:
# Extracts the request name from the URL path.
try:
request_name = request.url.path.split("/")[1]
except:
except: # noqa: E722 # FIXME: This is a broad exception catch, should be narrowed down.
raise InvalidRequestNameError(
f"Improperly formatted request. Could not parser request {request.url.path}."
)
Expand All @@ -1164,7 +1159,7 @@ async def preprocess(self, request: Request) -> bittensor.Synapse:

try:
synapse = request_synapse.from_headers(request.headers) # type: ignore
except Exception as e:
except Exception:
raise SynapseParsingError(
f"Improperly formatted request. Could not parse headers {request.headers} into synapse of type {request_name}."
)
Expand Down Expand Up @@ -1325,7 +1320,7 @@ async def priority(self, synapse: bittensor.Synapse):

async def submit_task(
executor: PriorityThreadPoolExecutor, priority: float
) -> Tuple[float, Any]:
) -> tuple[float, Any]:
"""
Submits the given priority function to the specified executor for asynchronous execution.
The function will run in the provided executor and return the priority value along with the result.
Expand Down
1 change: 0 additions & 1 deletion bittensor/btlogging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@

from bittensor.btlogging.loggingmachine import LoggingMachine


logging = LoggingMachine(LoggingMachine.config())
2 changes: 1 addition & 1 deletion bittensor/btlogging/defines.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

BASE_LOG_FORMAT = "%(asctime)s | %(levelname)s | %(message)s"
TRACE_LOG_FORMAT = (
f"%(asctime)s | %(levelname)s | %(name)s:%(filename)s:%(lineno)s | %(message)s"
"%(asctime)s | %(levelname)s | %(name)s:%(filename)s:%(lineno)s | %(message)s"
)
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
BITTENSOR_LOGGER_NAME = "bittensor"
Expand Down
18 changes: 8 additions & 10 deletions bittensor/btlogging/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@

import logging
import time
from typing import Dict

from colorama import init, Fore, Back, Style

from colorama import Back, Fore, Style, init

init(autoreset=True)

Expand All @@ -52,14 +50,14 @@ def _success(self, message: str, *args, **kws):
logging.addLevelName(TRACE_LEVEL_NUM, "TRACE")
logging.Logger.trace = _trace

emoji_map: Dict[str, str] = {
emoji_map: dict[str, str] = {
":white_heavy_check_mark:": "✅",
":cross_mark:": "❌",
":satellite:": "🛰️",
}


color_map: Dict[str, str] = {
color_map: dict[str, str] = {
"<red>": Fore.RED,
"</red>": Style.RESET_ALL,
"<blue>": Fore.BLUE,
Expand All @@ -69,7 +67,7 @@ def _success(self, message: str, *args, **kws):
}


log_level_color_prefix: Dict[int, str] = {
log_level_color_prefix: dict[int, str] = {
logging.NOTSET: Fore.RESET,
logging.TRACE: Fore.MAGENTA,
logging.DEBUG: Fore.BLUE,
Expand All @@ -81,12 +79,12 @@ def _success(self, message: str, *args, **kws):
}


LOG_FORMATS: Dict[int, str] = {
LOG_FORMATS: dict[int, str] = {
level: f"{Fore.BLUE}%(asctime)s{Fore.RESET} | {Style.BRIGHT}{color}%(levelname)s\033[0m | %(message)s"
for level, color in log_level_color_prefix.items()
}

LOG_TRACE_FORMATS: Dict[int, str] = {
LOG_TRACE_FORMATS: dict[int, str] = {
level: f"{Fore.BLUE}%(asctime)s{Fore.RESET}"
f" | {Style.BRIGHT}{color}%(levelname)s{Fore.RESET}{Back.RESET}{Style.RESET_ALL}"
f" | %(name)s:%(filename)s:%(lineno)s"
Expand Down Expand Up @@ -134,7 +132,7 @@ def formatTime(self, record, datefmt=None) -> str:
s = time.strftime(datefmt, created)
else:
s = time.strftime("%Y-%m-%d %H:%M:%S", created)
s += ".{:03d}".format(int(record.msecs))
s += f".{int(record.msecs):03d}"
return s

def format(self, record) -> str:
Expand Down Expand Up @@ -205,7 +203,7 @@ def formatTime(self, record, datefmt=None) -> str:
s = time.strftime(datefmt, created)
else:
s = time.strftime("%Y-%m-%d %H:%M:%S", created)
s += ".{:03d}".format(int(record.msecs))
s += f".{int(record.msecs):03d}"
return s

def format(self, record) -> str:
Expand Down
2 changes: 1 addition & 1 deletion bittensor/btlogging/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""

import logging
from typing import Generator
from collections.abc import Generator


def all_loggers() -> Generator[logging.Logger, None, None]:
Expand Down
16 changes: 8 additions & 8 deletions bittensor/btlogging/loggingmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@
from logging.handlers import QueueHandler, QueueListener, RotatingFileHandler
from typing import NamedTuple

from statemachine import StateMachine, State
from statemachine import State, StateMachine

import bittensor.config
from bittensor.btlogging.defines import (
TRACE_LOG_FORMAT,
DATE_FORMAT,
BITTENSOR_LOGGER_NAME,
DATE_FORMAT,
DEFAULT_LOG_BACKUP_COUNT,
DEFAULT_LOG_FILE_NAME,
DEFAULT_MAX_ROTATING_LOG_FILE_SIZE,
DEFAULT_LOG_BACKUP_COUNT,
TRACE_LOG_FORMAT,
)
from bittensor.btlogging.format import BtStreamFormatter, BtFileFormatter
from bittensor.btlogging.format import BtFileFormatter, BtStreamFormatter
from bittensor.btlogging.helpers import all_loggers


Expand Down Expand Up @@ -91,7 +91,7 @@ class LoggingMachine(StateMachine):

def __init__(self, config: bittensor.config, name: str = BITTENSOR_LOGGER_NAME):
# basics
super(LoggingMachine, self).__init__()
super().__init__()
self._queue = mp.Queue(-1)
self._primary_loggers = {name}
self._config = config
Expand Down Expand Up @@ -260,7 +260,7 @@ def after_transition(self, event, state):
# Default Logging
def before_enable_default(self):
"""Logs status before enable Default."""
self._logger.info(f"Enabling default logging.")
self._logger.info("Enabling default logging.")
self._logger.setLevel(stdlogging.INFO)
for logger in all_loggers():
if logger.name in self._primary_loggers:
Expand All @@ -284,7 +284,7 @@ def after_enable_trace(self):

def before_disable_trace(self):
"""Logs status before disable Trace."""
self._logger.info(f"Disabling trace.")
self._logger.info("Disabling trace.")
self._stream_formatter.set_trace(False)
self.enable_default()

Expand Down
Loading