A comprehensive package for enforcing coding standards in Frappe Framework projects. This package provides pre-commit hooks, custom scripts, and GitHub Actions workflows to maintain consistent, secure, and high-quality code across your Frappe applications.
- π§ Pre-commit hooks for automatic code quality checks
- π‘οΈ Security checks for SQL injection prevention and sensitive data encryption
- π Translation wrapper validation for internationalization
- π Coding standards enforcement (function length, indentation, naming conventions)
- π DocType naming convention checks
- βοΈ GitHub Actions workflows for CI/CD integration
- π» VS Code configuration for optimal development experience
- π¨ Automatic code formatting with Black, Prettier, and isort
# Install the package
pip install frappe-coding-standards
# Initialize in your Frappe project
cd /path/to/your/frappe-project
frappe-standards initThat's it! Your project now has comprehensive coding standards set up.
- β
.pre-commit-config.yaml- Pre-commit configuration with Frappe-specific hooks - β
.flake8- Python linting configuration - β
.prettierrc- JavaScript/CSS formatting configuration - β
pyproject.toml- Python project configuration - β
scripts/- Custom Frappe coding standards check scripts - β
.github/workflows/- GitHub Actions for automated quality checks - β
.vscode/settings.json- VS Code configuration (optional)
# Initialize coding standards in current project
frappe-standards init
# Update to latest configuration
frappe-standards update
# Run coding standards checks
frappe-standards check
# Run checks on specific files
frappe-standards check --files file1.py file2.js
# Run on all files
frappe-standards check --all-files
# Show setup status
frappe-standards status
# Setup only GitHub Actions (skip other configs)
frappe-standards setup-github# Run all hooks on all files
pre-commit run --all-files
# Run hooks on staged files only
pre-commit run
# Run specific hook
pre-commit run black
pre-commit run frappe-coding-standards
# Skip hooks for emergency commits
git commit -m "Emergency fix" --no-verifyAll user-facing strings must be wrapped in translation functions:
# β Wrong
frappe.msgprint("Document saved successfully")
# β
Correct
frappe.msgprint(_("Document saved successfully"))// β Wrong
frappe.msgprint("Document saved successfully");
// β
Correct
frappe.msgprint(__("Document saved successfully"));SQL Injection Prevention:
# β Wrong - SQL injection vulnerability
frappe.db.sql("SELECT * FROM tabUser WHERE name = '{}'".format(user_name))
# β
Correct - Parameterized query
frappe.db.sql("SELECT * FROM tabUser WHERE name = %s", user_name)Data Encryption:
# β Wrong - Plain text sensitive data
frappe.db.set_value("User", user_name, "api_key", plain_password)
# β
Correct - Encrypted sensitive data
encrypted_password = frappe.utils.password.encrypt(password)
frappe.db.set_value("User", user_name, "api_key", encrypted_password)Function Length:
- Functions should be β€20 lines
- Each function should have a single responsibility
- Break complex functions into smaller, focused functions
Indentation:
- Use tabs (not spaces) for indentation
- Maintain consistent indentation across Python and JavaScript
Naming Conventions:
# DocType Names - Title Case with Spaces
"Sales Order", "Purchase Invoice", "Item Price"
# Field Names - snake_case
"customer_name", "item_code", "posting_date"
# Function Names - snake_case
def validate_customer_details(self):
pass
def calculate_total_amount(self):
passThe package automatically sets up GitHub Actions workflows:
- Runs on every PR and push to main branches
- Executes all pre-commit hooks
- Performs Frappe-specific security and standards checks
- Comments on PRs with helpful feedback if checks fail
- Sets up complete Frappe environment
- Runs Python and JavaScript tests
- Includes database and Redis services
- Validates app installation and functionality
To fully enforce coding standards, set up branch protection rules:
- Go to Repository Settings β Branches
- Add protection rule for
main/developbranches:- β Require status checks to pass before merging
- β Select "Code Quality Checks" workflow
- β Require up-to-date branches
- β Include administrators
# Full setup (recommended)
frappe-standards init
# Skip GitHub Actions setup
frappe-standards init --no-github
# Skip VS Code configuration
frappe-standards init --no-vscode
# Force overwrite existing files
frappe-standards init --force
# Skip interactive prompts
frappe-standards init --no-vscode --no-github --forceThe package includes sensible defaults, but you can customize .pre-commit-config.yaml:
repos:
# Add your custom hooks here
- repo: local
hooks:
- id: custom-check
name: Custom Project Check
entry: python scripts/custom_check.py
language: system
files: \.py$- Install standards early in your project lifecycle
- Run checks locally before pushing:
pre-commit run --all-files - Fix issues immediately rather than accumulating technical debt
- Update regularly:
frappe-standards update
- Document the setup in your project README
- Include in onboarding for new developers
- Set up branch protection to enforce standards
- Regular reviews of coding standards compliance
# Cache pre-commit environments for faster execution
export PRE_COMMIT_HOME=~/.cache/pre-commit
# Run only on changed files during development
pre-commit run
# Full check before important commits
pre-commit run --all-files| File Type | Checks Applied |
|---|---|
| Python (.py) | Black formatting, isort imports, flake8 linting, function length, naming conventions, SQL security, translation wrappers |
| JavaScript (.js) | Prettier formatting, translation wrappers, basic syntax |
| JSON (.json) | Prettier formatting, DocType naming conventions, syntax validation |
| CSS/SCSS | Prettier formatting, consistent styling |
| YAML (.yml/.yaml) | Prettier formatting, syntax validation |
| Markdown (.md) | Prettier formatting, consistent documentation style |
Pre-commit hooks not running:
# Reinstall hooks
pre-commit clean
pre-commit installImport errors in scripts:
# Ensure package is installed
pip install frappe-coding-standards
# Update to latest version
pip install --upgrade frappe-coding-standardsGitHub Actions failing:
# Check workflow syntax
cd .github/workflows
yamllint code-quality.yml- Check the status:
frappe-standards status - Review logs: Look at pre-commit or GitHub Actions logs
- Update package:
pip install --upgrade frappe-coding-standards - Reset configuration:
frappe-standards init --force
We welcome contributions! Please see our Contributing Guidelines for details.
# Clone the repository
git clone https://github.com/dhwani-ris/frappe_coding_standards.git
cd frappe_coding_standards
# Install in development mode
pip install -e .
# Install development dependencies
pip install -e .[dev]
# Run tests
pytest
# Test CLI locally
frappe-standards --helpThis project is licensed under the MIT License - see the LICENSE file for details.
- β¨ Initial release
- π§ Complete pre-commit hooks setup
- π‘οΈ Security checks for SQL injection and encryption
- π Translation wrapper validation
- π Comprehensive coding standards enforcement
- βοΈ GitHub Actions integration
- π» VS Code configuration
- π Complete documentation and CLI tool
Ready to maintain high-quality Frappe code? Install frappe-coding-standards today! π