Skip to content

rewrite of as imports changes the behavior of the imports #1280

@keewis

Description

@keewis

#1107 introduced a rewrite of import test.module as m styled imports to something like from test import module as m.

These changes are actually not equivalent (even though the name clash probably indicates poor design): using the standard import machinery, import test.module always tries to import the module test/module.py from somewhere on sys.path while from test import module will take anything it can get.

Consider a package like

$ tree test
test
├── __init__.py
└── module.py

0 directories, 2 files
$ cat test/__init__.py
def module():
    pass

with that, we can see the difference:

$ python -c 'import test.module as m; print(m)'
<module 'test.module' from '.../test/module.py'>
$ python -c 'from test import module as m; print(m)'
<function module at 0x7fb259fcf700>

which makes me a bit nervous since I usually don't look that deep into the code of dependencies.

Is there a setting to disable the rewrite?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions