wemake-services/wemake-python-styleguide

WPS336 Explicit String Concat with Bytes

Closed this issue · 2 comments

What's wrong

msg = {'Hello': 'World'}
nul_term = json.dumps(msg).encode('utf8') + b'\x00' 

The second line gets flagged with WPS336.

How it should be

Not flagged? I'm not sure what the "proper" way to deal with bytes is for WPS.

Flake8 version and plugins

{
"platform": {
"python_implementation": "CPython",
"python_version": "3.9.18",
"system": "Linux"
},
"plugins": [
{
"plugin": "darglint",
"version": "1.8.1"
},
{
"plugin": "flake8-bandit",
"version": "4.1.1"
},
{
"plugin": "flake8-broken-line",
"version": "1.0.0"
},
{
"plugin": "flake8-bugbear",
"version": "24.10.31"
},
{
"plugin": "flake8-class-attributes-order",
"version": "0.1.3"
},
{
"plugin": "flake8-commas",
"version": "2.1.0"
},
{
"plugin": "flake8-comprehensions",
"version": "3.16.0"
},
{
"plugin": "flake8-debugger",
"version": "4.1.2"
},
{
"plugin": "flake8-docstrings",
"version": "1.7.0"
},
{
"plugin": "flake8-eradicate",
"version": "1.5.0"
},
{
"plugin": "flake8-isort",
"version": "6.1.1"
},
{
"plugin": "flake8-quotes",
"version": "3.4.0"
},
{
"plugin": "flake8-rst-docstrings",
"version": "0.3.0"
},
{
"plugin": "flake8-string-format",
"version": "0.3.0"
},
{
"plugin": "mccabe",
"version": "0.7.0"
},
{
"plugin": "pep8-naming",
"version": "0.13.3"
},
{
"plugin": "pycodestyle",
"version": "2.12.1"
},
{
"plugin": "pyflakes",
"version": "3.2.0"
},
{
"plugin": "wemake-python-styleguide",
"version": "0.19.2"
}
],
"version": "7.1.1"
}

pip information

pip 21.2.3 from /usr/lib/python3.9/site-packages/pip (python 3.9)
astor==0.8.1
attrs==24.2.0
bandit==1.7.10
chardet==4.0.0
crudini==0.9.5
darglint==1.8.1
dbus-python==1.2.18
decorator==4.4.2
docutils==0.21.2
eradicate==2.3.0
flake8==7.1.1
flake8-bandit==4.1.1
flake8-broken-line==1.0.0
flake8-bugbear==24.10.31
flake8-class-attributes-order==0.1.3
flake8-commas==2.1.0
flake8-comprehensions==3.16.0
flake8-debugger==4.1.2
flake8-docstrings==1.7.0
flake8-eradicate==1.5.0
flake8-isort==6.1.1
flake8-quotes==3.4.0
flake8-rst-docstrings==0.3.0
flake8-string-format==0.3.0
gpg==1.15.1
idna==2.10
iniparse==0.5
isort==5.13.2
libcomps==0.1.18
markdown-it-py==3.0.0
mccabe==0.7.0
mdurl==0.1.2
pbr==6.1.0
pep8-naming==0.13.3
pinliner==0.2.0
pycodestyle==2.12.1
pydocstyle==6.3.0
pyflakes==3.2.0
Pygments==2.18.0
PyGObject==3.40.1
pyinotify==0.9.6
PySocks==1.7.1
python-dateutil==2.8.1
PyYAML==6.0.2
requests==2.25.1
restructuredtext-lint==1.4.0
rich==13.9.4
rpm==4.16.1.3
six==1.15.0
snowballstemmer==2.2.0
stevedore==5.3.0
subscription-manager==1.29.40
systemd-python==234
typing_extensions==4.12.2
urllib3==1.26.5
wemake-python-styleguide==0.19.2

OS information

Ubi9 Docker Container

I think this should work. Defining b'\x00' as a constant (e.g., NULL_BYTE = b'\x00') should do the trick.

import json

Define a constant for the null byte

NULL_BYTE = b'\x00'

msg = {'Hello': 'World'}
nul_term = json.dumps(msg).encode('utf-8') + NULL_BYTE

Yes, I agree: define a constant.