Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of assert outside of tests #6680

Open
rdimaio opened this issue Apr 15, 2024 · 0 comments
Open

Usage of assert outside of tests #6680

rdimaio opened this issue Apr 15, 2024 · 0 comments

Comments

@rdimaio
Copy link
Contributor

rdimaio commented Apr 15, 2024

Description

Right now, we are using assert in production code, outside tests (see the linter log below).

Some usage examples:

assert len(rses) > 0

assert args.sum in set(record_type.get_fieldnames())

assert prev_date_fname is not None
assert next_date_fname is not None
else:
assert subcommand == 'consistency-manual'

assert isinstance(scope, InternalScope)

This is not necessarily bad, but assertions are removed when Python is run with optimization requested (compiling to optimised byte code, python -O ... producing *.opt-1.pyc files). This is likely never going to be an issue for Rucio, but I still wanted to open an issue to discuss this.

Additionally, when assert fails, it raises AssertionError. In the context of our usage of assert, I think we could raise more specific exceptions with clearer messages, which might also help from the point of view of debugging.

To do

I would like us to decide on one of these options:

  • We decide that the current production usage of assert is fine. In this case, we only need to set the linter to ignore S101 (usage of assert) and no other changes will be needed.
  • We decide that we want to replace our usage of assert with regular condition checking and custom exceptions. In this case, we'd need to make these changes.

See also

Linter log

❯ ruff check --select S101                                                                                                                                                                                                                                                                                               ─╯
bin/rucio-admin:1198:5: S101 Use of `assert` detected
bin/rucio-auditor:51:5: S101 Use of `assert` detected
bin/rucio-auditor:58:5: S101 Use of `assert` detected
bin/rucio-auditor:72:5: S101 Use of `assert` detected
bin/rucio-dumper:99:9: S101 Use of `assert` detected
bin/rucio-dumper:117:9: S101 Use of `assert` detected
bin/rucio-dumper:128:13: S101 Use of `assert` detected
lib/rucio/client/lockclient.py:61:9: S101 Use of `assert` detected
lib/rucio/common/dumper/__init__.py:73:9: S101 Use of `assert` detected
lib/rucio/common/dumper/consistency.py:44:13: S101 Use of `assert` detected
lib/rucio/common/dumper/consistency.py:45:13: S101 Use of `assert` detected
lib/rucio/common/dumper/consistency.py:47:13: S101 Use of `assert` detected
lib/rucio/common/dumper/consistency.py:169:5: S101 Use of `assert` detected
lib/rucio/common/dumper/consistency.py:304:5: S101 Use of `assert` detected
lib/rucio/common/dumper/consistency.py:392:5: S101 Use of `assert` detected
lib/rucio/common/dumper/data_models.py:160:13: S101 Use of `assert` detected
lib/rucio/common/dumper/data_models.py:240:9: S101 Use of `assert` detected
lib/rucio/common/dumper/data_models.py:267:9: S101 Use of `assert` detected
lib/rucio/common/dumper/data_models.py:295:13: S101 Use of `assert` detected
lib/rucio/common/utils.py:1910:9: S101 Use of `assert` detected
lib/rucio/common/utils.py:1948:9: S101 Use of `assert` detected
lib/rucio/core/lock.py:83:9: S101 Use of `assert` detected
lib/rucio/core/lock.py:92:9: S101 Use of `assert` detected
lib/rucio/core/oidc.py:100:9: S101 Use of `assert` detected
lib/rucio/daemons/auditor/srmdumps.py:67:9: S101 Use of `assert` detected
lib/rucio/db/sqla/session.py:225:5: S101 Use of `assert` detected
lib/rucio/db/sqla/session.py:261:5: S101 Use of `assert` detected
lib/rucio/db/sqla/session.py:280:5: S101 Use of `assert` detected
lib/rucio/db/sqla/util.py:125:13: S101 Use of `assert` detected
lib/rucio/db/sqla/util.py:309:9: S101 Use of `assert` detected
lib/rucio/transfertool/fts3_plugins.py:125:13: S101 Use of `assert` detected
lib/rucio/web/rest/flaskapi/v1/dids.py:457:13: S101 Use of `assert` detected
Found 32 errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants