This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 2to3 translates "import foobar" to "import .foobar" rather than "from . import foobar"
Type: behavior Stage:
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: wolever Nosy List: benjamin.peterson, collinwinter, dangyogi, loewis, wolever
Priority: normal Keywords:

Created on 2008-03-21 17:59 by dangyogi, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg64247 - (view) Author: Bruce Frederiksen (dangyogi) Date: 2008-03-21 17:59
2to3 svn rev 61696 translates "import local_module" into "import
.local_module" which isn't legal syntax.

Should be "from . import local_module".
msg64338 - (view) Author: David Wolever (wolever) * (Python committer) Date: 2008-03-22 20:36
Ok, I've fixed this in r61755.

I _think_ it does the right thing, but it might be good if someone else 
checks out the test cases to make sure.
msg64647 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2008-03-28 19:04
The problem with the "fix" in r61755 is that it doesn't distinguish
between stdlib and non-stdlib imports: "import os" becomes "from .
import os", which is clearly wrong.
msg64648 - (view) Author: David Wolever (wolever) * (Python committer) Date: 2008-03-28 19:19
Can you write a test case proving this?

At the moment, the second thing that the transform function in  
fix_import.py does is return if the import doesn't look like a local  
import (see probably_a_local_import in fix_import).

At the moment, all (with an exception or two) of the import test  
cases test both cases -- the local import exists and the local import  
does not exist -- automatically.

On 28-Mar-08, at 3:04 PM, Collin Winter wrote:

>
> Collin Winter <collinw@gmail.com> added the comment:
>
> The problem with the "fix" in r61755 is that it doesn't distinguish
> between stdlib and non-stdlib imports: "import os" becomes "from .
> import os", which is clearly wrong.
>
> __________________________________
> Tracker <report@bugs.python.org>
> <http://bugs.python.org/issue2446>
> __________________________________
msg64651 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2008-03-28 20:41
Looking at it, I was confused by a quirk introduced into test_fixers:
Test_import's check_both() helper method does the wrong thing in this case:

a = "import os"
self.check_both(a, a)  # Fails, tries to translate "import os" into
"from . import os"

self.unchanged(a) passes, though. What's going on with these methods?
msg64653 - (view) Author: David Wolever (wolever) * (Python committer) Date: 2008-03-28 20:59
Ah, yes -- that may be the fault of the confusingly named  
'check_both'.  The "both" means "both when the module in question  
exists and when it does not".  If you've got a better name, please  
change it!
Take a look at the method and you'll see how it works.

The problem is that testing this is a little tricky -- you've either  
got to give it files that you know exist, create files, or work some  
magic.
I took the work some magic route, and replaced the "exists" method  
which fix_import imports from os (that way the tests can check the  
list of files which are being checked).
See the test_files_checked method for that.
msg75017 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-10-21 02:25
I fixed this is r66707.
History
Date User Action Args
2022-04-11 14:56:32adminsetgithub: 46698
2008-10-21 02:25:47benjamin.petersonsetstatus: open -> closed
nosy: + benjamin.peterson
resolution: fixed
messages: + msg75017
2008-03-28 20:59:45woleversetmessages: + msg64653
2008-03-28 20:41:53collinwintersetmessages: + msg64651
2008-03-28 19:19:37woleversetmessages: + msg64648
2008-03-28 19:04:46collinwintersetmessages: + msg64647
2008-03-22 20:36:34woleversetmessages: + msg64338
2008-03-21 20:23:40loewissetnosy: + loewis
2008-03-21 20:22:41collinwintersetassignee: collinwinter -> wolever
nosy: + wolever
2008-03-21 17:59:41dangyogicreate