Skip to content

Conversation

@davidfstr
Copy link

@davidfstr davidfstr commented Jan 8, 2026

Type of Changes

Type
🐛 Bug fix

Description

Currently on Windows if PATH contains a UNC path like r"\\Mac\Code\" with a trailing slash, modutils.modpath_from_file won't be able to locate .py files like r"\\Mac\Code\tests\test_resources.py" inside such a path. Consequently, astroid will refuse to load the source code for .py files in such locations.

This change fixes the underlying modutils._is_subpath function used by modutils.modpath_from_file to handle paths with trailing os.sep correctly.


I would have preferred to update the ModPathFromFileTest test suite rather than creating a new IsSubpathTest suite, but ModPathFromFileTest is currently not passing on Windows.

@codecov
Copy link

codecov bot commented Jan 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.41%. Comparing base (e9571d8) to head (2d471d3).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2928   +/-   ##
=======================================
  Coverage   93.41%   93.41%           
=======================================
  Files          92       92           
  Lines       11237    11237           
=======================================
  Hits        10497    10497           
  Misses        740      740           
Flag Coverage Δ
linux 93.26% <100.00%> (ø)
pypy 93.41% <100.00%> (ø)
windows 93.39% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
astroid/modutils.py 89.61% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@davidfstr
Copy link
Author

In case anyone else is impacted by this issue and needs a quick fix, here's what I'm doing:

def _patch_astroid() -> None:
    """
    Patches astroid 4.0.3 to work with PATH entries that end with os.path.sep.
    
    Backports the fix: https://github.com/pylint-dev/astroid/pull/2928
    """
    if sys.platform != 'win32':
        # Patch only useful on Windows
        return
    try:
        import astroid.modutils
    except ImportError:
        pass
    else:
        path = '\\\\Mac\\Code\\tests\\test_resources.py'
        base = '\\\\mac\\code\\'
        if not astroid.modutils._is_subpath(path, base):
            def _is_subpath2(path: str, base: str) -> bool:
                path = os.path.normcase(os.path.normpath(path))
                base = os.path.normcase(os.path.normpath(base))
                if not path.startswith(base):
                    return False
                return (
                    (len(path) == len(base)) or 
                    (path[len(base)] == os.path.sep) or
                    # Fix
                    (base.endswith(os.path.sep) and path[len(base) - 1] == os.path.sep)
                )
            astroid.modutils._is_subpath = _is_subpath2
        assert astroid.modutils._is_subpath(path, base)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants