Skip to content

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

@sporksmith

Description

@sporksmith

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions