{"record":{"id":"ef89b16ff715e490","repo":"python/cpython","slug":"unsupported-type-for-timedelta-name-component","errorCode":null,"errorMessage":"unsupported type for timedelta {name} component: {type(value).__name__}","messagePattern":"unsupported type for timedelta (.+?) component: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":667,"sourceCode":"        # Doing this efficiently and accurately in C is going to be difficult\n        # and error-prone, due to ubiquitous overflow possibilities, and that\n        # C double doesn't have enough bits of precision to represent\n        # microseconds over 10K years faithfully.  The code here tries to make\n        # explicit where go-fast assumptions can be relied on, in order to\n        # guide the C implementation; it's way more convoluted than speed-\n        # ignoring auto-overflow-to-long idiomatic Python could be.\n\n        for name, value in (\n            (\"days\", days),\n            (\"seconds\", seconds),\n            (\"microseconds\", microseconds),\n            (\"milliseconds\", milliseconds),\n            (\"minutes\", minutes),\n            (\"hours\", hours),\n            (\"weeks\", weeks)\n        ):\n            if not isinstance(value, (int, float)):\n                raise TypeError(\n                    f\"unsupported type for timedelta {name} component: {type(value).__name__}\"\n                )\n\n        # Final values, all integer.\n        # s and us fit in 32-bit signed ints; d isn't bounded.\n        d = s = us = 0\n\n        # Normalize everything to days, seconds, microseconds.\n        days += weeks*7\n        seconds += minutes*60 + hours*3600\n        microseconds += milliseconds*1000\n\n        # Get rid of all fractions, and normalize s and us.\n        # Take a deep breath <wink>.\n        if isinstance(days, float):\n            dayfrac, days = _math.modf(days)\n            daysecondsfrac, daysecondswhole = _math.modf(dayfrac * (24.*3600.))\n            assert daysecondswhole == int(daysecondswhole)  # can't overflow","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L649-L685","documentation":"Raised by timedelta.__new__ when any of its keyword components (days, seconds, microseconds, milliseconds, minutes, hours, weeks) is not an int or float. The constructor loops over all seven names and type-checks each before normalizing; even unused components passed as wrong types (e.g. a string) fail.","triggerScenarios":"timedelta(days='7'); timedelta(minutes=None); timedelta(seconds=Decimal('30')) — Decimal is not int/float and fails; calling timedelta(**json_config) with string values from config.","commonSituations":"Values coming from JSON/YAML/env vars as strings; None defaults leaking from optional config (timedelta(minutes=cfg.get('timeout'))); Decimal monetary values passed unconverted.","solutions":["Convert numeric strings first: timedelta(minutes=int(cfg['timeout']))","Guard optional values: timedelta(minutes=cfg['timeout'] or 0)","Convert Decimal to float or int (mind precision) before passing"],"exampleFix":"// before\nwait = timedelta(seconds=config['retry_after'])  # '30' from JSON\n// after\nwait = timedelta(seconds=float(config['retry_after']))","handlingStrategy":"validation","validationCode":"parts = {k: float(v) for k, v in cfg.items()}\nwait = timedelta(**parts)","typeGuard":"def valid_td_component(v) -> bool:\n    return isinstance(v, (int, float)) and not isinstance(v, bool) or isinstance(v, bool)","tryCatchPattern":null,"preventionTips":["Convert config/env/JSON strings to int/float before timedelta()","Guard optional values: cfg.get('minutes') or 0","Convert Decimal explicitly (float(d) or int(d))"],"tags":["datetime","typeerror","timedelta","config"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}