Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Don't use assert to enforce interface constraints #1552

Closed
@tucked

Description

@tucked
# sscce.pyimportgitgit.Repo(".").tags[None]
$git initInitialized empty Git repository in /tmp/tmp.Xoz9gZndOi/.git/$venv/bin/python sscce.pyTraceback (most recent call last):  File "sscce.py", line 5, in <module>    git.Repo(".").tags[None]  File "/tmp/tmp.Xoz9gZndOi/venv/lib/python3.8/site-packages/git/util.py", line 1087, in __getitem__    assert isinstance(index, (int, str, slice)), "Index of IterableList should be an int or str"AssertionError: Index of IterableList should be an int or str

assertisinstance(index, (int,str,slice)),"Index of IterableList should be an int or str"

assert should not be used in product code because it can be ignored with-O:

$venv/bin/python -O sscce.pyTraceback (most recent call last):  File "sscce.py", line 5, in <module>    git.Repo(".").tags[None]  File "/tmp/tmp.Xoz9gZndOi/venv/lib/python3.8/site-packages/git/util.py", line 1095, in __getitem__    return getattr(self, index)TypeError: getattr(): attribute name must be string

(In fact, that behavior is probablybetter since aTypeError is more semantically meaningful.)

bandit can catch this kind of thing:

$venv/bin/bandit venv/lib/python3.8/site-packages/git/util.py...>> Issue: [B101:assert_used] Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.   Severity: Low   Confidence: High   CWE: CWE-703 (https://cwe.mitre.org/data/definitions/703.html)   Location: venv/lib/python3.8/site-packages/git/util.py:1087:8   More Info: https://bandit.readthedocs.io/en/1.7.4/plugins/b101_assert_used.html10861087            assert isinstance(index, (int, str, slice)), "Index of IterableList should be an int or str"10881089            if isinstance(index, int):...

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


      [8]ページ先頭

      ©2009-2025 Movatter.jp