Set stricter mypy parameters

This commit is contained in:
Correl Roush 2020-12-04 21:01:22 -05:00
parent 5a613a54b0
commit b91feab778
4 changed files with 26 additions and 15 deletions

3
mypy.ini Normal file
View file

@ -0,0 +1,3 @@
[mypy]
disallow_untyped_defs = True
disallow_incomplete_defs = True

View file

@ -1,10 +1,10 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Dict, List, Optional from typing import Any, Callable, Dict, List, Optional, Tuple
import unittest import unittest
from urllib.parse import urlencode, urlparse from urllib.parse import urlencode, urlparse
import attr import attr
from hypothesis import given, settings # type: ignore from hypothesis import given, settings
import hypothesis.strategies as s # type: ignore import hypothesis.strategies as s # type: ignore
from openapi_core import create_spec # type: ignore from openapi_core import create_spec # type: ignore
from openapi_core.exceptions import OpenAPIError # type: ignore from openapi_core.exceptions import OpenAPIError # type: ignore
@ -69,16 +69,18 @@ field_value = s.text(
) )
def headers(min_size=0): def headers(min_size: int = 0) -> s.SearchStrategy[Dict[str, str]]:
return s.dictionaries(field_name, field_value, min_size=min_size) return s.dictionaries(field_name, field_value, min_size=min_size)
def query_parameters(min_size=0): def query_parameters(min_size: int = 0) -> s.SearchStrategy[Dict[str, str]]:
return s.dictionaries(field_name, field_value, min_size=min_size) return s.dictionaries(field_name, field_value, min_size=min_size)
@s.composite @s.composite
def parameters(draw, min_headers=0, min_query_parameters=0) -> Parameters: def parameters(
draw: Callable[[Any], Any], min_headers: int = 0, min_query_parameters: int = 0
) -> Parameters:
return Parameters( return Parameters(
headers=draw(headers(min_size=min_headers)), headers=draw(headers(min_size=min_headers)),
query_parameters=draw(query_parameters(min_size=min_query_parameters)), query_parameters=draw(query_parameters(min_size=min_query_parameters)),
@ -92,7 +94,7 @@ class TestRequestFactory(unittest.TestCase):
s.tuples(s.just("http://example.com/foo"), query_parameters()), s.tuples(s.just("http://example.com/foo"), query_parameters()),
) )
) )
def test_http_request(self, opts) -> None: def test_http_request(self, opts: Tuple[str, Dict[str, str]]) -> None:
url, parameters = opts url, parameters = opts
request_url = f"{url}?{urlencode(parameters)}" if url else "" request_url = f"{url}?{urlencode(parameters)}" if url else ""
tornado_request = HTTPRequest(method="GET", url=request_url) tornado_request = HTTPRequest(method="GET", url=request_url)
@ -112,7 +114,7 @@ class TestRequestFactory(unittest.TestCase):
s.tuples(s.just("http://example.com/foo"), query_parameters()), s.tuples(s.just("http://example.com/foo"), query_parameters()),
) )
) )
def test_http_server_request(self, opts) -> None: def test_http_server_request(self, opts: Tuple[str, Dict[str, str]]) -> None:
url, parameters = opts url, parameters = opts
request_url = f"{url}?{urlencode(parameters)}" if url else "" request_url = f"{url}?{urlencode(parameters)}" if url else ""
parsed = urlparse(request_url) parsed = urlparse(request_url)

View file

@ -1,11 +1,11 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, Dict, Optional from typing import Any, Callable, Dict, Optional, TypeVar
import unittest import unittest
import attr import attr
from hypothesis import given, settings # type: ignore from hypothesis import given, settings
import hypothesis.strategies as s # type: ignore import hypothesis.strategies as s
from openapi_core import create_spec # type: ignore from openapi_core import create_spec # type: ignore
from openapi_core.validation.response.datatypes import OpenAPIResponse # type: ignore from openapi_core.validation.response.datatypes import OpenAPIResponse # type: ignore
@ -18,6 +18,7 @@ from tornado_openapi3 import (
TornadoResponseFactory, TornadoResponseFactory,
) )
T = TypeVar("T")
settings(deadline=None) settings(deadline=None)
@ -39,7 +40,7 @@ class Responses:
@s.composite @s.composite
def responses(draw, min_headers=0) -> Responses: def responses(draw: Callable[[Any], Any], min_headers: int = 0) -> Responses:
field_name = s.text( field_name = s.text(
s.characters( s.characters(
min_codepoint=33, min_codepoint=33,
@ -55,9 +56,11 @@ def responses(draw, min_headers=0) -> Responses:
), ),
min_size=1, min_size=1,
) )
code = s.sampled_from([200, 304, 400, 500])
headers = s.dictionaries(field_name, field_value, min_size=min_headers)
return Responses( return Responses(
code=draw(s.sampled_from([200, 304, 400, 500])), code=draw(code),
headers=draw(s.dictionaries(field_name, field_value, min_size=min_headers)), headers=draw(headers),
) )

View file

@ -3,8 +3,9 @@ from urllib.parse import parse_qsl
from typing import Union from typing import Union
from openapi_core.validation.request.datatypes import ( # type: ignore from openapi_core.validation.request.datatypes import ( # type: ignore
RequestParameters,
OpenAPIRequest, OpenAPIRequest,
RequestParameters,
RequestValidationResult,
) )
from openapi_core.validation.request import validators # type: ignore from openapi_core.validation.request import validators # type: ignore
from tornado.httpclient import HTTPRequest # type: ignore from tornado.httpclient import HTTPRequest # type: ignore
@ -50,7 +51,9 @@ class TornadoRequestFactory:
class RequestValidator(validators.RequestValidator): class RequestValidator(validators.RequestValidator):
def validate(self, request: Union[HTTPRequest, HTTPServerRequest]): def validate(
self, request: Union[HTTPRequest, HTTPServerRequest]
) -> RequestValidationResult:
return super().validate(TornadoRequestFactory.create(request)) return super().validate(TornadoRequestFactory.create(request))