Skip to content

Suppress 'duplicate module named xx' errors? #4008

Closed

Description

@OJFord

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?

Activity

  1. gvanrossum commented on Sep 25, 2017

    @gvanrossum
    Member

    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.

  2. leinardi commented on Sep 1, 2018

    @leinardi

    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?

  3. ilevkivskyi commented on Sep 1, 2018

    @ilevkivskyi
    Member

    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

  4. leinardi commented on Sep 1, 2018

    @leinardi

    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

  5. gvanrossum commented on Sep 1, 2018

    @gvanrossum
    Member
    $ 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.

  6. leinardi commented on Sep 1, 2018

    @leinardi

    Oh ok, thanks for the clarification 👍.

  7. leinardi commented on Sep 1, 2018

    @leinardi

    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/?

  8. gvanrossum commented on Sep 1, 2018

    @gvanrossum
    Member

    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

  9. leinardi commented on Sep 2, 2018

    @leinardi

    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'
    
  10. gvanrossum commented on Sep 2, 2018

    @gvanrossum
    Member
  11. leinardi commented on Sep 2, 2018

    @leinardi

    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 .?

  12. petergaultney commented on Oct 31, 2018

    @petergaultney

    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.

  13. gvanrossum commented on Oct 31, 2018

    @gvanrossum
    Member

    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.

  14. OJFord commented on Oct 31, 2018

    @OJFord
    Author

    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.)

  15. 17 remaining items

  16. nhtgl commented on Jul 5, 2023

    @nhtgl

    To those that are still looking for some solution for monorepo:

      - repo: local
        hooks:
          - id: mypy
            name: mypy
            entry: bash -c 'SUBREPO=$(echo "$@" | sed "s/\/.*//"); ABS_PATH="$PWD/$@"; cd $SUBREPO; mkdir -p .mypy_cache; mypy "$ABS_PATH" --install-types --non-interactive'
            language: python
            language_version: python3
            types: [python]
    1. First of all, specify pyproject.toml/mypy.ini for each of your sub-repo. You're likely to have different configs for each project.
    2. For mypy specifically - make sure you're using mypy installed in your local environment. By default pre-commit runs mypy in isolated environment that doesn't have all the info about other packages (like pydantic, sqlalchemy, etc.) and therefore will make mistakes. This shouldn't be an issue if you work in monorepo environment.
    3. This solution assumes that each sub-repo in monorepo has a sub-root directory under root (/project1, /project2, etc). SUBREPO is exactly this - the name of your project.
    4. ABS_PATH - absolute path to the file that will be checked, to avoid duplicate module named.
    5. We cd to the sub-repo directory and run mypy here on absolute path. Here is where your mypy config should be, if you share configuration among other sub-repos use --config-file ../.mypy.ini or similar. Use other args and flags as you wish.

    Now mypy should have correct idea of what is going on. I did the same trick with bandit. It works correctly even if you want to commit edits on few projects in your monorepo. This solution my not be "clean", but works. It's the best I could come up with and pre-commit maintainer have said his word about supporting monorepos, so here we are.

    I hope that some of you will find it useful.

  17. Soneji commented on Sep 5, 2023

    @Soneji

    My solution was to add this option in the mypy configuration:

    [tool.mypy]
    packages = ["package1", "package2"]
  18. jaklan commented on Mar 22, 2025

    @jaklan

    8 years since the issue was created, mypy still doesn't support PEP 420-based implicit namespace packages properly. We are forced to either:

    • put __init__.py file in the top-level package - which is against PEP 420
    • modify mypy_path - which is unmaintainable (unless you create mypy_path generator...) in a setup with dynamic list of packages (e.g. monorepo)

    It could be easily improved if mypy_path supports glob, but for whatever reason - it still hasn't been implemented (although we have an issue for that for 4 years): #9965. It's still not the final solution for the PEP 420 support*, but it would help a lot of people dealing with this problem.

    *In terms of that - if mypy is able to identify packages automagically based on __init__.py discovery, I don't understand why we can't extend this approach. Instead of looking for __init__.py , let us specify what to look for when traversing through parent directories. In many cases it could be src/ directory which would mean:

    if you find src/ directory when traversing, treat every direct subdirectory of src/ as a top-level package

    and that should be able to handle both normal packages and namespace packages, as we don't really on __init__.py anyhow. Of course - it's still possible to use src/ directory not for packages, that's why it should be configurable. The thing is - usually some convention is established anyway (and the one with src/ for packages is pretty common nowadays), so we could use this fact to make our life with mypy much easier.

  19. jaklan commented on Mar 23, 2025

    @jaklan

    For the ones looking for a mypy config in pre-commit working with namespace packages in monorepos - the below is sufficient for our convention, you can always modify find command to fit your use-case:

            entry: >
              bash -c 'MYPYPATH="$(paste -sd ":" <(find . -type d -name "src"))"
              mypy "$@"' --
            args:
              [
                --config-file=pyproject.toml,
                --install-types,
                --non-interactive,
              ]
    [tool.mypy]
    explicit_package_bases = true
    ignore_missing_imports = true
    follow_imports = "silent"  # not happy about this one, but afaik mypy doesn't allow to exclude some packages from "follow_imports"
    check_untyped_defs = true
    scripts_are_modules = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions