[RFC] Indentation / import rules

John Arbash Meinel john at arbash-meinel.com
Mon Jul 24 14:58:51 BST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I believe there is a general consensus to switch away from:

import bzrlib.foo
bzrlib.foo.func1()

or

from bzrlib.foo import func1
func1()

or

import bzrlib.foo as foo
foo.func1()

To using:

from bzrlib import foo
foo.func1()

It means we still have module level scope, so that if we need to replace
a function or member variable (say in testing, or by a plugin rewriting
something), it can be done in the original module, without tracking down
all other occurrences.
It also makes using something like 'demandload()' much easier, since we
are only importing modules.

I believe we want to switch to a single import, which imports multiple
modules at once. For example, we don't want to do:

from bzrlib import baz
from bzrlib import foo

Instead we want

from bzrlib import baz, foo

However, we now have to watch out for import conflicts, and putting them
all on one line makes it very likely to conflict.

So I think we want to switch to a multi-line import like:

from bzrlib import (baz,
                    foo,
                   )

There are lots of ways to do this, and we are pretty inconsistent at the
moment. Probably the most common is the above pattern, though my
preference is to at least switch to:

from bzrlib import (
                    baz,
                    foo,
                   )

I'm not sure why, but Robert prefers to move the final ')' one space
over, to line it up with the contents, rather than the other parethesis.
So he tends to do:
from bzrlib import (baz,
                    foo,
                    )

My personal favorite is:
from bzrlib import (
    baz,
    foo,
    )

I think we really just need a proclamation from Martin. They are all
valid, and I'm willing to defer to whatever is decided.

I read through PEP8, and I see a few places we could use some updates. I
recommend everyone read it again. But it doesn't cover this usage.
(I assume because from foo import () didn't exist before python 2.4)
PEP328 is where they introduced parethesis, and it just has all of the
imports in a row
http://www.python.org/dev/peps/pep-0328/
I think we need 1 per line to help avoid merge conflicts.

Anyway, I'd like to find out the preferred way, so we can continue on
from here.

Also, I've played with demandload, and it uses strings, separated by
whitespace. Which means that:

import bar
import foo

becomes:
demandload(globals(), 'bar foo')
which I prefer to translate into:
demandload(globals(),
    ' bar'
    ' foo'
    )

And
from bzrlib import bar, foo
becomes
demandload(globals(), 'bzrlib:bar,foo')

I don't know what the best thing is here, but I would like something
such as:

demandload(globals(),
    ' bzrlib:'
        'bar,'
        'foo,'
    )

I can make sure the demandload parser can handle whatever syntax we
decide upon. I don't really like the current demandload syntax. I would
rather have it be more like import syntax, and be able to do:
demandload(globals(),
    'bar,'
    'foo,'

    'bzrlib:('
             'bbb,'
             'xxx,'
    '),'
    )

Or something like that.

We *can* split each demandload line up into a separate function call.
But since the whole point is to make import time faster, it doesn't seem
like making N function calls is the best way to go.

Heck, if we wanted, we could change it so that demandload worked on
regular python statements, and then we do:
demandload(globals(), """
import bar
import foo

from bzrlib import (bbb, xxx)
""")

And then we just keep the same syntax that we decide upon now.
John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFExNIbJdeBCYSNAAMRAtmGAKCP1d0BCyru1woTOxbl1yuU0rbGqgCgh3E8
M8oylof6eJsfffhs/L0nGcM=
=xzp2
-----END PGP SIGNATURE-----




More information about the bazaar mailing list