Mopidy: Assert statement used outside of tests

Created on 25 Apr 2020  路  5Comments  路  Source: mopidy/mopidy

Since assert provides an easy way to check some condition and fail execution, it鈥檚 very common for developers to use it to check validity. But when the Python interpreter is invoked with the -O (optimize) flag, the assert statements are removed from the bytecode.

Files:

mopidy/audio/utils.py
mopidy/core/actor.py
mopidy/core/playback.py
mopidy/core/tracklist.py
mopidy/ext.py

are using assert statements.

good first issue

Most helpful comment

Hey @pnijhara @jodal I want to work on this issue. Can you please assign me this as I'm first time contributor.

All 5 comments

I marked this as a good first issue. The immediate solution is to change from:

assert condition, "Error message"

To:

if not condition:
    raise AssertionError("error message")

This makes the code behave in the exact same way as it does today, but won't be optimized away when running Python with -O.

@jodal, should I solve this or let other first time contributors to solve?

Moreover, if you want freshers to solve it then I need a suggestion from you, that is, what if we break this issue into several other issues? I mean breaking the issue according to files.

Feel free to tackle this if you're interested!

If it was very many instances of the issue, it could make sense to split it up into multiple pull requests by area of the codebase. However, it looks like there are not that many instances, so I suggest just making a single PR fixing all cases.

Counts of assert per file, excluding tests:

$ rg -c assert mopidy
mopidy/ext.py:3
mopidy/core/actor.py:1
mopidy/core/tracklist.py:8
mopidy/audio/utils.py:1
mopidy/core/playback.py:1

Hey @pnijhara @jodal I want to work on this issue. Can you please assign me this as I'm first time contributor.

There's information at https://docs.mopidy.com/en/latest/contributing/ and also at https://docs.mopidy.com/en/latest/devenv/ on how to setup a development environment and run the tests and linters etc.

Was this page helpful?
0 / 5 - 0 ratings