sqlnbfmt 0.1.9__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,24 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: sqlnbfmt
3
- Version: 0.1.9
3
+ Version: 0.3.0
4
4
  Summary: A tool to format SQL code in Jupyter notebooks.
5
5
  Author-email: Qing <qingye779@gmail.com>
6
6
  License: MIT
7
+ Project-URL: Homepage, https://github.com/flyersworder/sqlnbfmt
8
+ Project-URL: Repository, https://github.com/flyersworder/sqlnbfmt
9
+ Project-URL: Changelog, https://github.com/flyersworder/sqlnbfmt/blob/main/CHANGELOG.md
10
+ Project-URL: Issues, https://github.com/flyersworder/sqlnbfmt/issues
11
+ Keywords: sql,jupyter,notebook,formatter,sqlglot,pre-commit
7
12
  Classifier: Programming Language :: Python :: 3
8
13
  Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
9
15
  Requires-Python: >=3.12
10
16
  Description-Content-Type: text/markdown
11
17
  License-File: LICENSE
12
- Requires-Dist: astor>=0.8.1
13
- Requires-Dist: black>=24.10.0
14
18
  Requires-Dist: nbformat>=5.10.4
15
19
  Requires-Dist: pyyaml>=6.0.2
16
20
  Requires-Dist: sqlglot>=25.27.0
21
+ Dynamic: license-file
17
22
 
18
23
  # sqlnbfmt
19
24
 
@@ -21,16 +26,20 @@ Requires-Dist: sqlglot>=25.27.0
21
26
  [![License](https://img.shields.io/pypi/l/sqlnbfmt.svg)](https://github.com/flyersworder/sqlnbfmt/blob/main/LICENSE)
22
27
  [![Python Versions](https://img.shields.io/pypi/pyversions/sqlnbfmt.svg)](https://pypi.org/project/sqlnbfmt/)
23
28
 
29
+ > **Quick start:** `pip install sqlnbfmt && sqlnbfmt notebook.ipynb`
30
+
24
31
  A SQL formatter designed specifically for Jupyter Notebooks. `sqlnbfmt` automatically formats SQL queries embedded in code cells, including both Python strings and SQL magic cells (`%%sql`), helping you maintain clean and consistent code.
25
32
 
26
33
  ## Features
27
34
 
28
- - 🎯 **Smart SQL Detection**: Automatically identifies and formats SQL queries in code cells and magic SQL cells
29
- - 🌳 **AST-Powered**: Uses Abstract Syntax Tree parsing for accurate SQL string identification
30
- - 🔒 **Safe Formatting**: Preserves query parameters (e.g., `%s`, `?`) and SQL comments
31
- - ⚙️ **Highly Configurable**: Customize formatting through YAML configuration
32
- - 🔄 **Pre-commit Ready**: Seamlessly integrates with pre-commit hooks
33
- - 📦 **Zero Dependencies**: Minimal installation footprint
35
+ - **Zero-config**: Works out of the box with sensible defaults — no config file needed
36
+ - **Smart SQL Detection**: Automatically identifies and formats SQL queries in code cells and magic SQL cells
37
+ - **AST-Powered**: Uses Abstract Syntax Tree parsing for accurate SQL string identification
38
+ - **Safe Formatting**: Preserves Python comments, query parameters (e.g., `%s`, `?`), and SQL comments
39
+ - **CI-Friendly**: `--check` mode exits non-zero when formatting is needed; `--diff` shows what would change
40
+ - **Skip Hints**: Add `# sqlnbfmt: skip` to any cell to exclude it from formatting
41
+ - **Pre-commit Ready**: Seamlessly integrates with pre-commit hooks
42
+ - **Lightweight**: Only three runtime dependencies (sqlglot, nbformat, pyyaml)
34
43
 
35
44
  ## Installation
36
45
 
@@ -47,9 +56,23 @@ Format a single notebook:
47
56
  sqlnbfmt path/to/your_notebook.ipynb
48
57
  ```
49
58
 
50
- Format all notebooks in a directory:
59
+ Check formatting without modifying files (useful in CI):
60
+ ```bash
61
+ sqlnbfmt --check path/to/your_notebook.ipynb
62
+ ```
63
+
64
+ Show a diff of what would change:
51
65
  ```bash
52
- sqlnbfmt path/to/notebooks/
66
+ sqlnbfmt --diff path/to/your_notebook.ipynb
67
+ ```
68
+
69
+ ### Skipping Cells
70
+
71
+ Add a `# sqlnbfmt: skip` comment anywhere in a cell to skip formatting:
72
+
73
+ ```python
74
+ # sqlnbfmt: skip
75
+ query = "select * from my_special_table where id = 1"
53
76
  ```
54
77
 
55
78
  ### Pre-commit Integration
@@ -63,17 +86,15 @@ pip install pre-commit
63
86
  ```yaml
64
87
  repos:
65
88
  - repo: https://github.com/flyersworder/sqlnbfmt
66
- rev: v0.1.1
89
+ rev: v0.3.0
67
90
  hooks:
68
91
  - id: sqlnbfmt
69
- name: sqlnbfmt
70
92
  types: [jupyter]
71
- args: [--config, config.yaml, --dialect, postgres]
72
93
  ```
73
- Please fun the following command in your CMD for help:
74
94
 
75
- ```bash
76
- sqlnbfmt -h
95
+ All arguments are optional. To specify a dialect or custom config:
96
+ ```yaml
97
+ args: [--dialect, postgres, --config, config.yaml]
77
98
  ```
78
99
 
79
100
  3. Install the hook:
@@ -86,8 +107,21 @@ pre-commit install
86
107
  pre-commit run --all-files
87
108
  ```
88
109
 
110
+ ### CI Usage
111
+
112
+ Use `--check` in GitHub Actions to enforce formatting:
113
+
114
+ ```yaml
115
+ - name: Check SQL notebook formatting
116
+ run: |
117
+ pip install sqlnbfmt
118
+ sqlnbfmt --check **/*.ipynb
119
+ ```
120
+
89
121
  ## Configuration
90
122
 
123
+ `sqlnbfmt` works without any configuration file. A `config.yaml` is only needed to override defaults.
124
+
91
125
  Create a `config.yaml` file to customize formatting behavior. [Here](https://github.com/flyersworder/sqlnbfmt/blob/main/config.yaml) is a template.
92
126
 
93
127
  ### Configuration Options
@@ -95,8 +129,8 @@ Create a `config.yaml` file to customize formatting behavior. [Here](https://git
95
129
  | Option | Description | Default |
96
130
  |--------|-------------|---------|
97
131
  | `sql_keywords` | SQL keywords to recognize and format | Common SQL keywords |
98
- | `function_names` | Python functions containing SQL code | `[]` |
99
- | `sql_decorators` | Decorators indicating SQL code | `[]` |
132
+ | `function_names` | Python functions containing SQL code | `read_sql`, `execute`, etc. |
133
+ | `sql_decorators` | Decorators indicating SQL code | `query`, `sql_query`, etc. |
100
134
  | `single_line_threshold` | Maximum length before splitting SQL | 80 |
101
135
  | `indent_width` | Number of spaces for indentation | 4 |
102
136
 
@@ -125,6 +159,19 @@ ORDER BY
125
159
  """)
126
160
  ```
127
161
 
162
+ ## Troubleshooting
163
+
164
+ **SQL not being formatted?**
165
+ - Ensure the string contains at least 2 SQL keywords or a recognizable pattern like `SELECT...FROM`
166
+ - Check that the function name is in the recognized list (use `--config` to add custom ones)
167
+
168
+ **Comments being modified?**
169
+ - Python comments (`#`) are preserved. SQL comments (`--`) inside strings are converted to `/* */` block comments by sqlglot.
170
+
171
+ **Pre-commit hook fails?**
172
+ - Make sure the `rev` matches the installed version
173
+ - Run `pre-commit autoupdate` to get the latest version
174
+
128
175
  ## Contributing
129
176
 
130
177
  We welcome contributions! Here's how to get started:
@@ -146,6 +193,13 @@ source .venv/bin/activate # On Windows: .venv\Scripts\activate
146
193
  pytest
147
194
  ```
148
195
 
196
+ 4. Add eval cases: see `tests/eval/generate_fixtures.py` for examples. Run `python tests/eval/generate_fixtures.py` to regenerate fixtures.
197
+
198
+ 5. Install dev pre-commit hooks:
199
+ ```bash
200
+ pre-commit install
201
+ ```
202
+
149
203
  ## License
150
204
 
151
205
  This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -1,36 +1,23 @@
1
- Metadata-Version: 2.2
2
- Name: sqlnbfmt
3
- Version: 0.1.9
4
- Summary: A tool to format SQL code in Jupyter notebooks.
5
- Author-email: Qing <qingye779@gmail.com>
6
- License: MIT
7
- Classifier: Programming Language :: Python :: 3
8
- Classifier: Programming Language :: Python :: 3.12
9
- Requires-Python: >=3.12
10
- Description-Content-Type: text/markdown
11
- License-File: LICENSE
12
- Requires-Dist: astor>=0.8.1
13
- Requires-Dist: black>=24.10.0
14
- Requires-Dist: nbformat>=5.10.4
15
- Requires-Dist: pyyaml>=6.0.2
16
- Requires-Dist: sqlglot>=25.27.0
17
-
18
1
  # sqlnbfmt
19
2
 
20
3
  [![PyPI](https://img.shields.io/pypi/v/sqlnbfmt.svg)](https://pypi.org/project/sqlnbfmt/)
21
4
  [![License](https://img.shields.io/pypi/l/sqlnbfmt.svg)](https://github.com/flyersworder/sqlnbfmt/blob/main/LICENSE)
22
5
  [![Python Versions](https://img.shields.io/pypi/pyversions/sqlnbfmt.svg)](https://pypi.org/project/sqlnbfmt/)
23
6
 
7
+ > **Quick start:** `pip install sqlnbfmt && sqlnbfmt notebook.ipynb`
8
+
24
9
  A SQL formatter designed specifically for Jupyter Notebooks. `sqlnbfmt` automatically formats SQL queries embedded in code cells, including both Python strings and SQL magic cells (`%%sql`), helping you maintain clean and consistent code.
25
10
 
26
11
  ## Features
27
12
 
28
- - 🎯 **Smart SQL Detection**: Automatically identifies and formats SQL queries in code cells and magic SQL cells
29
- - 🌳 **AST-Powered**: Uses Abstract Syntax Tree parsing for accurate SQL string identification
30
- - 🔒 **Safe Formatting**: Preserves query parameters (e.g., `%s`, `?`) and SQL comments
31
- - ⚙️ **Highly Configurable**: Customize formatting through YAML configuration
32
- - 🔄 **Pre-commit Ready**: Seamlessly integrates with pre-commit hooks
33
- - 📦 **Zero Dependencies**: Minimal installation footprint
13
+ - **Zero-config**: Works out of the box with sensible defaults — no config file needed
14
+ - **Smart SQL Detection**: Automatically identifies and formats SQL queries in code cells and magic SQL cells
15
+ - **AST-Powered**: Uses Abstract Syntax Tree parsing for accurate SQL string identification
16
+ - **Safe Formatting**: Preserves Python comments, query parameters (e.g., `%s`, `?`), and SQL comments
17
+ - **CI-Friendly**: `--check` mode exits non-zero when formatting is needed; `--diff` shows what would change
18
+ - **Skip Hints**: Add `# sqlnbfmt: skip` to any cell to exclude it from formatting
19
+ - **Pre-commit Ready**: Seamlessly integrates with pre-commit hooks
20
+ - **Lightweight**: Only three runtime dependencies (sqlglot, nbformat, pyyaml)
34
21
 
35
22
  ## Installation
36
23
 
@@ -47,9 +34,23 @@ Format a single notebook:
47
34
  sqlnbfmt path/to/your_notebook.ipynb
48
35
  ```
49
36
 
50
- Format all notebooks in a directory:
37
+ Check formatting without modifying files (useful in CI):
51
38
  ```bash
52
- sqlnbfmt path/to/notebooks/
39
+ sqlnbfmt --check path/to/your_notebook.ipynb
40
+ ```
41
+
42
+ Show a diff of what would change:
43
+ ```bash
44
+ sqlnbfmt --diff path/to/your_notebook.ipynb
45
+ ```
46
+
47
+ ### Skipping Cells
48
+
49
+ Add a `# sqlnbfmt: skip` comment anywhere in a cell to skip formatting:
50
+
51
+ ```python
52
+ # sqlnbfmt: skip
53
+ query = "select * from my_special_table where id = 1"
53
54
  ```
54
55
 
55
56
  ### Pre-commit Integration
@@ -63,17 +64,15 @@ pip install pre-commit
63
64
  ```yaml
64
65
  repos:
65
66
  - repo: https://github.com/flyersworder/sqlnbfmt
66
- rev: v0.1.1
67
+ rev: v0.3.0
67
68
  hooks:
68
69
  - id: sqlnbfmt
69
- name: sqlnbfmt
70
70
  types: [jupyter]
71
- args: [--config, config.yaml, --dialect, postgres]
72
71
  ```
73
- Please fun the following command in your CMD for help:
74
72
 
75
- ```bash
76
- sqlnbfmt -h
73
+ All arguments are optional. To specify a dialect or custom config:
74
+ ```yaml
75
+ args: [--dialect, postgres, --config, config.yaml]
77
76
  ```
78
77
 
79
78
  3. Install the hook:
@@ -86,8 +85,21 @@ pre-commit install
86
85
  pre-commit run --all-files
87
86
  ```
88
87
 
88
+ ### CI Usage
89
+
90
+ Use `--check` in GitHub Actions to enforce formatting:
91
+
92
+ ```yaml
93
+ - name: Check SQL notebook formatting
94
+ run: |
95
+ pip install sqlnbfmt
96
+ sqlnbfmt --check **/*.ipynb
97
+ ```
98
+
89
99
  ## Configuration
90
100
 
101
+ `sqlnbfmt` works without any configuration file. A `config.yaml` is only needed to override defaults.
102
+
91
103
  Create a `config.yaml` file to customize formatting behavior. [Here](https://github.com/flyersworder/sqlnbfmt/blob/main/config.yaml) is a template.
92
104
 
93
105
  ### Configuration Options
@@ -95,8 +107,8 @@ Create a `config.yaml` file to customize formatting behavior. [Here](https://git
95
107
  | Option | Description | Default |
96
108
  |--------|-------------|---------|
97
109
  | `sql_keywords` | SQL keywords to recognize and format | Common SQL keywords |
98
- | `function_names` | Python functions containing SQL code | `[]` |
99
- | `sql_decorators` | Decorators indicating SQL code | `[]` |
110
+ | `function_names` | Python functions containing SQL code | `read_sql`, `execute`, etc. |
111
+ | `sql_decorators` | Decorators indicating SQL code | `query`, `sql_query`, etc. |
100
112
  | `single_line_threshold` | Maximum length before splitting SQL | 80 |
101
113
  | `indent_width` | Number of spaces for indentation | 4 |
102
114
 
@@ -125,6 +137,19 @@ ORDER BY
125
137
  """)
126
138
  ```
127
139
 
140
+ ## Troubleshooting
141
+
142
+ **SQL not being formatted?**
143
+ - Ensure the string contains at least 2 SQL keywords or a recognizable pattern like `SELECT...FROM`
144
+ - Check that the function name is in the recognized list (use `--config` to add custom ones)
145
+
146
+ **Comments being modified?**
147
+ - Python comments (`#`) are preserved. SQL comments (`--`) inside strings are converted to `/* */` block comments by sqlglot.
148
+
149
+ **Pre-commit hook fails?**
150
+ - Make sure the `rev` matches the installed version
151
+ - Run `pre-commit autoupdate` to get the latest version
152
+
128
153
  ## Contributing
129
154
 
130
155
  We welcome contributions! Here's how to get started:
@@ -146,6 +171,13 @@ source .venv/bin/activate # On Windows: .venv\Scripts\activate
146
171
  pytest
147
172
  ```
148
173
 
174
+ 4. Add eval cases: see `tests/eval/generate_fixtures.py` for examples. Run `python tests/eval/generate_fixtures.py` to regenerate fixtures.
175
+
176
+ 5. Install dev pre-commit hooks:
177
+ ```bash
178
+ pre-commit install
179
+ ```
180
+
149
181
  ## License
150
182
 
151
183
  This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlnbfmt"
3
- version = "0.1.9"
3
+ version = "0.3.0"
4
4
  description = "A tool to format SQL code in Jupyter notebooks."
5
5
  authors = [
6
6
  { name = "Qing", email = "qingye779@gmail.com" }
@@ -8,21 +8,31 @@ authors = [
8
8
  license = { text = "MIT" }
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12"
11
+ keywords = ["sql", "jupyter", "notebook", "formatter", "sqlglot", "pre-commit"]
11
12
  classifiers = [
12
13
  "Programming Language :: Python :: 3",
13
- "Programming Language :: Python :: 3.12"
14
+ "Programming Language :: Python :: 3.12",
15
+ "Programming Language :: Python :: 3.13"
14
16
  ]
15
17
  dependencies = [
16
- "astor>=0.8.1",
17
- "black>=24.10.0",
18
18
  "nbformat>=5.10.4",
19
19
  "pyyaml>=6.0.2",
20
20
  "sqlglot>=25.27.0",
21
21
  ]
22
22
 
23
- [tool.uv]
24
- dev-dependencies = [
23
+ [project.urls]
24
+ Homepage = "https://github.com/flyersworder/sqlnbfmt"
25
+ Repository = "https://github.com/flyersworder/sqlnbfmt"
26
+ Changelog = "https://github.com/flyersworder/sqlnbfmt/blob/main/CHANGELOG.md"
27
+ Issues = "https://github.com/flyersworder/sqlnbfmt/issues"
28
+
29
+ [project.scripts]
30
+ sqlnbfmt = "sqlnbfmt.formatter:main"
31
+
32
+ [dependency-groups]
33
+ dev = [
25
34
  "pytest-cov>=5.0.0",
35
+ "pytest-timeout>=2.3.1",
26
36
  "pytest>=8.3.3",
27
37
  ]
28
38
 
@@ -30,13 +40,13 @@ dev-dependencies = [
30
40
  requires = ["setuptools>=61.0", "wheel"]
31
41
  build-backend = "setuptools.build_meta"
32
42
 
33
- [project.scripts]
34
- sqlnbfmt = "sqlnbfmt.formatter:main"
35
-
36
43
  [tool.pytest.ini_options]
37
44
  markers = [
38
45
  "integration: mark a test as an integration test."
39
46
  ]
40
47
 
48
+ [tool.ruff]
49
+ extend-exclude = ["tests/eval/*/"]
50
+
41
51
  [tool.setuptools.package-data]
42
52
  sqlnbfmt = [".pre-commit-hooks.yaml"]