-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(python): Add attrs support to Python preset
- Loading branch information
Showing
5 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { PythonPreset } from '../PythonPreset'; | ||
|
||
export const PYTHON_ATTRS_PRESET: PythonPreset = { | ||
class: { | ||
self({ renderer }) { | ||
return `import attr\n\[email protected](auto_attribs=True)\n${renderer.defaultSelf()}`; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { PythonPreset } from '../PythonPreset'; | ||
|
||
export const PYTHON_DATACLASS_PRESET: PythonPreset = { | ||
class: { | ||
self({ renderer }) { | ||
return `from dataclasses import dataclass\n\n@dataclass\n${renderer.defaultSelf()}`; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { PYTHON_PYDANTIC_PRESET } from './Pydantic'; | ||
export { PYTHON_JSON_SERIALIZER_PRESET } from './JsonSerializer'; | ||
export { PYTHON_DATACLASS_PRESET } from './PythonDataClassPreset'; | ||
export { PYTHON_ATTRS_PRESET } from './PythonAttrsPreset'; |
16 changes: 16 additions & 0 deletions
16
test/generators/python/presets/PythonDataclassPreset.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { PythonGenerator } from '../../../../src/generators/python/PythonGenerator'; | ||
import { PYTHON_DATACLASS_PRESET } from '../../../../src/generators/python/presets/PythonDataClassPreset'; | ||
|
||
describe('PythonDataclassPreset', () => { | ||
test('should generate a class with @dataclass decorator', async () => { | ||
const generator = new PythonGenerator({ presets: [PYTHON_DATACLASS_PRESET] }); | ||
const inputModel = { | ||
name: 'User', | ||
properties: { name: { type: 'string' }, age: { type: 'integer' } } | ||
}; | ||
|
||
const output = await generator.generate(inputModel); | ||
expect(output).toContain('@dataclass'); | ||
expect(output).toContain('class User'); | ||
}); | ||
}); |