Python Requirements
ERPNext requires Python 3.14 or higher.
Automated Code Quality Tools
ERPNext uses several automated tools to enforce code quality standards. These tools run automatically through pre-commit hooks and CI/CD pipelines.Pre-commit Hooks
All contributors must use pre-commit hooks. These hooks run automatically before each commit to catch issues early.Install Pre-commit
Pre-commit hooks are configured in
.pre-commit-config.yaml and will run automatically when you commit.Understand What Runs
The following checks run on every commit:
- Trailing whitespace removal
- YAML, JSON, and TOML validation
- Merge conflict detection
- Python AST validation
- Debug statement detection
- Code formatting (Prettier, Ruff)
- Linting (ESLint, Ruff)
Python Code Style
Ruff Configuration
ERPNext uses Ruff for Python linting and formatting. Ruff is configured inpyproject.toml:
Line Length
Maximum line length is 110 characters.
Enabled Linting Rules
ERPNext enables the following Ruff rule sets:Rule Set Descriptions
Rule Set Descriptions
- F (Pyflakes): Detects various Python errors like undefined names, unused imports, etc.
- E/W (pycodestyle): Enforces PEP 8 style conventions
- I (isort): Ensures imports are properly sorted and organized
- UP (pyupgrade): Suggests modern Python syntax improvements
- B (bugbear): Catches likely bugs and design problems
- RUF: Ruff-specific rules for code quality
Ignored Rules
Certain rules are intentionally disabled for ERPNext:These rules are ignored due to ERPNext’s specific coding patterns and legacy code considerations.
Type Hints
ERPNext uses Frappe’s type hint system:Code Formatting
Ruff’s formatter is configured with these settings:Import Sorting
Ruff automatically sorts imports using the isort rules:- Standard library imports
- Third-party imports
- Frappe imports
- Local imports
JavaScript/Vue Code Style
Prettier
ERPNext uses Prettier for JavaScript, Vue, and SCSS files:Prettier automatically formats your JavaScript and Vue files with consistent style.
Excluded Patterns
The following files are excluded from Prettier formatting:erpnext/public/dist/*(build artifacts)cypress/*(test files)node_modules/**boilerplate*erpnext/templates/includes/*(Jinja templates)
ESLint
JavaScript files are linted with ESLint:Pre-commit Hook Configuration
Basic Checks
These checks run on every commit:Trailing Whitespace
Trailing Whitespace
Removes trailing whitespace from ERPNext files (excludes JSON, TXT, CSV, and MD files):
File Validation
File Validation
Validates file syntax:
check-yaml: YAML files must be validcheck-json: JSON files must be validcheck-toml: TOML files must be validcheck-ast: Python files must have valid syntax
Branch Protection
Branch Protection
Prevents direct commits to the
develop branch:Merge Conflict Detection
Merge Conflict Detection
Detects merge conflict markers:
Debug Statement Detection
Debug Statement Detection
Prevents committing Python debug statements like
pdb.set_trace():CI/CD Linting
All pull requests are automatically checked by GitHub Actions:Linters Workflow
The same pre-commit hooks that run locally also run in CI. If they pass locally, they’ll pass in CI.
Semgrep Analysis
ERPNext also runs Semgrep for advanced security and correctness checks:Best Practices
Server-Side Validation
This ensures:- Security: Client-side code can be bypassed
- API consistency: REST API and UI use the same logic
- Reliability: Logic is tested and version-controlled
Code Organization
Follow these guidelines for organizing your code:- Keep functions focused: Each function should do one thing well
- Use meaningful names: Variable and function names should be descriptive
- Add docstrings: Document functions with clear docstrings
- Write tests: Include unit tests for new functionality
- Handle errors: Use proper error handling and user-friendly messages
Example: Well-Formatted Python Code
Quick Reference
| Tool | Purpose | Configuration |
|---|---|---|
| Ruff | Python linting & formatting | pyproject.toml |
| Prettier | JavaScript/Vue/SCSS formatting | .pre-commit-config.yaml |
| ESLint | JavaScript linting | .pre-commit-config.yaml |
| Semgrep | Security & correctness analysis | GitHub Actions |
| Pre-commit | Automated pre-commit checks | .pre-commit-config.yaml |
Troubleshooting
Pre-commit hooks failing
Pre-commit hooks failing
If pre-commit hooks fail:
- Read the error message carefully
- Fix the reported issues (many are auto-fixed)
- Stage the fixed files:
git add <files> - Commit again:
git commit -m "your message"
Import sorting issues
Import sorting issues
If Ruff is reordering your imports unexpectedly:
- Let Ruff auto-fix:
ruff check --select=I --fix . - Commit the changes
- Review the new import order matches the standard
Line length violations
Line length violations
If you have lines longer than 110 characters:
- Break the line using appropriate Python style
- Use parentheses for implicit line continuation
- Break method chains into multiple lines