Repository navigation
Description
Describe the bug
When from __future__ import annotations is used in py3.11, all type annotations in the __annotations__ dict are actually str instead of the real type. This breaks assumptions inside pynecone where __annotations__ are accessed directly.
Instead, the manual suggests the use of typing.get_type_hints as a safe way to get the type hints as actual types instead of just the string of the type name.
To Reproduce
from __future__ import annotations
import pynecone as pc
class State(pc.State):
@pc.var
def computed(self) -> str:
return "hello pynecone"
def index() -> pc.Component:
return pc.center(
pc.input(placeholder=State.computed),
padding_top="10%",
)
# Add state and page to the app.
app = pc.App(state=State)
app.add_page(index)
app.compile()Expected behavior
Expect the app to compile and run, displaying "hello pynecode" as the input component's placeholder.
What actually happens is a compiler error:
Process SpawnProcess-10:
Traceback (most recent call last):
File "/opt/homebrew/Cellar/[email protected]/3.11.3/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.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/Users/masen/code/pynecone-io/VENV-dev/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "/Users/masen/code/pynecone-io/VENV-dev/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.3/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.3/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.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/Users/masen/code/pynecone-io/VENV-dev/lib/python3.11/site-packages/uvicorn/server.py", line 67, in serve
config.load()
File "/Users/masen/code/pynecone-io/VENV-dev/lib/python3.11/site-packages/uvicorn/config.py", line 477, in load
self.loaded_app = import_from_string(self.app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masen/code/pynecone-io/VENV-dev/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.3/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 1206, in _gcd_import
File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
File "<frozen importlib._bootstrap>", line 1149, 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/masen/code/pynecone-io/repro-broken-future-annotations/repro_broken_future_annotations/repro_broken_future_annotations.py", line 20, in <module>
app.add_page(index)
File "/Users/masen/code/pynecone-io/pynecone/pynecone/app.py", line 274, in add_page
raise e
File "/Users/masen/code/pynecone-io/pynecone/pynecone/app.py", line 265, in add_page
component = component if isinstance(component, Component) else component()
^^^^^^^^^^^
File "/Users/masen/code/pynecone-io/repro-broken-future-annotations/repro_broken_future_annotations/repro_broken_future_annotations.py", line 13, in index
pc.input(placeholder=State.computed),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masen/code/pynecone-io/pynecone/pynecone/components/component.py", line 365, in create
return cls(children=children, **props)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masen/code/pynecone-io/pynecone/pynecone/components/component.py", line 130, in __init__
if not types._issubclass(passed_type, expected_type):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/masen/code/pynecone-io/pynecone/pynecone/utils/types.py", line 113, in _issubclass
return cls_check_base == Any or issubclass(cls_base, cls_check_base)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class** Specifics (please complete the following information):**
- Python Version: 3.11.2
- Pynecone Version: 1.0.27 (+main)
- OS: macOS 13.2.1
Additional context
I debugged this out, and it turns out the __annotations__ are strings:
(Pdb) kwargs[key].fget.__annotations__
{'return': 'str'}
If I comment out the __future__ import, then the dict gets the actual type
(Pdb) kwargs[key].fget.__annotations__
{'return': <class 'str'>}
Additionally, the issue only appears when assigning the ComputedVar to a prop, not passing it as a child.
Activity
var: use typing.get_type_hints instead of __annotations__