Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_mkpath_exception_uncached #316

Merged
merged 5 commits into from
Dec 26, 2024

Conversation

Avasam
Copy link
Contributor

@Avasam Avasam commented Nov 24, 2024

Attempt (because Ruff failures right now make this hard to confirm in isolation) at fixing the following test failure https://github.com/pypa/distutils/actions/runs/11999395994/job/33447255455?pr=314#step:5:187

__________________ TestDirUtil.test_mkpath_exception_uncached __________________

self = <distutils.tests.test_dir_util.TestDirUtil object at 0x7f66ff36a490>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f66ff180850>
tmp_path = PosixPath('/tmp/pytest-of-runner/pytest-0/test_mkpath_exception_uncached0')

    def test_mkpath_exception_uncached(self, monkeypatch, tmp_path):
        """
        Caching should not remember failed attempts.
    
        pypa/distutils#304
        """
    
        class FailPath(pathlib.Path):
            def mkdir(self, *args, **kwargs):
                raise OSError("Failed to create directory")
    
        target = tmp_path / 'foodir'
    
        with pytest.raises(errors.DistutilsFileError):
>           mkpath(FailPath(target))

distutils/tests/test_dir_util.py:130: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/pathlib.py:871: in __new__
    self = cls._from_parts(args)
/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/pathlib.py:509: in _from_parts
    drv, root, parts = self._parse_args(args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'distutils.tests.test_dir_util.TestDirUtil.test_mkpath_exception_uncached.<locals>.FailPath'>
args = (PosixPath('/tmp/pytest-of-runner/pytest-0/test_mkpath_exception_uncached0/foodir'),)

    @classmethod
    def _parse_args(cls, args):
        # This is useful when you don't want to create an instance, just
        # canonicalize some constructor arguments.
        parts = []
        for a in args:
            if isinstance(a, PurePath):
                parts += a._parts
            else:
                a = os.fspath(a)
                if isinstance(a, str):
                    # Force-cast str subclasses to str (issue #21127)
                    parts.append(str(a))
                else:
                    raise TypeError(
                        "argument should be a str object or an os.PathLike "
                        "object returning str, not %r"
                        % type(a))
>       return cls._flavour.parse_parts(parts)
E       AttributeError: type object 'FailPath' has no attribute '_flavour'

The issue is that when instanciated, a Path checks whether it's an exact instance of Path before deferring its self/cls to WindowsPath or PosixPath, which do implement the _flavor attribute. But since FailPath is a subclass of Path, the overriding of cls never happens.

@jaraco
Copy link
Member

jaraco commented Dec 26, 2024

I believe the test failure was introduced in #304. The test passes on my local machine. I guess it only fails on some Pythons or platforms.

@jaraco
Copy link
Member

jaraco commented Dec 26, 2024

Looks like it passes on Python 3.12 but fails on Python 3.11.

@jaraco
Copy link
Member

jaraco commented Dec 26, 2024

This patch doesn't help. The tests still fail with it. Oh. The class expects the British spelling of flavor.

@jaraco jaraco merged commit be28187 into pypa:main Dec 26, 2024
8 of 22 checks passed
@Avasam Avasam deleted the fix-test_mkpath_exception_uncached branch December 26, 2024 17:27
@Avasam
Copy link
Contributor Author

Avasam commented Dec 26, 2024

Oh. The class expects the British spelling of flavor.

I can't believe we all read past it... Even in my PR description you can read _flavour vs _flavor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants