-
-
Notifications
You must be signed in to change notification settings - Fork 483
Using TypedDict for templates rendering #355
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
Comments
Oh, and one more thing - I don't really mind if the type check does not work "all the way through" - I think that the annotation of the returned dict already serves quite nicely as "executable documentation" for the templates authors :) |
@dmerejkowsky Hello! I've investigated your code, and I think that's not really a problem with Mypy. The reason is this part:
Here you define "user" as User instance, while Also, it's weird that Mypy hasn't warned you about doing
So, changing |
I don't think it's an issue in django-stubs actually This also fails: from typing import Any, Dict, TypedDict
IndexContext = TypedDict("IndexContext", {"user": str})
def get_context() -> IndexContext:
return {"user": "john"}
def render(context: Dict[str, Any]) -> None:
print(context)
def main() -> None:
context = get_context()
render(context) I've aked on mypy bug tracker: python/mypy#8923 |
@dmerejkowsky Now I see that my first comment was actually pretty dummy, so sorry for that. I haven't checked Mypy docs on TypedDict, and it was pretty late at night when I was writing it iirc. So, according to this Mypy issue it's done on purpose, and probably there's no convenient way to make |
No worries - it happens. Also, it forced me to write a simpler example, so this was actually a win :)
I agree. It looks like a good idea from where I'm standing but I'm not aware of all the implications. Thanks for your help anyway. |
Yes, we can try |
Thank you - keep up the good work! |
Bug report
What's wrong
So, not sure if it's a bug or not - maybe what I'm trying to achieve is
outside the scope of the typeddjango project. Anyway, here goes.
I have a
view_index()
view that delegates building the context usedfor template rendering to a
IndexPresenter
class with aget_context()
method:
Here are the relevant snippets (tell me if you need more):
You can see I'm using the new
TypedDict
from Python 3.8The problem is that
mypy
complains about the last line:How is that should be
I guess that mypy should see that a TypedDict is compatible with a
Dict[str, Any]
somehow?System information
python
version: 3.8.2django
version: 3.0.3mypy
version: 0.770django-stubs
version: 1.5.0The text was updated successfully, but these errors were encountered: