Skip to content

Fix crash with # type: ignore in incremental mode #1906

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

Conversation

Michael0x2a
Copy link
Collaborator

This commit fixes the bug described in #1904.

More precisely, the bug manifests when...

  1. You import a module normally
  2. From another file, you import the same module, but mark it with the # type: ignore comment
  3. You run mypy twice in this setup in incremental mode, making sure mypy encounters the #type: ignore comment after the regular import.

The bug appears to stem from how the semantic analyzer seemed to always mark import symbols as being of kind MODULE_REF and did not preserve whether the module was previously marked with the # type: ignore comment or not.

This caused a contradiction since how mypy decides to order dependencies differs whether you're starting with a cold cache or a warm cache.

When you start with a cold cache, mypy determines what each module's dependencies are based roughly on the order mypy encounters each file while parsing and whether the import is marked as ignorable or not.

However, when you start with a warm cache, mypy will use the previously computed list of dependencies, but will also attempt to typecheck each symbol marked as being of kind MODULE_REF. This presents a problem, since if the module was marked as being ignorable, it wouldn't be in the list of dependencies, but since it was marked as MODULE_REF, mypy assumes it is...

This pull request fixes this problem by modifying the semantic analyzer so that it determines whether it needs to mark import symbols of being kind MODULE_REF or UNBOUND_IMPORTED to keep the behavior consistent during both runs.

Previously, the semantic analyzer seemed to always mark import symbols as
being of kind MODULE_REF during the third pass and did not preserve
whether the module was previously marked with the `# type: ignore`
comment or not.

This caused a contradiction in some cases when mypy was run in incremental
mode: suppose file a.py imported file b.py, and marked that import with the
ignore annotation.

Since the import is being ignored, mypy would not register that import
as a dependency, but since the import (during the previous run) was
marked as a MODULE_REF, mypy would attempt to load that module anyways
in some cases.

This commit fixes python#1904.
@gvanrossum
Copy link
Member

I'm holding off reviewing this pending a fix for #1911.

Michael0x2a pushed a commit to Michael0x2a/mypy that referenced this pull request Jul 19, 2016
@Michael0x2a Michael0x2a deleted the incremental-and-type-ignore branch August 15, 2016 22:17
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

Successfully merging this pull request may close these issues.

3 participants