django-stubs
django-stubs copied to clipboard
django.template.Template.render() has incorrectly typed argument
Bug report
What's wrong
django.template.Template.render() has the following stub (see here):
def render(self, context: Optional[Union[Context, Dict[str, Any]]]) -> SafeString: ...
However, the implementation of this method below makes it impossible to pass in a Dict[str, Any] (see here) as it immediately attempts to access an attribute render_context:
def render(self, context):
"Display stage -- can be called many times"
with context.render_context.push_state(self):
if context.template is None:
with context.bind_template(self):
context.template_name = self.name
return self._render(context)
else:
return self._render(context)
From the looks of it, the various backends define their own Template class, some (all?) of which have a render() method that accepts dictionaries, but this is not the case for the standard django.template.Template class.
I don't know enough of the surrounding context to say whether this is intended or not, but it seems wrong from a first glance. Apologies if this has already been answered, I did have a look through the issues on the repo to try to find similar queries.
How is that should be
The stub for django.template.Template.render() should be changed to:
def render(self, context: Optional[Context]) -> SafeString: ...
System information
- OS: macOS 12.2.1
pythonversion: 3.10.6djangoversion: 4.0.7mypyversion: 0.971django-stubsversion: 1.12.0django-stubs-extversion: 0.5.0
PRs are welcome
Just to confirm - this is indeed a bug? Happy to work on a PR if so.
It looks like it, yes. The stubs are unfortunately not verified against Django's codebase at current, so we rely on spotting fixes from usage, like you've done 😊
I think, that since the function unconditionally accesses the render_context attribute, the context argument also shouldn't be typed as Optional. It should always expect a Context object to be passed in.
def render(self, context: Context) -> SafeString: ...