Skip to content

Commit

Permalink
mypy: Enable no_implicit_optional
Browse files Browse the repository at this point in the history
"Implicit-optional" mode is on by default, but that default is intended to change in the indefinite future (python/peps#689, python/typing#275). Go ahead and change to the future explicit use of Optional.
  • Loading branch information
robinro authored and bdarnell committed Jun 17, 2019
1 parent 87b5a4a commit c50aed0
Show file tree
Hide file tree
Showing 27 changed files with 363 additions and 280 deletions.
2 changes: 1 addition & 1 deletion docs/httpserver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

HTTP Server
-----------
.. autoclass:: HTTPServer(request_callback: Union[httputil.HTTPServerConnectionDelegate, Callable[[httputil.HTTPServerRequest], None]], no_keep_alive: bool = False, xheaders: bool = False, ssl_options: Union[Dict[str, Any], ssl.SSLContext] = None, protocol: str = None, decompress_request: bool = False, chunk_size: int = None, max_header_size: int = None, idle_connection_timeout: float = None, body_timeout: float = None, max_body_size: int = None, max_buffer_size: int = None, trusted_downstream: List[str] = None)
.. autoclass:: HTTPServer(request_callback: Union[httputil.HTTPServerConnectionDelegate, Callable[[httputil.HTTPServerRequest], None]], no_keep_alive: bool = False, xheaders: bool = False, ssl_options: Union[Dict[str, Any], ssl.SSLContext] = None, protocol: Optional[str] = None, decompress_request: bool = False, chunk_size: Optional[int] = None, max_header_size: Optional[int] = None, idle_connection_timeout: Optional[float] = None, body_timeout: Optional[float] = None, max_body_size: Optional[int] = None, max_buffer_size: Optional[int] = None, trusted_downstream: Optional[List[str]] = None)
:members:

The public interface of this class is mostly inherited from
Expand Down
2 changes: 1 addition & 1 deletion docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
Application configuration
-------------------------

.. autoclass:: Application(handlers: List[Union[Rule, Tuple]] = None, default_host: str = None, transforms: List[Type[OutputTransform]] = None, **settings)
.. autoclass:: Application(handlers: Optional[List[Union[Rule, Tuple]]] = None, default_host: Optional[str] = None, transforms: Optional[List[Type[OutputTransform]]] = None, **settings)

.. attribute:: settings

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ license_file = LICENSE

[mypy]
python_version = 3.5
no_implicit_optional = True

[mypy-tornado.*,tornado.platform.*]
disallow_untyped_defs = True
Expand Down
59 changes: 32 additions & 27 deletions tornado/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class OpenIdMixin(object):

def authenticate_redirect(
self,
callback_uri: str = None,
callback_uri: Optional[str] = None,
ax_attrs: List[str] = ["name", "email", "language", "username"],
) -> None:
"""Redirects to the authentication URL for this service.
Expand All @@ -114,7 +114,7 @@ def authenticate_redirect(
handler.redirect(endpoint + "?" + urllib.parse.urlencode(args))

async def get_authenticated_user(
self, http_client: httpclient.AsyncHTTPClient = None
self, http_client: Optional[httpclient.AsyncHTTPClient] = None
) -> Dict[str, Any]:
"""Fetches the authenticated user data upon redirect.
Expand Down Expand Up @@ -146,7 +146,10 @@ async def get_authenticated_user(
return self._on_authentication_verified(resp)

def _openid_args(
self, callback_uri: str, ax_attrs: Iterable[str] = [], oauth_scope: str = None
self,
callback_uri: str,
ax_attrs: Iterable[str] = [],
oauth_scope: Optional[str] = None,
) -> Dict[str, str]:
handler = cast(RequestHandler, self)
url = urllib.parse.urljoin(handler.request.full_url(), callback_uri)
Expand Down Expand Up @@ -286,9 +289,9 @@ class OAuthMixin(object):

async def authorize_redirect(
self,
callback_uri: str = None,
extra_params: Dict[str, Any] = None,
http_client: httpclient.AsyncHTTPClient = None,
callback_uri: Optional[str] = None,
extra_params: Optional[Dict[str, Any]] = None,
http_client: Optional[httpclient.AsyncHTTPClient] = None,
) -> None:
"""Redirects the user to obtain OAuth authorization for this service.
Expand Down Expand Up @@ -334,7 +337,7 @@ async def authorize_redirect(
self._on_request_token(url, callback_uri, response)

async def get_authenticated_user(
self, http_client: httpclient.AsyncHTTPClient = None
self, http_client: Optional[httpclient.AsyncHTTPClient] = None
) -> Dict[str, Any]:
"""Gets the OAuth authorized user and access token.
Expand Down Expand Up @@ -380,7 +383,9 @@ async def get_authenticated_user(
return user

def _oauth_request_token_url(
self, callback_uri: str = None, extra_params: Dict[str, Any] = None
self,
callback_uri: Optional[str] = None,
extra_params: Optional[Dict[str, Any]] = None,
) -> str:
handler = cast(RequestHandler, self)
consumer_token = self._oauth_consumer_token()
Expand Down Expand Up @@ -547,11 +552,11 @@ class OAuth2Mixin(object):

def authorize_redirect(
self,
redirect_uri: str = None,
client_id: str = None,
client_secret: str = None,
extra_params: Dict[str, Any] = None,
scope: str = None,
redirect_uri: Optional[str] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
extra_params: Optional[Dict[str, Any]] = None,
scope: Optional[str] = None,
response_type: str = "code",
) -> None:
"""Redirects the user to obtain OAuth authorization for this service.
Expand Down Expand Up @@ -582,11 +587,11 @@ def authorize_redirect(

def _oauth_request_token_url(
self,
redirect_uri: str = None,
client_id: str = None,
client_secret: str = None,
code: str = None,
extra_params: Dict[str, Any] = None,
redirect_uri: Optional[str] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
code: Optional[str] = None,
extra_params: Optional[Dict[str, Any]] = None,
) -> str:
url = self._OAUTH_ACCESS_TOKEN_URL # type: ignore
args = {} # type: Dict[str, str]
Expand All @@ -605,8 +610,8 @@ def _oauth_request_token_url(
async def oauth2_request(
self,
url: str,
access_token: str = None,
post_args: Dict[str, Any] = None,
access_token: Optional[str] = None,
post_args: Optional[Dict[str, Any]] = None,
**args: Any
) -> Any:
"""Fetches the given URL auth an OAuth2 access token.
Expand Down Expand Up @@ -709,7 +714,7 @@ async def get(self):
_OAUTH_NO_CALLBACKS = False
_TWITTER_BASE_URL = "https://api.twitter.com/1.1"

async def authenticate_redirect(self, callback_uri: str = None) -> None:
async def authenticate_redirect(self, callback_uri: Optional[str] = None) -> None:
"""Just like `~OAuthMixin.authorize_redirect`, but
auto-redirects if authorized.
Expand All @@ -735,7 +740,7 @@ async def twitter_request(
self,
path: str,
access_token: Dict[str, Any],
post_args: Dict[str, Any] = None,
post_args: Optional[Dict[str, Any]] = None,
**args: Any
) -> Any:
"""Fetches the given API path, e.g., ``statuses/user_timeline/btaylor``
Expand Down Expand Up @@ -930,7 +935,7 @@ async def get_authenticated_user(
client_id: str,
client_secret: str,
code: str,
extra_fields: Dict[str, Any] = None,
extra_fields: Optional[Dict[str, Any]] = None,
) -> Optional[Dict[str, Any]]:
"""Handles the login for the Facebook user, returning a user object.
Expand Down Expand Up @@ -1034,8 +1039,8 @@ async def get(self):
async def facebook_request(
self,
path: str,
access_token: str = None,
post_args: Dict[str, Any] = None,
access_token: Optional[str] = None,
post_args: Optional[Dict[str, Any]] = None,
**args: Any
) -> Any:
"""Fetches the given relative API path, e.g., "/btaylor/picture"
Expand Down Expand Up @@ -1099,7 +1104,7 @@ def _oauth_signature(
method: str,
url: str,
parameters: Dict[str, Any] = {},
token: Dict[str, Any] = None,
token: Optional[Dict[str, Any]] = None,
) -> bytes:
"""Calculates the HMAC-SHA1 OAuth signature for the given request.
Expand Down Expand Up @@ -1132,7 +1137,7 @@ def _oauth10a_signature(
method: str,
url: str,
parameters: Dict[str, Any] = {},
token: Dict[str, Any] = None,
token: Optional[Dict[str, Any]] = None,
) -> bytes:
"""Calculates the HMAC-SHA1 OAuth 1.0a signature for the given request.
Expand Down
11 changes: 7 additions & 4 deletions tornado/curl_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@
)
from tornado.log import app_log

from typing import Dict, Any, Callable, Union
from typing import Dict, Any, Callable, Union, Tuple, Optional
import typing

if typing.TYPE_CHECKING:
from typing import Deque, Tuple, Optional # noqa: F401
from typing import Deque # noqa: F401

curl_log = logging.getLogger("tornado.curl_httpclient")


class CurlAsyncHTTPClient(AsyncHTTPClient):
def initialize( # type: ignore
self, max_clients: int = 10, defaults: Dict[str, Any] = None
self, max_clients: int = 10, defaults: Optional[Dict[str, Any]] = None
) -> None:
super(CurlAsyncHTTPClient, self).initialize(defaults=defaults)
# Typeshed is incomplete for CurlMulti, so just use Any for now.
Expand Down Expand Up @@ -255,7 +255,10 @@ def _process_queue(self) -> None:
break

def _finish(
self, curl: pycurl.Curl, curl_error: int = None, curl_message: str = None
self,
curl: pycurl.Curl,
curl_error: Optional[int] = None,
curl_message: Optional[str] = None,
) -> None:
info = curl.info # type: ignore
curl.info = None # type: ignore
Expand Down
20 changes: 10 additions & 10 deletions tornado/http1connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class HTTP1ConnectionParameters(object):
def __init__(
self,
no_keep_alive: bool = False,
chunk_size: int = None,
max_header_size: int = None,
header_timeout: float = None,
max_body_size: int = None,
body_timeout: float = None,
chunk_size: Optional[int] = None,
max_header_size: Optional[int] = None,
header_timeout: Optional[float] = None,
max_body_size: Optional[int] = None,
body_timeout: Optional[float] = None,
decompress: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -113,8 +113,8 @@ def __init__(
self,
stream: iostream.IOStream,
is_client: bool,
params: HTTP1ConnectionParameters = None,
context: object = None,
params: Optional[HTTP1ConnectionParameters] = None,
context: Optional[object] = None,
) -> None:
"""
:arg stream: an `.IOStream`
Expand Down Expand Up @@ -377,7 +377,7 @@ def write_headers(
self,
start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
headers: httputil.HTTPHeaders,
chunk: bytes = None,
chunk: Optional[bytes] = None,
) -> "Future[None]":
"""Implements `.HTTPConnection.write_headers`."""
lines = []
Expand Down Expand Up @@ -765,8 +765,8 @@ class HTTP1ServerConnection(object):
def __init__(
self,
stream: iostream.IOStream,
params: HTTP1ConnectionParameters = None,
context: object = None,
params: Optional[HTTP1ConnectionParameters] = None,
context: Optional[object] = None,
) -> None:
"""
:arg stream: an `.IOStream`
Expand Down
89 changes: 48 additions & 41 deletions tornado/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class HTTPClient(object):
"""

def __init__(
self, async_client_class: Type["AsyncHTTPClient"] = None, **kwargs: Any
self,
async_client_class: "Optional[Type[AsyncHTTPClient]]" = None,
**kwargs: Any
) -> None:
# Initialize self._closed at the beginning of the constructor
# so that an exception raised here doesn't lead to confusing
Expand Down Expand Up @@ -211,7 +213,7 @@ def __new__(cls, force_instance: bool = False, **kwargs: Any) -> "AsyncHTTPClien
instance_cache[instance.io_loop] = instance
return instance

def initialize(self, defaults: Dict[str, Any] = None) -> None:
def initialize(self, defaults: Optional[Dict[str, Any]] = None) -> None:
self.io_loop = IOLoop.current()
self.defaults = dict(HTTPRequest._DEFAULTS)
if defaults is not None:
Expand Down Expand Up @@ -357,37 +359,39 @@ def __init__(
self,
url: str,
method: str = "GET",
headers: Union[Dict[str, str], httputil.HTTPHeaders] = None,
body: Union[bytes, str] = None,
auth_username: str = None,
auth_password: str = None,
auth_mode: str = None,
connect_timeout: float = None,
request_timeout: float = None,
if_modified_since: Union[float, datetime.datetime] = None,
follow_redirects: bool = None,
max_redirects: int = None,
user_agent: str = None,
use_gzip: bool = None,
network_interface: str = None,
streaming_callback: Callable[[bytes], None] = None,
header_callback: Callable[[str], None] = None,
prepare_curl_callback: Callable[[Any], None] = None,
proxy_host: str = None,
proxy_port: int = None,
proxy_username: str = None,
proxy_password: str = None,
proxy_auth_mode: str = None,
allow_nonstandard_methods: bool = None,
validate_cert: bool = None,
ca_certs: str = None,
allow_ipv6: bool = None,
client_key: str = None,
client_cert: str = None,
body_producer: Callable[[Callable[[bytes], None]], "Future[None]"] = None,
headers: Optional[Union[Dict[str, str], httputil.HTTPHeaders]] = None,
body: Optional[Union[bytes, str]] = None,
auth_username: Optional[str] = None,
auth_password: Optional[str] = None,
auth_mode: Optional[str] = None,
connect_timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
if_modified_since: Optional[Union[float, datetime.datetime]] = None,
follow_redirects: Optional[bool] = None,
max_redirects: Optional[int] = None,
user_agent: Optional[str] = None,
use_gzip: Optional[bool] = None,
network_interface: Optional[str] = None,
streaming_callback: Optional[Callable[[bytes], None]] = None,
header_callback: Optional[Callable[[str], None]] = None,
prepare_curl_callback: Optional[Callable[[Any], None]] = None,
proxy_host: Optional[str] = None,
proxy_port: Optional[int] = None,
proxy_username: Optional[str] = None,
proxy_password: Optional[str] = None,
proxy_auth_mode: Optional[str] = None,
allow_nonstandard_methods: Optional[bool] = None,
validate_cert: Optional[bool] = None,
ca_certs: Optional[str] = None,
allow_ipv6: Optional[bool] = None,
client_key: Optional[str] = None,
client_cert: Optional[str] = None,
body_producer: Optional[
Callable[[Callable[[bytes], None]], "Future[None]"]
] = None,
expect_100_continue: bool = False,
decompress_response: bool = None,
ssl_options: Union[Dict[str, Any], ssl.SSLContext] = None,
decompress_response: Optional[bool] = None,
ssl_options: Optional[Union[Dict[str, Any], ssl.SSLContext]] = None,
) -> None:
r"""All parameters except ``url`` are optional.
Expand Down Expand Up @@ -624,14 +628,14 @@ def __init__(
self,
request: HTTPRequest,
code: int,
headers: httputil.HTTPHeaders = None,
buffer: BytesIO = None,
effective_url: str = None,
error: BaseException = None,
request_time: float = None,
time_info: Dict[str, float] = None,
reason: str = None,
start_time: float = None,
headers: Optional[httputil.HTTPHeaders] = None,
buffer: Optional[BytesIO] = None,
effective_url: Optional[str] = None,
error: Optional[BaseException] = None,
request_time: Optional[float] = None,
time_info: Optional[Dict[str, float]] = None,
reason: Optional[str] = None,
start_time: Optional[float] = None,
) -> None:
if isinstance(request, _RequestProxy):
self.request = request.request
Expand Down Expand Up @@ -703,7 +707,10 @@ class HTTPClientError(Exception):
"""

def __init__(
self, code: int, message: str = None, response: HTTPResponse = None
self,
code: int,
message: Optional[str] = None,
response: Optional[HTTPResponse] = None,
) -> None:
self.code = code
self.message = message or httputil.responses.get(code, "Unknown")
Expand Down
Loading

0 comments on commit c50aed0

Please sign in to comment.