Skip to content

Redesign import handling  #8584

Closed
Closed
@JukkaL

Description

@JukkaL

The way mypy deals with imports is quite confusing. I'm proposing that we reconsider how various import-related features work. There are existing issues that cover some of these, but I want to discuss the changes in a central location here, since many of these are related.

I'd love to hear what everybody thinks about this proposal! My idea is that all (or most) of the changes below would be implemented as part of a big overhaul, though likely not in a single mypy release. I think that the current situation is sufficiently problematic that small incremental changes aren't enough.

1. Enable namespace packages by default

Mypy would support namespace packages by default. Currently --namespace-packages is needed, which is confusing for new users, in particular.

2. Generalize specifying namespace packages on the command line

Allow passing namespace packages using all the supported ways of specifying which files to type check on the command line. (See also #5759.)

3. Require package roots to be explicitly specified

To support namespace packages properly, the way mypy currently infers module name from a source file path by looking for __init__.py files can't be used, since namespace packages don't contain __init__.py files. Instead, all package roots would need to be explicitly defined using the mypy module search path. Some package roots will be enabled by default, such as the current directory and typeshed stub directories.

We may want to tweak how the module search path is specified, since many additional users might need to use it.

4. Use modular typeshed, and don't ship (most) third party package stubs with mypy

This is unrelated to namespace packages, but will be essential to allow typeshed to grow significantly. As third-party stubs would now be installed as packages, this would also make PEP-561 and non-PEP-561 packages behave closer to each other (for both you'll have to install some package first). Currently PEP 561 packages require a very different workflow, which is confusing.

This assumes that we'll actually switch to a modular typeshed. I'm hoping that we'll make progress in the next few months.

5. Allow selectively processing arbitrary installed packages

Make it possible to opt-in to type checking individual installed packages, even if they don't support PEP 561. This was proposed in #8545. This would be make it easier to get basic support for many third party packages that don't have stubs and don't support PEP 561.

This isn't enabled by default since some packages won't work properly or at all (C extensions, for example). One option would be disallow this option by default for popular packages with known issues.

6. Rethink import related error messages

Typical import related error messages should give explicit instructions about how to solve the issue. Ignoring missing imports should rarely be needed, and not recommended in the documentation. Examples of what might be suggested:

  • Install stub package <package> (this will be particularly important once we move to modular typeshed)
  • Install package <package> (in case it's known to support PEP 561)
  • Give the command line options that allow using an installed package (in case stubs don't exist, but the package is importable)
  • Update the module search path (if it doesn't look like a third-party library module)

7. Add flag to use the old module name inference behavior

Provide a backward compatibility option to use the old behavior of inferring module names, as migrating from the old behavior can be non-trivial for large projects. Still, the key of this proposal is that the new, backward incompatible behavior will be enabled by default, and should be used by most users.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions