Skip to content

Commit e39ea24

Browse files
committed
fix: emit pass for empty generated classes
1 parent 53fa0b2 commit e39ea24

4 files changed

Lines changed: 58 additions & 1 deletion

File tree

internal/endtoend/testdata/query_parameter_limit_zero/python/query.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33
# sqlc v1.28.0
44
# source: query.sql
55
import dataclasses
6+
from typing import Optional
67

78
import sqlalchemy
89
import sqlalchemy.ext.asyncio
910

1011
from querytest import models
1112

1213

14+
COUNT_BARS = """-- name: count_bars \\:one
15+
SELECT count(*) FROM bar
16+
"""
17+
18+
19+
@dataclasses.dataclass()
20+
class CountBarsParams:
21+
pass
22+
23+
1324
DELETE_BAR_BY_ID = """-- name: delete_bar_by_id \\:execrows
1425
DELETE FROM bar WHERE id = :p1
1526
"""
@@ -35,6 +46,12 @@ class Querier:
3546
def __init__(self, conn: sqlalchemy.engine.Connection):
3647
self._conn = conn
3748

49+
def count_bars(self, arg: CountBarsParams) -> Optional[int]:
50+
row = self._conn.execute(sqlalchemy.text(COUNT_BARS), {}).first()
51+
if row is None:
52+
return None
53+
return row[0]
54+
3855
def delete_bar_by_id(self, arg: DeleteBarByIDParams) -> int:
3956
result = self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": arg.id})
4057
return result.rowcount
@@ -48,6 +65,12 @@ class AsyncQuerier:
4865
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection):
4966
self._conn = conn
5067

68+
async def count_bars(self, arg: CountBarsParams) -> Optional[int]:
69+
row = (await self._conn.execute(sqlalchemy.text(COUNT_BARS), {})).first()
70+
if row is None:
71+
return None
72+
return row[0]
73+
5174
async def delete_bar_by_id(self, arg: DeleteBarByIDParams) -> int:
5275
result = await self._conn.execute(sqlalchemy.text(DELETE_BAR_BY_ID), {"p1": arg.id})
5376
return result.rowcount

internal/endtoend/testdata/query_parameter_limit_zero/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ DELETE FROM bar WHERE id = $1;
33

44
-- name: DeleteBarByIDAndName :execrows
55
DELETE FROM bar WHERE id = $1 AND name = $2;
6+
7+
-- name: CountBars :one
8+
SELECT count(*) FROM bar;

internal/printer/printer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) {
238238
w.print(")")
239239
}
240240
w.print(":\n")
241+
if len(cd.Body) == 0 {
242+
w.printIndent(indent + 1)
243+
w.print("pass\n")
244+
return
245+
}
241246
for i, node := range cd.Body {
242247
if i != 0 {
243248
if _, ok := node.Node.(*ast.Node_FunctionDef); ok {

internal/printer/printer_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ func TestPrinter(t *testing.T) {
6868
},
6969
},
7070
},
71-
Expected: `class Foo(str, enum.Enum):`,
71+
Expected: `
72+
class Foo(str, enum.Enum):
73+
pass
74+
`,
7275
},
7376
"dataclass": {
7477
Node: &ast.Node{
@@ -125,6 +128,29 @@ func TestPrinter(t *testing.T) {
125128
class Foo:
126129
bar: int
127130
bat: Optional[int]
131+
`,
132+
},
133+
"empty-dataclass": {
134+
Node: &ast.Node{
135+
Node: &ast.Node_ClassDef{
136+
ClassDef: &ast.ClassDef{
137+
Name: "Foo",
138+
DecoratorList: []*ast.Node{
139+
{
140+
Node: &ast.Node_Name{
141+
Name: &ast.Name{
142+
Id: "dataclass",
143+
},
144+
},
145+
},
146+
},
147+
},
148+
},
149+
},
150+
Expected: `
151+
@dataclass
152+
class Foo:
153+
pass
128154
`,
129155
},
130156
"call": {

0 commit comments

Comments
 (0)