sqlnbfmt 0.1.6__tar.gz → 0.1.8__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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlnbfmt
3
- Version: 0.1.6
3
+ Version: 0.1.8
4
4
  Summary: A tool to format SQL code in Jupyter notebooks.
5
5
  Author-email: Qing <qingye779@gmail.com>
6
6
  License: MIT
@@ -10,6 +10,7 @@ Requires-Python: >=3.12
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
12
12
  Requires-Dist: astor>=0.8.1
13
+ Requires-Dist: black>=24.10.0
13
14
  Requires-Dist: nbformat>=5.10.4
14
15
  Requires-Dist: pyyaml>=6.0.2
15
16
  Requires-Dist: sqlglot>=25.27.0
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sqlnbfmt"
3
- version = "0.1.6"
3
+ version = "0.1.8"
4
4
  description = "A tool to format SQL code in Jupyter notebooks."
5
5
  authors = [
6
6
  { name = "Qing", email = "qingye779@gmail.com" }
@@ -14,6 +14,7 @@ classifiers = [
14
14
  ]
15
15
  dependencies = [
16
16
  "astor>=0.8.1",
17
+ "black>=24.10.0",
17
18
  "nbformat>=5.10.4",
18
19
  "pyyaml>=6.0.2",
19
20
  "sqlglot>=25.27.0",
@@ -38,4 +39,4 @@ markers = [
38
39
  ]
39
40
 
40
41
  [tool.setuptools.package-data]
41
- sqlnbfmt = [".pre-commit-hooks.yaml"]
42
+ sqlnbfmt = [".pre-commit-hooks.yaml"]
@@ -7,6 +7,7 @@ from pathlib import Path
7
7
  from typing import Set, Optional, Dict, Union, Tuple
8
8
 
9
9
  import astor
10
+ import black
10
11
  import nbformat
11
12
  import yaml
12
13
  from sqlglot import parse_one, errors
@@ -254,7 +255,7 @@ class SQLStringFormatter(ast.NodeTransformer):
254
255
  formatted_sql = '\n'.join(indented_lines)
255
256
 
256
257
  # Reconstruct the string with appropriate quotes
257
- formatted_str = f'"""\n{formatted_sql}\n"""'
258
+ formatted_str = f'"""\n{formatted_sql}\n{" " * 4}"""'
258
259
  else:
259
260
  # Ensure newlines are added around the formatted SQL
260
261
  formatted_str = f'"""\n{formatted_sql}\n"""'
@@ -328,11 +329,18 @@ class SQLStringFormatter(ast.NodeTransformer):
328
329
 
329
330
  new_node = ast.JoinedStr(values=new_values)
330
331
  if '\n' in formatted_sql:
331
- formatted_fstring = ast.JoinedStr(values=[
332
- ast.Constant(value='\n'),
333
- *new_node.values,
334
- ast.Constant(value='\n')
335
- ])
332
+ if in_function:
333
+ formatted_fstring = ast.JoinedStr(values=[
334
+ ast.Constant(value='\n'),
335
+ *new_node.values,
336
+ ast.Constant(value=f'\n{" " * 4}')
337
+ ])
338
+ else:
339
+ formatted_fstring = ast.JoinedStr(values=[
340
+ ast.Constant(value='\n'),
341
+ *new_node.values,
342
+ ast.Constant(value='\n')
343
+ ])
336
344
  else:
337
345
  formatted_fstring = new_node
338
346
  return formatted_fstring
@@ -477,6 +485,9 @@ def process_notebook(
477
485
  if formatter.changed:
478
486
  # Use astor to convert AST back to source code
479
487
  formatted_code = astor.to_source(new_tree)
488
+ # Now format with black
489
+ formatted_code = black.format_str(formatted_code, mode=black.FileMode())
490
+
480
491
  if formatted_code != original_code:
481
492
  cell.source = formatted_code
482
493
  changed = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlnbfmt
3
- Version: 0.1.6
3
+ Version: 0.1.8
4
4
  Summary: A tool to format SQL code in Jupyter notebooks.
5
5
  Author-email: Qing <qingye779@gmail.com>
6
6
  License: MIT
@@ -10,6 +10,7 @@ Requires-Python: >=3.12
10
10
  Description-Content-Type: text/markdown
11
11
  License-File: LICENSE
12
12
  Requires-Dist: astor>=0.8.1
13
+ Requires-Dist: black>=24.10.0
13
14
  Requires-Dist: nbformat>=5.10.4
14
15
  Requires-Dist: pyyaml>=6.0.2
15
16
  Requires-Dist: sqlglot>=25.27.0
@@ -1,4 +1,5 @@
1
1
  astor>=0.8.1
2
+ black>=24.10.0
2
3
  nbformat>=5.10.4
3
4
  pyyaml>=6.0.2
4
5
  sqlglot>=25.27.0
@@ -45,7 +45,8 @@ WHERE
45
45
  # Test Case 2: SELECT Query with F-String and Variable
46
46
  (
47
47
  [nbf.new_code_cell('''table_name = "users"\nquery = f"select * from {table_name}"''')],
48
- [nbf.new_code_cell('''table_name = 'users'\nquery = f"""
48
+ [nbf.new_code_cell('''table_name = "users"
49
+ query = f"""
49
50
  SELECT
50
51
  *
51
52
  FROM {table_name}
@@ -54,7 +55,8 @@ FROM {table_name}
54
55
  # Test Case 3: Multi-line SELECT Query with Variables
55
56
  (
56
57
  [nbf.new_code_cell('''columns = "id, name"\nquery = f"select {columns} from users where active = 1"''')],
57
- [nbf.new_code_cell('''columns = 'id, name'\nquery = f"""
58
+ [nbf.new_code_cell('''columns = "id, name"
59
+ query = f"""
58
60
  SELECT
59
61
  {columns}
60
62
  FROM users
@@ -70,18 +72,26 @@ WHERE
70
72
  # Test Case 5: SQL Query with Cell Magic Command
71
73
  (
72
74
  [nbf.new_code_cell('''%%sql\nselect * from users where active = 1''')],
73
- [nbf.new_code_cell('''%%sql\nSELECT\n *\nFROM users\nWHERE\n active = 1''')],
75
+ [nbf.new_code_cell('''%%sql
76
+ SELECT
77
+ *
78
+ FROM users
79
+ WHERE
80
+ active = 1''')],
74
81
  ),
75
- # Test Case 6: INSERT Query with Variables (Updated to handle formatter limitations)
82
+ # Test Case 6: INSERT Query with Variables (Remains Unchanged)
76
83
  (
77
84
  [nbf.new_code_cell('''table_name = "users"\ncolumns = "(id, name)"\nvalues = "(1, 'Alice')"\nquery = f"insert into {table_name} {columns} values {values}"''')],
78
- # Since the formatter cannot handle placeholders here, we expect the original code to remain unchanged
79
- [nbf.new_code_cell('''table_name = "users"\ncolumns = "(id, name)"\nvalues = "(1, 'Alice')"\nquery = f"insert into {table_name} {columns} values {values}"''')],
85
+ [nbf.new_code_cell('''table_name = "users"
86
+ columns = "(id, name)"
87
+ values = "(1, 'Alice')"
88
+ query = f"insert into {table_name} {columns} values {values}"''')],
80
89
  ),
81
90
  # Test Case 7: UPDATE Query with Variables
82
91
  (
83
92
  [nbf.new_code_cell('''table_name = "users"\nquery = f"update {table_name} set active = 0 where id = 1"''')],
84
- [nbf.new_code_cell('''table_name = 'users'\nquery = f"""
93
+ [nbf.new_code_cell('''table_name = "users"
94
+ query = f"""
85
95
  UPDATE {table_name} SET active = 0
86
96
  WHERE
87
97
  id = 1
@@ -136,6 +146,32 @@ SELECT
136
146
  FROM users
137
147
  """''')],
138
148
  ),
149
+ # Test Case 13: In-function SQL Query
150
+ (
151
+ [nbf.new_code_cell('''pd.read_sql("""select id, name from users where active = 1""")''')],
152
+ [nbf.new_code_cell('''pd.read_sql(
153
+ """
154
+ SELECT
155
+ id,
156
+ name
157
+ FROM users
158
+ WHERE
159
+ active = 1
160
+ """
161
+ )''')],
162
+ ),
163
+ # Test Case 14: In-function SQL Query with F-String
164
+ (
165
+ [nbf.new_code_cell('''table_name = "users"\npd.read_sql(f"select * from {table_name}")''')],
166
+ [nbf.new_code_cell('''table_name = "users"
167
+ pd.read_sql(
168
+ f"""
169
+ SELECT
170
+ *
171
+ FROM {table_name}
172
+ """
173
+ )''')],
174
+ ),
139
175
  ])
140
176
  def test_sql_formatter(temp_nb_path, logger, input_cells, expected_cells):
141
177
  # Create a new notebook
File without changes
File without changes
File without changes
File without changes