Opened 3 weeks ago

Closed 3 weeks ago

#37307 closed Bug (wontfix)

Django uses `or` keyword to short-circuit in various places

Reported by: Twain Byrnes Owned by:
Component: Uncategorized Version: 6.1
Severity: Normal Keywords:
Cc: Twain Byrnes Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

There are several instances of a potentially problematic behavior in the Python code of the form:

Code highlighting:

def fun_name(par=None):
  par = par or default_value
  ...

which may be passed a non-None value of the wrong type, potentially causing errors. I have not found any instances where there it is possible for something to be passed in with the wrong type, but for future-proofing, it may be advisable to change them to the following form:

Code highlighting:

def fun_name(par=None):
   if par is None:
     par = default_value
   ...

I have listed several such instances below:

  1. In django/db/models/fields/__init__.py, lines 1101 and 1115
  2. In django/db/models/base.py, lines 705 and 706, lines 853 and 867, lines 962 and 974, lines 1333 and 1339

If there is interest, I can also submit a CodeQL query to uncover the remaining items of this form.

Change History (1)

comment:1 by Jacob Walls, 3 weeks ago

Easy pickings: unset
Resolution: wontfix
Status: newclosed
Summary: Short-circuit evaluation issueDjango uses `or` keyword to short-circuit in various places

Hi Twain,

Thanks for sharing your findings with us, but we don't consider this problematic as a general principle. Some parts of Django lean more toward duck-typing than others, and in many cases we avoid being specific about sequence/mapping types.

In the future, please also share the exact code or permanent links -- the line numbers you cite cannot be looked up against moving targets otherwise.

Note: See TracTickets for help on using tickets.
Back to Top