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

Import cycle with type aliasing causes invalid "Invalid type" #5275

Closed
ethanhs opened this issue Jun 25, 2018 · 2 comments · Fixed by #5635
Closed

Import cycle with type aliasing causes invalid "Invalid type" #5275

ethanhs opened this issue Jun 25, 2018 · 2 comments · Fixed by #5635
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-import-cycles

Comments

@ethanhs
Copy link
Collaborator

ethanhs commented Jun 25, 2018

Running stub checks with typeshed at fb92ee8 on master yielded the following:

[ethanhs@towerofart mypy]$ pytest -k testsamples
============================= test session starts ==============================
platform linux -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /home/ethanhs/mypy, inifile: pytest.ini
plugins: xdist-1.22.2, forked-0.2, cov-2.5.1
gw0 [2] / gw1 [2] / gw2 [2] / gw3 [2] / gw4 [2] / gw5 [2] / gw6 [2] / gw7 [2]
scheduling tests via LoadScheduling
.F                                                                       [100%]
=================================== FAILURES ===================================
___________________________ SamplesSuite.test_stubs ____________________________
[gw2] linux -- Python 3.6.5 /usr/bin/python
Sample check failed
----------------------------- Captured stdout call -----------------------------
typeshed/third_party/2and3/click/core.pyi:337: error: Invalid type "click.types._ConvertibleType"
typeshed/third_party/2and3/click/core.pyi:416: error: Invalid type "click.types._ConvertibleType"
===================== 1 failed, 1 passed in 13.15 seconds ======================

Ivan pointed out there is an import cycle, and it seems the cycle and type aliasing is giving mypy trouble.

@ethanhs ethanhs added bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low labels Jun 25, 2018
@ethanhs
Copy link
Collaborator Author

ethanhs commented Jun 26, 2018

Okay I have a minimal repro:

# a.pyi
from b import Parameter

class _ParamType:
    p: Parameter

_ConvertibleType = _ParamType

def convert_type(ty: _ConvertibleType):
    ...

# b.pyi
from a import _ConvertibleType

class Parameter:
    type: _ConvertibleType  # error: Invalid type "a._ConvertibleType

running mypy a.pyi (0.610 and master).

@ilevkivskyi
Copy link
Member

Great! It is much easier to fix when there is a repro. I raise priority to high since the problems with import cycles can be quite annoying.

@ilevkivskyi ilevkivskyi self-assigned this Jun 26, 2018
ilevkivskyi added a commit that referenced this issue Sep 21, 2018
Fixes #5275 
Fixes #4498 
Fixes #4442 

This is a simple _band-aid_ fix for `Invalid type` in import cycles where type aliases, named tuples, or typed dicts appear. Note that this is a partial fix that only fixes the `Invalid type` error when a type alias etc. appears in type context. This doesn't fix errors (e.g. `Cannot determine type of X`) that may appear if the type alias etc. appear in runtime context.

The motivation for partial fix is two-fold:
* The error often appears in stub files (especially for large libraries/frameworks) where we have more import cycles, but no runtime context at all.
* Ideally we should refactor semantic analysis to have deferred nodes, and process them in smaller passes when there is more info (like we do in type checking phase). But this is _much_ harder since this requires a large refactoring.  Also an alternative fix of updating node of every `NameExpr` and `MemberExpr` in third pass is costly from performance point of view, and still nontrivial.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high topic-import-cycles
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants