python / python/mypy

fails to import module from source directory for sources without .py extension

Open
#20,962 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

To Reproduce

Given the files:

testdir/integration-tests/common.py

class CommonConfig:
    pass

testdir/integration-tests/test1

#!/usr/bin/env python3
import common
assert __name__ == '__main__'
config = common.CommonConfig()

If I run mypy from testdir/ on integration-tests/test1, it fails to find the import common from the same directory:

$ mypy integration-tests/test1
integration-tests/test1:3: error: Cannot find implementation or library stub for module named "common"  [import-not-found]
integration-tests/test1:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I expect mypy to be able to find the common module from the directory containing the source file, and thus to exit without errors:

$ mypy integration-tests/test1

IIUC, it should find the import, since the search path should include: "The directories containing the sources given on the command line"

Other notes

If I rename test1 to test1.py, it works as expected.

If I run mypy from testdir/integration-tests instead of from testdir (such that common.py is in the current working directory), it works as expected.

Environment

$ python3 --version
Python 3.10.12
$ mypy --version
mypy 1.19.1 (compiled: yes)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with testdir/integration-tests/common.py and the extensionless testdir/integration-tests/test1, then run mypy from testdir. Trace how mypy builds its search path for a source without a .py extension; done means the import common is found and mypy exits without errors, while the .py and alternate-working-directory cases remain working.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.