[Python-il] Statically analyzing a Python module to find assignments

cool-RR cool-rr at cool-rr.com
Sat Apr 16 16:37:25 IDT 2011


On Sat, Apr 16, 2011 at 8:40 AM, Tal Einat <taleinat at gmail.com> wrote:

> Mangling __import__ temporarily might work, sounds like a hack I would
>>> prefer to avoid. And theoretically __init__.py can include other heavy code.
>>> And you could break stuff if __init__.py assumes that the imports work in a
>>> way that returning a mock object breaks.
>>>
>>
>> Definitely not pretty. But can you come up with an example of how it would
>> be broken? I think most such examples would be pretty contrived and not
>> something that's likely to be written in real life.
>>
>
> Even something as simple as this would break (if not in the import
> statement, then later):
> from some_package.some_module import some_thing
>

The import line wouldn't break. What kind of stuff that uses `some_thing`
will break later? I think that imports in `__init__.py` files are almost
always imports for the namespace's sake, and that nothing complex is done
with the imported objects. And even if the `__init__.py` file has some logic
in, I think nothing too bad will happen if it will operate on mock objects.


> Do you have a really good reason not to make the writer of a simpack
>>> responsible for making __init__.py quick to import? Then you can optimize
>>> only if a real need arises.
>>>
>>
>> I personally believe that `__init__.py` should import important stuff from
>> the package, and not only in simpacks but in general in Python packages.
>>
>
> Then perhaps such metadata shouldn't be kept in __init__.py in the first
> place?
>

> - Tal
>

That would definitely be an easy solution: Tell simpack writers to have a
file "metadata.txt" or whatever. But I find it less elegant. I believe that
almost every Python package (or module or function or class) should have a
docstring, so if I'll have to maintain the simpack description in both
places (docstring and "metadata.txt"), I'll be breaking DRY.

Also, people who are not experienced with distutils may have to spend some
"quality time" with it to make it package the "metadata.txt" file with their
code. As Guido said, "I'd like not to have such a newbie trap lying around."


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://hamakor.org.il/pipermail/python-il/attachments/20110416/b692262a/attachment-0001.htm>


More information about the Python-il mailing list