enable N rules for naming conventions (#4666)

* enable N rules for naming conventions

* fix pyi

* address comments

* remove unneeded code
This commit is contained in:
Thomas Brandého 2025-01-23 06:29:17 -08:00 committed by GitHub
parent 858a85a4dc
commit 7f1aee6dc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
31 changed files with 118 additions and 130 deletions

View File

@ -21,7 +21,7 @@ def get_package_size(venv_path: Path, os_name):
ValueError: when venv does not exist or python version is None. ValueError: when venv does not exist or python version is None.
""" """
python_version = get_python_version(venv_path, os_name) python_version = get_python_version(venv_path, os_name)
print("Python version:", python_version) # noqa: T201 print("Python version:", python_version)
if python_version is None: if python_version is None:
raise ValueError("Error: Failed to determine Python version.") raise ValueError("Error: Failed to determine Python version.")

View File

@ -85,15 +85,18 @@ build-backend = "poetry.core.masonry.api"
target-version = "py39" target-version = "py39"
output-format = "concise" output-format = "concise"
lint.isort.split-on-trailing-comma = false lint.isort.split-on-trailing-comma = false
lint.select = ["B", "C4", "D", "E", "ERA", "F", "FURB", "I", "PERF", "PTH", "RUF", "SIM", "T", "TRY", "W"] lint.select = ["B", "C4", "D", "E", "ERA", "F", "FURB", "I", "N", "PERF", "PTH", "RUF", "SIM", "T", "TRY", "W"]
lint.ignore = ["B008", "D205", "E501", "F403", "SIM115", "RUF006", "RUF012", "TRY0"] lint.ignore = ["B008", "D205", "E501", "F403", "SIM115", "RUF006", "RUF012", "TRY0"]
lint.pydocstyle.convention = "google" lint.pydocstyle.convention = "google"
[tool.ruff.lint.per-file-ignores] [tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"] "__init__.py" = ["F401"]
"tests/*.py" = ["D100", "D103", "D104", "B018", "PERF", "T"] "tests/*.py" = ["D100", "D103", "D104", "B018", "PERF", "T", "N"]
"benchmarks/*.py" = ["D100", "D103", "D104", "B018", "PERF", "T", "N"]
"reflex/.templates/*.py" = ["D100", "D103", "D104"] "reflex/.templates/*.py" = ["D100", "D103", "D104"]
"*.pyi" = ["D301", "D415", "D417", "D418", "E742"] "*.pyi" = ["D301", "D415", "D417", "D418", "E742", "N"]
"pyi_generator.py" = ["N802"]
"reflex/constants/*.py" = ["N"]
"*/blank.py" = ["I001"] "*/blank.py" = ["I001"]
[tool.pytest.ini_options] [tool.pytest.ini_options]

View File

@ -1197,11 +1197,11 @@ class App(MiddlewareMixin, LifespanMixin):
ValueError: If the custom exception handlers are invalid. ValueError: If the custom exception handlers are invalid.
""" """
FRONTEND_ARG_SPEC = { frontend_arg_spec = {
"exception": Exception, "exception": Exception,
} }
BACKEND_ARG_SPEC = { backend_arg_spec = {
"exception": Exception, "exception": Exception,
} }
@ -1209,8 +1209,8 @@ class App(MiddlewareMixin, LifespanMixin):
["frontend", "backend"], ["frontend", "backend"],
[self.frontend_exception_handler, self.backend_exception_handler], [self.frontend_exception_handler, self.backend_exception_handler],
[ [
FRONTEND_ARG_SPEC, frontend_arg_spec,
BACKEND_ARG_SPEC, backend_arg_spec,
], ],
): ):
if hasattr(handler_fn, "__name__"): if hasattr(handler_fn, "__name__"):

View File

@ -12,7 +12,7 @@ from typing import Callable, Coroutine, Set, Union
from fastapi import FastAPI from fastapi import FastAPI
from reflex.utils import console from reflex.utils import console
from reflex.utils.exceptions import InvalidLifespanTaskType from reflex.utils.exceptions import InvalidLifespanTaskTypeError
from .mixin import AppMixin from .mixin import AppMixin
@ -64,10 +64,10 @@ class LifespanMixin(AppMixin):
task_kwargs: The kwargs of the task. task_kwargs: The kwargs of the task.
Raises: Raises:
InvalidLifespanTaskType: If the task is a generator function. InvalidLifespanTaskTypeError: If the task is a generator function.
""" """
if inspect.isgeneratorfunction(task) or inspect.isasyncgenfunction(task): if inspect.isgeneratorfunction(task) or inspect.isasyncgenfunction(task):
raise InvalidLifespanTaskType( raise InvalidLifespanTaskTypeError(
f"Task {task.__name__} of type generator must be decorated with contextlib.asynccontextmanager." f"Task {task.__name__} of type generator must be decorated with contextlib.asynccontextmanager."
) )

View File

@ -14,7 +14,7 @@ class Html(Div):
""" """
# The HTML to render. # The HTML to render.
dangerouslySetInnerHTML: Var[Dict[str, str]] dangerouslySetInnerHTML: Var[Dict[str, str]] # noqa: N815
@classmethod @classmethod
def create(cls, *children, **props): def create(cls, *children, **props):

View File

@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Union
from reflex import constants from reflex import constants
from reflex.utils import imports from reflex.utils import imports
from reflex.utils.exceptions import DynamicComponentMissingLibrary from reflex.utils.exceptions import DynamicComponentMissingLibraryError
from reflex.utils.format import format_library_name from reflex.utils.format import format_library_name
from reflex.utils.serializers import serializer from reflex.utils.serializers import serializer
from reflex.vars import Var, get_unique_variable_name from reflex.vars import Var, get_unique_variable_name
@ -36,13 +36,15 @@ def bundle_library(component: Union["Component", str]):
component: The component to bundle the library with. component: The component to bundle the library with.
Raises: Raises:
DynamicComponentMissingLibrary: Raised when a dynamic component is missing a library. DynamicComponentMissingLibraryError: Raised when a dynamic component is missing a library.
""" """
if isinstance(component, str): if isinstance(component, str):
bundled_libraries.add(component) bundled_libraries.add(component)
return return
if component.library is None: if component.library is None:
raise DynamicComponentMissingLibrary("Component must have a library to bundle.") raise DynamicComponentMissingLibraryError(
"Component must have a library to bundle."
)
bundled_libraries.add(format_library_name(component.library)) bundled_libraries.add(format_library_name(component.library))

View File

@ -3,11 +3,13 @@
from typing import Any, Literal, Optional, Union from typing import Any, Literal, Optional, Union
from reflex.event import EventHandler, no_args_event_spec from reflex.event import EventHandler, no_args_event_spec
from reflex.utils import types from reflex.utils import console, types
from reflex.vars.base import Var from reflex.vars.base import Var
from .base import NextComponent from .base import NextComponent
DEFAULT_W_H = "100%"
class Image(NextComponent): class Image(NextComponent):
"""Display an image.""" """Display an image."""
@ -53,7 +55,7 @@ class Image(NextComponent):
loading: Var[Literal["lazy", "eager"]] loading: Var[Literal["lazy", "eager"]]
# A Data URL to be used as a placeholder image before the src image successfully loads. Only takes effect when combined with placeholder="blur". # A Data URL to be used as a placeholder image before the src image successfully loads. Only takes effect when combined with placeholder="blur".
blurDataURL: Var[str] blur_data_url: Var[str]
# Fires when the image has loaded. # Fires when the image has loaded.
on_load: EventHandler[no_args_event_spec] on_load: EventHandler[no_args_event_spec]
@ -80,8 +82,16 @@ class Image(NextComponent):
Returns: Returns:
_type_: _description_ _type_: _description_
""" """
if "blurDataURL" in props:
console.deprecate(
feature_name="blurDataURL",
reason="Use blur_data_url instead",
deprecation_version="0.7.0",
removal_version="0.8.0",
)
props["blur_data_url"] = props.pop("blurDataURL")
style = props.get("style", {}) style = props.get("style", {})
DEFAULT_W_H = "100%"
def check_prop_type(prop_name, prop_value): def check_prop_type(prop_name, prop_value):
if types.check_prop_in_allowed_types(prop_value, allowed_types=[int]): if types.check_prop_in_allowed_types(prop_value, allowed_types=[int]):

View File

@ -11,6 +11,8 @@ from reflex.vars.base import Var
from .base import NextComponent from .base import NextComponent
DEFAULT_W_H = "100%"
class Image(NextComponent): class Image(NextComponent):
@overload @overload
@classmethod @classmethod
@ -30,7 +32,7 @@ class Image(NextComponent):
loading: Optional[ loading: Optional[
Union[Literal["eager", "lazy"], Var[Literal["eager", "lazy"]]] Union[Literal["eager", "lazy"], Var[Literal["eager", "lazy"]]]
] = None, ] = None,
blurDataURL: Optional[Union[Var[str], str]] = None, blur_data_url: Optional[Union[Var[str], str]] = None,
style: Optional[Style] = None, style: Optional[Style] = None,
key: Optional[Any] = None, key: Optional[Any] = None,
id: Optional[Any] = None, id: Optional[Any] = None,
@ -71,7 +73,7 @@ class Image(NextComponent):
priority: When true, the image will be considered high priority and preload. Lazy loading is automatically disabled for images using priority. priority: When true, the image will be considered high priority and preload. Lazy loading is automatically disabled for images using priority.
placeholder: A placeholder to use while the image is loading. Possible values are blur, empty, or data:image/.... Defaults to empty. placeholder: A placeholder to use while the image is loading. Possible values are blur, empty, or data:image/.... Defaults to empty.
loading: The loading behavior of the image. Defaults to lazy. Can hurt performance, recommended to use `priority` instead. loading: The loading behavior of the image. Defaults to lazy. Can hurt performance, recommended to use `priority` instead.
blurDataURL: A Data URL to be used as a placeholder image before the src image successfully loads. Only takes effect when combined with placeholder="blur". blur_data_url: A Data URL to be used as a placeholder image before the src image successfully loads. Only takes effect when combined with placeholder="blur".
on_load: Fires when the image has loaded. on_load: Fires when the image has loaded.
on_error: Fires when the image has an error. on_error: Fires when the image has an error.
style: The style of the component. style: The style of the component.

View File

@ -485,11 +485,11 @@ to {
Returns: Returns:
The style of the component. The style of the component.
""" """
slideDown = LiteralVar.create( slide_down = LiteralVar.create(
"${slideDown} var(--animation-duration) var(--animation-easing)", "${slideDown} var(--animation-duration) var(--animation-easing)",
) )
slideUp = LiteralVar.create( slide_up = LiteralVar.create(
"${slideUp} var(--animation-duration) var(--animation-easing)", "${slideUp} var(--animation-duration) var(--animation-easing)",
) )
@ -503,8 +503,8 @@ to {
"display": "block", "display": "block",
"height": "var(--space-3)", "height": "var(--space-3)",
}, },
"&[data-state='open']": {"animation": slideDown}, "&[data-state='open']": {"animation": slide_down},
"&[data-state='closed']": {"animation": slideUp}, "&[data-state='closed']": {"animation": slide_up},
_inherited_variant_selector("classic"): { _inherited_variant_selector("classic"): {
"color": "var(--accent-contrast)", "color": "var(--accent-contrast)",
}, },

View File

@ -66,7 +66,7 @@ class DrawerRoot(DrawerComponent):
scroll_lock_timeout: Var[int] scroll_lock_timeout: Var[int]
# When `True`, it prevents scroll restoration. Defaults to `True`. # When `True`, it prevents scroll restoration. Defaults to `True`.
preventScrollRestoration: Var[bool] prevent_scroll_restoration: Var[bool]
# Enable background scaling, it requires container element with `vaul-drawer-wrapper` attribute to scale its background. # Enable background scaling, it requires container element with `vaul-drawer-wrapper` attribute to scale its background.
should_scale_background: Var[bool] should_scale_background: Var[bool]

View File

@ -81,7 +81,7 @@ class DrawerRoot(DrawerComponent):
snap_points: Optional[List[Union[float, str]]] = None, snap_points: Optional[List[Union[float, str]]] = None,
fade_from_index: Optional[Union[Var[int], int]] = None, fade_from_index: Optional[Union[Var[int], int]] = None,
scroll_lock_timeout: Optional[Union[Var[int], int]] = None, scroll_lock_timeout: Optional[Union[Var[int], int]] = None,
preventScrollRestoration: Optional[Union[Var[bool], bool]] = None, prevent_scroll_restoration: Optional[Union[Var[bool], bool]] = None,
should_scale_background: Optional[Union[Var[bool], bool]] = None, should_scale_background: Optional[Union[Var[bool], bool]] = None,
close_threshold: Optional[Union[Var[float], float]] = None, close_threshold: Optional[Union[Var[float], float]] = None,
as_child: Optional[Union[Var[bool], bool]] = None, as_child: Optional[Union[Var[bool], bool]] = None,
@ -129,7 +129,7 @@ class DrawerRoot(DrawerComponent):
snap_points: Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. Should go from least visible. Also Accept px values, which doesn't take screen height into account. snap_points: Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. Should go from least visible. Also Accept px values, which doesn't take screen height into account.
fade_from_index: Index of a snapPoint from which the overlay fade should be applied. Defaults to the last snap point. fade_from_index: Index of a snapPoint from which the overlay fade should be applied. Defaults to the last snap point.
scroll_lock_timeout: Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms scroll_lock_timeout: Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms
preventScrollRestoration: When `True`, it prevents scroll restoration. Defaults to `True`. prevent_scroll_restoration: When `True`, it prevents scroll restoration. Defaults to `True`.
should_scale_background: Enable background scaling, it requires container element with `vaul-drawer-wrapper` attribute to scale its background. should_scale_background: Enable background scaling, it requires container element with `vaul-drawer-wrapper` attribute to scale its background.
close_threshold: Number between 0 and 1 that determines when the drawer should be closed. close_threshold: Number between 0 and 1 that determines when the drawer should be closed.
as_child: Change the default rendered element for the one passed as a child. as_child: Change the default rendered element for the one passed as a child.
@ -567,7 +567,7 @@ class Drawer(ComponentNamespace):
snap_points: Optional[List[Union[float, str]]] = None, snap_points: Optional[List[Union[float, str]]] = None,
fade_from_index: Optional[Union[Var[int], int]] = None, fade_from_index: Optional[Union[Var[int], int]] = None,
scroll_lock_timeout: Optional[Union[Var[int], int]] = None, scroll_lock_timeout: Optional[Union[Var[int], int]] = None,
preventScrollRestoration: Optional[Union[Var[bool], bool]] = None, prevent_scroll_restoration: Optional[Union[Var[bool], bool]] = None,
should_scale_background: Optional[Union[Var[bool], bool]] = None, should_scale_background: Optional[Union[Var[bool], bool]] = None,
close_threshold: Optional[Union[Var[float], float]] = None, close_threshold: Optional[Union[Var[float], float]] = None,
as_child: Optional[Union[Var[bool], bool]] = None, as_child: Optional[Union[Var[bool], bool]] = None,
@ -615,7 +615,7 @@ class Drawer(ComponentNamespace):
snap_points: Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. Should go from least visible. Also Accept px values, which doesn't take screen height into account. snap_points: Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. Should go from least visible. Also Accept px values, which doesn't take screen height into account.
fade_from_index: Index of a snapPoint from which the overlay fade should be applied. Defaults to the last snap point. fade_from_index: Index of a snapPoint from which the overlay fade should be applied. Defaults to the last snap point.
scroll_lock_timeout: Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms scroll_lock_timeout: Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms
preventScrollRestoration: When `True`, it prevents scroll restoration. Defaults to `True`. prevent_scroll_restoration: When `True`, it prevents scroll restoration. Defaults to `True`.
should_scale_background: Enable background scaling, it requires container element with `vaul-drawer-wrapper` attribute to scale its background. should_scale_background: Enable background scaling, it requires container element with `vaul-drawer-wrapper` attribute to scale its background.
close_threshold: Number between 0 and 1 that determines when the drawer should be closed. close_threshold: Number between 0 and 1 that determines when the drawer should be closed.
as_child: Change the default rendered element for the one passed as a child. as_child: Change the default rendered element for the one passed as a child.

View File

@ -22,6 +22,8 @@ from ..base import (
LiteralButtonSize = Literal["1", "2", "3", "4"] LiteralButtonSize = Literal["1", "2", "3", "4"]
RADIX_TO_LUCIDE_SIZE = {"1": 12, "2": 24, "3": 36, "4": 48}
class IconButton(elements.Button, RadixLoadingProp, RadixThemesComponent): class IconButton(elements.Button, RadixLoadingProp, RadixThemesComponent):
"""A button designed specifically for usage with a single icon.""" """A button designed specifically for usage with a single icon."""
@ -72,8 +74,6 @@ class IconButton(elements.Button, RadixLoadingProp, RadixThemesComponent):
"IconButton requires a child icon. Pass a string as the first child or a rx.icon." "IconButton requires a child icon. Pass a string as the first child or a rx.icon."
) )
if "size" in props: if "size" in props:
RADIX_TO_LUCIDE_SIZE = {"1": 12, "2": 24, "3": 36, "4": 48}
if isinstance(props["size"], str): if isinstance(props["size"], str):
children[0].size = RADIX_TO_LUCIDE_SIZE[props["size"]] children[0].size = RADIX_TO_LUCIDE_SIZE[props["size"]]
else: else:

View File

@ -14,6 +14,7 @@ from reflex.vars.base import Var
from ..base import RadixLoadingProp, RadixThemesComponent from ..base import RadixLoadingProp, RadixThemesComponent
LiteralButtonSize = Literal["1", "2", "3", "4"] LiteralButtonSize = Literal["1", "2", "3", "4"]
RADIX_TO_LUCIDE_SIZE = {"1": 12, "2": 24, "3": 36, "4": 48}
class IconButton(elements.Button, RadixLoadingProp, RadixThemesComponent): class IconButton(elements.Button, RadixLoadingProp, RadixThemesComponent):
@overload @overload

View File

@ -28,6 +28,9 @@ LiteralStickyType = Literal[
] ]
ARIA_LABEL_KEY = "aria_label"
# The Tooltip inherits props from the Tooltip.Root, Tooltip.Portal, Tooltip.Content # The Tooltip inherits props from the Tooltip.Root, Tooltip.Portal, Tooltip.Content
class Tooltip(RadixThemesComponent): class Tooltip(RadixThemesComponent):
"""Floating element that provides a control with contextual information via pointer or focus.""" """Floating element that provides a control with contextual information via pointer or focus."""
@ -104,7 +107,6 @@ class Tooltip(RadixThemesComponent):
Returns: Returns:
The created component. The created component.
""" """
ARIA_LABEL_KEY = "aria_label"
if props.get(ARIA_LABEL_KEY) is not None: if props.get(ARIA_LABEL_KEY) is not None:
props[format.to_kebab_case(ARIA_LABEL_KEY)] = props.pop(ARIA_LABEL_KEY) props[format.to_kebab_case(ARIA_LABEL_KEY)] = props.pop(ARIA_LABEL_KEY)

View File

@ -14,6 +14,7 @@ from ..base import RadixThemesComponent
LiteralSideType = Literal["top", "right", "bottom", "left"] LiteralSideType = Literal["top", "right", "bottom", "left"]
LiteralAlignType = Literal["start", "center", "end"] LiteralAlignType = Literal["start", "center", "end"]
LiteralStickyType = Literal["partial", "always"] LiteralStickyType = Literal["partial", "always"]
ARIA_LABEL_KEY = "aria_label"
class Tooltip(RadixThemesComponent): class Tooltip(RadixThemesComponent):
@overload @overload

View File

@ -73,7 +73,7 @@ class Pie(Recharts):
data: Var[List[Dict[str, Any]]] data: Var[List[Dict[str, Any]]]
# Valid children components # Valid children components
_valid_children: List[str] = ["Cell", "LabelList"] _valid_children: List[str] = ["Cell", "LabelList", "Bare"]
# Stoke color. Default: rx.color("accent", 9) # Stoke color. Default: rx.color("accent", 9)
stroke: Var[Union[str, Color]] = LiteralVar.create(Color("accent", 9)) stroke: Var[Union[str, Color]] = LiteralVar.create(Color("accent", 9))

View File

@ -1,6 +1,6 @@
"""A component that wraps a recharts lib.""" """A component that wraps a recharts lib."""
from typing import Dict, Literal from typing import Literal
from reflex.components.component import Component, MemoizationLeaf, NoSSRComponent from reflex.components.component import Component, MemoizationLeaf, NoSSRComponent
@ -10,7 +10,7 @@ class Recharts(Component):
library = "recharts@2.15.0" library = "recharts@2.15.0"
def _get_style(self) -> Dict: def _get_style(self) -> dict:
return {"wrapperStyle": self.style} return {"wrapperStyle": self.style}

View File

@ -390,7 +390,7 @@ class EnvVar(Generic[T]):
os.environ[self.name] = str(value) os.environ[self.name] = str(value)
class env_var: # type: ignore class env_var: # type: ignore # noqa: N801
"""Descriptor for environment variables.""" """Descriptor for environment variables."""
name: str name: str
@ -823,16 +823,16 @@ class Config(Base):
if "api_url" not in self._non_default_attributes: if "api_url" not in self._non_default_attributes:
# If running in Github Codespaces, override API_URL # If running in Github Codespaces, override API_URL
codespace_name = os.getenv("CODESPACE_NAME") codespace_name = os.getenv("CODESPACE_NAME")
GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN = os.getenv( github_codespaces_port_forwarding_domain = os.getenv(
"GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN" "GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"
) )
# If running on Replit.com interactively, override API_URL to ensure we maintain the backend_port # If running on Replit.com interactively, override API_URL to ensure we maintain the backend_port
replit_dev_domain = os.getenv("REPLIT_DEV_DOMAIN") replit_dev_domain = os.getenv("REPLIT_DEV_DOMAIN")
backend_port = kwargs.get("backend_port", self.backend_port) backend_port = kwargs.get("backend_port", self.backend_port)
if codespace_name and GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN: if codespace_name and github_codespaces_port_forwarding_domain:
self.api_url = ( self.api_url = (
f"https://{codespace_name}-{kwargs.get('backend_port', self.backend_port)}" f"https://{codespace_name}-{kwargs.get('backend_port', self.backend_port)}"
f".{GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}" f".{github_codespaces_port_forwarding_domain}"
) )
elif replit_dev_domain and backend_port: elif replit_dev_domain and backend_port:
self.api_url = f"https://{replit_dev_domain}:{backend_port}" self.api_url = f"https://{replit_dev_domain}:{backend_port}"

View File

@ -534,10 +534,10 @@ class JavasciptKeyboardEvent:
"""Interface for a Javascript KeyboardEvent https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.""" """Interface for a Javascript KeyboardEvent https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent."""
key: str = "" key: str = ""
altKey: bool = False altKey: bool = False # noqa: N815
ctrlKey: bool = False ctrlKey: bool = False # noqa: N815
metaKey: bool = False metaKey: bool = False # noqa: N815
shiftKey: bool = False shiftKey: bool = False # noqa: N815
def input_event(e: Var[JavascriptInputEvent]) -> Tuple[Var[str]]: def input_event(e: Var[JavascriptInputEvent]) -> Tuple[Var[str]]:

View File

@ -26,7 +26,7 @@ def const(name, value) -> Var:
return Var(_js_expr=f"const {name} = {value}") return Var(_js_expr=f"const {name} = {value}")
def useCallback(func, deps) -> Var: def useCallback(func, deps) -> Var: # noqa: N802
"""Create a useCallback hook with a function and dependencies. """Create a useCallback hook with a function and dependencies.
Args: Args:
@ -42,7 +42,7 @@ def useCallback(func, deps) -> Var:
) )
def useContext(context) -> Var: def useContext(context) -> Var: # noqa: N802
"""Create a useContext hook with a context. """Create a useContext hook with a context.
Args: Args:
@ -57,7 +57,7 @@ def useContext(context) -> Var:
) )
def useRef(default) -> Var: def useRef(default) -> Var: # noqa: N802
"""Create a useRef hook with a default value. """Create a useRef hook with a default value.
Args: Args:
@ -72,7 +72,7 @@ def useRef(default) -> Var:
) )
def useState(var_name, default=None) -> Var: def useState(var_name, default=None) -> Var: # noqa: N802
"""Create a useState hook with a variable name and setter name. """Create a useState hook with a variable name and setter name.
Args: Args:

View File

@ -109,7 +109,7 @@ class DrawerSidebar(DrawerRoot):
snap_points: Optional[List[Union[float, str]]] = None, snap_points: Optional[List[Union[float, str]]] = None,
fade_from_index: Optional[Union[Var[int], int]] = None, fade_from_index: Optional[Union[Var[int], int]] = None,
scroll_lock_timeout: Optional[Union[Var[int], int]] = None, scroll_lock_timeout: Optional[Union[Var[int], int]] = None,
preventScrollRestoration: Optional[Union[Var[bool], bool]] = None, prevent_scroll_restoration: Optional[Union[Var[bool], bool]] = None,
should_scale_background: Optional[Union[Var[bool], bool]] = None, should_scale_background: Optional[Union[Var[bool], bool]] = None,
close_threshold: Optional[Union[Var[float], float]] = None, close_threshold: Optional[Union[Var[float], float]] = None,
as_child: Optional[Union[Var[bool], bool]] = None, as_child: Optional[Union[Var[bool], bool]] = None,

View File

@ -93,14 +93,14 @@ from reflex.event import (
) )
from reflex.utils import console, format, path_ops, prerequisites, types from reflex.utils import console, format, path_ops, prerequisites, types
from reflex.utils.exceptions import ( from reflex.utils.exceptions import (
ComputedVarShadowsBaseVars, ComputedVarShadowsBaseVarsError,
ComputedVarShadowsStateVar, ComputedVarShadowsStateVarError,
DynamicComponentInvalidSignature, DynamicComponentInvalidSignatureError,
DynamicRouteArgShadowsStateVar, DynamicRouteArgShadowsStateVarError,
EventHandlerShadowsBuiltInStateMethod, EventHandlerShadowsBuiltInStateMethodError,
ImmutableStateError, ImmutableStateError,
InvalidLockWarningThresholdError, InvalidLockWarningThresholdError,
InvalidStateManagerMode, InvalidStateManagerModeError,
LockExpiredError, LockExpiredError,
ReflexRuntimeError, ReflexRuntimeError,
SetUndefinedStateVarError, SetUndefinedStateVarError,
@ -815,7 +815,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
"""Check for shadow methods and raise error if any. """Check for shadow methods and raise error if any.
Raises: Raises:
EventHandlerShadowsBuiltInStateMethod: When an event handler shadows an inbuilt state method. EventHandlerShadowsBuiltInStateMethodError: When an event handler shadows an inbuilt state method.
""" """
overridden_methods = set() overridden_methods = set()
state_base_functions = cls._get_base_functions() state_base_functions = cls._get_base_functions()
@ -829,7 +829,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
overridden_methods.add(method.__name__) overridden_methods.add(method.__name__)
for method_name in overridden_methods: for method_name in overridden_methods:
raise EventHandlerShadowsBuiltInStateMethod( raise EventHandlerShadowsBuiltInStateMethodError(
f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead" f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
) )
@ -838,11 +838,11 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
"""Check for shadow base vars and raise error if any. """Check for shadow base vars and raise error if any.
Raises: Raises:
ComputedVarShadowsBaseVars: When a computed var shadows a base var. ComputedVarShadowsBaseVarsError: When a computed var shadows a base var.
""" """
for computed_var_ in cls._get_computed_vars(): for computed_var_ in cls._get_computed_vars():
if computed_var_._js_expr in cls.__annotations__: if computed_var_._js_expr in cls.__annotations__:
raise ComputedVarShadowsBaseVars( raise ComputedVarShadowsBaseVarsError(
f"The computed var name `{computed_var_._js_expr}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead" f"The computed var name `{computed_var_._js_expr}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead"
) )
@ -851,14 +851,14 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
"""Check for shadow computed vars and raise error if any. """Check for shadow computed vars and raise error if any.
Raises: Raises:
ComputedVarShadowsStateVar: When a computed var shadows another. ComputedVarShadowsStateVarError: When a computed var shadows another.
""" """
for name, cv in cls.__dict__.items(): for name, cv in cls.__dict__.items():
if not is_computed_var(cv): if not is_computed_var(cv):
continue continue
name = cv._js_expr name = cv._js_expr
if name in cls.inherited_vars or name in cls.inherited_backend_vars: if name in cls.inherited_vars or name in cls.inherited_backend_vars:
raise ComputedVarShadowsStateVar( raise ComputedVarShadowsStateVarError(
f"The computed var name `{cv._js_expr}` shadows a var in {cls.__module__}.{cls.__name__}; use a different name instead" f"The computed var name `{cv._js_expr}` shadows a var in {cls.__module__}.{cls.__name__}; use a different name instead"
) )
@ -1218,14 +1218,14 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
args: a dict of args args: a dict of args
Raises: Raises:
DynamicRouteArgShadowsStateVar: If a dynamic arg is shadowing an existing var. DynamicRouteArgShadowsStateVarError: If a dynamic arg is shadowing an existing var.
""" """
for arg in args: for arg in args:
if ( if (
arg in cls.computed_vars arg in cls.computed_vars
and not isinstance(cls.computed_vars[arg], DynamicRouteVar) and not isinstance(cls.computed_vars[arg], DynamicRouteVar)
) or arg in cls.base_vars: ) or arg in cls.base_vars:
raise DynamicRouteArgShadowsStateVar( raise DynamicRouteArgShadowsStateVarError(
f"Dynamic route arg '{arg}' is shadowing an existing var in {cls.__module__}.{cls.__name__}" f"Dynamic route arg '{arg}' is shadowing an existing var in {cls.__module__}.{cls.__name__}"
) )
for substate in cls.get_substates(): for substate in cls.get_substates():
@ -2353,8 +2353,7 @@ def dynamic(func: Callable[[T], Component]):
The dynamically generated component. The dynamically generated component.
Raises: Raises:
DynamicComponentInvalidSignature: If the function does not have exactly one parameter. DynamicComponentInvalidSignatureError: If the function does not have exactly one parameter or a type hint for the state class.
DynamicComponentInvalidSignature: If the function does not have a type hint for the state class.
""" """
number_of_parameters = len(inspect.signature(func).parameters) number_of_parameters = len(inspect.signature(func).parameters)
@ -2366,12 +2365,12 @@ def dynamic(func: Callable[[T], Component]):
values = list(func_signature.values()) values = list(func_signature.values())
if number_of_parameters != 1: if number_of_parameters != 1:
raise DynamicComponentInvalidSignature( raise DynamicComponentInvalidSignatureError(
"The function must have exactly one parameter, which is the state class." "The function must have exactly one parameter, which is the state class."
) )
if len(values) != 1: if len(values) != 1:
raise DynamicComponentInvalidSignature( raise DynamicComponentInvalidSignatureError(
"You must provide a type hint for the state class in the function." "You must provide a type hint for the state class in the function."
) )
@ -2875,7 +2874,7 @@ class StateManager(Base, ABC):
state: The state class to use. state: The state class to use.
Raises: Raises:
InvalidStateManagerMode: If the state manager mode is invalid. InvalidStateManagerModeError: If the state manager mode is invalid.
Returns: Returns:
The state manager (either disk, memory or redis). The state manager (either disk, memory or redis).
@ -2898,7 +2897,7 @@ class StateManager(Base, ABC):
lock_expiration=config.redis_lock_expiration, lock_expiration=config.redis_lock_expiration,
lock_warning_threshold=config.redis_lock_warning_threshold, lock_warning_threshold=config.redis_lock_warning_threshold,
) )
raise InvalidStateManagerMode( raise InvalidStateManagerModeError(
f"Expected one of: DISK, MEMORY, REDIS, got {config.state_manager_mode}" f"Expected one of: DISK, MEMORY, REDIS, got {config.state_manager_mode}"
) )
@ -4056,10 +4055,10 @@ def serialize_mutable_proxy(mp: MutableProxy):
return mp.__wrapped__ return mp.__wrapped__
_orig_json_JSONEncoder_default = json.JSONEncoder.default _orig_json_encoder_default = json.JSONEncoder.default
def _json_JSONEncoder_default_wrapper(self: json.JSONEncoder, o: Any) -> Any: def _json_encoder_default_wrapper(self: json.JSONEncoder, o: Any) -> Any:
"""Wrap JSONEncoder.default to handle MutableProxy objects. """Wrap JSONEncoder.default to handle MutableProxy objects.
Args: Args:
@ -4073,10 +4072,10 @@ def _json_JSONEncoder_default_wrapper(self: json.JSONEncoder, o: Any) -> Any:
return o.__wrapped__ return o.__wrapped__
except AttributeError: except AttributeError:
pass pass
return _orig_json_JSONEncoder_default(self, o) return _orig_json_encoder_default(self, o)
json.JSONEncoder.default = _json_JSONEncoder_default_wrapper json.JSONEncoder.default = _json_encoder_default_wrapper
class ImmutableMutableProxy(MutableProxy): class ImmutableMutableProxy(MutableProxy):

View File

@ -87,7 +87,7 @@ else:
# borrowed from py3.11 # borrowed from py3.11
class chdir(contextlib.AbstractContextManager): class chdir(contextlib.AbstractContextManager): # noqa: N801
"""Non thread-safe context manager to change the current working directory.""" """Non thread-safe context manager to change the current working directory."""
def __init__(self, path): def __init__(self, path):

View File

@ -42,10 +42,7 @@ def codespaces_port_forwarding_domain() -> str | None:
Returns: Returns:
The domain for port forwarding in Github Codespaces, or None if not running in Codespaces. The domain for port forwarding in Github Codespaces, or None if not running in Codespaces.
""" """
GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN = os.getenv( return os.getenv("GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN")
"GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"
)
return GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN
def is_running_in_codespaces() -> bool: def is_running_in_codespaces() -> bool:

View File

@ -11,7 +11,7 @@ class ConfigError(ReflexError):
"""Custom exception for config related errors.""" """Custom exception for config related errors."""
class InvalidStateManagerMode(ReflexError, ValueError): class InvalidStateManagerModeError(ReflexError, ValueError):
"""Raised when an invalid state manager mode is provided.""" """Raised when an invalid state manager mode is provided."""
@ -143,35 +143,35 @@ class EventFnArgMismatchError(ReflexError, TypeError):
"""Raised when the number of args required by an event handler is more than provided by the event trigger.""" """Raised when the number of args required by an event handler is more than provided by the event trigger."""
class DynamicRouteArgShadowsStateVar(ReflexError, NameError): class DynamicRouteArgShadowsStateVarError(ReflexError, NameError):
"""Raised when a dynamic route arg shadows a state var.""" """Raised when a dynamic route arg shadows a state var."""
class ComputedVarShadowsStateVar(ReflexError, NameError): class ComputedVarShadowsStateVarError(ReflexError, NameError):
"""Raised when a computed var shadows a state var.""" """Raised when a computed var shadows a state var."""
class ComputedVarShadowsBaseVars(ReflexError, NameError): class ComputedVarShadowsBaseVarsError(ReflexError, NameError):
"""Raised when a computed var shadows a base var.""" """Raised when a computed var shadows a base var."""
class EventHandlerShadowsBuiltInStateMethod(ReflexError, NameError): class EventHandlerShadowsBuiltInStateMethodError(ReflexError, NameError):
"""Raised when an event handler shadows a built-in state method.""" """Raised when an event handler shadows a built-in state method."""
class GeneratedCodeHasNoFunctionDefs(ReflexError): class GeneratedCodeHasNoFunctionDefsError(ReflexError):
"""Raised when refactored code generated with flexgen has no functions defined.""" """Raised when refactored code generated with flexgen has no functions defined."""
class PrimitiveUnserializableToJSON(ReflexError, ValueError): class PrimitiveUnserializableToJSONError(ReflexError, ValueError):
"""Raised when a primitive type is unserializable to JSON. Usually with NaN and Infinity.""" """Raised when a primitive type is unserializable to JSON. Usually with NaN and Infinity."""
class InvalidLifespanTaskType(ReflexError, TypeError): class InvalidLifespanTaskTypeError(ReflexError, TypeError):
"""Raised when an invalid task type is registered as a lifespan task.""" """Raised when an invalid task type is registered as a lifespan task."""
class DynamicComponentMissingLibrary(ReflexError, ValueError): class DynamicComponentMissingLibraryError(ReflexError, ValueError):
"""Raised when a dynamic component is missing a library.""" """Raised when a dynamic component is missing a library."""
@ -187,7 +187,7 @@ class EnvironmentVarValueError(ReflexError, ValueError):
"""Raised when an environment variable is set to an invalid value.""" """Raised when an environment variable is set to an invalid value."""
class DynamicComponentInvalidSignature(ReflexError, TypeError): class DynamicComponentInvalidSignatureError(ReflexError, TypeError):
"""Raised when a dynamic component has an invalid signature.""" """Raised when a dynamic component has an invalid signature."""

View File

@ -364,11 +364,11 @@ def run_uvicorn_backend_prod(host, port, loglevel):
app_module = get_app_module() app_module = get_app_module()
RUN_BACKEND_PROD = f"gunicorn --worker-class {config.gunicorn_worker_class} --max-requests {config.gunicorn_max_requests} --max-requests-jitter {config.gunicorn_max_requests_jitter} --preload --timeout {config.timeout} --log-level critical".split() run_backend_prod = f"gunicorn --worker-class {config.gunicorn_worker_class} --max-requests {config.gunicorn_max_requests} --max-requests-jitter {config.gunicorn_max_requests_jitter} --preload --timeout {config.timeout} --log-level critical".split()
RUN_BACKEND_PROD_WINDOWS = f"uvicorn --limit-max-requests {config.gunicorn_max_requests} --timeout-keep-alive {config.timeout}".split() run_backend_prod_windows = f"uvicorn --limit-max-requests {config.gunicorn_max_requests} --timeout-keep-alive {config.timeout}".split()
command = ( command = (
[ [
*RUN_BACKEND_PROD_WINDOWS, *run_backend_prod_windows,
"--host", "--host",
host, host,
"--port", "--port",
@ -377,7 +377,7 @@ def run_uvicorn_backend_prod(host, port, loglevel):
] ]
if constants.IS_WINDOWS if constants.IS_WINDOWS
else [ else [
*RUN_BACKEND_PROD, *run_backend_prod,
"--bind", "--bind",
f"{host}:{port}", f"{host}:{port}",
"--threads", "--threads",

View File

@ -38,7 +38,7 @@ from reflex.compiler import templates
from reflex.config import Config, environment, get_config from reflex.config import Config, environment, get_config
from reflex.utils import console, net, path_ops, processes, redir from reflex.utils import console, net, path_ops, processes, redir
from reflex.utils.exceptions import ( from reflex.utils.exceptions import (
GeneratedCodeHasNoFunctionDefs, GeneratedCodeHasNoFunctionDefsError,
SystemPackageMissingError, SystemPackageMissingError,
) )
from reflex.utils.format import format_library_name from reflex.utils.format import format_library_name
@ -1816,7 +1816,7 @@ def initialize_main_module_index_from_generation(app_name: str, generation_hash:
generation_hash: The generation hash from reflex.build. generation_hash: The generation hash from reflex.build.
Raises: Raises:
GeneratedCodeHasNoFunctionDefs: If the fetched code has no function definitions GeneratedCodeHasNoFunctionDefsError: If the fetched code has no function definitions
(the refactored reflex code is expected to have at least one root function defined). (the refactored reflex code is expected to have at least one root function defined).
""" """
# Download the reflex code for the generation. # Download the reflex code for the generation.
@ -1833,7 +1833,7 @@ def initialize_main_module_index_from_generation(app_name: str, generation_hash:
# Determine the name of the last function, which renders the generated code. # Determine the name of the last function, which renders the generated code.
defined_funcs = re.findall(r"def ([a-zA-Z_]+)\(", resp.text) defined_funcs = re.findall(r"def ([a-zA-Z_]+)\(", resp.text)
if not defined_funcs: if not defined_funcs:
raise GeneratedCodeHasNoFunctionDefs( raise GeneratedCodeHasNoFunctionDefsError(
f"No function definitions found in generated code from {url!r}." f"No function definitions found in generated code from {url!r}."
) )
render_func_name = defined_funcs[-1] render_func_name = defined_funcs[-1]

View File

@ -1555,7 +1555,7 @@ def figure_out_type(value: Any) -> types.GenericType:
return type(value) return type(value)
class cached_property_no_lock(functools.cached_property): class cached_property_no_lock(functools.cached_property): # noqa: N801
"""A special version of functools.cached_property that does not use a lock.""" """A special version of functools.cached_property that does not use a lock."""
def __init__(self, func): def __init__(self, func):

View File

@ -18,7 +18,7 @@ from typing import (
) )
from reflex.constants.base import Dirs from reflex.constants.base import Dirs
from reflex.utils.exceptions import PrimitiveUnserializableToJSON, VarTypeError from reflex.utils.exceptions import PrimitiveUnserializableToJSONError, VarTypeError
from reflex.utils.imports import ImportDict, ImportVar from reflex.utils.imports import ImportDict, ImportVar
from .base import ( from .base import (
@ -987,10 +987,10 @@ class LiteralNumberVar(LiteralVar, NumberVar):
The JSON representation of the var. The JSON representation of the var.
Raises: Raises:
PrimitiveUnserializableToJSON: If the var is unserializable to JSON. PrimitiveUnserializableToJSONError: If the var is unserializable to JSON.
""" """
if math.isinf(self._var_value) or math.isnan(self._var_value): if math.isinf(self._var_value) or math.isnan(self._var_value):
raise PrimitiveUnserializableToJSON( raise PrimitiveUnserializableToJSONError(
f"No valid JSON representation for {self}" f"No valid JSON representation for {self}"
) )
return json.dumps(self._var_value) return json.dumps(self._var_value)

View File

@ -1,11 +1,8 @@
"""Test fixtures.""" """Test fixtures."""
import asyncio import asyncio
import contextlib
import os
import platform import platform
import uuid import uuid
from pathlib import Path
from typing import Dict, Generator, Type from typing import Dict, Generator, Type
from unittest import mock from unittest import mock
@ -14,6 +11,7 @@ import pytest
from reflex.app import App from reflex.app import App
from reflex.event import EventSpec from reflex.event import EventSpec
from reflex.model import ModelRegistry from reflex.model import ModelRegistry
from reflex.testing import chdir
from reflex.utils import prerequisites from reflex.utils import prerequisites
from .states import ( from .states import (
@ -191,33 +189,6 @@ def router_data(router_data_headers) -> Dict[str, str]:
} }
# borrowed from py3.11
class chdir(contextlib.AbstractContextManager):
"""Non thread-safe context manager to change the current working directory."""
def __init__(self, path):
"""Prepare contextmanager.
Args:
path: the path to change to
"""
self.path = path
self._old_cwd = []
def __enter__(self):
"""Save current directory and perform chdir."""
self._old_cwd.append(Path.cwd())
os.chdir(self.path)
def __exit__(self, *excinfo):
"""Change back to previous directory on stack.
Args:
excinfo: sys.exc_info captured in the context block
"""
os.chdir(self._old_cwd.pop())
@pytest.fixture @pytest.fixture
def tmp_working_dir(tmp_path): def tmp_working_dir(tmp_path):
"""Create a temporary directory and chdir to it. """Create a temporary directory and chdir to it.

View File

@ -12,7 +12,7 @@ from reflex.base import Base
from reflex.constants.base import REFLEX_VAR_CLOSING_TAG, REFLEX_VAR_OPENING_TAG from reflex.constants.base import REFLEX_VAR_CLOSING_TAG, REFLEX_VAR_OPENING_TAG
from reflex.state import BaseState from reflex.state import BaseState
from reflex.utils.exceptions import ( from reflex.utils.exceptions import (
PrimitiveUnserializableToJSON, PrimitiveUnserializableToJSONError,
UntypedComputedVarError, UntypedComputedVarError,
) )
from reflex.utils.imports import ImportVar from reflex.utils.imports import ImportVar
@ -1061,7 +1061,7 @@ def test_inf_and_nan(var, expected_js):
assert str(var) == expected_js assert str(var) == expected_js
assert isinstance(var, NumberVar) assert isinstance(var, NumberVar)
assert isinstance(var, LiteralVar) assert isinstance(var, LiteralVar)
with pytest.raises(PrimitiveUnserializableToJSON): with pytest.raises(PrimitiveUnserializableToJSONError):
var.json() var.json()