From 4635343544537c161302752120d03fec65c3cabe Mon Sep 17 00:00:00 2001 From: LuoChen Date: Sun, 14 Jul 2024 16:37:12 +0800 Subject: [PATCH] fix: typing issues of funix() --- backend/funix/decorator/__init__.py | 11 ++++++++--- backend/funix/hint/__init__.py | 14 +++++++------- backend/funix/py.typed | 0 pyproject.toml | 10 ++++++++++ 4 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 backend/funix/py.typed diff --git a/backend/funix/decorator/__init__.py b/backend/funix/decorator/__init__.py index 84e4177a..376e61f5 100644 --- a/backend/funix/decorator/__init__.py +++ b/backend/funix/decorator/__init__.py @@ -8,7 +8,7 @@ from inspect import getsource, isgeneratorfunction, signature from secrets import token_hex from types import ModuleType -from typing import Callable, Optional, Union +from typing import Callable, Optional, Union, ParamSpec, TypeVar from uuid import uuid4 from funix.app import app, sock @@ -189,6 +189,11 @@ def object_is_handled(object_id: int) -> bool: return object_id in handled_object +P = ParamSpec("P") +R = TypeVar("R") + +RateLimiter = Union[Limiter, list['RateLimiter'], dict[str, 'RateLimiter'], None] + def funix( path: Optional[str] = None, title: Optional[str] = None, @@ -211,7 +216,7 @@ def funix( pre_fill: PreFillType = None, menu: Optional[str] = None, default: bool = False, - rate_limit: Union[Limiter, list, dict, None] = None, + rate_limit: RateLimiter = None, reactive: ReactiveType = None, print_to_web: bool = False, autorun: bool = False, @@ -268,7 +273,7 @@ def funix( Check code for details """ - def decorator(function: Callable) -> callable: + def decorator(function: Callable[P, R]) -> Callable[P, R]: """ Decorator for functions to convert them to web apps diff --git a/backend/funix/hint/__init__.py b/backend/funix/hint/__init__.py index ee60370a..07e9ae8c 100644 --- a/backend/funix/hint/__init__.py +++ b/backend/funix/hint/__init__.py @@ -23,7 +23,7 @@ For yodas only, the destination of the page. """ -Parameters = str | tuple +Parameters = str | tuple[str, ...] """ Parameters. @@ -51,9 +51,9 @@ """ WidgetsValue = ( - list[AcceptableWidgets | tuple[AcceptableWidgets, dict]] + list[AcceptableWidgets | tuple[AcceptableWidgets, dict[str, Any]]] | AcceptableWidgets - | tuple[AcceptableWidgets, dict] + | tuple[AcceptableWidgets, dict[str, Any]] ) """ The value of the `widgets`. @@ -96,7 +96,7 @@ {("a", "b"): "cell"} -> The parameter `a` and `b` are cells. """ -WhitelistValues = list[list] | list +WhitelistValues = list[list[str]] | list[str] """ The value of the `whitelist`. @@ -115,7 +115,7 @@ `["a", "b"]`, for `b` the whitelist is `["c", "d"]`. """ -ExamplesValues = list[list] | list +ExamplesValues = list[list[str]] | list[str] """ The value of the `examples`. @@ -226,7 +226,7 @@ class ConditionalVisible(TypedDict): {"a": {"widget": "sheet"}} -> The parameter `a` has a widget, and the widget is `sheet`. """ -PreFillArgumentFrom = Callable | tuple[Callable, int] +PreFillArgumentFrom = Callable[..., Any] | tuple[Callable[..., Any], int] """ Pre-fill argument from. @@ -246,7 +246,7 @@ class ConditionalVisible(TypedDict): PreFillEmpty = TypeVar("PreFillEmpty") -ReactiveType = Optional[dict[str, Callable | tuple[Callable, dict[str, str]]]] +ReactiveType = Optional[dict[str, Callable[..., Any] | tuple[Callable[..., Any], dict[str, str]]]] """ Document is on the way """ diff --git a/backend/funix/py.typed b/backend/funix/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/pyproject.toml b/pyproject.toml index 0ecb7be4..bb7b5ed2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,5 +51,15 @@ homepage = "https://github.com/TexteaInc/funix" [project.scripts] funix = "funix.__main__:cli_main" +[tool.pyright] +include = ["src"] +exclude = ["**/node_modules", "**/__pycache__"] +strict = ["src"] +typeCheckingMode = "strict" + +useLibraryCodeForTypes = true +reportMissingImports = true +reportMissingTypeStubs = false + [tool.setuptools.packages.find] where = ["backend"]