middleware: Fix exception typing.

Mypy seems to have trouble understanding `Exception` inheritance
here, so we create a `Union` for the only `Exception` we are
looking for.
This commit is contained in:
Wyatt Hoodes 2019-07-26 11:57:18 -10:00 committed by Tim Abbott
parent 167d0bad61
commit b807c4273e
1 changed files with 8 additions and 2 deletions

View File

@ -3,7 +3,8 @@ import logging
import time
import traceback
from typing import Any, AnyStr, Dict, \
Iterable, List, MutableMapping, Optional
Iterable, List, MutableMapping, Optional, \
Union
from django.conf import settings
from django.contrib.sessions.backends.base import UpdateError
@ -337,7 +338,12 @@ class RateLimitMiddleware(MiddlewareMixin):
response['X-RateLimit-Remaining'] = str(ratelimit_user_results['remaining'])
return response
def process_exception(self, request: HttpRequest, exception: Exception) -> Optional[HttpResponse]:
# TODO: When we have Django stubs, we should be able to fix the
# type of exception back to just Exception; the problem is without
# stubs, mypy doesn't know that RateLimited's superclass
# PermissionDenied inherits from Exception.
def process_exception(self, request: HttpRequest,
exception: Union[Exception, RateLimited]) -> Optional[HttpResponse]:
if isinstance(exception, RateLimited):
entity_type = str(exception) # entity type is passed to RateLimited when raising
resp = json_error(