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

Release/0.1.2 #3

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/python-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
python -m pip install --upgrade pip
python -m pip install flake8
pip install setuptools wheel
pip install -r requirements.txt
pip install -r requirements_test.txt
pip install .
- name: Lint with flake8
run: |
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "crimson-auto-pydantic"
version = "0.1.0"
version = "0.1.2"
description = "Template Tools"
readme = "README.md"
authors = [
Expand All @@ -23,7 +23,8 @@ classifiers = [
dependencies = [
"pydantic",
"crimson-code-extractor",
"crimson-ast-dev-tool"
"crimson-ast-dev-tool",
"inflection"
]
requires-python = ">=3.9"

Expand Down
7 changes: 2 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
inflection
jinja2
pydantic
crimson-code-extractor
crimson-ast-dev-tool
crimson-file-loader
pytest
pytest-cov
inflection
Empty file added requirements_dev.txt
Empty file.
2 changes: 2 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
pytest-cov
32 changes: 32 additions & 0 deletions src/crimson/auto_pydantic/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@


def generate_constructor(function_node: ast.FunctionDef, indent: int = 4) -> str:
"""
# generate_constructor

## Description
The `generate_constructor` function generates a constructor method for a Pydantic model based on the given function node. It creates an `__init__` method that initializes the model's attributes using the function's parameters.

## Parameters
- `function_node` (ast.FunctionDef): An Abstract Syntax Tree (AST) node representing the function for which the constructor is being generated.
- `indent` (int, optional): The number of spaces to use for indentation. Default is 4.

## Returns
- `str`: A string containing the generated constructor method.

## Functionality
1. Extracts the function specification from the given AST node.
2. Determines if the function is already an `__init__` method or a regular function.
3. Generates the method signature with appropriate parameters.
4. Creates a `super().__init__()` call to initialize the Pydantic model.
5. Includes all function parameters in the `super().__init__()` call, except for `self` and `cls`.

## Example Output
```python
def __init__(self, arg1: int, arg2: str = 'default'):
super().__init__(arg1=arg1, arg2=arg2)
```

## Notes
- The function handles both regular functions and existing `__init__` methods.
- It properly handles default values and type annotations from the original function.
- The generated constructor is compatible with Pydantic's model initialization.
"""

func_spec = extract.extract_func_spec(function_node)
indent = " " * indent
if func_spec.name == "__init__":
Expand Down
4 changes: 2 additions & 2 deletions src/crimson/auto_pydantic/validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable, Dict, Any
import ast
from crimson.ast_dev_tool import get_first_node
from crimson.ast_dev_tool import collect_nodes
from crimson.auto_pydantic.generator import generate_input_props, _generate_input_props_name
from inspect import getsource
import typing
Expand Down Expand Up @@ -31,7 +31,7 @@ def _prepare_namespace(currentframe, args, kwargs) -> Dict[str, Any]:

def _get_function_node(func: Callable) -> ast.FunctionDef:
func_source = getsource(func)
return get_first_node(func_source, ast.FunctionDef)
return collect_nodes(func_source, ast.FunctionDef)[0]


def _get_or_create_input_props(
Expand Down
Loading