Skip to content

Importing unstubbed submodules of stubbed modules with silent imports gives inconsistent errors #1852

@ddfisher

Description

@ddfisher

In this example, the boto package has a stub, but the boto.foo module does not:

$ python3 -m mypy --py2 --silent-imports -c 'import boto.foo'
# no error
$ python3 -m mypy --py2 --silent-imports -c 'from boto import foo'
<string>:1: error: Module has no attribute 'foo'
$ python3 -m mypy --py2 --silent-imports -c 'import boto.foo; boto.foo'
<string>:1: error: "module" has no attribute "foo"
# in real code, this error is on line where boto.foo is used

As you can see, the behavior here is inconsistent. import boto.foo gives no error, but attempting to use boto.foo later in the program does give an error.

I think the correct behavior here is for import x.y with --silent-imports to be an error when x has a stub but y does not, because this feels conceptually similar to other cases of incomplete stubs. Otherwise, you'd be in danger of importing nonexistent submodules from packages you have full stubs for, with no way to lock that down. The only downside to this approach is it means you have to use a type ignore when the stubs are incomplete, but that's already the case most of the time.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions