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

typing.Literal can cause false-positive import #213

Open
Jasha10 opened this issue May 21, 2022 · 3 comments
Open

typing.Literal can cause false-positive import #213

Jasha10 opened this issue May 21, 2022 · 3 comments
Labels
bug Something isn't working upstream-issue

Comments

@Jasha10
Copy link
Contributor

Jasha10 commented May 21, 2022

Hello,

Unfortunately I have a bug to report. I'm getting a false-positive import with certain typing.Literal usage.

before running autoimport:

from typing import Literal as L

var: L["* time"] = "* time"

after running autoimport:

from typing import Literal as L
import time

var: L["* time"] = "* time"

Importing the library time is not correct.

Context:

This bug came up when using the nptyping library, which encourages liberal use of Literal.

Environment:

$ python -c "import autoimport.version; print(autoimport.version.version_info())"
           autoimport version: 1.2.2
               python version: 3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:39:04) [GCC 10.3.0]
                     platform: Linux-5.13.0-35-generic-x86_64-with-glibc2.31
@Jasha10 Jasha10 added the bug Something isn't working label May 21, 2022
@lyz-code
Copy link
Owner

Hi @Jasha10 thanks as always for opening the issue. I was able to reproduce the bug, although I think the solution should be done in the upstream library we use to detect import errors. We blindly trust autoflake to detect import errors, and they are giving a false positive with your code when they shouldn't. If it's critical for you, I'm happy to accept a PR to prevent this issue until it's fixed in autoflake. Although maybe it's easier to use the next snippet that works as expected:

from typing import Literal

var: Literal["* time"] = "* time"

What do you think?

@lyz-code
Copy link
Owner

Weird, I've tried to reproduce the error with autoflake --check script.py but it returns no issues even if you run it on the next snippet:

os.getcwd()

Which should say that os is not imported. Nevertheless autoflake.check is what we use to detect the import errors.

I was going to suggest that you showed them the output of autoflake -c script.py but it looks like it's not a good idea, unless you want to open another issue xD

@Jasha10
Copy link
Contributor Author

Jasha10 commented May 26, 2022

I was going to suggest that you showed them the output of autoflake -c script.py but it looks like it's not a good idea, unless you want to open another issue xD

Haha! Yes, that's strange indeed :)
Both autoflake -c script.py and even autoflake script.py seem to be no-ops.

Anyway, I've opened an issue against autoflake: PyCQA/autoflake#109.

If it's critical for you...

It's not super critical -- I'm happy to wait until we hear back from the autoflake folks :P

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

No branches or pull requests

2 participants