Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions codegen/layouts/partials/model-dataclass.hbs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@dataclass
class {{className}}:
{{#each properties}}
{{name}}: {{type}}
{{safeName}}: {{type}}
{{/each}}

@staticmethod
def from_dict(d: Dict[str, Any]):
return {{className}}(
{{#each properties}}
{{name}}={{#if isDictParam}}DeepAttrDict({{/if}}d.get("{{name}}", None){{#if isDictParam}}){{/if}},
{{safeName}}={{#if isDictParam}}DeepAttrDict({{/if}}d.get("{{name}}", None){{#if isDictParam}}){{/if}},
{{/each}}
)
54 changes: 53 additions & 1 deletion codegen/lib/layouts/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,61 @@ import { flattenObjSchema } from '../openapi/flatten-obj-schema.js'
import type { ObjSchema, OpenapiSchema } from '../openapi/types.js'
import { getMethodLayoutContext } from './route.js'

// Python hard keywords cannot be used as identifiers. When a property name
// collides with one (e.g. "from"), the dataclass field and keyword argument
// are suffixed with an underscore while the original name is preserved as the
// dict key. No existing property name is a hard keyword, so this leaves all
// other generated output unchanged.
const PYTHON_KEYWORDS = new Set([
'False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield',
])

const toSafeIdentifier = (name: string): string =>
PYTHON_KEYWORDS.has(name) ? `${name}_` : name

export interface ModelsLayoutContext {
resources: Array<{
className: string
properties: Array<{ name: string; type: string; isDictParam: boolean }>
properties: Array<{
name: string
safeName: string
type: string
isDictParam: boolean
}>
}>
abstractClasses: Array<{
className: string
Expand Down Expand Up @@ -55,6 +106,7 @@ export const setModelsLayoutContext = (
const type = mapPythonType(propertySchema)
return {
name,
safeName: toSafeIdentifier(name),
type,
isDictParam: type.startsWith('Dict') || name === 'properties',
}
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"start": "fake-seam-connect --seed"
},
"devDependencies": {
"@seamapi/blueprint": "0.55.0",
"@seamapi/blueprint": "^0.56.0",
"@seamapi/fake-seam-connect": "1.86.0",
"@seamapi/smith": "^0.5.2",
"@seamapi/types": "1.910.0",
"@seamapi/types": "1.963.0",
"change-case": "^5.4.4",
"prettier": "^3.2.5"
}
Expand Down
3 changes: 3 additions & 0 deletions seam/routes/access_codes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion seam/routes/access_grants.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions seam/routes/devices.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion seam/routes/models.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading