Dumping a Tweet's JSON .data field sometimes reports "TypeError: Object of type datetime is not JSON serializable" #2166
Labels
Unconfirmed Bug
This is a bug report that has not been triaged yet
Summary
Non-JSON serializeable field in supposed JSON object.
Reproduction Steps
According to Tweepy documentation about the Tweet class's .data field, it says this field is "The JSON data representing the Tweet."
That means, this
.data
object should be serializable by Python's json module, encoding it as a JSON string.When the
edit_controls
field is passed to as atweet_field
to.get_users_tweets
like so:The object contains a
datetime.datetime
object, which results in a "TypeError: Object of type datetime is not JSON serializable" error message.Removing the "
edit_controls
" causes this field not to be added to theTweet
object, and the serialization a JSON string works.Note, a similar field in the object,
created_at
, works just fine, as it holds the date/timestamp as a string, not adatetime.datetime
object.Minimal Reproducible Example
Expected Results
I expected to have a JSON string of the Tweet's
.data
field produce this serialized object:NOTE: the
editable_until
fields should be a string, easily serialized by JSONActual Results
Program reported an error: TypeError: Object of type datetime is not JSON serializable
Here's what the object dump effectively looks like (notice the
datetime.datetime() entry
):NOTE: The
editable_util
field is adatetime.datetime
object, while thecreated_at
field is a string!Twitter API Access Plan
Basic
Tweepy Version
v4.14.0
Checklist
Additional Context
I think the issue is in
tweepy/tweet.py
in this section of code, howevercreated_at
is working andeditable_until
is not. It's not immediately obvious as to why, but at debugger confirms it.Something is coercing the assigned
datetime.datetime
object back to a string in one case and not in the other.In tracing through the code,
data.get("created_at")
has a value of "2023-09-03T16:16:34.000Z
", anddata.get("edit_controls")
has a value of "2023-09-03T17:16:34.000Z
".While
self.created_at =
ends up being a string, when assignedparse_datetime
's value ofdatetime.datetime(2023, 9, 3, 16, 16, 34, tzinfo=datetime.timezone.utc)
.The value of
self.edit_controls["editable_until"] =
(which appears to be an entry in a sub dictionary) ends up being adatetime.datetime
object, when assigned a value ofdatetime.datetime(2023, 9, 3, 17, 16, 34, tzinfo=datetime.timezone.utc)
.The text was updated successfully, but these errors were encountered: