Skip to content

Stubgen stuck in an endless loop in 0.750 #8065

@hoefling

Description

@hoefling
  • Are you reporting a bug, or opening a feature request? Bug

Only reproducible on Windows b/c of different path separator. On a windows machine, issue:

> mkdir foo bar
> touch foo\__init__.py foo\spam.py bar\__init__.py bar\eggs.py
> stubgen foo\spam.py bar\eggs.py

Result: stubgen never finishes, because caught in the endless while loop: out\foo/ doesn't start with out\bar/, then doesn't start with out/, then with / repeatedly.

mypy/mypy/stubutil.py

Lines 245 to 249 in 4e021d9

while True:
path = os.path.dirname(path)
if (cur + '/').startswith(path + '/'):
cur = path
break

Since mypy supports only Python 3.5 and onwards anyway - may I suggest using pathlib instead of string manipulation? With pathlib, the task of finding the common parent directory is a walk in the park and works on all OSes. For example, the common_dir_prefix method could look like this:

from pathlib import Path

def common_dir_prefix(paths: List[str]) -> str:
    if not paths:
        return '.'
    cur = Path(paths[0])
    for parent in cur.parents:
        # if the parent is parent of all paths, we're good to go
        if all(parent in Path(path).parents for path in paths[1:]):
            return str(parent)
    return '.'

In addition, if an extensive path manipulation is done in mypy (didn't check that), I would also plead switching to pathlib exclusively.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy