__all__ attribute: bug and proposal

Pavel S pavel at schon.cz
Mon Jun 27 16:56:36 EDT 2016


Hi,
I today uncovered subtle bug and would like to share it with you.

By a mistake, I forgot to put comma into '__all__' tuple of some module. Notice missing comma after 'B'.

# module foo.py
__all__ = (
    'A',
    'B'
    'C',
)

class A: pass
class B: pass
class C: pass

If you try to import * from the module, it will raise an error, because 'B' and 'C' will be concatenated into 'BC'.

>>> from foo import *
AttributeError: 'module' object has no attribute 'BC'

The bug won't be found until someone imports *.
In order to identify problems as soon as possible, here's the proposal.

Porposal: allow putting objects into __all__ directly, so possible problems will be found earlier:

# module foo.py
class A: pass
class B: pass
class C: pass

__all__ = (A, B, C)

Note: this currently don't work.

>>> from foo import *
TypeError: attribute name must be string, not 'type'



More information about the Python-list mailing list