Skip to content

Commit

Permalink
[cdd/compound/openapi/gen_routes.py] Ensure Module body is actually…
Browse files Browse the repository at this point in the history
… a list ; [cdd/sqlalchemy/parse.py] Correctly unwrap a `Module` when provided
  • Loading branch information
SamuelMarks committed Sep 18, 2023
1 parent e13def3 commit b185b7d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cdd/compound/openapi/gen_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def gen_routes(app, model_path, model_name, crud, route):
None,
)
sqlalchemy_ir = cdd.sqlalchemy.parse.sqlalchemy(
Module(body=sqlalchemy_node, stmt=None, type_ignores=[])
Module(body=[sqlalchemy_node], stmt=None, type_ignores=[])
)
primary_key = next(
map(
Expand Down
10 changes: 7 additions & 3 deletions cdd/sqlalchemy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

import ast
from ast import AnnAssign, Assign, Call, ClassDef
from ast import AnnAssign, Assign, Call, ClassDef, Module
from collections import OrderedDict
from inspect import getsource

Expand All @@ -19,7 +19,7 @@
from cdd.docstring.parse import docstring
from cdd.shared.ast_utils import get_value
from cdd.shared.defaults_utils import extract_default
from cdd.shared.pure_utils import assert_equal
from cdd.shared.pure_utils import assert_equal, rpartial
from cdd.sqlalchemy.utils.parse_utils import column_call_to_param


Expand Down Expand Up @@ -125,7 +125,11 @@ def sqlalchemy(class_def, parse_original_whitespace=False):
"""

if not isinstance(class_def, ClassDef):
class_def = ast.parse(getsource(class_def)).body[0]
class_def = (
next(filter(rpartial(isinstance, ClassDef), class_def.body))
if isinstance(class_def, Module)
else ast.parse(getsource(class_def)).body[0]
)
assert isinstance(class_def, ClassDef), "Expected `ClassDef` got `{!r}`".format(
type(class_def).__name__
)
Expand Down

0 comments on commit b185b7d

Please sign in to comment.