Skip to content

crash when same file appears with different module names #4881

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
msullivan opened this issue Apr 10, 2018 · 5 comments · Fixed by #8644
Closed

crash when same file appears with different module names #4881

msullivan opened this issue Apr 10, 2018 · 5 comments · Fixed by #8644
Labels
bug mypy got something wrong crash priority-0-high

Comments

@msullivan
Copy link
Collaborator

msullivan commented Apr 10, 2018

This bug found via a report from @eddieschoute

mypy crashes on the following cmdline test:

[case testDuplicateModules]
# cmd: mypy src
[file mypy.ini]
[[mypy]
mypy_path = src
[file src/__init__.py]
[file src/a.py]
import foo.bar
[file src/foo/__init__.py]
[file src/foo/bar.py]
1+'x'
[out]
-- The below would be what we produced if it didn't crash, but really we should
-- produce a useful error.
src/foo/bar.py:1: error: Unsupported operand types for + ("int" and "str")
src/foo/bar.py:1: error: Unsupported operand types for + ("int" and "str")

This crashes with

File "/home/msullivan/src/mypy/mypy/errors.py", line 264, in _add_error_info
assert file not in self.flushed_files

Here, the "canonical" module name for src/foo/bar.py is src.foo.bar. But if src is on the search path, both python and mypy will also happily let it have the name foo.bar.
In the test above, the inclusion on the command line results in bar.py being processed as src.foo.bar while the import for foo.bar in a.py, combined with src being on the mypy_path, also leads to it being processed as foo.bar. An error is generated in both, which trips an assert in Errors as we try to add a new error to a file that has already had its error messages flushed to the user.

One simple fix would be to track flushed_files by module id instead of file name, but it would probably be better to generate an error when a file appears multiple times with different module names, since it is unlikely to be intended?

@msullivan msullivan added bug mypy got something wrong crash labels Apr 10, 2018
@gvanrossum
Copy link
Member

Yeah, I think when we detect this we should give an error. As you said this is also an issue in Python itself and there are some ideas for detecting and rejecting it there as well. (Though sadly I cannot find the thread about it.)

@ilevkivskyi
Copy link
Member

Raising priority to high, since this is a crash, and it appeared several times.

@canassa
Copy link

canassa commented Jul 17, 2019

Hello,

I created a small project that replicates this error:

https://github.com/canassa/mypy-error

What I don't understand is that I saw on some other comments that "this errors only happens when doing something nonsensical" like adding a package to mypy_path. But the way that I have configured my project, I have no other option besides having the src directory on the mypy_path.

Is there any workaround for this? This issue is blocking me from using mypy

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 18, 2019

Even if we fix the bug, it's not clear that mypy would support this use case. It would probably produce an error instead of crashing.

Why have you configured your project like this? Why can't you remove the __init__.py file from your src directory?

@canassa
Copy link

canassa commented Jul 18, 2019

You are correct, that __ini__.py shouldn't be there, I guess that's the nonsensical part 😁

I wasn't aware that's what was causing the crash. Thanks! that solves my problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong crash priority-0-high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants