Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed request context bug, silenced Ruff warnings #7

Closed
wants to merge 1 commit into from

Conversation

shawnsw
Copy link

@shawnsw shawnsw commented Feb 6, 2024

Fixing the following error:

    return self.templates.TemplateResponse(name=template_name, request=request, context=result)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Jinja2Templates.TemplateResponse() got an unexpected keyword argument 'request'

The error is due to how the TemplateResponse is being constructed in the render function within the Jinja class. Specifically, the error arises because the TemplateResponse method is being called with an incorrect argument: request=request. The request should be part of the context dictionary, not passed as a separate argument.

Also fixed some obvious Ruff warnings and silenced the rest non-critical comment related warnings.

@@ -17,52 +19,54 @@ def _append_to_signature(
func: Callable[_P, _T],
*params: inspect.Parameter,
) -> Callable[_P, _T]:
"""
Appends the given parameters to the *end* of signature of the given function.
"""Appends the given parameters to the *end* of signature of the given function.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not reformat the docstring. I understand that this is probably the most common style, but starting multiline docstrings on a newline is much more readable. Also, please don't add horizontal lines after Notes, Sections and Returns.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understand. I only changed it because VS Code was going bananas, the entire files was covered in red and yellow. Most of these docstring changes are just so that I can actually read the code. I think this is because I didn't load up your ruff configs, more below.

The received function with an extended `__signature__`.
"""

""" # noqa: D401
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyproject.toml contains the linter config - I'm assuming your local development environment did not load the settings from there, otherwise there wouldn't be a linter warning here. I'll update the CI to run on PRs - that would flag your changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps worth putting down a contributor guideline on these things. And yeah, definitely a good idea to have CI check on these things.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, adding more docs on how to do linting and formatting locally (poethepoet tasks) would be helpful, but I think following the already existing coding and documentation pattern should be possible regardless.

Th CI check is already in place - I added it not long ago.

return self.templates.TemplateResponse(name=template_name, request=request, context=result)
""" # noqa: D205, D401

def render(result: Any, *, context: dict[str, Any], request: Request): # type: ignore[no-untyped-def]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the original return type, there'd be no need for the type ignore then.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was also an autocorrect by Ruff. I do apologise, this is my first week of writing Python, still getting familiar with the tools and patterns etc.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this happened, but ruff shouldn't remove the return type annotation or add type: ignore comments. Static type analysis is done with mypy.

""" # noqa: D205, D401

def render(result: Any, *, context: dict[str, Any], request: Request): # type: ignore[no-untyped-def]
context_with_request = {"request": request, **context}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to move this logic into a _build_context(result: Any, *, route_context: dict[str, Any], request: Request) -> dict[str, Any] hook method that can be overridden by people who would also like to include the route's "context" (its resolved dependencies) in the Jinja rendering context. See issue #4

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think you're making a mistake here, see the docs of HTMXRenderer: context is the route's context (=its resolved dependencies) in this setting, not the Jinja rendering context. Actually, the route's return value will end up being the Jinja rendering context. Again, please have a look at #4

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining. I admit I didn't see #4, feel free to close this one.

@volfpeter volfpeter closed this Feb 7, 2024
@volfpeter
Copy link
Owner

Closed following this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants