[Django] #32794: Get rid of boolean traps in Engine

6 views
Skip to first unread message

Django

unread,
May 27, 2021, 9:36:10 PM5/27/21
to django-...@googlegroups.com
#32794: Get rid of boolean traps in Engine
-------------------------------------+-------------------------------------
Reporter: Abhyudai | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Template | Version: 3.2
system |
Severity: Normal | Keywords: template, engine
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
While working on adding `repr` method to the
`django.template.engine.Engine` class, I noticed the class definition,
(essentially the `__init__` function) accepts some parameters like
`app_dirs`, `debug` and `autoescape` which are `boolean` but are accepted
as positional arguments. I think this design pattern pretty much falls
into the real of [https://ariya.io/2011/08/hall-of-api-shame-boolean-trap
boolean-trap].

One of the ways that we can deal by not making this backward incompatible
would be to add `**kwargs` to the function definition and do something of
this type along the lines:

{{{#!python
def __init__(self, dirs=None, app_dirs=False, context_processors=None,
debug=False, loaders=None, string_if_invalid='',
file_charset='utf-8', libraries=None, builtins=None,
autoescape=True, /, **kwargs):
# because False is it's default value
if not debug and 'debug' not in kwargs:
raise DeprecationWarning('this argument will be made named only
in a later release')

# the same thing can be done with the other two arguments
}}}

Another way would be to fiddle with `inspect.signature`. While the first
one appears lesser complex to me, I'm open to any other suggestions.

If the proposal seems acceptable, I would want to work on it.

--
Ticket URL: <https://code.djangoproject.com/ticket/32794>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 28, 2021, 12:11:27 AM5/28/21
to django-...@googlegroups.com
#32794: Get rid of boolean traps in Engine
-------------------------------------+-------------------------------------
Reporter: Abhyudai | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Template system | Version: 3.2
Severity: Normal | Resolution: wontfix

Keywords: template, engine | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Uncategorized => Cleanup/optimization
* resolution: => wontfix


Comment:

There are dozens of methods and functions which accept boolean positional
arguments in Django. It's not worth the additional complexity and backward
compatibility concerns to change them. Moreover users do not initialize
`Engine()` themself in most of cases.

By the way, you'll reach a wider audience if you write to the
DevelopersMailingList about your ideas.

--
Ticket URL: <https://code.djangoproject.com/ticket/32794#comment:1>

Reply all
Reply to author
Forward
0 new messages