Isort: Question about differences between v4.3.21+rc and v5.0.4 in behaviors to local files in a subdirectory.

Created on 7 Jul 2020  ·  6Comments  ·  Source: PyCQA/isort

I thank authors for developing such a great tool.

According to the upgrade guide to v5, we should remove -rc option to traverse directories recursively in v5. However, in my environment, just simply removing -rc option changes the output. Specifically, v5.0.4 and v4.3.21+-rc seemingly behave differently for local files in a subdirectory. It seems v5 treats a local file in a subdirectory as a third party library. I am wondering if it is an expected behavior.

I would like you to let me know if I should give you more information. This is the first time I register the issue to this project. So, I am sorry if I miss some information.

Code to reproduce
Setting

%tree .                 
.
└── example
    ├── a.py
    └── main.py

1 directory, 2 files

%cat example/main.py 
import numpy as np

import a

a.foo()

v5.0.4

%isort -V                

                 _                 _
                (_) ___  ___  _ __| |_
                | |/ _/ / _ \/ '__  _/
                | |\__ \/\_\/| |  | |_
                |_|\___/\___/\_/   \_/

      isort your imports, so you don't have to.

                    VERSION 5.0.4

%isort --df --check-only example/
ERROR: /Users/oonokenta/sandbox/isort_test/example/main.py Imports are incorrectly sorted and/or formatted.
--- /Users/oonokenta/sandbox/isort_test/example/main.py:before  2020-07-07 10:19:08.205966
+++ /Users/oonokenta/sandbox/isort_test/example/main.py:after   2020-07-07 13:26:44.708299
@@ -1,5 +1,4 @@
+import a
 import numpy as np

-import a
-
 a.foo()

v4.3.21

%isort -v

/#######################################################################\

     `sMMy`
     .yyyy-                                                      `
    ##soos##                                                    ./o.
          `     ``..-..`         ``...`.``         `   ```` ``-ssso```
     .s:-y-   .+osssssso/.     ./ossss+:so+:`    :+o-`/osso:+sssssssso/
     .s::y-   osss+.``.``     -ssss+-.`-ossso`   ssssso/::..::+ssss:::.
     .s::y-   /ssss+//:-.`   `ssss+     `ssss+   sssso`       :ssss`
     .s::y-   `-/+oossssso/  `ssss/      sssso   ssss/        :ssss`
     .y-/y-       ````:ssss`  ossso.    :ssss:   ssss/        :ssss.
     `/so:`    `-//::/osss+   `+ssss+-/ossso:    /sso-        `osssso/.
       \/      `-/oooo++/-      .:/++:/++/-`      ..           `://++/.


         isort your Python imports for you so you don't have to

                            VERSION 4.3.21

\########################################################################/

%isort -rc -df --check-only .   # => no outputs
question

Most helpful comment

@honzajavorek There's a mention of this change on the v5.0 Upgrade Guide here.

The TL;DR of which is that isort has now changed from default_section=FIRSTPARTY to default_section=THIRDPARTY.

All 6 comments

I'm experiencing the same with or without --recursive/-rc (v5.0.4).

Can I send a PR with a failing test?

I was thinking about adding a test to https://github.com/timothycrosley/isort/blob/develop/tests/test_regressions.py in a PR to manifest the failing behavior, but all of the tests there are nicely isolated, I don't think that would work. There needs to be a context of what's installed and what's local, but I don't know how to pass that context.

My case

In my case, the unrecognized local module is testing_utils.py in a non-package directory with tests. My imports look like from testing_utils import a, b, c and previous versions of isort correctly recognized them as first party. The new versions seem to classify them as third party. For the record, my settings are:

[settings]
skip = venv,node_modules
virtual_env = venv
multi_line_output = 0
balanced_wrapping = True
lines_after_imports = 2

Workaround

I've set the following in my isort settings to mitigate the problem as of now:

[settings]
...
known_first_party = testing_utils

@honzajavorek There's a mention of this change on the v5.0 Upgrade Guide here.

The TL;DR of which is that isort has now changed from default_section=FIRSTPARTY to default_section=THIRDPARTY.

@delta2323 and @honzajavorek, @fyber is correct. There is a new src path setting (see: https://timothycrosley.github.io/isort/docs/configuration/options/#src-paths) that may be helpful, if you don't want to manually specify the known_first_party (which is also a correct and encouraged to solve this!).

Makes sense, thank you!

@fyber @timothycrosley Thank you for your explanation! I can solve my problem by configuring default_section value. Should I close this issue by myself?

@delta2323 Glad to hear! I can close on my end, just wanted to make sure everyone's issue was resolved before I closed the ticket :)

Was this page helpful?
0 / 5 - 0 ratings