[py-usr] flake8 gives me a W605 but Python don't

2022-09-10 Thread c.buhtz
Hello,

My `flake8` gives me a "W605 invalid escape sequence" [1] warning for
this piece of example code.

import re

def foobar():
rex = re.compile('Version: \d+.\d+.\d+.*', re.MULTILINE)

for match in rex.findall(' Version: 1.2.3 '):
print(match)


if __name__ == '__main__':
foobar()

But running this with Python 3.9.2 makes no problem. Python doesn't
give me a `SyntaxWarning` or anything else. Python doesn't give me an
error or warning. Only `flymake8` gives me this error.

I do understand what is wrong with the pattern string in `compile()`.
There should be a `r''` or the regex-escape characters should be
escaped them selfs (e.g. `'Version: \\d'`).

But my question is about why Python doesn't give me an error about it
and why does it work. The pattern matches. Shouldn't there be an error
or something? Does Python identify this string as an r-String by itself?

[1] -- 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [py-usr] flake8 gives me a W605 but Python don't

2022-09-10 Thread MRAB

On 2022-09-10 19:46, c.bu...@posteo.jp wrote:

Hello,

My `flake8` gives me a "W605 invalid escape sequence" [1] warning for
this piece of example code.

 import re
 
 def foobar():

 rex = re.compile('Version: \d+.\d+.\d+.*', re.MULTILINE)
 
 for match in rex.findall(' Version: 1.2.3 '):

 print(match)
 
 
 if __name__ == '__main__':

 foobar()

But running this with Python 3.9.2 makes no problem. Python doesn't
give me a `SyntaxWarning` or anything else. Python doesn't give me an
error or warning. Only `flymake8` gives me this error.

I do understand what is wrong with the pattern string in `compile()`.
There should be a `r''` or the regex-escape characters should be
escaped them selfs (e.g. `'Version: \\d'`).

But my question is about why Python doesn't give me an error about it
and why does it work. The pattern matches. Shouldn't there be an error
or something? Does Python identify this string as an r-String by itself?

[1] -- 


Historically, invalid escapes such as '\d' have always been treated as 
'\\d', but that is, and always, bad idea; it's better to treat them as 
an error (or reserved for future use).


At some point, in some future version of Python, they should become an 
error, but there's always a chance that some code out there could break, 
so there's a long period of deprecation first. How long? Who knows!


Just take note of the warning, and don't write bad escapes.
--
https://mail.python.org/mailman/listinfo/python-list


Re: [py-usr] flake8 gives me a W605 but Python don't

2022-09-10 Thread Reto
On Sat, Sep 10, 2022 at 06:46:33PM +, c.bu...@posteo.jp wrote:
> 
> But running this with Python 3.9.2 makes no problem. Python doesn't
> give me a `SyntaxWarning` or anything else. Python doesn't give me an
> error or warning. Only `flymake8` gives me this error.

Well, it's not a syntax error.
Escape sequences with no meaning simply yield the escape sequence.

As in, '\d' is simply '\d', in contrast  with say '\u' which is invalid
and if fact throws a SyntaxError.


>Unlike Standard C, all unrecognized escape sequences are left in the string 
>unchanged, i.e., the backslash is left in the result. (This behavior is useful 
>when debugging: if an escape sequence is mistyped, the resulting output is 
>more easily recognized as broken.) It is also important to note that the 
>escape sequences only recognized in string literals fall into the category of 
>unrecognized escapes for bytes literals.

from 
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [py-usr] flake8 gives me a W605 but Python don't

2022-09-10 Thread Thomas Passin
I don't know what flake8 is complaining about, but I think you want a 
literal "." character in front of the two "\d" escapes.  So "." should 
be replaced by "\." in two places.  In addition to using a raw string, 
that is.


If you may need to detect versions larger than 9 in any position, you 
would need to generalize your pattern, since "\d" is only going to look 
at a single character, IIUC.


If you could be sure that the version string would always look exactly 
like your example, you could avoid a regex by splitting:


_, version = version_string.split('Version:')
version_parts = version.split('.')

That's probably hoping for too much, though.

On 9/10/2022 2:46 PM, c.bu...@posteo.jp wrote:

Hello,

My `flake8` gives me a "W605 invalid escape sequence" [1] warning for
this piece of example code.

 import re
 
 def foobar():

 rex = re.compile('Version: \d+.\d+.\d+.*', re.MULTILINE)
 
 for match in rex.findall(' Version: 1.2.3 '):

 print(match)
 
 
 if __name__ == '__main__':

 foobar()

But running this with Python 3.9.2 makes no problem. Python doesn't
give me a `SyntaxWarning` or anything else. Python doesn't give me an
error or warning. Only `flymake8` gives me this error.

I do understand what is wrong with the pattern string in `compile()`.
There should be a `r''` or the regex-escape characters should be
escaped them selfs (e.g. `'Version: \\d'`).

But my question is about why Python doesn't give me an error about it
and why does it work. The pattern matches. Shouldn't there be an error
or something? Does Python identify this string as an r-String by itself?

[1] -- 


--
https://mail.python.org/mailman/listinfo/python-list


Re: [py-usr] flake8 gives me a W605 but Python don't

2022-09-10 Thread Mats Wichmann

On 9/10/22 13:20, Stefan Ram wrote:

 writes:

'Version: \d+.\d+.\d+.*'


   All unrecognized escape sequences are left in the string
   unchanged, i.e., the backslash is left in the result.

   This behavior is useful when debugging: if an escape
   sequence is mistyped, the resulting output is more easily
   recognized as broken.

   Some Python versions may emit a DeprecationWarning when
   an unrecognized escape sequence is encountered; future
   Python versions might emit a SyntaxWarning or SyntaxError.


Consider:

$ /usr/bin/python3.11 -X dev stuff.py
/tmp/stuff.py:4: DeprecationWarning: invalid escape sequence '\d'
  rex = re.compile('Version: \d+.\d+.\d+.*', re.MULTILINE)
Version: 1.2.3




--
https://mail.python.org/mailman/listinfo/python-list