Closed Bug 1325838 Opened 7 years ago Closed 6 years ago

Stop using Python asserts for conditions that should be checked in production

Categories

(Tree Management :: Treeherder, defect, P3)

defect

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: emorley, Assigned: ghickman)

Details

Attachments

(2 files, 1 obsolete file)

`assert` statements are removed depending on the Python optimisation level, so should not be used outside of checks that are intended to be test or debug only.

However there are several places where we currently use them, when we should use if statements and explicit raises instead.

eg:

https://github.com/mozilla/treeherder/search?utf8=%E2%9C%93&q=assert+-path%3Atests%2F&type=Code
Hey,

Is this just a simple case of converting all the asserts to if statements and raising the required exceptions?
I'd like to take a look.
Yeah that sounds right :-)
Alright, I'll try and get this done. Do you only need this changed in the places you mentioned in your initial comment?

Some input will be appreciated. (what type of errors to raise etc)
Priority: P2 → P3
Assignee: nobody → ghickman
Attachment #8959565 - Flags: review?(emorley)
Attachment #8959565 - Flags: review?(emorley) → review?(cdawson)
Commit pushed to master at https://github.com/mozilla/treeherder

https://github.com/mozilla/treeherder/commit/8a1fb7befbd30310fb5808bbaf45715ccafd0052
Bug 1325838 - Remove non-testing assert statements (#3348)

* Replace settings asserts with ImproperlyConfigured
* Replace attribute asserts with AttributeError
* Replace duplicate key assert with a custom exception
   There doesn't seem like a better exception to use here so I added a
   custom one.
* Check the id exists
* Replace asserts with manual AssertionErrors
Comment on attachment 8959565 [details] [review]
Link to GitHub pull-request: https://github.com/mozilla/treeherder/pull/3348

Thanks!!
Attachment #8959565 - Flags: review?(cdawson) → review+
I noticed a couple other ``assert`` statements in one of our model managers.  I'm not clear if those should be converted as well?

https://github.com/mozilla/treeherder/blob/5052c5dfaae007011cfb8ec7e025a89b8974263c/treeherder/model/models.py#L1159-L1164

Ed: What do you think?
Flags: needinfo?(emorley)
Yeah let's change those too - something like this from the obsoleted previous PR should work (or whatever else looks better):
https://github.com/mozilla/treeherder/pull/3128/files#diff-e432623caa985e5ac4d48814de254a58R1164
Flags: needinfo?(emorley)
Attachment #8942245 - Attachment is obsolete: true
Attachment #8960653 - Flags: review?(cdawson)
Attachment #8960653 - Flags: review?(cdawson) → review+
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: