python / python/mypy

Files without extensions are able to be imported as Python files

Open
#14,400 0 comments 1 reaction 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

Repro

In file.py:

import other

In other (no file extension):

#!/bin/sh

echo syntax error

Running:

$ mypy file.py
other:3: error: invalid syntax  [syntax]
Found 1 error in 1 file (errors prevented further checking)

When running in CPython, you get this:

$ python file.py
Traceback (most recent call last):
  File "file.py", line 1, in <module>
    import other
ModuleNotFoundError: No module named 'other'

So I would expect Mypy to return either a "module not found" error or a "this is not a python file" error instead actually parsing other as a Python file. From reading the docs it would seem that the other module should only be importable from other.py[i] or other/__init__.py[i], but not a file called other.

I found this while running Mypy on beartype which has shell files called pytest and sphinx in the root directory, which causes Mypy to import the shell scripts if pytest or sphinx is not installed.

Environment
$ mypy --version
mypy 0.991 (compiled: yes)

$ python --version
Python 3.10.9

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 behavior with file.py importing the extensionless other file, using the mypy command shown in the issue, and compare it with CPython. Start with the documented import-discovery rules in the running_mypy import section; done means extensionless files are not parsed as Python modules and the resulting diagnostic matches the intended behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
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.