Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion reflex/utils/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

# Console for pretty printing.
_console = Console()
_console_stderr = Console(stderr=True)

# The current log level.
_LOG_LEVEL = LogLevel.INFO
Expand Down Expand Up @@ -96,6 +97,21 @@ def print(msg: str, *, dedupe: bool = False, **kwargs):
_console.print(msg, **kwargs)


def _print_stderr(msg: str, *, dedupe: bool = False, **kwargs):
"""Print a message to stderr.

Args:
msg: The message to print.
dedupe: If True, suppress multiple console logs of print message.
kwargs: Keyword arguments to pass to the print function.
"""
if dedupe:
if msg in _EMITTED_PRINTS:
return
_EMITTED_PRINTS.add(msg)
_console_stderr.print(msg, **kwargs)


@once
def log_file_console():
"""Create a console that logs to a file.
Expand Down Expand Up @@ -342,7 +358,7 @@ def error(msg: str, *, dedupe: bool = False, **kwargs):
if msg in _EMITTED_ERRORS:
return
_EMITTED_ERRORS.add(msg)
print(f"[red]{msg}[/red]", **kwargs)
_print_stderr(f"[red]{msg}[/red]", **kwargs)
if should_use_log_file_console():
print_to_log_file(f"[red]{msg}[/red]", **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ def __get__(self, instance: BaseState | None, owner: type):
def _check_deprecated_return_type(self, instance: BaseState, value: Any) -> None:
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"Computed var '{type(instance).__name__}.{self._name}' must return"
f" a value of type '{escape(str(self._var_type))}', got '{value!s}' of type {type(value)}."
)

Expand Down Expand Up @@ -2395,7 +2395,7 @@ def _deps(
except Exception as e:
console.warn(
"Failed to automatically determine dependencies for computed var "
f"{objclass.__name__}.{self._js_expr}: {e}. "
f"{objclass.__name__}.{self._name}: {e}. "
"Provide static_deps and set auto_deps=False to suppress this warning."
)
return d
Expand Down
4 changes: 2 additions & 2 deletions tests/units/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ def reset(self):


@pytest.mark.asyncio
async def test_state_with_invalid_yield(capsys, mock_app):
async def test_state_with_invalid_yield(capsys: pytest.CaptureFixture[str], mock_app):
"""Test that an error is thrown when a state yields an invalid value.
Args:
Expand Down Expand Up @@ -1664,7 +1664,7 @@ def invalid_handler(self):
token="",
)
captured = capsys.readouterr()
assert "must only return/yield: None, Events or other EventHandlers" in captured.out
assert "must only return/yield: None, Events or other EventHandlers" in captured.err


@pytest_asyncio.fixture(
Expand Down
Loading