Repository: marshmallow-code/marshmallow

Issue:
Address deprecation warnings in tests
A number of these warnings appear when running the tests in Python 3.12:

```
DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
```

This one's a good first issue to tackle!

Discussion:

Patch:
diff --git a/examples/flask_example.py b/examples/flask_example.py
index 0d8b9e8d..69a1520d 100644
--- a/examples/flask_example.py
+++ b/examples/flask_example.py
@@ -128,7 +128,9 @@ def new_quote():
         db.session.add(author)
     # Create new quote
     quote = Quote(
-        content=data["content"], author=author, posted_at=datetime.datetime.utcnow()
+        content=data["content"],
+        author=author,
+        posted_at=datetime.datetime.now(datetime.UTC),
     )
     db.session.add(quote)
     db.session.commit()
diff --git a/examples/peewee_example.py b/examples/peewee_example.py
index eda1647c..0e99e5db 100644
--- a/examples/peewee_example.py
+++ b/examples/peewee_example.py
@@ -93,7 +93,7 @@ class TodoSchema(Schema):
         return Todo(
             content=data["content"],
             is_done=data["is_done"],
-            posted_on=dt.datetime.utcnow(),
+            posted_on=dt.datetime.now(dt.timezone.utc),
         )
 
 


Test patch:
diff --git a/tests/test_deserialization.py b/tests/test_deserialization.py
index 069c2f0c..6c32fead 100644
--- a/tests/test_deserialization.py
+++ b/tests/test_deserialization.py
@@ -1288,7 +1288,7 @@ class TestFieldDeserialization:
         assert MethodDeserializeOnly().load({"name": "ALEC"})["name"] == "alec"
 
     def test_datetime_list_field_deserialization(self):
-        dtimes = dt.datetime.now(), dt.datetime.now(), dt.datetime.utcnow()
+        dtimes = dt.datetime.now(), dt.datetime.now(), dt.datetime.now(dt.timezone.utc)
         dstrings = [each.isoformat() for each in dtimes]
         field = fields.List(fields.DateTime())
         result = field.deserialize(dstrings)
diff --git a/tests/test_schema.py b/tests/test_schema.py
index 64d9144e..443aa300 100644
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -85,7 +85,7 @@ def test_load_validation_error_stores_input_data_and_valid_data():
 
     schema = MySchema()
     input_data = {
-        "always_valid": dt.datetime.utcnow().isoformat(),
+        "always_valid": dt.datetime.now(dt.timezone.utc).isoformat(),
         "always_invalid": 24,
     }
     try:
diff --git a/tests/test_serialization.py b/tests/test_serialization.py
index bf3ccf72..fcb5a5e5 100644
--- a/tests/test_serialization.py
+++ b/tests/test_serialization.py
@@ -827,7 +827,7 @@ class TestFieldSerialization:
             fields.TimeDelta(fields.TimeDelta.SECONDS, str)
 
     def test_datetime_list_field(self):
-        obj = DateTimeList([dt.datetime.utcnow(), dt.datetime.now()])
+        obj = DateTimeList([dt.datetime.now(dt.timezone.utc), dt.datetime.now()])
         field = fields.List(fields.DateTime)
         result = field.serialize("dtimes", obj)
         assert all(type(each) is str for each in result)
@@ -839,7 +839,7 @@ class TestFieldSerialization:
 
     def test_list_field_work_with_generator_single_value(self):
         def custom_generator():
-            yield dt.datetime.utcnow()
+            yield dt.datetime.now(dt.timezone.utc)
 
         obj = DateTimeList(custom_generator())
         field = fields.List(fields.DateTime)
@@ -848,7 +848,7 @@ class TestFieldSerialization:
 
     def test_list_field_work_with_generators_multiple_values(self):
         def custom_generator():
-            yield from [dt.datetime.utcnow(), dt.datetime.now()]
+            yield from [dt.datetime.now(dt.timezone.utc), dt.datetime.now()]
 
         obj = DateTimeList(custom_generator())
         field = fields.List(fields.DateTime)
@@ -910,7 +910,7 @@ class TestFieldSerialization:
             fields.List(ASchema)
 
     def test_datetime_integer_tuple_field(self):
-        obj = DateTimeIntegerTuple((dt.datetime.utcnow(), 42))
+        obj = DateTimeIntegerTuple((dt.datetime.now(dt.timezone.utc), 42))
         field = fields.Tuple([fields.DateTime, fields.Integer])
         result = field.serialize("dtime_int", obj)
         assert type(result[0]) is str