Skip to content

Normalize paths before checking  #13587

Closed
@mgilson

Description

@mgilson

Bug Report

I'm running into some issues in some scripting where we have a local repository of stubs that generally sits in the repository at some path relative to the python code in the project. Our linting scripts might look something like:

MYPYPATH=${PWD}/../stubs

This seems to be causing some issues in

mypy/mypy/modulefinder.py

Lines 816 to 842 in 3c7e216

# Start with a MYPYPATH environment variable at the front of the mypy_path, if defined.
mypypath = mypy_path()
# Add a config-defined mypy path.
mypypath.extend(options.mypy_path)
# If provided, insert the caller-supplied extra module path to the
# beginning (highest priority) of the search path.
if alt_lib_path:
mypypath.insert(0, alt_lib_path)
sys_path, site_packages = get_search_dirs(options.python_executable)
# We only use site packages for this check
for site in site_packages:
assert site not in lib_path
if (
site in mypypath
or any(p.startswith(site + os.path.sep) for p in mypypath)
or (os.path.altsep and any(p.startswith(site + os.path.altsep) for p in mypypath))
):
print(f"{site} is in the MYPYPATH. Please remove it.", file=sys.stderr)
print(
"See https://mypy.readthedocs.io/en/stable/running_mypy.html"
"#how-mypy-handles-imports for more info",
file=sys.stderr,
)
sys.exit(1)

Would it be problematic to normalize the paths before doing the p.startswith(...) check?

To Reproduce

  1. Find a python file that you want to check by itself.
  2. change your working directory to be the same directory as the file to check
  3. Run something like MYPYPATH=${PWD}/../any_sibling_directory mypy file_to_check.py (edit as necessary if you don't use bash :)

Expected Behavior

I would expect mypy to be able to check this file without any problems.

Actual Behavior

mypy basically just says it won't check the file and exits (with a non-successful exit status)

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: reproduced with/without --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: python3.8
  • Operating system and version: Linux (ubuntu 20.04)

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