Skip to content

Commit

Permalink
fix(python): Add attrs support to Python preset
Browse files Browse the repository at this point in the history
  • Loading branch information
Manancode committed Feb 14, 2025
1 parent 39e0dae commit 0b94c86
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/generators/python/PythonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
PYTHON_DEFAULT_ENUM_PRESET
} from './renderers/EnumRenderer';

import { PYTHON_DATACLASS_PRESET } from './presets/PythonDataClassPreset';
import { PYTHON_ATTRS_PRESET } from './presets/PythonAttrsPreset';

export type ClassPresetType<O> = ClassPreset<ClassRenderer, O>;
export type EnumPresetType<O> = EnumPreset<EnumRenderer, O>;

Expand All @@ -22,3 +25,9 @@ export const PYTHON_DEFAULT_PRESET: PythonPreset<PythonOptions> = {
class: PYTHON_DEFAULT_CLASS_PRESET,
enum: PYTHON_DEFAULT_ENUM_PRESET
};

export const PYTHON_PRESETS = {
default: PYTHON_DEFAULT_PRESET,
dataclass: PYTHON_DATACLASS_PRESET,
attrs: PYTHON_ATTRS_PRESET
};
9 changes: 9 additions & 0 deletions src/generators/python/presets/PythonAttrsPreset.ts
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()}`;
}
}
};
9 changes: 9 additions & 0 deletions src/generators/python/presets/PythonDataClassPreset.ts
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()}`;
}
}
};
2 changes: 2 additions & 0 deletions src/generators/python/presets/index.ts
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 test/generators/python/presets/PythonDataclassPreset.test.ts
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');
});
});

0 comments on commit 0b94c86

Please sign in to comment.