Skip to content

Too strict behavior with --ignore-missing-imports #5226

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

Closed
ilevkivskyi opened this issue Jun 17, 2018 · 7 comments
Closed

Too strict behavior with --ignore-missing-imports #5226

ilevkivskyi opened this issue Jun 17, 2018 · 7 comments

Comments

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Jun 17, 2018

I think the --ignore-missing-imports currently behaves a bit inconsistently. If I have an existing directory with only an empty __init__.py, then running mypy with --ignore-missing-imports gives this:

import whatever.works
import existing.nonexisting

x = whatever.works.f()  # No errors, as expected
y = existing.nonexisting.g()  # Module has no attribute "nonexisting"

At the same time both these work (in the same program, otherwise the second fails):

from whatever import works  # no errors
from existing import nonexisting # no errors

I think the first example should work as well, since all non-existing modules should be replaced with a dummy Any (and apparently it looks easy to fix, unless this is intentional behavior for some reasons).

EDIT: see #5226 (comment)

@gvanrossum
Copy link
Member

gvanrossum commented Jun 17, 2018 via email

@ilevkivskyi
Copy link
Member Author

Isn't this the reason we don't like partial packages in typeshed?

Maybe. But what is the value of that error "Module has no attribute ..."?

@ilevkivskyi
Copy link
Member Author

ilevkivskyi commented Jun 17, 2018

Correction: from existing import nonexisting also fails. It however doesn't fail, if there is an import existing.nonexisting elsewhere.

@gvanrossum
Copy link
Member

For me the deciding difference is whether it could reference something other than a module. import existing.nonexisting has to refer to a module, so --ignore-missing-imports applies. But from existing import nonexisting could refer to a class, function or variable defined in existing and as such it is not covered by --ignore-missing-imports. The presence of import existing.nonexisting elsewhere implies that it refers to a submodule, so then the flag applies.

So I don't necessarily believe that "fixing" this will make things better -- it will just trade false positives for false negatives.

@ilevkivskyi
Copy link
Member Author

So I don't necessarily believe that "fixing" this will make things better -- it will just trade false positives for false negatives.

So do you prefer false positives? I prefer false negatives. Also as Jukka mentioned in the PR some users may be surprised that refactoring import a.b into from a import b turns it into an error.

@gvanrossum
Copy link
Member

gvanrossum commented Jun 18, 2018 via email

@ilevkivskyi ilevkivskyi changed the title Inconsistency in --ignore-missing-imports Too strict behavior with --ignore-missing-imports Jun 18, 2018
@ilevkivskyi
Copy link
Member Author

OK, I updated the title.

ilevkivskyi added a commit that referenced this issue Jun 21, 2018
Fixes #5226

With this change, the following
```
import existing_package.non_existing_mod
x = existing_package.non_existing_mod.some_func()
```
will be allowed under `--ignore-missing-imports`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants