mypy
mypy copied to clipboard
Ignore `str-format` error when object implements `__format__()` with custom type
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
- #17714
- #11723
- How does type-checking work for
datetime.datetime, which implements a rich set of custom formatters? - string - Format Mini Language