Skip to content

Inaccurate error message for datetime.datetime.fromtimestamp #111513

@ericli-splunk

Description

@ericli-splunk

Bug report

Bug description:

datetime.datetime.fromtimestamp recognizes floating point arguments, but when I mistakenly enter a string as an argument, I get the error message "TypeError: 'str' object cannot be interpreted as an integer". This error message implies that fromtimestamp only accepts integers, which is confusing.

Expected behavior: see error message "TypeError: 'str' object cannot be interpreted as a float" or similar.

The same problem happens for datetime.datetime.utcfromtimestamp.

>>> import datetime
>>> datetime.datetime.fromtimestamp('1.234')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>> datetime.datetime.fromtimestamp(1.234)
datetime.datetime(1969, 12, 31, 16, 0, 1, 234000)
>>> datetime.datetime.utcfromtimestamp('1.234')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>> datetime.datetime.utcfromtimestamp(1.234)
datetime.datetime(1970, 1, 1, 0, 0, 1, 234000)
>>> datetime.datetime.fromtimestamp(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object cannot be interpreted as an integer
>>> 

CPython versions tested on:

3.11

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions