I want a rule that will prevent people from doing from sys import stderr, stdout
#321
Labels
enhancement
New feature or request
from sys import stderr, stdout
#321
Rule request
Description
$sbj, basically. Specifically, in the regular runtime code, not so much in tests.
Rationale
I sometimes forget about this and have something like
And so I want to test this with
capsys
in a while, not immediately after writing that code. Next thing I know, I write a test that looks likeI run it, and immediately get puzzled as to why it captures an empty string. Eventually, I remember that
from .. import ..
, re-read the pytest docs and realize that it patches thesys
module's attributesstderr
andstdout
. So when I import it usingfrom sys import stderr
,stderr
becomes a module-scoped reference to that native IO object. This means that pytest patching thesys
module does not influence the fact that the runtime code references that unpatched IO object.And pytest silently fails to intercept stderr/stdout.
It seems so obvious once you get through all the troubleshooting steps, but is frustrating at first. It's an easy mistake to make. Hence, the motivation — I think that such a rule is well within the scope of this plugin.
The text was updated successfully, but these errors were encountered: