Skip to content

Commit 9d54e79

Browse files
style: Enforce mypy no-implicit-optional setting (#294)
Which has been introduced in `mypy==0.981` and make sure proper type annotations for args / kwargs with default `None` values.
1 parent c0d8fc6 commit 9d54e79

File tree

16 files changed

+138
-109
lines changed

16 files changed

+138
-109
lines changed

examples/hobotnica/src/hobotnica/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import List
2+
from typing import List, Union
33

44
from aiohttp import web
55
from aiohttp_middlewares import (
@@ -13,7 +13,9 @@
1313

1414

1515
def create_app(
16-
argv: List[str] = None, *, settings: BaseSettings = None
16+
argv: Union[List[str], None] = None,
17+
*,
18+
settings: Union[BaseSettings, None] = None,
1719
) -> web.Application:
1820
if settings is None:
1921
settings = BaseSettings.from_environ()

examples/petstore/src/petstore/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import List
2+
from typing import List, Union
33

44
from aiohttp import web
55

@@ -9,7 +9,9 @@
99

1010

1111
def create_app(
12-
argv: List[str] = None, *, settings: Settings = None
12+
argv: Union[List[str], None] = None,
13+
*,
14+
settings: Union[Settings, None] = None,
1315
) -> web.Application:
1416
"""Create aiohttp applicaiton for OpenAPI 3 Schema.
1517

examples/simulations/src/simulations/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22
from pathlib import Path
3-
from typing import List
3+
from typing import List, Union
44

55
import yaml
66
from aiohttp import web
@@ -15,7 +15,7 @@
1515
operations = OperationTableDef()
1616

1717

18-
def create_app(argv: List[str] = None) -> web.Application:
18+
def create_app(argv: Union[List[str], None] = None) -> web.Application:
1919
schema = yaml.load(
2020
(Path(__file__).parent / "openapi.yaml").read_bytes(),
2121
Loader=get_default_yaml_loader(),

examples/todobackend/src/todobackend/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import AsyncIterator, List
2+
from typing import AsyncIterator, List, Union
33

44
from aiohttp import web
55
from aiohttp_middlewares import https_middleware
@@ -14,7 +14,9 @@
1414

1515

1616
def create_app(
17-
argv: List[str] = None, *, settings: Settings = None
17+
argv: Union[List[str], None] = None,
18+
*,
19+
settings: Union[Settings, None] = None,
1820
) -> web.Application:
1921
if settings is None:
2022
settings = Settings.from_environ()

poetry.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[build-system]
2+
requires = ["poetry-core>=1.0.0"]
3+
build-backend = "poetry.core.masonry.api"
4+
15
[tool.badabump]
26
version_type = "semver"
37
version_files = [
@@ -20,7 +24,7 @@ source = ["./src/"]
2024

2125
[tool.coverage.report]
2226
exclude_lines = ["if TYPE_CHECKING:", "@overload"]
23-
omit = ["./src/rororo/annotations.py"]
27+
omit = ["./src/*/main.py", "./src/*/annotations.py"]
2428
fail_under = 95
2529
skip_covered = true
2630
show_missing = true
@@ -54,6 +58,7 @@ follow_imports = "normal"
5458
follow_imports_for_stubs = true
5559
ignore_missing_imports = false
5660
namespace_packages = true
61+
no_implicit_optional = true
5762
mypy_path = "./src/"
5863
python_executable = "./.venv/bin/python3"
5964
show_column_numbers = true
@@ -208,7 +213,3 @@ commands_pre =
208213
pyrsistent==0.16 \
209214
PyYAML==5.1
210215
"""
211-
212-
[build-system]
213-
requires = ["poetry-core>=1.0.0"]
214-
build-backend = "poetry.core.masonry.api"

src/rororo/aio.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ class AddResourceFunc(Protocol):
2828
def __call__(
2929
self,
3030
url: str,
31-
get: Handler = None,
31+
get: Union[Handler, None] = None,
3232
*,
33-
name: str = None,
33+
name: Union[str, None] = None,
3434
**kwargs: Handler,
3535
) -> web.Resource:
3636
...
3737

3838

3939
@contextmanager
4040
def add_resource_context(
41-
router: web.UrlDispatcher, url_prefix: str = None, name_prefix: str = None
41+
router: web.UrlDispatcher,
42+
url_prefix: Union[str, None] = None,
43+
name_prefix: Union[str, None] = None,
4244
) -> Iterator[AddResourceFunc]:
4345
"""Context manager for adding resources for given router.
4446
@@ -65,7 +67,11 @@ def add_resource_context(
6567
"""
6668

6769
def add_resource(
68-
url: str, get: Handler = None, *, name: str = None, **kwargs: Handler
70+
url: str,
71+
get: Union[Handler, None] = None,
72+
*,
73+
name: Union[str, None] = None,
74+
**kwargs: Handler,
6975
) -> web.Resource:
7076
"""Inner function to create resource and add necessary routes to it.
7177

src/rororo/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def update_sentry_logging(
8383
logging_dict: DictStrAny,
8484
sentry_dsn: Optional[str],
8585
*loggers: str,
86-
level: Union[str, int] = None,
86+
level: Union[str, int, None] = None,
8787
**kwargs: Any,
8888
) -> None:
8989
r"""Enable Sentry logging if Sentry DSN passed.

src/rororo/openapi/annotations.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1-
from typing import Any, Dict, List, Union
1+
from typing import Any, Dict, List, Tuple, Union
2+
3+
from aiohttp_middlewares.annotations import (
4+
ExceptionType,
5+
Handler,
6+
StrCollection,
7+
UrlCollection,
8+
)
9+
from aiohttp_middlewares.error import Config as ErrorMiddlewareConfig
210

311
from rororo.annotations import TypedDict
412

513

614
SecurityDict = Dict[str, List[str]]
715

816

17+
class CorsMiddlewareKwargsDict(TypedDict, total=False):
18+
allow_all: bool
19+
origins: Union[UrlCollection, None]
20+
urls: Union[UrlCollection, None]
21+
expose_headers: Union[StrCollection, None]
22+
allow_headers: StrCollection
23+
allow_methods: StrCollection
24+
allow_credentials: bool
25+
max_age: Union[int, None]
26+
27+
28+
class ErrorMiddlewareKwargsDict(TypedDict, total=False):
29+
default_handler: Handler
30+
config: Union[ErrorMiddlewareConfig, None]
31+
ignore_exceptions: Union[ExceptionType, Tuple[ExceptionType, ...], None]
32+
33+
934
class ValidateEmailKwargsDict(TypedDict, total=False):
1035
allow_smtputf8: bool
1136
allow_empty_local: bool

src/rororo/openapi/core_validators.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import deque
22
from functools import partial
3-
from typing import Any, cast, Dict, Iterator, List, Optional, Tuple
3+
from typing import Any, cast, Dict, Iterator, List, Optional, Tuple, Union
44

55
import pyrsistent
66
from email_validator import EmailNotValidError, validate_email
@@ -86,7 +86,9 @@ class EmailFormatter(Formatter):
8686

8787
kwargs: ValidateEmailKwargsDict
8888

89-
def __init__(self, kwargs: ValidateEmailKwargsDict = None) -> None:
89+
def __init__(
90+
self, kwargs: Union[ValidateEmailKwargsDict, None] = None
91+
) -> None:
9092
self.kwargs: ValidateEmailKwargsDict = kwargs or {
9193
"check_deliverability": False
9294
}
@@ -168,7 +170,7 @@ class SchemaUnmarshallersFactory(CoreSchemaUnmarshallersFactory):
168170
def get_formatter(
169171
self,
170172
default_formatters: Dict[str, Formatter],
171-
type_format: str = None,
173+
type_format: Union[str, None] = None,
172174
) -> Formatter:
173175
if type_format == SchemaFormat.DATETIME.value:
174176
return DATE_TIME_FORMATTER
@@ -263,7 +265,7 @@ def _unmarshal(self, param_or_media_type: Any, value: Any) -> Any: # type: igno
263265

264266

265267
def get_custom_formatters(
266-
*, validate_email_kwargs: ValidateEmailKwargsDict = None
268+
*, validate_email_kwargs: Union[ValidateEmailKwargsDict, None] = None
267269
) -> Dict[str, Formatter]:
268270
return {"email": EmailFormatter(validate_email_kwargs)}
269271

@@ -272,7 +274,7 @@ def validate_core_request(
272274
spec: Spec,
273275
core_request: OpenAPIRequest,
274276
*,
275-
validate_email_kwargs: ValidateEmailKwargsDict = None,
277+
validate_email_kwargs: Union[ValidateEmailKwargsDict, None] = None,
276278
) -> Tuple[MappingStrAny, OpenAPIParameters, Any]:
277279
"""
278280
Instead of validating request parameters & body in two calls, validate them
@@ -304,7 +306,7 @@ def validate_core_response(
304306
core_request: OpenAPIRequest,
305307
core_response: OpenAPIResponse,
306308
*,
307-
validate_email_kwargs: ValidateEmailKwargsDict = None,
309+
validate_email_kwargs: Union[ValidateEmailKwargsDict, None] = None,
308310
) -> Any:
309311
"""Pass custom formatters for validating response data."""
310312
custom_formatters = get_custom_formatters(

0 commit comments

Comments
 (0)