[PATCH 2 of 8] localrepo: use absolute import

Gregory Szorc gregory.szorc at gmail.com
Tue May 26 00:19:16 CDT 2015

On Mon, May 25, 2015 at 9:56 PM, Pierre-Yves David <
pierre-yves.david at ens-lyon.org> wrote:

> On 05/25/2015 12:23 PM, Gregory Szorc wrote:
>> # HG changeset patch
>> # User Gregory Szorc <gregory.szorc at gmail.com>
>> # Date 1432580350 25200
>> #      Mon May 25 11:59:10 2015 -0700
>> # Node ID c2b845cda536ef0abc665befb19319f53bb6216e
>> # Parent  f085ecda3b0e11341ecc119683d5fc47038540d4
>> localrepo: use absolute import
>> Python 3 changes the behavior of "import" to only be absolute. In Python
>> 2.x "import foo" could import a top-level package or a module from the
>> current package. This resulted in ambiguity when the name of a module
>> conflicted with the name of a package. It also resulted in a slight
>> performance overhead. Since the name of a module could be ambiguous,
>> Python would stat() multiple paths until a module was located. With
>> absolute import, the module name is not ambiguous and excessive stat()
>> calls can be avoided.
>> Python 2.5 introduced the "absolute_import" feature to __future__ to
>> enable Python 2.x to import like Python 3. Adopting "absolute_import"
>> makes dual compatibility with Python 3 easier to achieve. In my
>> opinion, it also makes code easier to read since there is no ambiguity
>> as to where a module is coming from. With a project of Mercurial's
>> size (~120 .py files), this can be a real concern, especially for
>> new contributors.
> I think the overall goal is great, CCing Augie on that as he has the
> Python 3 compat hat on.
> But I think using absolute import have some effect of how python handle
> cycle, Augie will have that in his head. More feedback below

The first time I sent a patch like this out, someone privately replied and
pointed me to https://bugs.python.org/issue17636. tl;dr "import foo" and
"from . import foo" behave differently when import cycles are involved.
"import foo" apparently works more often than "from . import foo."

Fortunately, the number of import cycles is low, so hopefully the number of
"import mercurial..." statements required to break cycles is low.

>  PEP-0328 also introduced the multiple line import statement. Simply
>> add parenthesis and you get more readable import statements. This
>> PEP also introduced relative imports ("from . import foo").
> I would personnaly be happier to use explicite relative imports though I
> feel they are more compact and easier to read while staying explicit
>  This patch switches localrepo.py to use absolute imports as well as
>> modern import syntax. While I was here, I also reordered imports:
>> standard library first, Mercurial second, each alphabetized within its
>> subset.
> +1 for standard library first
> +1 for one import per line
> +1 alphabetized import
> --
> Pierre-Yves David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20150525/2349ebfd/attachment.html>

More information about the Mercurial-devel mailing list