Skip to content

Replace and rewrite "assert" statements in non-test code #175

Open

Description

@pombredanne

See https://www.reddit.com/r/learnpython/comments/zd217y/is_using_assert_considered_good_practice/

assert statements should only be used for debugging purposes. They are removed when not running in debug mode (i.e. when invoking the Python command with the -O or -OO options).

Instead use a raise statement to throw a proper, informative runtime exception, if you need to. That would definitely be best practice when there'd otherwise be no sensible return value for the function.

We are using a few asserts in our main code. See https://github.com/search?q=org%3Aaboutcode-org+NOT+path%3A*test*++"assert"++language%3APython&type=code

Activity

  1. TirthPanchal commented on Jan 12, 2025

    @TirthPanchal

    Heyyy, I'd like to work on this issue. Can you assign it to me?

  2. coderhuBypassion commented on Jan 13, 2025

    @coderhuBypassion

    so, assert statements should be removed from all the repos of aboutcode-org., or only from aboutcode-org/scancode-toolkit ?

  3. Dhruv276R commented on Jan 14, 2025

    @Dhruv276R

    Hey, I'd like to work on this issue. Can you assign this to me? And the assert statements are to be removed from all the repos or a specific one? @pombredanne

  4. Gurleen-kansray commented on Jan 15, 2025

    @Gurleen-kansray

    Hi @pombredanne , I have started working on this issue. I will replace the assert statements with raise statements in the main code (outside test files). I'll submit a PR soon. Let me know if there are specific repos other than scancode-toolkit where this change is needed.

  5. Dhruv276R commented on Jan 15, 2025

    @Dhruv276R

    @pombredanne I have made a PR on this https://github.com/aboutcode-org/aboutcode-toolkit/pull/574
    Check and let me know for further enhacements. Do you need me to changes in other repos as well?

  6. ONKARBH commented on Jan 17, 2025

    @ONKARBH

    Hi @pombredanne, I would like to work on this issue to replace the assert statements with proper raise exceptions in the main code (outside test files). Could you please assign this issue to me? Let me know if any specific guidelines or repos need to be considered. Thank you!

  7. pombredanne commented on Jan 17, 2025

    @pombredanne
    MemberAuthor

    @TirthPanchal re:

    Heyyy, I'd like to work on this issue. Can you assign it to me?

    we do not "assign" issue per se. You are welcome to work on this, but you want to sync up here with others to avoid double, duplicated work!

    @Dhruv276R re:

    Hey, I'd like to work on this issue. Can you assign this to me?

    As explained above, we cannot "assign" work. You guys need to coordinate and collaborate to avoid stepping on each other.

    And the assert statements are to be removed from all the repos or a specific one?

    Rather replaced in all the repos, and only asserts that are not for tests, and only asserts that are for production code that could be ignored from a -O python run.

    @Gurleen-kansray re:

    Hi , I have started working on this issue. I will replace the assert statements with raise statements in the main code (outside test files). I'll submit a PR soon. Let me know if there are specific repos other than scancode-toolkit where this change is needed.

    As said above, all repos, but not in test code: there we use "assert" and pytest does an awesome job assert rewriting job, so we surely do not want to change this.

    @ONKARBH re:

    Hi, I would like to work on this issue to replace the assert statements with proper raise exceptions in the main code (outside test files). Could you please assign this issue to me? Let me know if any specific guidelines or repos need to be considered. Thank you!

    Same as above: all repos, not the tests, and you need to coordinate here with others

  8. ONKARBH commented on Jan 17, 2025

    @ONKARBH

    Hi everyone, I’d like to contribute to this issue by focusing on specific files/modules for replacing assert statements outside the test files. Please let me know how I can best coordinate with others to avoid overlap. Thank you!

  9. pombredanne commented on Jan 17, 2025

    @pombredanne
    MemberAuthor

    @Dhruv276R re:

    I have made a PR on this https://github.com/aboutcode-org/aboutcode-toolkit/pull/574 Check and let me know for further enhacements. Do you need me to changes in other repos as well?

    This is awesome, but I cannot see the PR at https://github.com/aboutcode-org/aboutcode/pulls/574 ?

  10. ONKARBH commented on Jan 17, 2025

    @ONKARBH

    Hi @Dhruv276R, your work is great! However, the PR link seems incorrect. Can you confirm if the PR is in the aboutcode-toolkit repo and share the correct link? Thank you!

  11. pombredanne commented on Jan 17, 2025

    @pombredanne
    MemberAuthor
  12. Pratz2005 commented on Jan 26, 2025

    @Pratz2005

    Is this issue still open?

  13. 24 remaining items

  14. vignathi123-vi commented on Feb 23, 2026

    @vignathi123-vi

    Hey @pombredanne,
    I'm a GSoc 2026 applicant. Can I work on this issue? Is it open?

  15. va1bhav-06 commented on Feb 26, 2026

    @va1bhav-06

    Hi, I’m interested in working on this issue. Is it still available?

  16. Satyasubhash01 commented on Mar 1, 2026

    @Satyasubhash01

    Hey @pombredanne,
    I'm a GSoc 2026 applicant. Can I work on this issue?, I’m interested in working on this issue

  17. ParthMozarkar commented on Mar 8, 2026

    @ParthMozarkar

    could you please assign this to me?

  18. sagarnaidu04 commented on Mar 16, 2026

    @sagarnaidu04

    @pombredanne
    Hi, I’m interested in working on this issue.
    Could you please point me to the specific files or repositories where the assert statements need to be replaced?

  19. mili609 commented on Mar 16, 2026

    @mili609

    Hi, I would like to work on this issue. Could you please assign it to me?

  20. mili609 commented on Mar 16, 2026

    @mili609

    Hi, I checked the repository but I couldn't find any assert statements
    in the current codebase. Could you please point me to the files where
    these asserts still need to be replaced?

  21. tech-nitin commented on Mar 20, 2026

    @tech-nitin

    Hi, I would like to work on this issue. Can I take this?

  22. huda-code commented on May 7, 2026

    @huda-code

    Hi @pombredanne,

    I’d like to contribute to this issue by focusing on a specific subset of non-test production asserts to avoid overlap with existing work.

    I’m planning to audit one repository/module first, replace runtime-critical assert statements with explicit exceptions, and ensure behavior remains unchanged under optimized Python runs (-O / -OO).

    I’ll avoid test files and coordinate here before opening a PR if I notice overlap with existing contributions.

    Thanks!

  23. duplicatenode commented on May 28, 2026

    @duplicatenode

    Hi! I'd like to contribute to this. I'll pick a specific repo/file that no one else has claimed and leave a note here before starting. Happy to coordinate.

  24. ankitprajapat42 commented on Jun 4, 2026

    @ankitprajapat42

    Hi, I would like to work on this issue. Can I take this

  25. Saumyajaiswal9794 commented on Aug 17, 2026

    @Saumyajaiswal9794

    Hi @pombredanne, I would like to work on this issue. Could you please assign it to me?

  26. RachitSinghh commented on Sep 29, 2026

    @RachitSinghh

    Is this still open?

  27. SuryaNarayana-C-K commented on Oct 9, 2026

    @SuryaNarayana-C-K

    Hi! I'm interested in contributing to issue #175 as my first open-source PR.
    I read the discussion and understand that the work spans multiple repositories and that contributors should coordinate to avoid overlapping changes.
    Could you suggest a repository or module that still has production-code assert statements needing replacement and is not already covered by an existing PR? I'd like to take one small, well-defined part of the issue and add appropriate tests.
    Thank you!

  28. NathN07 commented on Oct 13, 2026

    @NathN07
    Contributor

    Hi @pombredanne, I've opened PR aboutcode-org/container-inspector#66 replacing production assert statements with ValueError in src/container_inspector/cli.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions