Update black, and run auto formatting over the codebase (#9381)

- Update black version to the latest
 - Run black auto formatting over the codebase
    - Run autoformatting according to [`docs/code_style.md
`](80d6dc9783/docs/code_style.md)
 - Update `code_style.md` docs around installing black to use the correct version
This commit is contained in:
Eric Eastwood 2021-02-16 16:32:34 -06:00 committed by GitHub
parent 5636e597c3
commit 0a00b7ff14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
271 changed files with 2802 additions and 1713 deletions

View file

@ -54,8 +54,7 @@ class QuieterFileBodyProducer(FileBodyProducer):
def get_request_user_agent(request: IRequest, default: str = "") -> str:
"""Return the last User-Agent header, or the given default.
"""
"""Return the last User-Agent header, or the given default."""
# There could be raw utf-8 bytes in the User-Agent header.
# N.B. if you don't do this, the logger explodes cryptically

View file

@ -398,7 +398,8 @@ class SimpleHttpClient:
body_producer = None
if data is not None:
body_producer = QuieterFileBodyProducer(
BytesIO(data), cooperator=self._cooperator,
BytesIO(data),
cooperator=self._cooperator,
)
request_deferred = treq.request(
@ -413,7 +414,9 @@ class SimpleHttpClient:
# we use our own timeout mechanism rather than treq's as a workaround
# for https://twistedmatrix.com/trac/ticket/9534.
request_deferred = timeout_deferred(
request_deferred, 60, self.hs.get_reactor(),
request_deferred,
60,
self.hs.get_reactor(),
)
# turn timeouts into RequestTimedOutErrors

View file

@ -195,8 +195,7 @@ class MatrixFederationAgent:
@implementer(IAgentEndpointFactory)
class MatrixHostnameEndpointFactory:
"""Factory for MatrixHostnameEndpoint for parsing to an Agent.
"""
"""Factory for MatrixHostnameEndpoint for parsing to an Agent."""
def __init__(
self,
@ -261,8 +260,7 @@ class MatrixHostnameEndpoint:
self._srv_resolver = srv_resolver
def connect(self, protocol_factory: IProtocolFactory) -> defer.Deferred:
"""Implements IStreamClientEndpoint interface
"""
"""Implements IStreamClientEndpoint interface"""
return run_in_background(self._do_connect, protocol_factory)

View file

@ -81,8 +81,7 @@ class WellKnownLookupResult:
class WellKnownResolver:
"""Handles well-known lookups for matrix servers.
"""
"""Handles well-known lookups for matrix servers."""
def __init__(
self,

View file

@ -254,7 +254,8 @@ class MatrixFederationHttpClient:
# Use a BlacklistingAgentWrapper to prevent circumventing the IP
# blacklist via IP literals in server names
self.agent = BlacklistingAgentWrapper(
self.agent, ip_blacklist=hs.config.federation_ip_range_blacklist,
self.agent,
ip_blacklist=hs.config.federation_ip_range_blacklist,
)
self.clock = hs.get_clock()
@ -652,7 +653,7 @@ class MatrixFederationHttpClient:
backoff_on_404: bool = False,
try_trailing_slash_on_400: bool = False,
) -> Union[JsonDict, list]:
""" Sends the specified json data using PUT
"""Sends the specified json data using PUT
Args:
destination: The remote server to send the HTTP request to.
@ -740,7 +741,7 @@ class MatrixFederationHttpClient:
ignore_backoff: bool = False,
args: Optional[QueryArgs] = None,
) -> Union[JsonDict, list]:
""" Sends the specified json data using POST
"""Sends the specified json data using POST
Args:
destination: The remote server to send the HTTP request to.
@ -799,7 +800,11 @@ class MatrixFederationHttpClient:
_sec_timeout = self.default_timeout
body = await _handle_json_response(
self.reactor, _sec_timeout, request, response, start_ms,
self.reactor,
_sec_timeout,
request,
response,
start_ms,
)
return body
@ -813,7 +818,7 @@ class MatrixFederationHttpClient:
ignore_backoff: bool = False,
try_trailing_slash_on_400: bool = False,
) -> Union[JsonDict, list]:
""" GETs some json from the given host homeserver and path
"""GETs some json from the given host homeserver and path
Args:
destination: The remote server to send the HTTP request to.
@ -994,7 +999,10 @@ class MatrixFederationHttpClient:
except BodyExceededMaxSize:
msg = "Requested file is too large > %r bytes" % (max_size,)
logger.warning(
"{%s} [%s] %s", request.txn_id, request.destination, msg,
"{%s} [%s] %s",
request.txn_id,
request.destination,
msg,
)
raise SynapseError(502, msg, Codes.TOO_LARGE)
except Exception as e:

View file

@ -213,8 +213,7 @@ class RequestMetrics:
self.update_metrics()
def update_metrics(self):
"""Updates the in flight metrics with values from this request.
"""
"""Updates the in flight metrics with values from this request."""
new_stats = self.start_context.get_resource_usage()
diff = new_stats - self._request_stats

View file

@ -76,8 +76,7 @@ HTML_ERROR_TEMPLATE = """<!DOCTYPE html>
def return_json_error(f: failure.Failure, request: SynapseRequest) -> None:
"""Sends a JSON error response to clients.
"""
"""Sends a JSON error response to clients."""
if f.check(SynapseError):
error_code = f.value.code
@ -106,12 +105,17 @@ def return_json_error(f: failure.Failure, request: SynapseRequest) -> None:
pass
else:
respond_with_json(
request, error_code, error_dict, send_cors=True,
request,
error_code,
error_dict,
send_cors=True,
)
def return_html_error(
f: failure.Failure, request: Request, error_template: Union[str, jinja2.Template],
f: failure.Failure,
request: Request,
error_template: Union[str, jinja2.Template],
) -> None:
"""Sends an HTML error page corresponding to the given failure.
@ -189,8 +193,7 @@ ServletCallback = Callable[
class HttpServer(Protocol):
""" Interface for registering callbacks on a HTTP server
"""
"""Interface for registering callbacks on a HTTP server"""
def register_paths(
self,
@ -199,7 +202,7 @@ class HttpServer(Protocol):
callback: ServletCallback,
servlet_classname: str,
) -> None:
""" Register a callback that gets fired if we receive a http request
"""Register a callback that gets fired if we receive a http request
with the given method for a path that matches the given regex.
If the regex contains groups these gets passed to the callback via
@ -235,8 +238,7 @@ class _AsyncResource(resource.Resource, metaclass=abc.ABCMeta):
self._extract_context = extract_context
def render(self, request):
""" This gets called by twisted every time someone sends us a request.
"""
"""This gets called by twisted every time someone sends us a request."""
defer.ensureDeferred(self._async_render_wrapper(request))
return NOT_DONE_YET
@ -287,13 +289,18 @@ class _AsyncResource(resource.Resource, metaclass=abc.ABCMeta):
@abc.abstractmethod
def _send_response(
self, request: SynapseRequest, code: int, response_object: Any,
self,
request: SynapseRequest,
code: int,
response_object: Any,
) -> None:
raise NotImplementedError()
@abc.abstractmethod
def _send_error_response(
self, f: failure.Failure, request: SynapseRequest,
self,
f: failure.Failure,
request: SynapseRequest,
) -> None:
raise NotImplementedError()
@ -308,10 +315,12 @@ class DirectServeJsonResource(_AsyncResource):
self.canonical_json = canonical_json
def _send_response(
self, request: Request, code: int, response_object: Any,
self,
request: Request,
code: int,
response_object: Any,
):
"""Implements _AsyncResource._send_response
"""
"""Implements _AsyncResource._send_response"""
# TODO: Only enable CORS for the requests that need it.
respond_with_json(
request,
@ -322,15 +331,16 @@ class DirectServeJsonResource(_AsyncResource):
)
def _send_error_response(
self, f: failure.Failure, request: SynapseRequest,
self,
f: failure.Failure,
request: SynapseRequest,
) -> None:
"""Implements _AsyncResource._send_error_response
"""
"""Implements _AsyncResource._send_error_response"""
return_json_error(f, request)
class JsonResource(DirectServeJsonResource):
""" This implements the HttpServer interface and provides JSON support for
"""This implements the HttpServer interface and provides JSON support for
Resources.
Register callbacks via register_paths()
@ -443,10 +453,12 @@ class DirectServeHtmlResource(_AsyncResource):
ERROR_TEMPLATE = HTML_ERROR_TEMPLATE
def _send_response(
self, request: SynapseRequest, code: int, response_object: Any,
self,
request: SynapseRequest,
code: int,
response_object: Any,
):
"""Implements _AsyncResource._send_response
"""
"""Implements _AsyncResource._send_response"""
# We expect to get bytes for us to write
assert isinstance(response_object, bytes)
html_bytes = response_object
@ -454,10 +466,11 @@ class DirectServeHtmlResource(_AsyncResource):
respond_with_html_bytes(request, 200, html_bytes)
def _send_error_response(
self, f: failure.Failure, request: SynapseRequest,
self,
f: failure.Failure,
request: SynapseRequest,
) -> None:
"""Implements _AsyncResource._send_error_response
"""
"""Implements _AsyncResource._send_error_response"""
return_html_error(f, request, self.ERROR_TEMPLATE)
@ -534,7 +547,9 @@ class _ByteProducer:
min_chunk_size = 1024
def __init__(
self, request: Request, iterator: Iterator[bytes],
self,
request: Request,
iterator: Iterator[bytes],
):
self._request = request
self._iterator = iterator
@ -654,7 +669,10 @@ def respond_with_json(
def respond_with_json_bytes(
request: Request, code: int, json_bytes: bytes, send_cors: bool = False,
request: Request,
code: int,
json_bytes: bytes,
send_cors: bool = False,
):
"""Sends encoded JSON in response to the given request.
@ -769,7 +787,7 @@ def respond_with_redirect(request: Request, url: bytes) -> None:
def finish_request(request: Request):
""" Finish writing the response to the request.
"""Finish writing the response to the request.
Twisted throws a RuntimeException if the connection closed before the
response was written but doesn't provide a convenient or reliable way to

View file

@ -258,7 +258,7 @@ def assert_params_in_dict(body, required):
class RestServlet:
""" A Synapse REST Servlet.
"""A Synapse REST Servlet.
An implementing class can either provide its own custom 'register' method,
or use the automatic pattern handling provided by the base class.

View file

@ -249,8 +249,7 @@ class SynapseRequest(Request):
)
def _finished_processing(self):
"""Log the completion of this request and update the metrics
"""
"""Log the completion of this request and update the metrics"""
assert self.logcontext is not None
usage = self.logcontext.get_resource_usage()
@ -276,7 +275,8 @@ class SynapseRequest(Request):
# authenticated (e.g. and admin is puppetting a user) then we log both.
if self.requester.user.to_string() != authenticated_entity:
authenticated_entity = "{},{}".format(
authenticated_entity, self.requester.user.to_string(),
authenticated_entity,
self.requester.user.to_string(),
)
elif self.requester is not None:
# This shouldn't happen, but we log it so we don't lose information
@ -322,8 +322,7 @@ class SynapseRequest(Request):
logger.warning("Failed to stop metrics: %r", e)
def _should_log_request(self) -> bool:
"""Whether we should log at INFO that we processed the request.
"""
"""Whether we should log at INFO that we processed the request."""
if self.path == b"/health":
return False