2 ms·
So I'm writing a unit test. I bet everyone here can correctly guess the language. assert ('Content-Type', 'text/plain') in dupefail.headers assert b"re
by memorysafety 7y ago
So I'm writing a unit test. I bet everyone here can correctly guess the language.
assert ('Content-Type', 'text/plain') in dupefail.headers
assert b"registration denied" in dupefail.body
assert "403 Forbidden" == dupefail.status
I also happen to pay attention to the linter fart^Woutput:
C: 61,11: Comparison should be dupefail.status == '403 Forbidden' (misplaced-comparison-constant)
I totally admit my thorough hate of pylint, it hasn't really ever helped me once -- mostly led to more `#pylint: disable=…` garbage in the source. I still force myself to use it, as a duty by fellow... other engineers. But that's an aside.
Please, tell me how much more "natural" and "un-ugly" the 3-line snippet would read to you had it the third line assert flipped away from Yoda style, just as pylint suggests. I'm eager to hear you.
- yegle 7y agoThat's why normally you do self.assertEqual() instead of assert. To properly support assert with good error reporting, pytest has to rewrite the byte code. See http://doc.pytest.org/en/latest/assert.html#assert-details http://doc.pytest.org/en/latest/assert.html#assert-details
- np_tedious 7y agoEven with them, I do find it Yoda-like that you are supposed to make the expected value be the first argument
- memorysafety 7y agoLook, I'm trying to humanly argue against the statement "Yoda conditions are unnatural". Nature has no boolean conditions. That statement should instead say "Yoda conditions are unfamiliar (to me)" -- at which point it's way easier to see the statement's applicability limits, and dramatically narrow down its consequences. On the contrary; there're many common contexts where Yoda comparisons looks more "natural", meaning they avoid breaking the surrounding code flow, and bring the important part (the constant) up-front. I even brought up a real-world example. Having read `assert "403 Forbidden" == ` and remembering the context, can't you already guess the RHS (and just skim over it)? Sure you can. Non-yoda loses here. Be aware: you don't have to take a "for/against" side in this debate, as our buggy brains try to in every flame war. Both sides have a point. Familiarize yourself, and decide on case-by-case basis. While we're at it: self.assertEqual() is super-ugly and unnatural, in my judgement. Why am I forced to use _thrice_ as much words to express the simple single-word concept of an assert? Why can't I spare the extra pair of parens, and spell == directly? I see nothing wrong with bytecode rewriting; it's amazing they can do it, and I appreciate the effort.