Mypy: Suppress 'duplicate module named xx' errors?

Created on 25 Sep 2017  路  18Comments  路  Source: python/mypy

Am I right in thinking it's not presently possible to ignore errors for having multiple (as the error should really say, rather than 'duplicate') modules with the same name?

I'm running mypy on files in a repo that contains multiple packages with some commonality among them; for example they each have a src/wsgi.py, which renders:

error: Duplicate module named 'src.wsgi'

  1. I think this should be reworded Multiple modules named xx
  2. Would you be open to an --allow-same-name-modules/allow_same_name_modules = True or similar?

Most helpful comment

I hit this again, but found a solution (for specifically the pre-commit case) that seems to be workable:

- id: mypy
   name: mypy-one
    files: ^one/
    entry: mypy one/
    pass_filenames: false
- id: mypy
   name: mypy-two
   files: ^two/
   entry: mypy two/
   pass_filenames: false
...

and of course whatever args or other options you were using on your single entry, but that's the barebones.

Bit of a pain to have to maintain this - especially because if you add a three/ it's not linted unless you remember to add it to the whitelist - but at least it seems to work.

(NB: Note that the id is the same on each (just not the name) and it's that that's the optional argument to pre-commit run, so you can still run them all with pre-commit run mypy.)

All 18 comments

Those probably aren't packages, they're folders (or there are __init__.py files that shouldn't be there). You should really run mypy separately over each package in that case, since otherwise it won't know what to do when one of these is imported from another place being checked.

For this reason, implementing such a flag would not be easy, and probably not very useful.

Hi @gvanrossum, I am writing a PyCharm/IntelliJ IDEA plugin to run mypy scanning and I am encountering the same issue.

PyCharm/IntelliJ IDEA code inspection API sends me a list of open files in the IDE but, if the file list contains both a __init__.py and a python file from the same package, I get the "Duplicate module named" error:

$ mypy --show-column-numbers  /home/leinardi/PycharmProjects/gsi/gsi/__init__.py /home/leinardi/PycharmProjects/gsi/gsi.py
gsi.py: error: Duplicate module named 'gsi'

Do you have any suggestion to scan both this files without running mypy separately for each of them?

I am writing a PyCharm/IntelliJ IDEA plugin to run mypy scanning

Would you like to contribute to existing one instead? https://github.com/dropbox/mypy-PyCharm-plugin

Hi @ilevkivskyi, normally that would be the best thing to do, but I have already done adapting the code of my Pylint plugin to work with mypy.

This plugin is based on Checkstyle-IDEA plugin and offers also real-time scanning, checkin validations and various on-demand scan actions.

This is my latest build, is still not production ready but should give you an idea of what feature are there: Mypy-0.7.0.zip

$ mypy --show-column-numbers  /home/leinardi/PycharmProjects/gsi/gsi/__init__.py /home/leinardi/PycharmProjects/gsi/gsi.py
gsi.py: error: Duplicate module named 'gsi'

This is because apparently you seem to have both a folder named gsi and a file name gsi.py. This is a problem for Python too (IIRC the folder/package wins) so you can't really blame mypy for complaining about this.

Oh ok, thanks for the clarification :+1:.

Hi @gvanrossum, I am now getting the same error when I try to check these files from mypy project:

$ mypy  /home/leinardi/PycharmProjects/mypy/test-data/packages/typedpkg-stubs/setup.py /home/leinardi/PycharmProjects/mypy/extensions/setup.py /home/leinardi/PycharmProjects/mypy/setup.py
extensions/setup.py: error: Duplicate module named 'setup'

or just simply these 2 files:

$ mypy /home/leinardi/PycharmProjects/mypy/extensions/setup.py /home/leinardi/PycharmProjects/mypy/setup.py
setup.py: error: Duplicate module named 'setup'

Is it also in this case a project problem? And why it does not happen when I scan the entire project using mypy /home/leinardi/PycharmProjects/mypy/?

Presumably the folders containing the setup.py files don't have __init__.py files, so mypy treats them both as top-level modules. You can fix this using --scripts-are-modules: https://mypy.readthedocs.io/en/latest/command_line.html#miscellaneous

Hey @gvanrossum, thank you for your answer. You are correct, the folders don't have __init__.py files.

But unfortunately the option --scripts-are-modules is not fixing the issue:

$ mypy --scripts-are-modules extensions/setup.py setup.py
setup.py: error: Duplicate module named 'setup'

I suggest asking for help in a more interactive forum, e.g.
https://gitter.im/python/typing

Done, but still no answer:
image

But isn't this a mypy bug? why I cannot scan these 2 files together but it is fine if I scan the entire directory with mypy .?

I have this problem frequently where I'm scanning separate packages with pre-commit hooks. It would be very useful to be able to disable this check completely, or else have an obvious workaround.

Scripts-are-modules does not help.

You'll have to write a script that scans the packages and produces a full list of files to pass to mypy except for the files you don't want.

That's exactly when this problem occurs - when that full list of files contains 'duplicate modules named x'.

(An example of the script that does that is pre-commit--all-files.)

Obviously that's a bug in your script, not in mypy. :-)

I hit this again, but found a solution (for specifically the pre-commit case) that seems to be workable:

- id: mypy
   name: mypy-one
    files: ^one/
    entry: mypy one/
    pass_filenames: false
- id: mypy
   name: mypy-two
   files: ^two/
   entry: mypy two/
   pass_filenames: false
...

and of course whatever args or other options you were using on your single entry, but that's the barebones.

Bit of a pain to have to maintain this - especially because if you add a three/ it's not linted unless you remember to add it to the whitelist - but at least it seems to work.

(NB: Note that the id is the same on each (just not the name) and it's that that's the optional argument to pre-commit run, so you can still run them all with pre-commit run mypy.)

The "duplicate module" error is in the misc category. You can see the error code by adding show_error_codes = True to _mypy.ini_. If you're not concerned about omitting the other miscellaneous checks, try adding disable_error_code = misc to _mypy.ini_.

[mypy]
disable_error_code = misc
show_error_codes = True
files = **/*.py

For the pre-commit case mentioned above (https://github.com/python/mypy/issues/4008#issuecomment-582458665, also see https://github.com/pre-commit/mirrors-mypy/issues/5), try using pre-commit/mirrors-mypy with language: system and pass_filenames: false.

language: system tells pre-commit to "rely on whatever state the user's machine is in" (https://github.com/pre-commit/mirrors-mypy/issues/3#issuecomment-547999959), which may include mypy already installed in the environment and a _mypy.ini_ configuration file. This may be an acceptable tradeoff if running both pre-commit and mypy from the same environment, such as a virtual environment or CI environment. When using language: system for pre-commit in CI such as GitHub Actions, it is necessary to configure the Python environment before running pre-commit (set up Python and install dependencies).

pass_filenames: false tells pre-commit not to override the list of files to check. This can be helpful simply to avoid maintaining duplicate lists of files. _mypy.ini_ uses glob pattern matching to specify files to check, but _.pre-commit-config.yaml_ uses Python regex for its files key.

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.790
    hooks:
      - id: mypy
        language: system
        pass_filenames: false

You should not use @br3ndonland's disable_error_code = misc hack... more details at https://github.com/python/mypy/issues/9727

Was this page helpful?
0 / 5 - 0 ratings