Skip to content

Commit 0917910

Browse files
Copilotsanjay-kv
andcommitted
Add comprehensive Python linting and code quality infrastructure
Co-authored-by: sanjay-kv <[email protected]>
1 parent ead94d5 commit 0917910

File tree

12 files changed

+629
-0
lines changed

12 files changed

+629
-0
lines changed

.flake8

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[flake8]
2+
# Maximum line length
3+
max-line-length = 88
4+
5+
# Ignore specific error codes
6+
ignore =
7+
E203, # whitespace before ':'
8+
E501, # line too long (handled by black)
9+
W503, # line break before binary operator (conflicts with W504)
10+
F401, # imported but unused (handled by isort)
11+
12+
# Exclude directories
13+
exclude =
14+
.git,
15+
__pycache__,
16+
build,
17+
dist,
18+
*.egg-info,
19+
.venv,
20+
venv,
21+
.tox,
22+
.pytest_cache,
23+
node_modules
24+
25+
# Select error/warning codes to check
26+
select = E,W,F,C
27+
28+
# Enable complexity checking
29+
max-complexity = 10
30+
31+
# File patterns to check
32+
filename = *.py
33+
34+
# Show source code for each error
35+
show-source = True
36+
37+
# Count errors and warnings
38+
count = True
39+
40+
# Display statistics
41+
statistics = True

.github/workflows/lint.yml

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
name: Python Code Quality and Linting
2+
3+
on:
4+
pull_request:
5+
branches: [ main, develop ]
6+
paths:
7+
- '**.py'
8+
- 'requirements*.txt'
9+
- 'pyproject.toml'
10+
- '.flake8'
11+
- '.pre-commit-config.yaml'
12+
- '.github/workflows/lint.yml'
13+
push:
14+
branches: [ main, develop ]
15+
paths:
16+
- '**.py'
17+
- 'requirements*.txt'
18+
- 'pyproject.toml'
19+
- '.flake8'
20+
- '.pre-commit-config.yaml'
21+
- '.github/workflows/lint.yml'
22+
23+
jobs:
24+
lint:
25+
runs-on: ubuntu-latest
26+
strategy:
27+
matrix:
28+
python-version: [3.8, 3.9, '3.10', '3.11']
29+
30+
steps:
31+
- uses: actions/checkout@v4
32+
33+
- name: Set up Python ${{ matrix.python-version }}
34+
uses: actions/setup-python@v5
35+
with:
36+
python-version: ${{ matrix.python-version }}
37+
38+
- name: Cache pip dependencies
39+
uses: actions/cache@v4
40+
with:
41+
path: ~/.cache/pip
42+
key: ${{ runner.os }}-pip-${{ hashFiles('requirements-dev.txt') }}
43+
restore-keys: |
44+
${{ runner.os }}-pip-
45+
46+
- name: Install dependencies
47+
run: |
48+
python -m pip install --upgrade pip
49+
pip install -r requirements-dev.txt
50+
51+
- name: Run Black (code formatter check)
52+
run: |
53+
black --check --diff --color .
54+
55+
- name: Run isort (import sorting check)
56+
run: |
57+
isort --check-only --diff --color .
58+
59+
- name: Run flake8 (linting)
60+
run: |
61+
flake8 .
62+
63+
- name: Run mypy (static type checking)
64+
run: |
65+
mypy .
66+
67+
- name: Run bandit (security linting)
68+
run: |
69+
bandit -r . -f json -o bandit-report.json || true
70+
bandit -r . -f txt
71+
72+
- name: Run safety (dependency security check)
73+
run: |
74+
safety check --json --output safety-report.json || true
75+
safety check
76+
77+
- name: Upload bandit results
78+
if: always()
79+
uses: actions/upload-artifact@v4
80+
with:
81+
name: bandit-report-${{ matrix.python-version }}
82+
path: bandit-report.json
83+
84+
- name: Upload safety results
85+
if: always()
86+
uses: actions/upload-artifact@v4
87+
with:
88+
name: safety-report-${{ matrix.python-version }}
89+
path: safety-report.json
90+
91+
pre-commit:
92+
runs-on: ubuntu-latest
93+
steps:
94+
- uses: actions/checkout@v4
95+
96+
- name: Set up Python 3.11
97+
uses: actions/setup-python@v5
98+
with:
99+
python-version: 3.11
100+
101+
- name: Install pre-commit
102+
run: |
103+
python -m pip install --upgrade pip
104+
pip install pre-commit
105+
106+
- name: Run pre-commit on all files
107+
run: |
108+
pre-commit run --all-files
109+
110+
test:
111+
runs-on: ubuntu-latest
112+
if: github.event_name == 'pull_request'
113+
strategy:
114+
matrix:
115+
python-version: [3.8, 3.9, '3.10', '3.11']
116+
117+
steps:
118+
- uses: actions/checkout@v4
119+
120+
- name: Set up Python ${{ matrix.python-version }}
121+
uses: actions/setup-python@v5
122+
with:
123+
python-version: ${{ matrix.python-version }}
124+
125+
- name: Install dependencies
126+
run: |
127+
python -m pip install --upgrade pip
128+
pip install -r requirements-dev.txt
129+
130+
- name: Run tests with pytest
131+
run: |
132+
pytest --cov=. --cov-report=xml --cov-report=html
133+
134+
- name: Upload coverage to Codecov
135+
if: matrix.python-version == '3.11'
136+
uses: codecov/codecov-action@v4
137+
with:
138+
file: ./coverage.xml
139+
flags: unittests
140+
name: codecov-umbrella

.pre-commit-config.yaml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
repos:
2+
# Black - Python code formatter
3+
- repo: https://github.com/psf/black
4+
rev: 24.8.0
5+
hooks:
6+
- id: black
7+
language_version: python3
8+
args: [--line-length=88]
9+
10+
# isort - Import sorting
11+
- repo: https://github.com/pycqa/isort
12+
rev: 5.13.2
13+
hooks:
14+
- id: isort
15+
args: [--profile=black, --line-length=88]
16+
17+
# flake8 - Linting and style checking
18+
- repo: https://github.com/pycqa/flake8
19+
rev: 7.1.1
20+
hooks:
21+
- id: flake8
22+
additional_dependencies:
23+
- flake8-docstrings==1.7.0
24+
- flake8-import-order==0.18.2
25+
- flake8-bugbear==24.8.19
26+
27+
# mypy - Static type checking
28+
- repo: https://github.com/pre-commit/mirrors-mypy
29+
rev: v1.11.2
30+
hooks:
31+
- id: mypy
32+
additional_dependencies: [types-requests]
33+
args: [--ignore-missing-imports]
34+
35+
# bandit - Security linting
36+
- repo: https://github.com/pycqa/bandit
37+
rev: 1.7.10
38+
hooks:
39+
- id: bandit
40+
args: [--skip, "B101,B601"]
41+
42+
# General pre-commit hooks
43+
- repo: https://github.com/pre-commit/pre-commit-hooks
44+
rev: v4.6.0
45+
hooks:
46+
# Check for files that would conflict in case-insensitive filesystems
47+
- id: check-case-conflict
48+
# Check for files that contain merge conflict strings
49+
- id: check-merge-conflict
50+
# Check for debugger imports and py37+ breakpoint() calls
51+
- id: debug-statements
52+
# Check for files with names that would conflict on a case-insensitive filesystem
53+
- id: check-added-large-files
54+
args: [--maxkb=500]
55+
# Trim trailing whitespace
56+
- id: trailing-whitespace
57+
args: [--markdown-linebreak-ext=md]
58+
# Make sure files end in a newline and only a newline
59+
- id: end-of-file-fixer
60+
# Check for valid JSON
61+
- id: check-json
62+
# Check for valid YAML
63+
- id: check-yaml
64+
args: [--allow-multiple-documents]
65+
# Check for valid TOML
66+
- id: check-toml
67+
# Prevent giant files from being committed
68+
- id: check-added-large-files
69+
70+
# Check Python AST
71+
- repo: https://github.com/pre-commit/pre-commit-hooks
72+
rev: v4.6.0
73+
hooks:
74+
- id: check-ast
75+
76+
# Dockerfile linting (if needed for Python containerization)
77+
- repo: https://github.com/hadolint/hadolint
78+
rev: v2.12.0
79+
hooks:
80+
- id: hadolint-docker
81+
files: Dockerfile*

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,85 @@ flowchart LR
7070

7171
This command will start a development server and open the application in your default web browser.
7272

73+
## Python Backend Development Setup
74+
75+
If you're working on Python backend components, follow these additional setup steps:
76+
77+
### Prerequisites
78+
- Python 3.8 or higher
79+
- pip (Python package installer)
80+
81+
### Installing Python Development Dependencies
82+
83+
1. **Install development dependencies:**
84+
```bash
85+
pip install -r requirements-dev.txt
86+
```
87+
88+
2. **Install pre-commit hooks:**
89+
```bash
90+
pre-commit install
91+
```
92+
93+
### Running Python Code Quality Tools
94+
95+
#### Code Formatting
96+
- **Format code with black:**
97+
```bash
98+
black .
99+
```
100+
101+
- **Sort imports with isort:**
102+
```bash
103+
isort .
104+
```
105+
106+
#### Linting and Type Checking
107+
- **Run flake8 linting:**
108+
```bash
109+
flake8 .
110+
```
111+
112+
- **Run mypy type checking:**
113+
```bash
114+
mypy .
115+
```
116+
117+
- **Run security checks:**
118+
```bash
119+
bandit -r .
120+
safety check
121+
```
122+
123+
#### Running All Checks
124+
- **Run pre-commit on all files:**
125+
```bash
126+
pre-commit run --all-files
127+
```
128+
129+
#### Testing
130+
- **Run tests with pytest:**
131+
```bash
132+
pytest
133+
```
134+
135+
- **Run tests with coverage:**
136+
```bash
137+
pytest --cov=. --cov-report=html
138+
```
139+
140+
### Code Quality Standards
141+
142+
Our Python codebase follows these standards:
143+
- **PEP 8** compliance enforced by flake8
144+
- **Code formatting** with black (88 character line length)
145+
- **Import sorting** with isort
146+
- **Static type checking** with mypy
147+
- **Security scanning** with bandit
148+
- **Pre-commit hooks** to ensure quality before commits
149+
150+
All pull requests must pass the automated linting and testing pipeline.
151+
73152
**If you'd like to contribute to CodeHarborHub, please follow these guidelines:**
74153

75154
- **Fork** the repository and clone it locally.

backend/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""Backend package initialization."""
2+
3+
__version__ = "0.1.0"
4+
__author__ = "RecodeHive Team"
229 Bytes
Binary file not shown.
4.46 KB
Binary file not shown.
2.83 KB
Binary file not shown.

0 commit comments

Comments
 (0)