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

Bad circular imports are not rejected #61

Open
ashleyh opened this issue Jan 27, 2013 · 12 comments
Open

Bad circular imports are not rejected #61

ashleyh opened this issue Jan 27, 2013 · 12 comments
Labels
feature priority-2-low topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@ashleyh
Copy link
Contributor

ashleyh commented Jan 27, 2013

If a.py contains

import b
name = 'a'
print(b.name)

and b.py contains

import a
name = 'b'
print(a.name)

then mypy compiles a.py without error, but the generated python throws an error because of the circular import.

(Found this one while refactoring mypy itself into packages)

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 27, 2013

When using the new Python-compatible syntax, the issue is that we do not detect invalid circular imports statically. This is a potential new feature -- updated labels accordingly.

@JukkaL JukkaL changed the title Circular imports cause invalid python to be generated Circular imports are not rejected Jul 25, 2014
@o11c
Copy link
Contributor

o11c commented Jul 14, 2015

Circular imports are valid somtimes, but mypy is buggy, I hit this in #721

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 26, 2015

Yeah, circular imports are pretty common in larger programs (even mypy has several). Some circular imports cause the program to always fail, and mypy could detect some of these. I'll edit the title to be less confusing.

@JukkaL JukkaL changed the title Circular imports are not rejected Bad circular imports are not rejected Jul 26, 2015
@o11c
Copy link
Contributor

o11c commented Jul 26, 2015

@JukkaL in my driver PR I remove all the circular imports I think. They are only needed for typechecking (i.e. in string-quoted types), not actually running mypy.

The workaround I use is if False: import mypy.foo. Any fix that mypy forms should take this into account.

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 26, 2015

@o11c I saw that and it's a pretty neat trick. Alternatively, we could define a constant such as MYPY that is always False and do something like this:

from bar import MYPY
if MYPY: import foo

def f(x: 'foo.ClassName') -> None: ...

The if statement trick also supports from ... import, unlike a real circular import:

from bar import MYPY
if MYPY: from foo import ClassName

def f(x: 'ClassName') -> None: ...

It may be worth it to describe this in mypy documentation.

Also, mypy could complain if a type imported as above is used without literal escapes.

@dosisod
Copy link
Contributor

dosisod commented May 4, 2020

Instead of a MYPY constant, we can use TYPE_CHECKING:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from foo import ClassName

def f(x: 'ClassName') -> None: ...

@mahmoudajawad

This comment has been minimized.

@JelleZijlstra

This comment has been minimized.

@hauntsaninja

This comment has been minimized.

@mahmoudajawad

This comment has been minimized.

@mahmoudajawad

This comment has been minimized.

@JelleZijlstra

This comment has been minimized.

@JelleZijlstra JelleZijlstra added the topic-runtime-semantics mypy doesn't model runtime semantics correctly label Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature priority-2-low topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

7 participants