Removal of Python 2 (Request for Comments)
José Matos
jaomatos at gmail.com
Fri May 31 14:40:41 UTC 2024
This is the code that I intend to submit to remove Python 2 support.
I am also using formatted strings heavily, so in this case I am using
Python 3.8 syntax.
FWIW this patch is a mix between manual changes and using
$ pyupgrade --py38-plus file.py
I am putting this here before applying to get your opinion on this.
I am mainly waiting for those who usually apply Python changes like
Jürgen, Riki or Enrico. Naturally that everyone is welcome to express
their opinion on this. :-)
The changes are in several categories:
* In Python 3 all code files are utf-8;
* remove code that is conditional to Python 2;
* print is a function (remove "from __future__ import print_function");
* strings are unicode (thus remove the _u_ preffix);
* use formatted strings by default instead of interpolation for
strings. As an example (unified diff):
- raise FileError("error listing files in '%s': %s" % (src, errstr))
+ raise FileError(f"error listing files in '{src}': {errstr}")
The use of formatted strings (from which the fmt C++ library took a
direct inspiration) can become a bit awkward when using curly brackets
as we use sometimes. This is better described by an example:
- lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label)
+ lines[i - 1] = f"{lines[i - 1][:-1]}[{ref}]{{{label}}}"
Notice that since the third argument goes inside curly brackets we have
to double the opening and closing bracket. In a sense this is similar
to using regular expressions where we have to double the backslash (for
example).
Notice that using any editor that understands Python syntax and that
uses color the issue is almost gone. I have no problem with emacs, vi,
kate, ...
What do you think?
Best regards,
--
José Abílio
-------------- next part --------------
A non-text attachment was scrubbed...
Name: remove_python2.patch
Type: text/x-patch
Size: 210996 bytes
Desc: not available
URL: <http://lists.lyx.org/pipermail/lyx-devel/attachments/20240531/d387a076/attachment-0001.bin>
More information about the lyx-devel
mailing list