Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type hint #45

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions fastapi_limiter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from math import ceil
from typing import Callable, Union
from typing import Callable, Optional, Union

from fastapi import HTTPException
from starlette.requests import Request
Expand Down Expand Up @@ -46,11 +46,11 @@ async def ws_default_callback(ws: WebSocket, pexpire: int):

class FastAPILimiter:
redis = None
prefix: str = None
lua_sha: str = None
identifier: Callable = None
http_callback: Callable = None
ws_callback: Callable = None
prefix: Optional[str] = None
lua_sha: Optional[str] = None
identifier: Optional[Callable] = None
http_callback: Optional[Callable] = None
ws_callback: Optional[Callable] = None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Optional

lua_script = """local key = KEYS[1]
local limit = tonumber(ARGV[1])
local expire_time = ARGV[2]
Expand All @@ -76,7 +76,7 @@ async def init(
identifier: Callable = default_identifier,
http_callback: Callable = http_default_callback,
ws_callback: Callable = ws_default_callback,
):
) -> None:
cls.redis = redis
cls.prefix = prefix
cls.identifier = identifier
Expand All @@ -85,5 +85,5 @@ async def init(
cls.lua_sha = await redis.script_load(cls.lua_script)

@classmethod
async def close(cls):
async def close(cls) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my project mypy says...

error: Call to untyped function "close" of "FastAPILimiter" in typed context  [no-untyped-call]

await cls.redis.close()
14 changes: 7 additions & 7 deletions fastapi_limiter/depends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable, Optional
from typing import Annotated, Callable, Optional

from pydantic import conint
from pydantic import Field
from starlette.requests import Request
from starlette.responses import Response
from starlette.websockets import WebSocket
Expand All @@ -12,11 +12,11 @@
class RateLimiter:
def __init__(
self,
times: conint(ge=0) = 1,
milliseconds: conint(ge=-1) = 0,
seconds: conint(ge=-1) = 0,
minutes: conint(ge=-1) = 0,
hours: conint(ge=-1) = 0,
times: Annotated[int, Field(ge=0)] = 1,
milliseconds: Annotated[int, Field(ge=-1)] = 0,
seconds: Annotated[int, Field(ge=-1)] = 0,
minutes: Annotated[int, Field(ge=-1)] = 0,
hours: Annotated[int, Field(ge=-1)] = 0,
identifier: Optional[Callable] = None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback: Optional[Callable] = None,
):
Expand Down
Loading