From 4fec267cbe4b0dbd9a5ea5b5c22fb6550d0dc94d Mon Sep 17 00:00:00 2001 From: Martin Mahner Date: Thu, 4 Jun 2020 08:45:28 +0200 Subject: [PATCH] Further typing cleanup --- .gitignore | 1 + dpaste/forms.py | 4 ++-- dpaste/views.py | 38 +++++++++++--------------------------- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index ca5b1de..46cb02b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ dpaste.egg-info dpaste.sqlite node_modules monkeytype.sqlite3 +.mypy_cache/ diff --git a/dpaste/forms.py b/dpaste/forms.py index 2b555ba..6a81f25 100644 --- a/dpaste/forms.py +++ b/dpaste/forms.py @@ -1,5 +1,5 @@ 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.apps import apps @@ -89,7 +89,7 @@ class SnippetForm(forms.ModelForm): self.cleaned_data["expire_type"] = expire_type 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, this is likely spam. diff --git a/dpaste/views.py b/dpaste/views.py index 7a7c69e..cde9ad6 100644 --- a/dpaste/views.py +++ b/dpaste/views.py @@ -96,9 +96,7 @@ class SnippetDetailView(DetailView, FormView): always expire. """ if "delete" in self.request.POST: - snippet = get_object_or_404( - Snippet, secret_id=self.kwargs["snippet_id"] - ) + snippet = get_object_or_404(Snippet, secret_id=self.kwargs["snippet_id"]) snippet.delete() # Append `#` so #delete goes away in Firefox @@ -138,7 +136,7 @@ class SnippetDetailView(DetailView, FormView): return response - def get_initial(self) -> Dict[str, Union[str, bool]]: + def get_initial(self) -> Dict[str, Any]: snippet = self.get_object() return { "content": snippet.content, @@ -146,14 +144,7 @@ class SnippetDetailView(DetailView, FormView): "rtl": snippet.rtl, } - def get_form_kwargs( - self, - ) -> Dict[ - str, - Optional[ - Union[Dict[str, Union[str, bool]], MultiValueDict, WSGIRequest] - ], - ]: + def get_form_kwargs(self,) -> Dict[str, Dict[str, Any]]: kwargs = super().get_form_kwargs() kwargs.update({"request": self.request}) return kwargs @@ -162,7 +153,7 @@ class SnippetDetailView(DetailView, FormView): snippet = form.save(parent=self.get_object()) return HttpResponseRedirect(snippet.get_absolute_url()) - def get_snippet_diff(self) -> None: + def get_snippet_diff(self) -> str: snippet = self.get_object() if not snippet.parent_id: @@ -252,9 +243,7 @@ class SnippetHistory(TemplateView): add_never_cache_headers(response) return response - def post( - self, request: WSGIRequest, *args, **kwargs - ) -> HttpResponseRedirect: + def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponseRedirect: """ Delete all user snippets at once. """ @@ -265,9 +254,7 @@ class SnippetHistory(TemplateView): url = "{0}#".format(reverse("snippet_history")) return HttpResponseRedirect(url) - def get_context_data( - self, **kwargs - ) -> Dict[str, Union["SnippetHistory", QuerySet, str]]: + def get_context_data(self, **kwargs) -> Dict[str, Any]: ctx = super().get_context_data(**kwargs) ctx.update({"snippet_list": self.get_user_snippets()}) ctx.update(config.extra_template_context) @@ -357,16 +344,11 @@ class APIView(View): ) expires, expire_type = get_expire_values(expires) else: - expires = datetime.datetime.now() + datetime.timedelta( - seconds=60 * 60 * 24 - ) + expires = datetime.datetime.now() + datetime.timedelta(seconds=60 * 60 * 24) expire_type = Snippet.EXPIRE_TIME snippet = Snippet.objects.create( - content=content, - lexer=lexer, - expires=expires, - expire_type=expire_type, + content=content, lexer=lexer, expires=expires, expire_type=expire_type, ) # Custom formatter for the API response @@ -397,7 +379,9 @@ def page_not_found( 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.update(config.extra_template_context) response = render(request, template_name, context, status=500)