typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Incompatible typing for `logging.Formatter.converter` and `time.gmtime`/`time/localtime` ?

Open jrabasco opened this issue 2 months ago • 1 comments

The typing for converter here is as follows:

converter: Callable[[float | None], struct_time]

And the typing for time.gmtime/time.localtime here is:

def gmtime(seconds: float | None = None, /) -> struct_time: ...
def localtime(seconds: float | None = None, /) -> struct_time: ...

Yet when I try to typecheck this code:

import logging
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

I get the following error:

$ mypy main.py
main.py:5: error: Incompatible types in assignment (expression has type "Callable[[], struct_time]", base class "Formatter" defined the type as "Callable[[float | None], struct_time]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

I tested this on a fresh python3.13 venv with the following deps installed:

mypy==1.18.2
mypy_extensions==1.1.0
pathspec==0.12.1
typing_extensions==4.15.0

jrabasco avatar Oct 15 '25 12:10 jrabasco

I suspect that converter should be typed as ClassVar and that mypy basically thinks that the first argument of gmtime is self, since it's not. PR with tests welcome!

srittau avatar Oct 15 '25 13:10 srittau