python / python/cpython

Timedelta cannot be used in match statements

Open
#123,882 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Timedelta cannot be evaluated in match statements, see example below

from datetime import timedelta

td = timedelta(hours=1)

print(td == timedelta(hours=1)) # evaluates to True

match td:
    case timedelta(days=1):
        print('day')
    case timedelta(hours=1):
        print('hour')
    case _:
        print('unhandled') # prints `unhandled`
CPython versions tested on:

3.11

Operating systems tested on:

macOS

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 by reproducing the provided timedelta match statement on CPython 3.11 and compare its behavior with equality. The issue names no source file or test entry point, so trace how class patterns handle timedelta attributes and determine the expected matching behavior before identifying an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.