Skip to content

Commit 89b586b

Browse files
Daverballhenri-hulski
authored andcommitted
Add type hints
1 parent e65e583 commit 89b586b

13 files changed

Lines changed: 368 additions & 185 deletions

File tree

HISTORY.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changelog
44
0.5.2 (unreleased)
55
~~~~~~~~~~~~~~~~~~~
66

7+
- Minor robustness improvements
8+
9+
- Add type hints
10+
711
- Switch to PEP-420 namespace package
812

913
- Drop support for Python below 3.10

more/webassets/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
from more.webassets.core import WebassetsApp # noqa
1+
from more.webassets.core import WebassetsApp
2+
3+
__all__ = ("WebassetsApp",)

more/webassets/core.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
from more.webassets.tweens import InjectorTween, PublisherTween
2-
from morepath.request import Request
3-
from morepath.app import App
4-
from dectate import directive
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
55
from ordered_set import OrderedSet
6+
7+
from dectate import directive
8+
from morepath.app import App
69
from morepath.core import excview_tween_factory
10+
from morepath.request import Request
11+
712
from . import directives
13+
from .tweens import InjectorTween, PublisherTween
14+
15+
if TYPE_CHECKING:
16+
from typing_extensions import TypeVar
17+
18+
from morepath.types import Tween
819

20+
_AppT = TypeVar(
21+
"_AppT", bound="WebassetsApp", default="WebassetsApp", covariant=True
22+
)
23+
else:
24+
from typing import TypeVar
925

10-
class IncludeRequest(Request):
26+
_AppT = TypeVar("_AppT", bound="WebassetsApp", covariant=True)
27+
28+
29+
class IncludeRequest(Request[_AppT]):
1130
"""Adds the ability to include webassets bundles on the request.
1231
1332
If the bundle does not exist, a KeyError will be raised during the
@@ -27,11 +46,16 @@ def view(self, request):
2746
2847
"""
2948

30-
def __init__(self, *args, **kwargs):
31-
super().__init__(*args, **kwargs)
32-
self.included_assets = OrderedSet()
49+
included_assets: OrderedSet[str]
50+
51+
# NOTE: Preserve the signature of the super-class, without duplicating it
52+
if not TYPE_CHECKING:
53+
54+
def __init__(self, *args, **kwargs):
55+
super().__init__(*args, **kwargs)
56+
self.included_assets = OrderedSet()
3357

34-
def include(self, resource):
58+
def include(self, resource: str) -> None:
3559
self.included_assets.add(resource)
3660

3761

@@ -54,7 +78,7 @@ class WebassetsApp(App):
5478

5579

5680
@WebassetsApp.tween_factory(over=excview_tween_factory)
57-
def webassets_injector_tween(app, handler):
81+
def webassets_injector_tween(app: WebassetsApp, handler: Tween) -> Tween:
5882
"""Wraps the response with the injector and the publisher tween.
5983
6084
See :class:`webassets.tweens.InjectorTween` and

0 commit comments

Comments
 (0)