AnswerDotAI / AnswerDotAI/fasthtml

[BUG] Mounted apps cannot access the root app state property

Open
#687 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Jupyter Notebook
Stars
7k
Forks
319
Avg merge
28m
Merged PRs (30d)
3

Description

**Describe the bug**
When mounting an app as described in [Combining Routes](https://fastht.ml/docs/explains/routes.html#combining-routes) the `app.state` property is inaccessible in the mounted app.
The mounted app is the one made available through the `request` object which means that the handler cannot access `app.state`.
This behaviour also affects the beforeware code which cannot be used to inject dependencies into the state object.

**Minimal Reproducible Example**
```python
from fasthtml.common import *
from starlette.testclient import TestClient

sub_app, _ = fast_app()

@sub_app.get("/", name="list")
def sub(request):
has_state = hasattr(request.app.state, "key")
key = "Got it" if has_state else "Nope"
return Titled("Sub", P(key))

app, _ = fast_app(debug=False, routes=[Mount("/sub", sub_app, name="sub")])

@app.get("/")
def home(request):
has_state = hasattr(request.app.state, "key")
key = "Got it" if has_state else "Nope"
return Titled("Home", P(key))

if __name__ == "__main__":
app.state.key = "have_state"
with TestClient(app) as client:
print("Getting Home")
resp = client.get("/")
assert "Got it" in resp.text

print("Getting Sub")
resp = client.get("/sub/")
assert "Got it" in resp.text

```

**Expected behavior**
What I expected is that in the `sub()` and `home()` functions the `request.app.state.key` attribute would be available.

**Environment Information**
- fastlite version: 0.1.1
- fastcore version: 1.7.29
- fasthtml version: 0.12.1

**Confirmation**
Please confirm the following:
- [x] I have read the FAQ (https://docs.fastht.ml/explains/faq.html)
- [x] I have provided a minimal reproducible example
- [x] I have included the versions of fastlite, fastcore, and fasthtml
- [x] I understand that this is a volunteer open source project with no commercial support.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.