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

[1.12 regression] os.path functions on a os.PathLike #17952

Open
donn opened this issue Oct 15, 2024 · 6 comments
Open

[1.12 regression] os.path functions on a os.PathLike #17952

donn opened this issue Oct 15, 2024 · 6 comments
Labels
bug mypy got something wrong

Comments

@donn
Copy link

donn commented Oct 15, 2024

Bug Report

In 1.11.0, classes that inherit from os.PathLike could be used with os.path methods. As of 3.12, these methods claim to accept "any object implementing the os.PathLike protocol". However, in 1.12.0, mypy emits an error.

To Reproduce

import os


class MyPath(os.PathLike):
    def __init__(self, path: str):
        super().__init__()
        self.path = path

    def __fspath__(self):
        return self.path


print(os.path.abspath(MyPath(".")))

Expected Behavior

Unless I'm missing something, MyPath should be valid to pass, no?

Actual Behavior

pathlike.py:13: error: Value of type variable "AnyStr" of "abspath" cannot be "MyPath | Any"

Your Environment

  • Mypy version used: 1.12.0
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12
@donn donn added the bug mypy got something wrong label Oct 15, 2024
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Oct 15, 2024

Bisects to #17458 , probably caused by python/typeshed#12208
Are you interested in opening a PR to typeshed reverting that change?

Note explicitly inheriting from os.PathLike[str] will fix.

@hauntsaninja hauntsaninja changed the title (Apparent) regression with os.PathLike in 1.12.0 [1.12 regression] os.path functions on a os.PathLike Oct 15, 2024
@Avasam
Copy link
Contributor

Avasam commented Oct 17, 2024

Looks like the same change may have broken Python 3.8 support of passing a StrPath to these methods. I know 3.8 just went EOL, but I thought I'd let others know in case they also get the Value of type variable "AnyStr" of "abspath" cannot be "Union[str, PathLike[Any], Any]" [type-var] error on Python 3.8.
Edit: See my other comment below

Setuptools failure: pypa/setuptools#4688 & https://github.com/pypa/setuptools/actions/runs/11389961011/job/31690481419?pr=4688#step:9:68

@hauntsaninja
Copy link
Collaborator

Are you interested in picking up reverting that typeshed PR?

@Avasam
Copy link
Contributor

Avasam commented Oct 17, 2024

Nevermind, after failing to replicate in a typeshed test, and further investigation, mine was an issue with a setuptools declaration that was revealed by mypy 1.12
https://github.com/pypa/setuptools/blob/89b44013c79d2295ac0941e91b6904c0ce5522ba/setuptools/_path.py#L14-L17

if sys.version_info >= (3, 9):
    StrPath: TypeAlias = Union[str, os.PathLike[str]]  #  Same as _typeshed.StrPath
else:
    StrPath: TypeAlias = Union[str, os.PathLike]

Changing to:

if TYPE_CHECKING:
    StrPath: TypeAlias = Union[str, os.PathLike[str]]  #  Same as _typeshed.StrPath
else:
    # Python 3.8 support
    StrPath: TypeAlias = Union[str, os.PathLike]

Which explains why I was only seeing this on 3.8.
As far as I'm concerned, I don't see my case as a regression, but rather a mypy finding a missing generic param it didn't see before.

@Avasam
Copy link
Contributor

Avasam commented Oct 17, 2024

For OP's issue and another related regression I found in setuptools: python/typeshed#12837

@hauntsaninja
Copy link
Collaborator

For the purposes of making further changes to mypy, here's a repro that's independent of abspath in typeshed:

# mypy: disable-error-code=empty-body
import os
from typing import AnyStr

class MyPath(os.PathLike):
    def __init__(self, path: str):
        self.path = path

    def __fspath__(self):
        return self.pat

def abspath(path: os.PathLike[AnyStr] | AnyStr) -> AnyStr: ...

reveal_type(abspath(MyPath(".")))

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

No branches or pull requests

3 participants