python / python/typeshed

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

Open
#14,880 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stubs: false positive
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stdlib/logging/init.pyi and stdlib/time.pyi at the referenced definitions, then reproduce the assignment with mypy using the example in the issue. Check why the two callable annotations are treated as incompatible and update the relevant stub typing so the example type-checks without an assignment error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.