Skip to content

Commit

Permalink
Fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sanders41 committed Jan 10, 2023
1 parent 4079399 commit 9938792
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions slowapi/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def emit(*_):
C.HEADERS_ENABLED, False
)
self._storage_options.update(self.get_app_config(C.STORAGE_OPTIONS, {}))
self._storage: Union[Storage, AsyncStorage] = storage_from_string(
self._storage = storage_from_string(
self._storage_uri or self.get_app_config(C.STORAGE_URL, "memory://"),
**self._storage_options,
)
Expand Down Expand Up @@ -334,7 +334,11 @@ def get_app_config(self, key: str, default_value: T = None) -> T:
"""
Place holder until we find a better way to load config from app
"""
return self.app_config(key, default=default_value, cast=type(default_value))
return (
self.app_config(key, default=default_value, cast=type(default_value))
if default_value
else self.app_config(key, default=default_value)
)

def __should_check_backend(self) -> bool:
if self.__check_backend_count > MAX_BACKEND_CHECKS:
Expand Down
12 changes: 10 additions & 2 deletions slowapi/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from starlette.requests import Request


Expand All @@ -11,11 +13,17 @@ def get_ipaddr(request: Request) -> str:
if "X_FORWARDED_FOR" in request.headers:
return request.headers["X_FORWARDED_FOR"]
else:
return request.client.host or "127.0.0.1"
if not request.client or not request.client.host:
return "127.0.0.1"

return request.client.host


def get_remote_address(request: Request) -> str:
"""
Returns the ip address for the current request (or 127.0.0.1 if none found)
"""
return request.client.host or "127.0.0.1"
if not request.client or not request.client.host:
return "127.0.0.1"

return request.client.host

0 comments on commit 9938792

Please sign in to comment.