bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnulib-tool.py: Fix 'consider-using-with' pylint warnings.


From: Collin Funk
Subject: gnulib-tool.py: Fix 'consider-using-with' pylint warnings.
Date: Thu, 4 Apr 2024 21:15:37 -0700
User-agent: Mozilla Thunderbird

Here is two small patches to fix some pylint warnings. The first is
'consider-using-with'. This recommends to use context managers that
handle cleanup for you. In this case two Popen calls, but here is an
example with open():

     with open(file_name, 'r') as file:
         data = file.read()
     # File is closed for us.

is cleaner than:

     file = open(file_name, 'r')
     data = file.read()
     file.close()

In the case of Popen, "Popen objects are supported as context managers
via the with statement: on exit, standard file descriptors are closed,
and the process is waited for [1]."

I've just changed these to 'sp.run()' since that function deals with
everything for us and is recommended.

I've left it wrapped in os.chdir() calls since I rather convert all
occurrences of those to sp.run(cwd=directory) in one patch. Things seem
less likely to break that way.

The second is 'consider-using-set-comprehension'. Instead of calling
set() on a list comprehension we can just use a set comprehension.
I've additionally simplified this case:

-            version = sorted(set([ float(version)
-                                   for version in versions ]))[-1]
+            version = max({ float(version)
+                            for version in versions })

The previous method is just a less clear way of finding the max:

    print([1, 2, 3, 4][-1])
    4

[1] https://docs.python.org/3/library/subprocess.html#popen-constructor

Collin

Attachment: 0001-gnulib-tool.py-Fix-consider-using-with-pylint-warnin.patch
Description: Text Data

Attachment: 0002-gnulib-tool.py-Fix-consider-using-set-comprehension-.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]