Skip to content

Commit e963a19

Browse files
committed
(v2) Replace deprecated datetime.datetime.utcnow()
It was deprecated in Python 3.12[1]. [1] https://docs.python.org/3.12/library/datetime.html#datetime.datetime.utcnow
1 parent 7c087f4 commit e963a19

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

examples/openapi3/sqlalchemy/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def put_pet(pet_id, pet):
2929
p.update(**pet)
3030
else:
3131
logging.info("Creating pet %s..", pet_id)
32-
pet["created"] = datetime.datetime.utcnow()
32+
pet["created"] = datetime.datetime.now(tz=datetime.timezone.utc).replace(
33+
tzinfo=None
34+
)
3335
db_session.add(orm.Pet(**pet))
3436
db_session.commit()
3537
return NoContent, (200 if p is not None else 201)

examples/swagger2/sqlalchemy/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def put_pet(pet_id, pet):
2929
p.update(**pet)
3030
else:
3131
logging.info("Creating pet %s..", pet_id)
32-
pet["created"] = datetime.datetime.utcnow()
32+
pet["created"] = datetime.datetime.now(tz=datetime.timezone.utc).replace(
33+
tzinfo=None
34+
)
3335
db_session.add(orm.Pet(**pet))
3436
db_session.commit()
3537
return NoContent, (200 if p is not None else 201)

tests/test_flask_encoder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def test_json_encoder():
1818
s = json.dumps(datetime.date.today(), cls=FlaskJSONEncoder)
1919
assert len(s) == 12
2020

21-
s = json.dumps(datetime.datetime.utcnow(), cls=FlaskJSONEncoder)
21+
s = json.dumps(
22+
datetime.datetime.now(tz=datetime.timezone.utc).replace(tzinfo=None),
23+
cls=FlaskJSONEncoder,
24+
)
2225
assert s.endswith('Z"')
2326

2427
s = json.dumps(Decimal(1.01), cls=FlaskJSONEncoder)

0 commit comments

Comments
 (0)