Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import headings in blocks lead to check errors #1301

Closed
mir06 opened this issue Jul 8, 2020 · 6 comments
Closed

Import headings in blocks lead to check errors #1301

mir06 opened this issue Jul 8, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@mir06
Copy link

mir06 commented Jul 8, 2020

When using import_heading for a section and the import is not at module level the isort-ed code is now perfectly formatted (#1290) but it does not pass the check anymore.

Here's a simple example for demonstration

import isort
code = """
try: 
    import logging 
        from os import abc, path 
    except ImportError: 
        pass
"""
extra_config = {"import_heading_stdlib": "stdlib"}
# checking code leads to error
isort.check_code(isort.code(code, **extra_config), **extra_config)
ERROR:  Imports are incorrectly sorted and/or formatted.
False
# the fixes are applied once
isort.code(code, **extra_config) == isort.code(isort.code(code, **extra_config), **extra_config)
True

When running isort on the command-line it says that the file is fixed but nothing is changed and this leads to an infinite loop.

@timothycrosley
Copy link
Member

timothycrosley commented Jul 9, 2020

@mir06

Thanks for reporting! I'm a bit confused, but I think I've determined what it is you want:

Currently, given

code = """
try: 
    import logging
    from os import abc, path 
except ImportError: 
    pass
"""

if you do isort.code(code, import_heading_stdlib="stdlib")
isort, will currently correctly follow your request and give you:

"""
try: 
    # stdlib
    import logging
    from os import abc, path 
except ImportError: 
    pass
 """

Which means that the check above the above example will correctly spot the inconsistency.

My best guess is that you instead expected:

"""
try: 
    import logging
    from os import abc, path 
except ImportError: 
    pass
 """

Which would mean you want a way to not have import headings apply to nested import sections? Is that correct? Let me know if I'm off base here in any way!

@timothycrosley timothycrosley added the enhancement New feature or request label Jul 9, 2020
@mir06
Copy link
Author

mir06 commented Jul 9, 2020

@timothycrosley thank you for your quick response (and of course for the great tool you provide)

Well now I am a bit confused. I would expect that check_code returns True for the correctly formatted code

"""
try: 
    # stdlib
    import logging
    from os import abc, path 
except ImportError: 
    pass
"""

I do not understand your

Which means that the check above the above example will correctly spot the inconsistency.

@timothycrosley
Copy link
Member

Yes for the second example isort does and should return true for check, because the comment exists, but for the first (which is the same as the issue) isort returns False on check because the comment is missing - so it's not valid based on the configuration set

@mir06
Copy link
Author

mir06 commented Jul 10, 2020

Yes, first it shall complain. But after reformatting (and getting the correct code) it still complains and isort will never apply any changes anymore.
In fact the bug report sould have just been about check_code:

code = """
try: 
    # stdlib
    import logging
    from os import abc, path 
except ImportError: 
    pass
"""
extra_config = {"import_heading_stdlib": "stdlib"}
isort.check_code(code, **extra_config)
ERROR:  Imports are incorrectly sorted and/or formatted.
False

but should return True.

@timothycrosley
Copy link
Member

Sorry for my confusion! And thank you for bearing with me! I finally was able to reproduce the error, fix it, and push that fix to PYPI. If you download the latest 5.0.9 release of isort, this issue should no longer be present.

Thanks!

~Timothy

@timothycrosley timothycrosley added bug Something isn't working and removed enhancement New feature or request labels Jul 11, 2020
@mir06
Copy link
Author

mir06 commented Jul 11, 2020

Wow, thank you very much! I checked and confirm it is working perfectly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants