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
60 changes: 60 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: publish to PyPI

on:
push:
tags:
- "v*.*.*"

permissions:
id-token: write
contents: read

jobs:
lint:
name: lint
runs-on: ubuntu-latest
strategy:
matrix:
just-trigger: [ format, lint, mypy ]

steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- uses: astral-sh/setup-uv@v4

- run: uv python install 3.12
- run: uv sync --all-extras --dev
- run: just ${{ matrix.just-trigger }}

tests:
name: tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- uses: astral-sh/setup-uv@v4

- run: uv python install 3.12
- run: uv sync --all-extras --dev

- name: Run tests
run: just tests

publish:
name: publish to PyPI
runs-on: ubuntu-latest
needs: [ lint, tests ]

steps:
- uses: actions/checkout@v4

- uses: astral-sh/setup-uv@v4
- run: uv python install 3.12
- run: uv sync --all-extras --dev

- name: Build package
run: uv build

- name: Publish to PyPI
run: uv publish
46 changes: 46 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: tests

on:
pull_request:
branches: [ main ]
push:
branches: [ main ]
tags:
- "v*.*.*"

jobs:
linters:
runs-on: ubuntu-latest
strategy:
matrix:
just-trigger: [ format, lint, mypy ]
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- uses: astral-sh/setup-uv@v4
- run: uv python install 3.12
- run: uv sync --all-extras --dev
- run: just ${{ matrix.just-trigger }}

tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.10", "3.11", "3.12", "3.13" ]
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- uses: astral-sh/setup-uv@v4

- run: uv python install ${{ matrix.python-version }}
- run: uv sync --all-extras --dev

- name: Run tests
run: just tests

- name: Upload coverage to Coveralls
if: matrix.python-version == '3.12'
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: tests.lcov
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down Expand Up @@ -173,7 +173,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

# Abstra
# Abstra is an AI-powered process automation framework.
Expand Down
24 changes: 24 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
SOURCE_PATH := "def_form"
TESTS_PATH := "tests"

upgrade:
uv lock --upgrade

format:
uv run ruff format {{ SOURCE_PATH }}

lint:
uv run ruff check {{ SOURCE_PATH }}

mypy:
uv run python -m mypy --pretty {{ SOURCE_PATH }}

fix:
uv run ruff check --fix --unsafe-fixes {{ SOURCE_PATH }}

tests:
uv run pytest \
--cov=def_form \
--cov-report=lcov:tests.lcov \
tests/

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 TopNik073

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
85 changes: 84 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
# def-form
# def-form

Python function definition formatter

[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![PyPI](https://img.shields.io/pypi/v/def-form.svg)](https://pypi.python.org/pypi/def-form)
[![PyPI](https://img.shields.io/pypi/dm/def-form.svg)](https://pypi.python.org/pypi/def-form)
[![Coverage Status](https://coveralls.io/repos/github/TopNik073/def-form/badge.svg?branch=init)](https://coveralls.io/github/TopNik073/def-form?branch=init)
## Overview

`def-form` is a code formatting tool that focuses specifically on Python function definitions. It helps maintain consistent formatting of function signatures by automatically organizing arguments vertically when they exceed specified thresholds.

## Features

- **Automatic argument formatting**: Converts inline function arguments to vertical format based on configurable rules
- **CI/CD integration**: Provides both `format` and `check` commands for use in development pipelines
- **Configuration file support**: Uses `pyproject.toml` for project-specific settings
- **Customizable thresholds**: Control when arguments should be formatted vertically

## Installation

```bash
pip install def-form
```

## Usage

### Format code

* Format all Python files in a directory:

```bash
def-form format src/
```

* Format a specific file:

```bash
def-form format my_module.py
```

### Check code without formatting

* Check if code follows formatting rules:

```bash
def-form check src/
```

### Command line options

```text
def-form format [OPTIONS] [PATH]

Options:
--max-def-length INTEGER Maximum length of function definition
--max-inline-args INTEGER Maximum number of inline arguments
--indent-size INTEGER indent size in spaces (default: 4)
--exclude TEXT Paths or files to exclude from checking/formatting
--show-skipped Show skipped files/directories
--config TEXT Path to pyproject.toml configuration file
```

### Configuration

Create a pyproject.toml file in your project root:

```toml
[tool.def-form]
max_def_length = 100
max_inline_args = 2
indent_size = 4
exclude = [
'.venv',
'migrations'
]
```

### Configuration options

* max_def_length: Maximum allowed characters in a single-line function definition
* max_inline_args: Maximum number of arguments allowed in inline format
* indent_size: Indent for arguments in spaces
* exclude: Files or directories you want to exclude
Empty file added def_form/__init__.py
Empty file.
Empty file added def_form/cli/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions def_form/cli/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import sys

import click

from def_form.exceptions.base import BaseDefFormException
from def_form.formatters import DefManager


@click.command()
@click.argument('path', type=str, default='src')
@click.option('--max-def-length', type=int, default=None, help='max length of your function definition')
@click.option('--max-inline-args', type=int, default=None, help='max number of inline arguments')
@click.option('--indent-size', type=int, default=None, help='indent size in spaces (default: 4)')
@click.option('--config', type=str, default=None, help='path to pyproject.toml')
@click.option('--exclude', multiple=True, help='paths to exclude from formatting')
@click.option('--show-skipped', is_flag=True, help='show skipped files/directories')
def format( # noqa: PLR0913
path: str,
max_def_length: int | None,
max_inline_args: int | None,
indent_size: int | None,
config: str | None,
exclude: tuple[str, ...],
show_skipped: bool,
) -> None:
click.echo('Start formatting your code')
try:
DefManager(
path=path,
excluded=exclude,
max_def_length=max_def_length,
max_inline_args=max_inline_args,
indent_size=indent_size,
config=config,
show_skipped=show_skipped,
).format()
except Exception as e:
click.echo(f'Something went wrong: {e}', err=True)
sys.exit(1)
else:
click.echo('Formatted!')


@click.command()
@click.argument('path', type=str, default='src')
@click.option('--max-def-length', type=int, default=None, help='max length of your function definition')
@click.option('--max-inline-args', type=int, default=None, help='max number of inline arguments')
@click.option('--indent-size', type=int, default=None, help='indent size in spaces (default: 4)')
@click.option('--config', type=str, default=None, help='path to pyproject.toml')
@click.option('--exclude', multiple=True, help='paths to exclude from checking')
@click.option('--show-skipped', is_flag=True, help='show skipped files/directories')
def check( # noqa: PLR0913
path: str,
max_def_length: int | None,
max_inline_args: int | None,
indent_size: int | None,
config: str | None,
exclude: tuple[str, ...],
show_skipped: bool,
) -> None:
click.echo('Start checking your code')
try:
DefManager(
path=path,
excluded=exclude,
max_def_length=max_def_length,
max_inline_args=max_inline_args,
indent_size=indent_size,
config=config,
show_skipped=show_skipped,
).check()
except BaseDefFormException:
sys.exit(1)
except Exception as e:
click.echo(f'Something went wrong: {e}', err=True)
sys.exit(1)
else:
click.echo('All checks passed!')
16 changes: 16 additions & 0 deletions def_form/cli/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import click

from def_form.cli.cli import check
from def_form.cli.cli import format


@click.group(name='def-form')
def main() -> None:
click.help_option()


main.add_command(format)
main.add_command(check)

if __name__ == '__main__':
main()
Empty file added def_form/exceptions/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions def_form/exceptions/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class BaseDefFormException(Exception):
path: str | None = None
message: str | None = None
description: str | None = None
31 changes: 31 additions & 0 deletions def_form/exceptions/def_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from dataclasses import dataclass

from def_form.exceptions.base import BaseDefFormException


@dataclass
class BaseDefFormatterException(BaseDefFormException):
path: str
message: str
description: str | None = None


@dataclass
class DefStringTooLongException(BaseDefFormException):
path: str
message: str = 'String is too long'
description: str | None = None


@dataclass
class TooManyInlineArgumentsException(BaseDefFormException):
path: str
message: str = 'Too many inline arguments'
description: str | None = None


@dataclass
class InvalidMultilineParamsIndentException(BaseDefFormException):
path: str
message: str = 'Invalid multiline params indentation'
description: str | None = None
Loading