Opened 3 years ago
Closed 3 years ago
#32794 closed Cleanup/optimization (wontfix)
Get rid of boolean traps in Engine
Reported by: | Abhyudai | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | 3.2 |
Severity: | Normal | Keywords: | template, engine |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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 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:
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.
Change History (1)
comment:1 by , 3 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Type: | Uncategorized → Cleanup/optimization |
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.