pytelegraf icon indicating copy to clipboard operation
pytelegraf copied to clipboard

Deprecation warning due to invalid escape sequences

Open tirkarthi opened this issue 4 years ago • 2 comments

Deprecation warnings are raised due to invalid escape sequences. This can be fixed by using raw strings or escaping the literals. pyupgrade also helps in automatic conversion : https://github.com/asottile/pyupgrade/

find . -iname '*.py' | grep -Ev 'vendor|example|doc|sphinx' | xargs -P4 -I{} python3.8 -Wall -m py_compile {}
./telegraf/utils.py:9: DeprecationWarning: invalid escape sequence \)
  """
./telegraf/utils.py:19: DeprecationWarning: invalid escape sequence \,
  key = key.replace(",", "\,")
./telegraf/utils.py:20: DeprecationWarning: invalid escape sequence \ 
  key = key.replace(" ", "\ ")
./telegraf/utils.py:21: DeprecationWarning: invalid escape sequence \=
  key = key.replace("=", "\=")
./telegraf/tests.py:14: DeprecationWarning: invalid escape sequence \,
  self.assertEquals(format_string('foo,bar'), 'foo\,bar')
./telegraf/tests.py:15: DeprecationWarning: invalid escape sequence \ 
  self.assertEquals(format_string('foo bar'), 'foo\ bar')
./telegraf/tests.py:16: DeprecationWarning: invalid escape sequence \ 
  self.assertEquals(format_string('foo ,bar'), 'foo\ \,bar')
./telegraf/tests.py:17: DeprecationWarning: invalid escape sequence \ 
  self.assertEquals(format_string('foo ,bar,baz=foobar'), 'foo\ \,bar\,baz\=foobar')
./telegraf/tests.py:59: DeprecationWarning: invalid escape sequence \ 
  """white\ space,tag\ with\,\ comma=hello\,\ world value\,\ comma="foo\""""

tirkarthi avatar Jul 20 '20 12:07 tirkarthi

This is due to this line right here:https://github.com/paksu/pytelegraf/blob/master/telegraf/utils.py#L40

value = value.replace('"', '\"') does not escape as the author intended. '"' == '\"' is True, the backslash is not interpreted as a literal. To fix, replace that line with value = value.replace('"', '\\"')

ianjohnsonspacex avatar Oct 29 '21 15:10 ianjohnsonspacex

We are no longer getting an warning but syntax error:

  File "/opt/virtualenvs/py311/lib64/python3.11/site-packages/telegraf/__init__.py", line 4, in <module>
    from .client import TelegrafClient, HttpClient
  File "/opt/virtualenvs/py311/lib64/python3.11/site-packages/telegraf/client.py", line 2, in <module>
    from telegraf.protocol import Line
  File "/opt/virtualenvs/py311/lib64/python3.11/site-packages/telegraf/protocol.py", line 1, in <module>
    from telegraf.utils import format_string, format_value
  File "/opt/virtualenvs/py311/lib64/python3.11/site-packages/telegraf/utils.py", line 9
    """
    ^^^
SyntaxError: invalid escape sequence '\)'
Exit code: 1

matejsp avatar Oct 03 '23 07:10 matejsp