Explorar o código

escape type info in error messages (#5107)

Khaleel Al-Adhami hai 1 mes
pai
achega
7da9697bbd
Modificáronse 3 ficheiros con 7 adicións e 4 borrados
  1. 2 1
      reflex/components/component.py
  2. 3 2
      reflex/state.py
  3. 2 1
      reflex/vars/base.py

+ 2 - 1
reflex/components/component.py

@@ -30,6 +30,7 @@ from typing import (
 )
 
 import pydantic.v1
+from rich.markup import escape
 
 import reflex.state
 from reflex.base import Base
@@ -217,7 +218,7 @@ def satisfies_type_hint(obj: Any, type_hint: Any) -> bool:
         console.deprecate(
             "implicit-none-for-component-fields",
             reason="Passing Vars with possible None values to component fields not explicitly marked as Optional is deprecated. "
-            + f"Passed {obj!s} of type {type(obj) if not isinstance(obj, Var) else obj._var_type} to {type_hint}.",
+            + f"Passed {obj!s} of type {escape(str(type(obj) if not isinstance(obj, Var) else obj._var_type))} to {escape(str(type_hint))}.",
             deprecation_version="0.7.2",
             removal_version="0.8.0",
         )

+ 3 - 2
reflex/state.py

@@ -48,6 +48,7 @@ from pydantic.v1.fields import ModelField
 from redis.asyncio import Redis
 from redis.asyncio.client import PubSub
 from redis.exceptions import ResponseError
+from rich.markup import escape
 from sqlalchemy.orm import DeclarativeBase
 from typing_extensions import Self
 
@@ -698,7 +699,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
 
             if not _isinstance(result, of_type, nested=1, treat_var_as_type=False):
                 console.warn(
-                    f"Inline ComputedVar {f} expected type {of_type}, got {type(result)}. "
+                    f"Inline ComputedVar {f} expected type {escape(str(of_type))}, got {type(result)}. "
                     "You can specify expected type with `of_type` argument."
                 )
 
@@ -1364,7 +1365,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
             field_type = _unwrap_field_type(true_type_for_pydantic_field(field))
             if not _isinstance(value, field_type, nested=1, treat_var_as_type=False):
                 console.error(
-                    f"Expected field '{type(self).__name__}.{name}' to receive type '{field_type}',"
+                    f"Expected field '{type(self).__name__}.{name}' to receive type '{escape(str(field_type))}',"
                     f" but got '{value}' of type '{type(value)}'."
                 )
 

+ 2 - 1
reflex/vars/base.py

@@ -44,6 +44,7 @@ from typing import (
     overload,
 )
 
+from rich.markup import escape
 from sqlalchemy.orm import DeclarativeBase
 from typing_extensions import deprecated, override
 
@@ -2371,7 +2372,7 @@ class ComputedVar(Var[RETURN_TYPE]):
         if not _isinstance(value, self._var_type, nested=1, treat_var_as_type=False):
             console.error(
                 f"Computed var '{type(instance).__name__}.{self._js_expr}' must return"
-                f" a value of type '{self._var_type}', got '{value!s}' of type {type(value)}."
+                f" a value of type '{escape(str(self._var_type))}', got '{value!s}' of type {type(value)}."
             )
 
     def _deps(