mypy icon indicating copy to clipboard operation
mypy copied to clipboard

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

Open pmhahn opened this issue 1 year ago • 0 comments

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 avatar Oct 15 '24 06:10 pmhahn