alecthomas / importmagic

A Python library for finding unresolved symbols in Python code, and the corresponding imports
BSD 2-Clause "Simplified" License
120 stars 20 forks source link

importmagic shouldn't order relative imports before third-party imports #29

Closed ChillarAnand closed 8 years ago

ChillarAnand commented 8 years ago

Here is a test file

import os
from .forms import Foo
from django.contrib.auth.decorators import login_required

os.path
Foo()
login_required

When i run import fixup, it changes to

import os

from .forms import Foo

from django.contrib.auth.decorators import login_required

os.path
Foo()
login_required

But it should be

import os

from django.contrib.auth.decorators import login_required

from .forms import Foo

os.path
Foo()
login_required
alecthomas commented 8 years ago

Are you using master? Under master, this works for me. If you can try it, let me know if it works.

If not, I believe this is actually a misclassification of django, treating it as a local import for some reason.

If you look in your importmagic index.json file, you should be able to see each packages location under the key .location.

alecthomas commented 8 years ago

I just released 0.1.5 which has a lot of fixes. I'm going to close this under the assumption that the issues are fixed. Reopen it if not.

ChillarAnand commented 8 years ago

Unfortunately, its not working yet.

In [27]: importmagic.__version__
Out[27]: '0.1.5'

In [28]: python_source = '''                                                                                                                     
import os
from .forms import Foo
from django.contrib.auth.decorators import login_required

os.path
Foo()
login_required
'''

In [29]: start_line, end_line, import_block = importmagic.get_update(python_source, index, unresolved, unreferenced)

In [30]: import_block
Out[30]: 'import os\n\nfrom .forms import Foo\nfrom django.contrib.auth.decorators import login_required\n\n\n'
alecthomas commented 8 years ago

Did you check the index?

ChillarAnand commented 8 years ago

Yes, it is treating it as local package.

In [12]: index.find('django').location
Out[12]: u'L'
ChillarAnand commented 8 years ago

In latest version, it is identifying django as 3rd party package. Still ordering doesn't change.

alecthomas commented 8 years ago

I can't replicate this. It's working fine for me:

$ python -mimportmagic.index > index.json

Then:

>>> python_source = '''
import os
from .forms import Foo
from django.contrib.auth.decorators import login_required

os.path
Foo()
login_required
'''
>>> scope = importmagic.Scope.from_source(python_source)
>>> unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
>>> with open('index.json') as fd:
        index = importmagic.SymbolIndex.deserialize(fd)
   ...:
>>> start_line, end_line, import_block = importmagic.get_update(python_source, index, unresolved, unreferenced)
>>> print import_block
import os

from django.contrib.auth.decorators import login_required

from .forms import Foo

>>>