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

Ignore str-format error when object implements __format__() with custom type #17951

Open
pmhahn opened this issue Oct 15, 2024 · 0 comments
Open
Labels
bug mypy got something wrong

Comments

@pmhahn
Copy link

pmhahn commented Oct 15, 2024

Bug Report

I have inherited a project, where one class implements __format__(self, format_spec: str) -> str to implement a custom type for formatting. Running mypy on that code throws a false-positive error:

mypy-format.py:5 error: Unsupported format character "l" [str-format]

To Reproduce

class MyType:
    def __format__(self, format_spec: str) -> str:
        return "a" if format_spec == "l" else str(self)

print("{:l}".format(MyType()))

Expected Behavior

For a type implementing __format__ mypy/checkstrformat.py should not output any error.

Actual Behavior

mypy-format.py:5 error: Unsupported format character "l"  [str-format]

Your Environment

  • mypy 1.11.2 (compiled: yes)
  • Python 3.11.2

Links

@pmhahn pmhahn added the bug mypy got something wrong label Oct 15, 2024
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

1 participant