Repository navigation
Closed
Description
Describe the bug
If a module has from __future__ import annotations at the top, then event signature functions do not resolve correctly.
To Reproduce
# from __future__ import annotations
from typing import Any, Dict, List
import reflex as rx
class FooEvent(rx.Base):
foo: str
def foo_signature(event: FooEvent) -> List[Any]:
return [event.foo]
class FooComponent(rx.Fragment):
def get_event_triggers(self) -> Dict[str, Any]:
return {"on_foo": foo_signature}
def index() -> rx.Component:
return FooComponent.create(on_foo=rx.console_log)
app = rx.App()
app.add_page(index)Expected behavior
It should work the same whether or not from __future__ import annotations is present.
Screenshots
If the top line is uncommented, then compiling gives the following trace
Process SpawnProcess-4:
Traceback (most recent call last):
File "/opt/homebrew/Cellar/[email protected]/3.11.7_2/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
self.run()
File "/opt/homebrew/Cellar/[email protected]/3.11.7_2/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/Users/masenf/code/reflex-dev/VENV-web/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "/Users/masenf/code/reflex-dev/VENV-web/lib/python3.11/site-packages/uvicorn/server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.7_2/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.7_2/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.7_2/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/VENV-web/lib/python3.11/site-packages/uvicorn/server.py", line 67, in serve
config.load()
File "/Users/masenf/code/reflex-dev/VENV-web/lib/python3.11/site-packages/uvicorn/config.py", line 477, in load
self.loaded_app = import_from_string(self.app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/VENV-web/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.7_2/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/masenf/code/reflex-dev/reflex/reflex/app_module_for_backend.py", line 13, in <module>
app_module = get_app(reload=False)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/utils/prerequisites.py", line 190, in get_app
app = __import__(module, fromlist=(constants.CompileVars.APP,))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/repro-event-trigger-signature/repro_event_trigger_signature/repro_event_trigger_signature.py", line 25, in <module>
app.add_page(index)
File "/Users/masenf/code/reflex-dev/reflex/reflex/app.py", line 454, in add_page
component = self._generate_component(component)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/app.py", line 394, in _generate_component
return component if isinstance(component, Component) else component()
^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/repro-event-trigger-signature/repro_event_trigger_signature/repro_event_trigger_signature.py", line 21, in index
return FooComponent.create(on_foo=rx.console_log)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/components/component.py", line 636, in create
return cls(children=children, **props)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/components/component.py", line 314, in __init__
kwargs["event_triggers"][key] = self._create_event_chain(key, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/components/component.py", line 407, in _create_event_chain
events = call_event_fn(value, arg_spec) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/event.py", line 810, in call_event_fn
out = fn(*parse_args_spec(arg)) # type: ignore
^^^^^^^^^^^^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/event.py", line 774, in parse_args_spec
return arg_spec(
^^^^^^^^^
File "/Users/masenf/code/reflex-dev/repro-event-trigger-signature/repro_event_trigger_signature/repro_event_trigger_signature.py", line 12, in foo_signature
return [event.foo]
^^^^^^^^^
File "/Users/masenf/code/reflex-dev/reflex/reflex/vars.py", line 714, in __getattr__
raise AttributeError(
AttributeError: The State var `_event` has no attribute 'foo' or may have been annotated wrongly.Specifics (please complete the following information):
- Python Version: 3.11.5
- Reflex Version: 0.4.4
- OS: macOS
- Browser (Optional):
Additional context
The problem appears to arise in how deferred annotations are handled:
Here is the repr(event) without future annotations
BaseVar(_var_name='_event', _var_type=<class 'repro_event_trigger_signature.repro_event_trigger_signature.FooEvent'>, _var_is_local=True, _var_is_string=False, _var_full_name_needs_state_prefix=False, _var_data=None)And with future annotations
BaseVar(_var_name='_event', _var_type='FooEvent', _var_is_local=True, _var_is_string=False, _var_full_name_needs_state_prefix=False, _var_data=None)Notice how the _var_type is just a str 🤔
Reacted by benedikt-bartscher
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]Resolving event signature is broken with `__future__` annotations[/-][+][REF-2204] Resolving event signature is broken with `__future__` annotations[/+]parse_args_spec: resolve annotations with typing.get_type_hints
parse_args_spec: resolve annotations with typing.get_type_hints (#2849)