Skip to content

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

@jrabasco

Description

@jrabasco

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions