Title: Usage of assert in http/
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.10
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, fbidu, veky
Priority: normal Keywords: patch

Created on 2020-10-17 12:53 by fbidu, last changed 2020-10-20 02:34 by benjamin.peterson.

Pull Requests
URL Status Linked Edit
PR 22737 open fbidu, 2020-10-17 12:55
Messages (3)
msg378810 - (view) Author: Felipe Rodrigues (fbidu) * Date: 2020-10-17 12:53
Hi all!

I was testing some static analysis tool and decided to use the HTTP module as testing ground. While running `bandit` at the client module, it detected 3 instances of using `assert` inside the code. Twice in the HTTPResponse class and once in the HTTPConnection class.

Now, I know that this will only cause any trouble when running python with the optimize settings turned on and if someone is that concerned about optimization, they probably won't be using the stdlib's HTTP implementation, but I think it would be fitting to fix this corner case.

I've written a PR that fixes this but I'm not sure if the raised exceptions and messages are ok
msg378903 - (view) Author: Vedran Čačić (veky) * Date: 2020-10-18 21:44
If I understand correctly, those are _private methods_ (names start with underscore). In such cases, asserts can be completely fine, since the library writer controls the situations where the method is called. If it is never called with _UNKNOWN argument, assert is just that, assertion that it is so. (It would be different if it were a public method, since the library writer doesn't know it will never be called with _UNKNOWN.)
msg379078 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2020-10-20 02:34
Are these assertion failures actually reachable due to malformed input? If so, there should be some tests to that effect. Otherwise, if they're actually "impossible" situations, I don't think this change is necessary.
Date User Action Args
2020-10-20 02:34:38benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg379078
2020-10-18 21:44:41vekysetnosy: + veky
messages: + msg378903
2020-10-17 12:55:55fbidusetkeywords: + patch
stage: patch review
pull_requests: + pull_request21700
2020-10-17 12:53:09fbiducreate