Further typing cleanup

This commit is contained in:
Martin Mahner 2020-06-04 08:45:28 +02:00
parent cf3ad14795
commit 4fec267cbe
3 changed files with 14 additions and 29 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ dpaste.egg-info
dpaste.sqlite dpaste.sqlite
node_modules node_modules
monkeytype.sqlite3 monkeytype.sqlite3
.mypy_cache/

View file

@ -1,5 +1,5 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Dict, Optional, Tuple, Type, Union from typing import Any, Dict, Optional, Tuple, Type, Union
from django import forms from django import forms
from django.apps import apps from django.apps import apps
@ -89,7 +89,7 @@ class SnippetForm(forms.ModelForm):
self.cleaned_data["expire_type"] = expire_type self.cleaned_data["expire_type"] = expire_type
return expires return expires
def clean(self) -> Dict[str, Optional[Union[Type[object], None]]]: def clean(self) -> Dict[str, Any]:
""" """
The `title` field is a hidden honeypot field. If its filled, The `title` field is a hidden honeypot field. If its filled,
this is likely spam. this is likely spam.

View file

@ -96,9 +96,7 @@ class SnippetDetailView(DetailView, FormView):
always expire. always expire.
""" """
if "delete" in self.request.POST: if "delete" in self.request.POST:
snippet = get_object_or_404( snippet = get_object_or_404(Snippet, secret_id=self.kwargs["snippet_id"])
Snippet, secret_id=self.kwargs["snippet_id"]
)
snippet.delete() snippet.delete()
# Append `#` so #delete goes away in Firefox # Append `#` so #delete goes away in Firefox
@ -138,7 +136,7 @@ class SnippetDetailView(DetailView, FormView):
return response return response
def get_initial(self) -> Dict[str, Union[str, bool]]: def get_initial(self) -> Dict[str, Any]:
snippet = self.get_object() snippet = self.get_object()
return { return {
"content": snippet.content, "content": snippet.content,
@ -146,14 +144,7 @@ class SnippetDetailView(DetailView, FormView):
"rtl": snippet.rtl, "rtl": snippet.rtl,
} }
def get_form_kwargs( def get_form_kwargs(self,) -> Dict[str, Dict[str, Any]]:
self,
) -> Dict[
str,
Optional[
Union[Dict[str, Union[str, bool]], MultiValueDict, WSGIRequest]
],
]:
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
kwargs.update({"request": self.request}) kwargs.update({"request": self.request})
return kwargs return kwargs
@ -162,7 +153,7 @@ class SnippetDetailView(DetailView, FormView):
snippet = form.save(parent=self.get_object()) snippet = form.save(parent=self.get_object())
return HttpResponseRedirect(snippet.get_absolute_url()) return HttpResponseRedirect(snippet.get_absolute_url())
def get_snippet_diff(self) -> None: def get_snippet_diff(self) -> str:
snippet = self.get_object() snippet = self.get_object()
if not snippet.parent_id: if not snippet.parent_id:
@ -252,9 +243,7 @@ class SnippetHistory(TemplateView):
add_never_cache_headers(response) add_never_cache_headers(response)
return response return response
def post( def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponseRedirect:
self, request: WSGIRequest, *args, **kwargs
) -> HttpResponseRedirect:
""" """
Delete all user snippets at once. Delete all user snippets at once.
""" """
@ -265,9 +254,7 @@ class SnippetHistory(TemplateView):
url = "{0}#".format(reverse("snippet_history")) url = "{0}#".format(reverse("snippet_history"))
return HttpResponseRedirect(url) return HttpResponseRedirect(url)
def get_context_data( def get_context_data(self, **kwargs) -> Dict[str, Any]:
self, **kwargs
) -> Dict[str, Union["SnippetHistory", QuerySet, str]]:
ctx = super().get_context_data(**kwargs) ctx = super().get_context_data(**kwargs)
ctx.update({"snippet_list": self.get_user_snippets()}) ctx.update({"snippet_list": self.get_user_snippets()})
ctx.update(config.extra_template_context) ctx.update(config.extra_template_context)
@ -357,16 +344,11 @@ class APIView(View):
) )
expires, expire_type = get_expire_values(expires) expires, expire_type = get_expire_values(expires)
else: else:
expires = datetime.datetime.now() + datetime.timedelta( expires = datetime.datetime.now() + datetime.timedelta(seconds=60 * 60 * 24)
seconds=60 * 60 * 24
)
expire_type = Snippet.EXPIRE_TIME expire_type = Snippet.EXPIRE_TIME
snippet = Snippet.objects.create( snippet = Snippet.objects.create(
content=content, content=content, lexer=lexer, expires=expires, expire_type=expire_type,
lexer=lexer,
expires=expires,
expire_type=expire_type,
) )
# Custom formatter for the API response # Custom formatter for the API response
@ -397,7 +379,9 @@ def page_not_found(
return response return response
def server_error(request, template_name="dpaste/500.html"): def server_error(
request: WSGIRequest, template_name: str = "dpaste/500.html"
) -> HttpResponse:
context = {} context = {}
context.update(config.extra_template_context) context.update(config.extra_template_context)
response = render(request, template_name, context, status=500) response = render(request, template_name, context, status=500)