#31877: TemplateView.get_context_data()'s kwargs returns SimpleLazyObjects that causes a crash when filtering. -------------------------------------+------------------------------------- Reporter: Tim L. White | Owner: Adam | (Chainz) Johnson Type: Bug | Status: new Component: Generic views | Version: 3.1 Severity: Release blocker | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Tom Forbes): There isn't really a general purpose way of wrapping primitive/arbitrary types like this in Python that won't hit some corner cases. You can make an object appear to be a duck by adapting it's quacking dynamically (i.e `wrapt.ObjectProxy`), but if someone looks close enough they can always see that it's actually dog. And on the whole that's a good thing, IMO. Our use of `kwargs` makes this harder as we lose the ability to construct a container that can trigger the deprecation warning which would be the typical easy approach. There is no way to control what lands on the other side of `get_context_data()` (it's always a plain kwargs dict), and there is no way to construct a wrapper value that looks _exactly_ like the value it's wrapping. That basically leaves only "crazy" approaches, some of which are fun to consider but none of which are suitable. Here's one that uses `settrace()` to do what we need: {{{ import sys class DeprecatedWrapper(dict): def __getitem__(self, key): warnings.warn("stop right there, scoundrel!") return super().__getitem__(key) def wrap_kwargs(frame, *args): frame['kwargs'] = DeprecatedWrapper(frame['kwargs']) sys.settrace(None) class TemplateView(...): def get(...): ... sys.settrace(wrap_kwargs) context = self.get_context_data(**context_kwargs) return self.render_to_response(context) }}} Given these issues, I'm not sure if we can go ahead with deprecating this. -- Ticket URL: <https://code.djangoproject.com/ticket/31877#comment:12> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.0c00e4c44f25ad2f8bd7bd0365ae4830%40djangoproject.com.