bug-gnulib
[Top][All Lists]
Advanced

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

gnulib-tool.py: Use absolute imports consistently.


From: Collin Funk
Subject: gnulib-tool.py: Use absolute imports consistently.
Date: Sun, 21 Apr 2024 17:38:51 -0700
User-agent: Mozilla Thunderbird

Hi Bruno,

I want to make the imports and use of functions from other modules
consistent. This patch makes all files use absolute imports like
main.py. So this:

   from pygnulib.GLEmiter import GLEmiter

instead of:

   from .GLEmiter import GLEmiter

I want to make another change but would like opinions before writing
the patch. Right now we have:

   import os.path
   from pygnulib import constants
   [...]
   DIRS = constants.DIRS
   substart = constants.substart
   isfile = os.path.isfile

This works fine, but stops editors from tracking function usage
properly. For example, GLEmiter might use 'substart' 30 times but an
editor will say there is only 1 usage since we define
'constants.substart' to a variable. Therefore, I would prefer:

  from constants import substart, bold_escapes, ...

If lines get too long you can do something like:

   from constants import (
       substart,
       bold_escapes,
       ...
    )

While making that change it would also be a good time to fix another
inconsistency. Right now some files will use a mix of functions
prefixed and not prefixed by a module. For example, both
'os.path.isfile' and 'isfile'. I think the best solution is to import
our own code directly and use the module prefix for standard library
(and third-party code if ever needed). So like this:

    # Import like this.
    import os.path
    from constants import substart
    # Use them like this.
    os.path.join(...)
    substart(...)

This feels like what I am used to seeing. It also should be clear when
we have situations like 'constants.rmtree()' vs. 'shutil.rmtree()'.

Slight exception for the subprocess module, since everyone does
'import subprocess as sp'. It would feel strange not too. :)

Any disagreement with those changes?

Collin

Attachment: 0001-gnulib-tool.py-Use-absolute-imports-consistently.patch
Description: Text Data


reply via email to

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