
Hi folks.
I've been porting things from Python2 to Python3 for a while, but I just found out today about python3's -b option, which emits a warning when comparing str to bytes (or vice versa), or when converting bytes to str using the str initializer. I noticed that it also seems to even help with dictionary keys - nice.
My question is: if -b is no longer detecting things to warn about, how much does that say about how far along your str-vs-bytes-correctness is?
Thanks.

I don't understand the question
Sent from my phone with my typo-happy thumbs. Please excuse my brevity
On Tue, Jul 19, 2022, 18:17 Dan Stromberg drsalists@gmail.com wrote:
Hi folks.
I've been porting things from Python2 to Python3 for a while, but I just found out today about python3's -b option, which emits a warning when comparing str to bytes (or vice versa), or when converting bytes to str using the str initializer. I noticed that it also seems to even help with dictionary keys - nice.
My question is: if -b is no longer detecting things to warn about, how much does that say about how far along your str-vs-bytes-correctness is?
Thanks.
code-quality mailing list -- code-quality@python.org To unsubscribe send an email to code-quality-leave@python.org https://mail.python.org/mailman3/lists/code-quality.python.org/ Member address: graffatcolmingov@gmail.com

When porting code from python2 -> python3, one of the biggest hurdles is getting all the things that used to be just str, converted to sometimes-str-sometimes-bytes.
python3 -b warns about comparisons and likely-unsafe conversions.
If -b is no longer alerting about these, what does that really mean? Is it just instrumenting the str and bytes __eq__ methods and str initializer?
Or is it somehow more thorough than that?
On Tue, Jul 19, 2022 at 4:17 PM Dan Stromberg drsalists@gmail.com wrote:
Hi folks.
I've been porting things from Python2 to Python3 for a while, but I just found out today about python3's -b option, which emits a warning when comparing str to bytes (or vice versa), or when converting bytes to str using the str initializer. I noticed that it also seems to even help with dictionary keys - nice.
My question is: if -b is no longer detecting things to warn about, how much does that say about how far along your str-vs-bytes-correctness is?
Thanks.
code-quality mailing list -- code-quality@python.org To unsubscribe send an email to code-quality-leave@python.org https://mail.python.org/mailman3/lists/code-quality.python.org/ Member address: strombrg@gmail.com

On 7/19/22 16:43, Dan Stromberg wrote:
When porting code from python2 -> python3, one of the biggest hurdles is getting all the things that used to be just str, converted to sometimes-str-sometimes-bytes.
python3 -b warns about comparisons and likely-unsafe conversions.
If -b is no longer alerting about these, what does that really mean?
Another way to phrase the question:
What kind of str/bytes problems does -b not warn about?
-- ~Ethan~

I think the documentation is fairly clear. The -b option only checks *comparisons*, not any other operations.
And only equality and inequality comparisons at that.
https://docs.python.org/3/using/cmdline.html#cmdoption-b
Although the documentation doesn't mentioned it, -b also checks for calling str() on a bytes object.
Other operations always fail:
* Order comparisons (less than, greater than) between string and bytes are always an error in Python3
* Likewise for concatenation b'' + ''
* And other string methods, e.g. 'hello'.index(b'e') raises.
Most operations on mixed str/bytes already fail. The only troublesome cases are the calls to str() and == and != comparisons, and they are checked by -b. What else is there to check?
Not checked: you can mix bytes and strings in the `and` and `or` operators, same as you can mix any combination of types:
'hello' or b'goodbye' or [2.5, 3.6] # Legal.
Also not checked: identity comparison with `is`. A bytes object and a string object will never be identical.
Neither of those should be checked.
As for what that means with regard to your application, that depends on how extensive your testing is. -b only checks comparisons which are actually reached at runtime, not comparisons that aren't reached:
[steve ~]$ python3 -b -c "print(('a' == 'a') if True else ('a' == b'a'))" True

I think the documentation is fairly clear. The -b option only checks *comparisons*, not any other operations.
And only equality and inequality comparisons at that.
https://docs.python.org/3/using/cmdline.html#cmdoption-b
Although the documentation doesn't mentioned it, -b also checks for calling str() on a bytes object.
Other operations always fail:
* Order comparisons (less than, greater than) between string and bytes are always an error in Python3
* Likewise for concatenation b'' + ''
* And other string methods, e.g. 'hello'.index(b'e') raises.
Most operations on mixed str/bytes already fail. The only troublesome cases are the calls to str() and == and != comparisons, and they are checked by -b. What else is there to check?
Not checked: you can mix bytes and strings in the `and` and `or` operators, same as you can mix any combination of types:
'hello' or b'goodbye' or [2.5, 3.6] # Legal.
Also not checked: identity comparison with `is`. A bytes object and a string object will never be identical.
Neither of those should be checked.
As for what that means with regard to your application, that depends on how extensive your testing is. -b only checks comparisons which are actually reached at runtime, not comparisons that aren't reached:
[steve ~]$ python3 -b -c "print(('a' == 'a') if True else ('a' == b'a'))" True
participants (6)
-
Dan Stromberg
-
Dan Stromberg
-
Ethan Furman
-
Ian Stapleton Cordasco
-
Steven D'Aprano
-
Steven D'Aprano