Yapf. Pylint

pull/21954/head
Richard Belleville 5 years ago
parent ac864c7502
commit e5b287ead8
  1. 2
      .pylintrc
  2. 31
      src/python/grpcio/grpc/_simple_stubs.py
  3. 7
      src/python/grpcio/grpc/experimental/__init__.py
  4. 6
      src/python/grpcio_tests/tests/unit/py3_only/_simple_stubs_test.py

@ -12,7 +12,7 @@ extension-pkg-whitelist=grpc._cython.cygrpc
# TODO(https://github.com/PyCQA/pylint/issues/1345): How does the inspection # TODO(https://github.com/PyCQA/pylint/issues/1345): How does the inspection
# not include "unused_" and "ignored_" by default? # not include "unused_" and "ignored_" by default?
dummy-variables-rgx=^ignored_|^unused_ dummy-variables-rgx=^ignored_|^unused_|_
[DESIGN] [DESIGN]

@ -1,13 +1,26 @@
# TODO: Flowerbox. # Copyright 2020 The gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions that obviate explicit stubs and explicit channels."""
import collections import collections
import datetime import datetime
import os import os
import logging import logging
import threading import threading
from typing import Any, AnyStr, Callable, Iterator, Optional, Sequence, Tuple, TypeVar, Union
import grpc import grpc
from typing import Any, AnyStr, Callable, Iterator, Optional, Sequence, Tuple, TypeVar, Union
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -16,14 +29,14 @@ if _EVICTION_PERIOD_KEY in os.environ:
_EVICTION_PERIOD = datetime.timedelta( _EVICTION_PERIOD = datetime.timedelta(
seconds=float(os.environ[_EVICTION_PERIOD_KEY])) seconds=float(os.environ[_EVICTION_PERIOD_KEY]))
_LOGGER.info( _LOGGER.info(
f"Setting managed channel eviction period to {_EVICTION_PERIOD}") "Setting managed channel eviction period to %s", _EVICTION_PERIOD)
else: else:
_EVICTION_PERIOD = datetime.timedelta(minutes=10) _EVICTION_PERIOD = datetime.timedelta(minutes=10)
_MAXIMUM_CHANNELS_KEY = "GRPC_PYTHON_MANAGED_CHANNEL_MAXIMUM" _MAXIMUM_CHANNELS_KEY = "GRPC_PYTHON_MANAGED_CHANNEL_MAXIMUM"
if _MAXIMUM_CHANNELS_KEY in os.environ: if _MAXIMUM_CHANNELS_KEY in os.environ:
_MAXIMUM_CHANNELS = int(os.environ[_MAXIMUM_CHANNELS_KEY]) _MAXIMUM_CHANNELS = int(os.environ[_MAXIMUM_CHANNELS_KEY])
_LOGGER.info(f"Setting maximum managed channels to {_MAXIMUM_CHANNELS}") _LOGGER.info("Setting maximum managed channels to %d", _MAXIMUM_CHANNELS)
else: else:
_MAXIMUM_CHANNELS = 2**8 _MAXIMUM_CHANNELS = 2**8
@ -72,7 +85,7 @@ class ChannelCache:
# TODO: Type annotate key. # TODO: Type annotate key.
def _evict_locked(self, key): def _evict_locked(self, key):
channel, _ = self._mapping.pop(key) channel, _ = self._mapping.pop(key)
_LOGGER.info(f"Evicting channel {channel} with configuration {key}.") _LOGGER.info("Evicting channel %s with configuration %s.", channel, key)
channel.close() channel.close()
del channel del channel
@ -89,7 +102,7 @@ class ChannelCache:
ChannelCache._singleton._evict_locked(key) ChannelCache._singleton._evict_locked(key)
# And immediately reevaluate. # And immediately reevaluate.
else: else:
key, (channel, eviction_time) = next( key, (_, eviction_time) = next(
iter(ChannelCache._singleton._mapping.items())) iter(ChannelCache._singleton._mapping.items()))
now = datetime.datetime.now() now = datetime.datetime.now()
if eviction_time <= now: if eviction_time <= now:
@ -142,8 +155,7 @@ ResponseType = TypeVar('ResponseType')
# #
# Make this the default option. # Make this the default option.
# pylint: disable=too-many-arguments
# TODO: Make LocalChannelCredentials the default.
def unary_unary( def unary_unary(
request: RequestType, request: RequestType,
target: str, target: str,
@ -215,6 +227,7 @@ def unary_unary(
timeout=timeout) timeout=timeout)
# pylint: disable=too-many-arguments
def unary_stream( def unary_stream(
request: RequestType, request: RequestType,
target: str, target: str,
@ -285,6 +298,7 @@ def unary_stream(
timeout=timeout) timeout=timeout)
# pylint: disable=too-many-arguments
def stream_unary( def stream_unary(
request_iterator: Iterator[RequestType], request_iterator: Iterator[RequestType],
target: str, target: str,
@ -355,6 +369,7 @@ def stream_unary(
timeout=timeout) timeout=timeout)
# pylint: disable=too-many-arguments
def stream_stream( def stream_stream(
request_iterator: Iterator[RequestType], request_iterator: Iterator[RequestType],
target: str, target: str,

@ -56,11 +56,13 @@ def insecure_channel_credentials():
class ExperimentalApiWarning(Warning): class ExperimentalApiWarning(Warning):
"""A warning that an API is experimental.""" """A warning that an API is experimental."""
def warn_experimental(api_name): def warn_experimental(api_name):
msg = ("{} is an experimental API. It is subject to change or ".format(api_name) + msg = ("{} is an experimental API. It is subject to change or ".format(
"removal between minor releases. Proceed with caution.") api_name) + "removal between minor releases. Proceed with caution.")
warnings.warn(msg, ExperimentalApiWarning, stacklevel=2) warnings.warn(msg, ExperimentalApiWarning, stacklevel=2)
__all__ = ( __all__ = (
'ChannelOptions', 'ChannelOptions',
'ExperimentalApiWarning', 'ExperimentalApiWarning',
@ -71,4 +73,3 @@ __all__ = (
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
from grpc._simple_stubs import unary_unary, unary_stream, stream_unary, stream_stream from grpc._simple_stubs import unary_unary, unary_stream, stream_unary, stream_stream
__all__ = __all__ + (unary_unary, unary_stream, stream_unary, stream_stream) __all__ = __all__ + (unary_unary, unary_stream, stream_unary, stream_stream)

@ -156,7 +156,8 @@ class SimpleStubsTest(unittest.TestCase):
_REQUEST, _REQUEST,
target, target,
_UNARY_UNARY, _UNARY_UNARY,
channel_credentials=grpc.experimental.insecure_channel_credentials()) channel_credentials=grpc.experimental.
insecure_channel_credentials())
self.assertEqual(_REQUEST, response) self.assertEqual(_REQUEST, response)
def test_unary_unary_secure(self): def test_unary_unary_secure(self):
@ -172,7 +173,8 @@ class SimpleStubsTest(unittest.TestCase):
def test_channel_credentials_default(self): def test_channel_credentials_default(self):
with _server(grpc.local_server_credentials()) as (_, port): with _server(grpc.local_server_credentials()) as (_, port):
target = f'localhost:{port}' target = f'localhost:{port}'
response = grpc.experimental.unary_unary(_REQUEST, target, _UNARY_UNARY) response = grpc.experimental.unary_unary(_REQUEST, target,
_UNARY_UNARY)
self.assertEqual(_REQUEST, response) self.assertEqual(_REQUEST, response)
def test_channels_cached(self): def test_channels_cached(self):

Loading…
Cancel
Save