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

Duplicate module error with stub files #17362

Closed
gmgunter opened this issue Jun 11, 2024 · 3 comments
Closed

Duplicate module error with stub files #17362

gmgunter opened this issue Jun 11, 2024 · 3 comments
Labels
bug mypy got something wrong

Comments

@gmgunter
Copy link

Bug Report

When a project contains an untyped source (.py) file and a corresponding stub (.pyi) file, Mypy may emit "Duplicate module" errors depending on how the files were passed via the command line.

Consider a simple package foo with this project layout:

foo
├── __init__.py
└── __init__.pyi

Mypy runs without error if I pass it the foo directory as argument

$ mypy foo
Success: no issues found in 1 source file

But if I pass the directory contents as separate arguments, Mypy emits an error due to the two files having the same module name

$ mypy foo/*
foo/__init__.pyi: error: Duplicate module named "foo" (also at "foo/__init__.py")
foo/__init__.pyi: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules for more info
foo/__init__.pyi: note: Common resolutions include: a) using `--exclude` to avoid checking one of them, b) adding `__init__.py` somewhere, c) using `--explicit-package-bases` or adjusting MYPYPATH
Found 1 error in 1 file (errors prevented further checking)

Is this the intended behavior?

To Reproduce

(see above)

Expected Behavior

I expected that the .pyi file would supersede the .py file in both cases above (regardless of how the files were passed from the command line), or perhaps that Mypy would process the files independently in the second case and ignore naming collisions.

In my actual use case, I am running Mypy via a pre-commit hook, which always passes individual files as separate arguments. The addition of a stub file caused Mypy to raise the "Duplicate module" error. I was able to work around the error by configuring pre-commit to exclude the original .py file, but this seems like a pretty typical use case that Mypy might want to support.

isce-framework/snaphu-py#68

Actual Behavior

(see above)

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.3
@gmgunter gmgunter added the bug mypy got something wrong label Jun 11, 2024
@MarcBresson
Copy link

MarcBresson commented Oct 14, 2024

Hello, I'm having the same issue here with my pre-commit hook. @gmgunter how did you manage to exclude your file ? It seems like my pre-commit hook is not picking up the args i'm giving.

Using command line works fine though.

mypy v1.11.2
Python 3.10.11
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none

@hauntsaninja
Copy link
Collaborator

If you're using pre-commit with mypy, see the following advice: #13916

@hauntsaninja
Copy link
Collaborator

Use pass_filenames: false in your pre-commit config, as per #13916

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants