Skip to content

Commit

Permalink
Merge pull request #134 from piercefreeman/feature/update-docs-for-ne…
Browse files Browse the repository at this point in the history
…w-website

Update docstrings for new website
  • Loading branch information
piercefreeman authored Dec 3, 2024
2 parents 2752fbf + 42bae5d commit 2238d24
Show file tree
Hide file tree
Showing 17 changed files with 358 additions and 90 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ harness = false
path = "src/benches/lexers_benchmark.rs"
name = "lexers_benchmark"
harness = false

[lints.rust]
# Global allow to fix pymethods's macro expansion, which can't be
# escaped with an inline #[allow]
non_local_definitions = "allow"
4 changes: 2 additions & 2 deletions mountaineer/__tests__/actions/test_passthrough_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async def test_can_call_iterable():
assert isinstance(return_value_sync, StreamingResponse)

# StreamingResponses are intended to be read by an ASGI server, so we'll use the TestClient to simulate one instead of calling directly
controller_definition = app.definition_for_controller(controller)
controller_definition = app._definition_for_controller(controller)
passthrough_url = controller_definition.get_url_for_metadata(
get_function_metadata(controller.get_data)
)
Expand Down Expand Up @@ -229,7 +229,7 @@ def call_passthrough(self, payload: dict) -> JSONResponse:
controller = ExampleController()
app.register(controller)

controller_definition = app.definition_for_controller(controller)
controller_definition = app._definition_for_controller(controller)

client = TestClient(app.app)
response = client.post(
Expand Down
2 changes: 1 addition & 1 deletion mountaineer/__tests__/actions/test_sideeffect_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def call_sideeffect(self, payload: dict) -> None:
controller = ExampleController()
app.register(controller)

controller_definition = app.definition_for_controller(controller)
controller_definition = app._definition_for_controller(controller)
sideeffect_url = controller_definition.get_url_for_metadata(
get_function_metadata(ExampleController.call_sideeffect)
)
Expand Down
16 changes: 8 additions & 8 deletions mountaineer/__tests__/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ def client_function(self) -> None:
)

# Test that the controller definitions remain separate
assert parent_controller.definition
assert child_controller.definition
assert parent_controller._definition
assert child_controller._definition

assert parent_controller.definition.render_router
assert child_controller.definition.render_router
assert parent_controller._definition.render_router
assert child_controller._definition.render_router

parent_routes = parent_controller.definition.render_router.routes
child_routes = child_controller.definition.render_router.routes
parent_routes = parent_controller._definition.render_router.routes
child_routes = child_controller._definition.render_router.routes

assert len(parent_routes) == 1
assert parent_routes[0].path == "/parent" # type: ignore
Expand Down Expand Up @@ -287,7 +287,7 @@ class MockConfig(ConfigBase):
mock_resolve_package_path.return_value = tmp_path

app = AppController(config=MockConfig())
assert app.view_root == tmp_path / "views"
assert app._view_root == tmp_path / "views"

assert mock_resolve_package_path.call_count == 1
assert mock_resolve_package_path.call_args[0] == ("test_webapp",)
Expand Down Expand Up @@ -437,7 +437,7 @@ def target_fn(a: int, b: str):
}

app = AppController(view_root=Path(""))
assert app.get_value_mask_for_signature(
assert app._get_value_mask_for_signature(
signature(target_fn),
values,
) == {
Expand Down
2 changes: 1 addition & 1 deletion mountaineer/__tests__/test_app_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async def test_handle_dev_exception(manager: DevAppManager):
if controller.controller.__class__.__name__
!= manager.exception_controller.__class__.__name__
]
manager.app_controller.controller_names.remove(
manager.app_controller._controller_names.remove(
manager.exception_controller.__class__.__name__
)

Expand Down
6 changes: 3 additions & 3 deletions mountaineer/__tests__/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def test_resolve_paths(tmp_path: Path):
assert controller.resolve_paths(view_base)

# Now ensure that the paths are correctly set
assert controller.view_base_path == view_base
assert controller.ssr_path == ssr_base / "stub_controller.js"
assert controller.bundled_scripts == [f"stub_controller-{random_hash}.js"]
assert controller._view_base_path == view_base
assert controller._ssr_path == ssr_base / "stub_controller.js"
assert controller._bundled_scripts == [f"stub_controller-{random_hash}.js"]
20 changes: 10 additions & 10 deletions mountaineer/actions/passthrough_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,25 @@ def passthrough(*args, **kwargs): # type: ignore
`JSONResponse` if you need full flexibility on return headers and content structure.
If you do return a JSONResponse note that we will handle the merging of the response for you - so
on the client side you will still access your endpoint contents with:
on the client side you will still access your endpoint contents as `response.passthrough`.
```typescript
const response = await serverState.my_action({});
console.log(response.passthrough);
```typescript {{ sticky: True }}
const response = await serverState.my_action({
name: "John Appleseed",
});
console.log(response.passthrough.name);
```
Usage:
```python
```python {{ sticky: True }}
from pydantic import BaseModel
class ResponseModel(BaseModel):
pass
name: str
class MyController(ControllerBase):
@passthrough
async def my_action(self) -> ResponseModel:
...
async def my_action(self, name: str) -> ResponseModel:
return ResponseModel(name=name)
```
:param exception_models: List of APIException subclasses that this function is known
Expand Down
34 changes: 31 additions & 3 deletions mountaineer/actions/sideeffect_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ def sideeffect(*args, **kwargs): # type: ignore
on return headers and content structure. Unlike @passthrough, it does not allow you to provide a non-JSON response since we need
to internally merge it with render() sideeffect update.
```python {{sticky: True}}
from mountaineer import sideeffect, RenderBase, ControllerBase, Depends
from iceaxe import DBConnection, select
from iceaxe.mountaineer import DatabaseDependencies
from myapp import models
class ControllerRender(RenderBase):
count: str
class MyController(ControllerBase):
async def render(
self,
db_connection: DBConnection = Depends(DatabaseDependencies.get_db_connection),
) -> ControllerRender:
elements = await db_connection.exec(select(models.MyModel.id))
return ControllerRender(count=len(elements))
@sideeffect
async def increment_count(
self,
db_connection: DBConnection = Depends(DatabaseDependencies.get_db_connection),
) -> None:
new_model = models.MyModel()
await db_connection.insert([new_model])
```
:param exception_models: List of APIException subclasses that this function is known
to throw. These will be parsed and available to frontend clients.
:type exception_models: list[Type[APIException]] | None
Expand Down Expand Up @@ -260,7 +288,7 @@ async def get_render_parameters(
}
)

if not controller.definition:
if not controller._definition:
raise RuntimeError(
"Controller definition is not set. This might indicate you're calling a"
" sideeffect from outside of an AppController context."
Expand All @@ -272,8 +300,8 @@ async def get_render_parameters(
# match non-relevant paths.
# https://github.com/encode/starlette/blob/5c43dde0ec0917673bb280bcd7ab0c37b78061b7/starlette/routing.py#L544
for route in (
controller.definition.render_router.routes
if controller.definition.render_router is not None
controller._definition.render_router.routes
if controller._definition.render_router is not None
else []
):
match, child_scope = route.matches(view_request.scope)
Expand Down
Loading

0 comments on commit 2238d24

Please sign in to comment.