Fix some type issues in storage_client.py

This commit is contained in:
Itamar Turner-Trauring 2023-06-14 16:02:04 -04:00
parent 65775cd6bd
commit 11e0151838
3 changed files with 15 additions and 7 deletions

View File

@ -17,11 +17,13 @@ if PY2:
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, pow, round, super, range, max, min # noqa: F401 from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, pow, round, super, range, max, min # noqa: F401
from past.builtins import long from past.builtins import long
from typing import Dict
from zope.interface import Interface, Attribute from zope.interface import Interface, Attribute
from twisted.plugin import ( from twisted.plugin import (
IPlugin, IPlugin,
) )
from twisted.internet.defer import Deferred
from foolscap.api import StringConstraint, ListOf, TupleOf, SetOf, DictOf, \ from foolscap.api import StringConstraint, ListOf, TupleOf, SetOf, DictOf, \
ChoiceOf, IntegerConstraint, Any, RemoteInterface, Referenceable ChoiceOf, IntegerConstraint, Any, RemoteInterface, Referenceable
@ -307,12 +309,15 @@ class RIStorageServer(RemoteInterface):
store that on disk. store that on disk.
""" """
# The result of IStorageServer.get_version():
VersionMessage = Dict[bytes, object]
class IStorageServer(Interface): class IStorageServer(Interface):
""" """
An object capable of storing shares for a storage client. An object capable of storing shares for a storage client.
""" """
def get_version(): def get_version() -> Deferred[VersionMessage]:
""" """
:see: ``RIStorageServer.get_version`` :see: ``RIStorageServer.get_version``
""" """

View File

@ -57,6 +57,7 @@ from .http_common import (
CBOR_MIME_TYPE, CBOR_MIME_TYPE,
get_spki_hash, get_spki_hash,
) )
from ..interfaces import VersionMessage
from .common import si_b2a, si_to_human_readable from .common import si_b2a, si_to_human_readable
from ..util.hashutil import timing_safe_compare from ..util.hashutil import timing_safe_compare
from ..util.deferredutil import async_to_deferred from ..util.deferredutil import async_to_deferred
@ -576,7 +577,7 @@ class StorageClientGeneral(object):
_client: StorageClient _client: StorageClient
@async_to_deferred @async_to_deferred
async def get_version(self) -> dict[bytes, object]: async def get_version(self) -> VersionMessage:
""" """
Return the version metadata for the server. Return the version metadata for the server.
""" """
@ -585,7 +586,7 @@ class StorageClientGeneral(object):
): ):
return await self._get_version() return await self._get_version()
async def _get_version(self) -> dict[bytes, object]: async def _get_version(self) -> VersionMessage:
"""Implementation of get_version().""" """Implementation of get_version()."""
url = self._client.relative_url("/storage/v1/version") url = self._client.relative_url("/storage/v1/version")
response = await self._client.request("GET", url) response = await self._client.request("GET", url)

View File

@ -33,7 +33,7 @@ Ported to Python 3.
from __future__ import annotations from __future__ import annotations
from six import ensure_text from six import ensure_text
from typing import Union, Callable, Any, Optional from typing import Union, Callable, Any, Optional, cast
from os import urandom from os import urandom
import re import re
import time import time
@ -53,6 +53,7 @@ from twisted.python.failure import Failure
from twisted.web import http from twisted.web import http
from twisted.internet.task import LoopingCall from twisted.internet.task import LoopingCall
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
from twisted.internet.interfaces import IReactorTime
from twisted.application import service from twisted.application import service
from twisted.plugin import ( from twisted.plugin import (
getPlugins, getPlugins,
@ -70,6 +71,7 @@ from allmydata.interfaces import (
IServer, IServer,
IStorageServer, IStorageServer,
IFoolscapStoragePlugin, IFoolscapStoragePlugin,
VersionMessage
) )
from allmydata.grid_manager import ( from allmydata.grid_manager import (
create_grid_manager_verifier, create_grid_manager_verifier,
@ -1089,7 +1091,7 @@ class HTTPNativeStorageServer(service.MultiService):
self._connection_status = connection_status.ConnectionStatus.unstarted() self._connection_status = connection_status.ConnectionStatus.unstarted()
self._version = None self._version = None
self._last_connect_time = None self._last_connect_time = None
self._connecting_deferred = None self._connecting_deferred : Optional[defer.Deferred[object]]= None
def get_permutation_seed(self): def get_permutation_seed(self):
return self._permutation_seed return self._permutation_seed
@ -1266,7 +1268,7 @@ class HTTPNativeStorageServer(service.MultiService):
# If we've gotten this far, we've found a working NURL. # If we've gotten this far, we've found a working NURL.
storage_client = await self._storage_client_factory.create_storage_client( storage_client = await self._storage_client_factory.create_storage_client(
nurl, reactor, None nurl, cast(IReactorTime, reactor), None
) )
self._istorage_server = _HTTPStorageServer.from_http_client(storage_client) self._istorage_server = _HTTPStorageServer.from_http_client(storage_client)
return self._istorage_server return self._istorage_server
@ -1507,7 +1509,7 @@ class _HTTPStorageServer(object):
""" """
return _HTTPStorageServer(http_client=http_client) return _HTTPStorageServer(http_client=http_client)
def get_version(self): def get_version(self) -> defer.Deferred[VersionMessage]:
return StorageClientGeneral(self._http_client).get_version() return StorageClientGeneral(self._http_client).get_version()
@defer.inlineCallbacks @defer.inlineCallbacks