Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sqlalchemy example to work with Python 3.8, 3.9 and 3.10 #2025

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ifeLawal
Copy link

@ifeLawal ifeLawal commented Jan 4, 2025

When trying to work with this example I received the error:

AttributeError: module 'datetime' has no attribute 'UTC'

Doing some research, it seems the new pattern is to pull UTC from timezone. Making the suggested edit as a PR

Fixes # .

Changes proposed in this pull request:

When trying to work with this example I received the error:
```bash
AttributeError: module 'datetime' has no attribute 'UTC'
```

Doing some research, it seems the new pattern is to pull UTC from timezone.
Making the suggested edit as a PR
@chrisinmtown
Copy link
Contributor

chrisinmtown commented Jan 4, 2025

Thanks for using my example! What version of Python are you using to run it? I'm using Python 3.13 and when I clone your repo, checkout your branch and run the example, I see this error:

INFO:root:Creating pet 1..
Traceback (most recent call last):
  File "/Users/me/git/github/connexion-ifelawal/examples/sqlalchemy/app.py", line 59, in <module>
    put_pet(id_, pet)
    ~~~~~~~^^^^^^^^^^
  File "/Users/me/git/github/connexion-ifelawal/examples/sqlalchemy/app.py", line 33, in put_pet
    pet["created"] = datetime.datetime.now(timezone.UTC)
                                           ^^^^^^^^^^^^
AttributeError: type object 'datetime.timezone' has no attribute 'UTC'. Did you mean: 'utc'?

In recent Python versions the datetime package has a UTC constant as an alias for datetime.timezone.utc according to https://docs.python.org/3/library/datetime.html. It was added in 2022 in this PR: python/cpython#91928

Still you have definitely caught something that blocks running the example in old(er) versions of Python. I recommend updating your PR to have the following code that works for me and also, I think, sorts the imports in the order that isort wants them to appear:

% git diff app.py
diff --git a/examples/sqlalchemy/app.py b/examples/sqlalchemy/app.py
index 7d04bc6..a61d383 100644
--- a/examples/sqlalchemy/app.py
+++ b/examples/sqlalchemy/app.py
@@ -1,9 +1,9 @@
-import datetime
 import logging
+from datetime import datetime, timezone
 
-import connexion
 import orm
-from connexion import NoContent
+
+from connexion import FlaskApp, NoContent
 
 
 def get_pets(limit, animal_type=None):
@@ -29,7 +29,7 @@ def put_pet(pet_id, pet):
             p.update(**pet)
         else:
             logging.info("Creating pet %s..", pet_id)
-            pet["created"] = datetime.datetime.now(datetime.UTC)
+            pet["created"] = datetime.now(timezone.utc)
             db_session.add(orm.Pet(**pet))
         db_session.commit()
         return NoContent, (200 if p is not None else 201)
@@ -56,7 +56,7 @@ pets = {
 }
 for id_, pet in pets.items():
     put_pet(id_, pet)
-app = connexion.FlaskApp(__name__, specification_dir="spec")
+app = FlaskApp(__name__, specification_dir="spec")
 app.add_api("openapi.yaml")
 app.add_api("swagger.yaml")

Updating the import orders and the datetime usage to follow a pattern that works with 3.9.7 and 3.13
@ifeLawal
Copy link
Author

ifeLawal commented Jan 5, 2025

Good catch! Appreciate the python links.

I was on Python 3.9.7 for my run of the example, an old(er) Python version that is not current. I updated the commit to match your diff for the PR.

@chrisinmtown
Copy link
Contributor

chrisinmtown commented Jan 5, 2025

I finally learned how to supply the appropriate arguments to isort to check examples. Unfortunately it is still not happy with your branch, a blank line is missing:

% isort --thirdparty connexion --check-only --diff app.py
ERROR: /Users/me/git/github/connexion-ifelawal/examples/sqlalchemy/app.py Imports are incorrectly sorted and/or formatted.
--- /Users/me/git/github/connexion-ifelawal/examples/sqlalchemy/app.py:before	2025-01-05 07:25:57.485152
+++ /Users/me/git/github/connexion-ifelawal/examples/sqlalchemy/app.py:after	2025-01-05 07:27:50.626244
@@ -3,6 +3,7 @@
 
 import orm
 from connexion import FlaskApp, NoContent
+
 
 def get_pets(limit, animal_type=None):
     with db_session_factory() as db_session:

I created a Python 3.9 venv to run your code, but the example still fails with the same error as before. Does this work for you?

% python3.9 app.py
INFO:root:Creating pet 1..
Traceback (most recent call last):
  File "/Users/me/git/github/connexion-ifelawal/examples/sqlalchemy/app.py", line 56, in <module>
    put_pet(id_, pet)
  File "/Users/me/git/github/connexion-ifelawal/examples/sqlalchemy/app.py", line 30, in put_pet
    pet["created"] = datetime.now(timezone.UTC)
AttributeError: type object 'datetime.timezone' has no attribute 'UTC'

Here's a diff that makes everything work:

% git diff app.py
diff --git a/examples/sqlalchemy/app.py b/examples/sqlalchemy/app.py
index 6e2e37c..f1d4042 100644
--- a/examples/sqlalchemy/app.py
+++ b/examples/sqlalchemy/app.py
@@ -4,6 +4,7 @@ from datetime import datetime, timezone
 import orm
 from connexion import FlaskApp, NoContent
 
+
 def get_pets(limit, animal_type=None):
     with db_session_factory() as db_session:
         q = db_session.query(orm.Pet)
@@ -27,7 +28,7 @@ def put_pet(pet_id, pet):
             p.update(**pet)
         else:
             logging.info("Creating pet %s..", pet_id)
-            pet["created"] = datetime.now(timezone.UTC)
+            pet["created"] = datetime.now(timezone.utc)
             db_session.add(orm.Pet(**pet))
         db_session.commit()
         return NoContent, (200 if p is not None else 201)

@ifeLawal
Copy link
Author

ifeLawal commented Jan 5, 2025

Appreciate the diffs! I did a test with the lowercase timezone.utc on my local and that worked. Updated the spacing and the utc casing

@chrisinmtown
Copy link
Contributor

Cool. You might want to update the first line of the commit message to be a bit more descriptive, I recommend something like this:

Update sqlalchemy example to work with Python 3.8, 3.9 and 3.10

@ifeLawal ifeLawal changed the title datetime.utc not valid Update sqlalchemy example to work with Python 3.8, 3.9 and 3.10 Jan 6, 2025
@chrisinmtown
Copy link
Contributor

+1 from me - but I'm not a maintainer so it doesn't really count lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants