Skip to content

Commit b8bafed

Browse files
committed
Better living through py3.8 compatible typing
1 parent d8293d2 commit b8bafed

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

Diff for: webpack_loader/loaders.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import json
22
import os
33
import time
4-
from functools import cached_property, lru_cache
4+
from functools import lru_cache
55
from io import open
66
from typing import Dict, Optional
77
from urllib.parse import urlparse
88
from warnings import warn
99

1010
from django.conf import settings
1111
from django.contrib.staticfiles.storage import staticfiles_storage
12-
from django.http import HttpRequest
12+
from django.http.request import HttpRequest
1313

1414
from .exceptions import (
1515
WebpackError,
@@ -22,19 +22,19 @@
2222
'The crossorigin attribute might be necessary but you did not pass a '
2323
'request object. django_webpack_loader needs a request object to be able '
2424
'to know when to emit the crossorigin attribute on link and script tags. '
25-
'Bundle name: {chunk_name}')
25+
'Chunk name: {chunk_name}')
2626
_CROSSORIGIN_NO_HOST = (
2727
'You have passed the request object but it does not have a "HTTP_HOST", '
2828
'thus django_webpack_loader can\'t know if the crossorigin header will '
29-
'be necessary or not. Bundle name: {chunk_name}')
29+
'be necessary or not. Chunk name: {chunk_name}')
3030
_NONCE_NO_REQUEST = (
3131
'You have enabled the adding of nonce attributes to generated tags via '
3232
'django_webpack_loader, but haven\'t passed a request. '
33-
'Bundle name: {chunk_name}')
33+
'Chunk name: {chunk_name}')
3434
_NONCE_NO_CSPNONCE = (
3535
'django_webpack_loader can\'t generate a nonce tag for a bundle, '
3636
'because the passed request doesn\'t contain a "csp_nonce". '
37-
'Bundle name: {chunk_name}')
37+
'Chunk name: {chunk_name}')
3838

3939

4040
@lru_cache(maxsize=100)

Diff for: webpack_loader/templatetags/webpack_loader.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from typing import Optional
12
from warnings import warn
23

4+
from django.http.request import HttpRequest
35
from django.template import Library
46
from django.utils.safestring import mark_safe
57

@@ -20,23 +22,24 @@ def render_bundle(
2022
if skip_common_chunks is None:
2123
skip_common_chunks = utils.get_skip_common_chunks(config)
2224

23-
request = context.get('request')
24-
url_to_tag_dict = utils.get_as_url_to_tag_dict(
25+
request: Optional[HttpRequest] = context.get('request')
26+
tags = utils.get_as_url_to_tag_dict(
2527
bundle_name, request=request, extension=extension, config=config,
2628
suffix=suffix, attrs=attrs, is_preload=is_preload)
2729

2830
if request is None:
2931
if skip_common_chunks:
3032
warn(message=_WARNING_MESSAGE, category=RuntimeWarning)
31-
return mark_safe('\n'.join(url_to_tag_dict.values()))
33+
return mark_safe('\n'.join(tags.values()))
3234

3335
used_urls = getattr(request, '_webpack_loader_used_urls', None)
34-
if not used_urls:
35-
used_urls = request._webpack_loader_used_urls = set()
36+
if used_urls is None:
37+
used_urls = set()
38+
setattr(request, '_webpack_loader_used_urls', used_urls)
3639
if skip_common_chunks:
37-
url_to_tag_dict = {url: tag for url, tag in url_to_tag_dict.items() if url not in used_urls}
38-
used_urls.update(url_to_tag_dict)
39-
return mark_safe('\n'.join(url_to_tag_dict.values()))
40+
tags = {url: tag for url, tag in tags.items() if url not in used_urls}
41+
used_urls.update(tags)
42+
return mark_safe('\n'.join(tags.values()))
4043

4144

4245
@register.simple_tag

Diff for: webpack_loader/utils.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
from collections import OrderedDict
21
from functools import lru_cache
32
from importlib import import_module
3+
from typing import Optional, OrderedDict
4+
45
from django.conf import settings
6+
from django.http.request import HttpRequest
7+
58
from .config import load_config
9+
from .loaders import WebpackLoader
610

711

812
def import_string(dotted_path):
@@ -21,7 +25,7 @@ def import_string(dotted_path):
2125

2226

2327
@lru_cache(maxsize=None)
24-
def get_loader(config_name):
28+
def get_loader(config_name) -> WebpackLoader:
2529
config = load_config(config_name)
2630
loader_class = import_string(config['LOADER_CLASS'])
2731
return loader_class(config_name, config)
@@ -56,8 +60,9 @@ def get_files(bundle_name, extension=None, config='DEFAULT'):
5660

5761

5862
def get_as_url_to_tag_dict(
59-
bundle_name, request=None, extension=None, config='DEFAULT', suffix='',
60-
attrs='', is_preload=False):
63+
bundle_name, request: Optional[HttpRequest] = None, extension=None,
64+
config='DEFAULT', suffix='', attrs='', is_preload=False
65+
) -> OrderedDict[str, str]:
6166
'''
6267
Get a dict of URLs to formatted <script> & <link> tags for the assets in the
6368
named bundle.
@@ -70,7 +75,7 @@ def get_as_url_to_tag_dict(
7075

7176
loader = get_loader(config)
7277
bundle = _get_bundle(loader, bundle_name, extension)
73-
result = OrderedDict()
78+
result = OrderedDict[str, str]()
7479
attrs_l = attrs.lower()
7580

7681
for chunk in bundle:
@@ -130,6 +135,7 @@ def get_static(asset_name, config='DEFAULT'):
130135

131136
return '{0}{1}'.format(public_path, asset_name)
132137

138+
133139
def get_asset(source_filename, config='DEFAULT'):
134140
'''
135141
Equivalent to Django's 'static' look up but for webpack assets, given its original filename.
@@ -141,6 +147,7 @@ def get_asset(source_filename, config='DEFAULT'):
141147
'''
142148
loader = get_loader(config)
143149
asset = loader.get_asset_by_source_filename(source_filename)
144-
if not asset: return None
150+
if not asset:
151+
return None
145152

146153
return get_static(asset['name'], config)

0 commit comments

Comments
 (0)