python / python/mypy

mypy recursively follows relative symlinks to the package root

Open
#15,863 0 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

Bug Report

I use a weird hack to make package importable when workdir is it's root:

python -c 'import my_package'
cd my_package
ln -s . my_package
python -c 'import my_package'

I will probably burn in hell for this hack, but it works surprisingly well, both in runtime and when using linters. However, mypy dives into this symlink over and over checking the same files until the recursion limit. The only downside is execution time, but on large projects it can grow significantly.

To Reproduce

# Create a package `mypy_test`
$ mkdir mypy_test
$ touch mypy_test/__init__.py

# Check it
$ python -c 'import mypy_test'
$ mypy mypy_test
Success: no issues found in 1 source file

# Create a relative symlink to the project root
$ cd mypy_test
$ ln -s . mypy_test
$ file mypy_test 
mypy_test: symbolic link to .

# Check the package again
$ python -c 'import mypy_test'
$ mypy .
Success: no issues found in 41 source files

Expected Behavior

mypy detects the loop and doesn't enter it.

Actual Behavior

mypy follows the symlink checking the same files until hitting recursion limit (I guess). Here's how to confirm it:

$ mypy -v . |& grep 'Metadata fresh for mypy_test.' | head -n 5 
LOG:  Metadata fresh for mypy_test: file ./__init__.py
LOG:  Metadata fresh for mypy_test.mypy_test: file ./mypy_test/__init__.py
LOG:  Metadata fresh for mypy_test.mypy_test.mypy_test: file ./mypy_test/mypy_test/__init__.py
LOG:  Metadata fresh for mypy_test.mypy_test.mypy_test.mypy_test: file ./mypy_test/mypy_test/mypy_test/__init__.py
LOG:  Metadata fresh for mypy_test.mypy_test.mypy_test.mypy_test.mypy_test: file ./mypy_test/mypy_test/mypy_test/mypy_test/__init__.py

The workaround is: mypy . --exclude mypy_test

Your Environment

  • Mypy version used: mypy 1.5.0 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.11.4 (main, Jun 7 2023, 17:17:32) [GCC 13.1.1 20230525] on linux

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 the shell commands in the report, especially the relative symlink and mypy -v . output. Trace the package-discovery path that produces repeated names such as mypy_test.mypy_test; done means mypy detects the loop and avoids checking the same files repeatedly while preserving the successful normal-package check.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.