Skip to content

Issues with solving value constrained type var in presence of Any #17952

@donn

Description

@donn

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

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