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

Semantic analyzer confuses a function with a module #7203

Open
Cito opened this issue Jul 13, 2019 · 7 comments
Open

Semantic analyzer confuses a function with a module #7203

Cito opened this issue Jul 13, 2019 · 7 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high semantic-analyzer Problems that happen during semantic analysis

Comments

@Cito
Copy link

Cito commented Jul 13, 2019

The following code creates a package structure that demonstrates what I think is a bug in mypy's new semantic analyzer.

from os import makedirs

makedirs('src/core')
makedirs('src/stuff')

open('src/__init__.py', 'w').write("""
# nothing here
""")

open('src/core/__init__.py', 'w').write("""
from .run import config
""")

open('src/core/run.py', 'w').write("""
from ..stuff import somefunction

config = None

somefunction()
""")

open('src/stuff/__init__.py', 'w').write("""
from .somemore import more
from .somefunction import somefunction
""")

open('src/stuff/somefunction.py', 'w').write("""
def somefunction():
    print("Hello, World!")
""")

open('src/stuff/somemore.py', 'w').write("""
def more():
    from ..core import config
""")

When checking the created src package with mypy 0.720 and Python 3.7, I get the following error:

src/core/run.py:6: error: Module not callable

What mypy thinks is a module is actually a function. It just has the same name as its containing module.

When I check with --no-new-semantic-analyzer, I do not get this error.

@Cito Cito changed the title Semantic Analyzer thinks function is a module Semantic analyzer wrongly complains that a function is a module Jul 13, 2019
@Cito Cito changed the title Semantic analyzer wrongly complains that a function is a module Semantic confuses a function with a module Jul 13, 2019
@Cito Cito changed the title Semantic confuses a function with a module Semantic analyzer confuses a function with a module Jul 13, 2019
@ilevkivskyi
Copy link
Member

I confirm this. Also I was able to find a much simpler repro. Here is a test case:

[case testSubModuleVsFunction]
# cmd: mypy -m test stuff stuff.somefunction

[file test.py]
from stuff import somefunction
somefunction()

[file stuff/__init__.py]
import test
from stuff.somefunction import somefunction

[file stuff/somefunction.py]
def somefunction(): ...

[builtins fixtures/module.pyi]

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code semantic-analyzer Problems that happen during semantic analysis priority-0-high labels Jul 15, 2019
@ilevkivskyi
Copy link
Member

cc @JukkaL

@JukkaL JukkaL self-assigned this Jul 15, 2019
@ilevkivskyi
Copy link
Member

Note there is a similar issue modulo function -> class, see #7172:

[case testCycleRenames]
# flags: --new-semantic-analyzer
import MyX

[file MyX.py]
from inner import MyBase

class MyX:
    x: MyBase = None

[file inner/__init__.py]
from inner.MyBase import MyBase as MyBase

[file inner/MyBase.py]
from MyX import MyX

class MyBase:
    pass

[file inner/MyDerived1.py]
from inner.MyBase import MyBase

class MyDerived1(MyBase):
    pass

@eguiraud
Copy link

Hi, is there any known workaround for this issue?

@JukkaL JukkaL removed their assignment Nov 19, 2019
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 19, 2019

A possible workaround is to move the definition of the function to __init__.py, instead of importing it from a submodule.

pkg/__init__.py:

def submodule() -> int:
     return 4

pkg/submodule.py:

# no definition of submodule() here

Alternatively, maybe you can define the function in both pkg/__init__.py and pkg/submodule.py. To avoid code duplication, both of them can just wrap a call to another function such as _submodule().

Does this help? The underlying issue may be hard to fix.

@eguiraud
Copy link

I ended up importing as

from package.Object import Object

instead of

from package import Object

Note that my specific case was classes, not functions, but I guess it's the same issue.

@KaleabTessera
Copy link

I am having a similiar , any progress on this ?

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 semantic-analyzer Problems that happen during semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants