Skip to content

Commit

Permalink
Move backwards compatibility related content to `bittensor/utils/back…
Browse files Browse the repository at this point in the history
…wards_compatibility` subpackage
  • Loading branch information
roman-opentensor committed Aug 6, 2024
1 parent 79a4393 commit 70f1545
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 16 deletions.
Empty file removed bittensor/api/__init__.py
Empty file.
14 changes: 10 additions & 4 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@
from scalecodec.types import ScaleType
from substrateinterface.base import QueryMapResult, SubstrateInterface

from bittensor.api.extrinsics.prometheus import prometheus_extrinsic
from bittensor.api.extrinsics.serving import (
from bittensor.utils.backwards_compatibility.extrinsics.prometheus import (
prometheus_extrinsic,
)
from bittensor.utils.backwards_compatibility.extrinsics.serving import (
serve_extrinsic,
serve_axon_extrinsic,
publish_metadata,
get_metadata,
)
from bittensor.api.extrinsics.set_weights import set_weights_extrinsic
from bittensor.api.extrinsics.transfer import transfer_extrinsic
from bittensor.utils.backwards_compatibility.extrinsics.set_weights import (
set_weights_extrinsic,
)
from bittensor.utils.backwards_compatibility.extrinsics.transfer import (
transfer_extrinsic,
)
from bittensor.core import settings
from bittensor.core.axon import Axon
from bittensor.core.chain_data import (
Expand Down
14 changes: 8 additions & 6 deletions bittensor/utils/backwards_compatibility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@
__tao_symbol__ = settings.TAO_SYMBOL
__rao_symbol__ = settings.RAO_SYMBOL

# Makes the `bittensor.api.extrinsics` subpackage available as `bittensor.extrinsics` for backwards compatibility.
extrinsics = importlib.import_module("bittensor.api.extrinsics")
sys.modules["bittensor.extrinsics"] = extrinsics

# Makes the `bittensor.utils.mock` subpackage available as `bittensor.mock` for backwards compatibility.
extrinsics = importlib.import_module("bittensor.utils.mock")
sys.modules["bittensor.mock"] = extrinsics
mock_subpackage = importlib.import_module("bittensor.utils.mock")
sys.modules["bittensor.mock"] = mock_subpackage

# Makes the `bittensor.utils.backwards_compatibility.extrinsics` subpackage available as `bittensor.extrinsics` for backwards compatibility.
extrinsics_subpackage = importlib.import_module(
"bittensor.utils.backwards_compatibility.extrinsics"
)
sys.modules["bittensor.extrinsics"] = extrinsics_subpackage
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions tests/unit_tests/extrinsics/test_backwards_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# The MIT License (MIT)
# Copyright © 2024 Opentensor Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import sys


def test_mock_import():
"""
Tests that `bittensor.mock` can be imported and is the same as `bittensor.utils.mock`.
"""
import bittensor.mock as redirected_mock
import bittensor.utils.mock as real_mock

assert "bittensor.mock" in sys.modules
assert redirected_mock is real_mock


def test_extrinsics_import():
"""Tests that `bittensor.extrinsics` can be imported and is the same as `bittensor.utils.backwards_compatibility.extrinsics`."""
import bittensor.extrinsics as redirected_extrinsics
import bittensor.utils.backwards_compatibility.extrinsics as real_extrinsics

assert "bittensor.extrinsics" in sys.modules
assert redirected_extrinsics is real_extrinsics
4 changes: 3 additions & 1 deletion tests/unit_tests/extrinsics/test_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import pytest
from bittensor_wallet import Wallet

from bittensor.api.extrinsics.prometheus import prometheus_extrinsic
from bittensor.utils.backwards_compatibility.extrinsics.prometheus import (
prometheus_extrinsic,
)
from bittensor.core.subtensor import Subtensor
from bittensor.core.settings import version_as_int

Expand Down
17 changes: 13 additions & 4 deletions tests/unit_tests/extrinsics/test_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from bittensor_wallet import Wallet

from bittensor.core.axon import Axon
from bittensor.api.extrinsics.serving import (
from bittensor.utils.backwards_compatibility.extrinsics.serving import (
serve_extrinsic,
publish_metadata,
serve_axon_extrinsic,
Expand Down Expand Up @@ -119,7 +119,10 @@ def test_serve_extrinsic_happy_path(
):
# Arrange
mock_subtensor.do_serve_axon.return_value = (True, "")
with patch("bittensor.api.extrinsics.serving.Confirm.ask", return_value=True):
with patch(
"bittensor.utils.backwards_compatibility.extrinsics.serving.Confirm.ask",
return_value=True,
):
# Act
result = serve_extrinsic(
mock_subtensor,
Expand Down Expand Up @@ -176,7 +179,10 @@ def test_serve_extrinsic_edge_cases(
):
# Arrange
mock_subtensor.do_serve_axon.return_value = (True, "")
with patch("bittensor.api.extrinsics.serving.Confirm.ask", return_value=True):
with patch(
"bittensor.utils.backwards_compatibility.extrinsics.serving.Confirm.ask",
return_value=True,
):
# Act
result = serve_extrinsic(
mock_subtensor,
Expand Down Expand Up @@ -233,7 +239,10 @@ def test_serve_extrinsic_error_cases(
):
# Arrange
mock_subtensor.do_serve_axon.return_value = (False, "Error serving axon")
with patch("bittensor.api.extrinsics.serving.Confirm.ask", return_value=True):
with patch(
"bittensor.utils.backwards_compatibility.extrinsics.serving.Confirm.ask",
return_value=True,
):
# Act
result = serve_extrinsic(
mock_subtensor,
Expand Down
4 changes: 3 additions & 1 deletion tests/unit_tests/extrinsics/test_set_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from bittensor.core.subtensor import Subtensor
from bittensor_wallet import Wallet
from bittensor.api.extrinsics.set_weights import set_weights_extrinsic
from bittensor.utils.backwards_compatibility.extrinsics.set_weights import (
set_weights_extrinsic,
)


@pytest.fixture
Expand Down

0 comments on commit 70f1545

Please sign in to comment.