From 46ad1ff8e1a74fb26b68286d23f0f74d33b7b709 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Tue, 7 Dec 2021 20:49:08 -0800 Subject: [PATCH 01/12] First pass at AST-based codegen --- Makefile | 7 + internal/python/ast/ast.pb.go | 542 ++++++++++++++++++++++++++++++++++ internal/python/dump.py | 4 + internal/python/out.py | 19 ++ protos/python/ast.proto | 42 +++ 5 files changed, 614 insertions(+) create mode 100644 internal/python/ast/ast.pb.go create mode 100644 internal/python/dump.py create mode 100644 internal/python/out.py create mode 100644 protos/python/ast.proto diff --git a/Makefile b/Makefile index 692b35514d..a7050ac189 100644 --- a/Makefile +++ b/Makefile @@ -26,3 +26,10 @@ psql: mysqlsh: mysqlsh --sql --user root --password mysecretpassword --database dinotest 127.0.0.1:3306 + + +# $ protoc --version +# libprotoc 3.17.3 +# go install google.golang.org/protobuf/cmd/protoc-gen-go@latest +proto: + protoc -I ./protos --go_out=. --go_opt=module=github.com/kyleconroy/sqlc ./protos/**/*.proto diff --git a/internal/python/ast/ast.pb.go b/internal/python/ast/ast.pb.go new file mode 100644 index 0000000000..0cdbe4ff58 --- /dev/null +++ b/internal/python/ast/ast.pb.go @@ -0,0 +1,542 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.17.3 +// source: python/ast.proto + +package ast + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Node struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // Types that are assignable to Node: + // *Node_ClassDef + // *Node_Import + // *Node_ImportFrom + // *Node_Module + Node isNode_Node `protobuf_oneof:"node"` +} + +func (x *Node) Reset() { + *x = Node{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Node) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Node) ProtoMessage() {} + +func (x *Node) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Node.ProtoReflect.Descriptor instead. +func (*Node) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{0} +} + +func (m *Node) GetNode() isNode_Node { + if m != nil { + return m.Node + } + return nil +} + +func (x *Node) GetClassDef() *ClassDef { + if x, ok := x.GetNode().(*Node_ClassDef); ok { + return x.ClassDef + } + return nil +} + +func (x *Node) GetImport() *Import { + if x, ok := x.GetNode().(*Node_Import); ok { + return x.Import + } + return nil +} + +func (x *Node) GetImportFrom() *ImportFrom { + if x, ok := x.GetNode().(*Node_ImportFrom); ok { + return x.ImportFrom + } + return nil +} + +func (x *Node) GetModule() *Module { + if x, ok := x.GetNode().(*Node_Module); ok { + return x.Module + } + return nil +} + +type isNode_Node interface { + isNode_Node() +} + +type Node_ClassDef struct { + ClassDef *ClassDef `protobuf:"bytes,1,opt,name=class_def,json=ClassDef,proto3,oneof"` +} + +type Node_Import struct { + Import *Import `protobuf:"bytes,2,opt,name=import,json=Import,proto3,oneof"` +} + +type Node_ImportFrom struct { + ImportFrom *ImportFrom `protobuf:"bytes,3,opt,name=import_from,json=ImportFrom,proto3,oneof"` +} + +type Node_Module struct { + Module *Module `protobuf:"bytes,4,opt,name=module,json=Module,proto3,oneof"` +} + +func (*Node_ClassDef) isNode_Node() {} + +func (*Node_Import) isNode_Node() {} + +func (*Node_ImportFrom) isNode_Node() {} + +func (*Node_Module) isNode_Node() {} + +type ClassDef struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Bases []*Node `protobuf:"bytes,2,rep,name=bases,proto3" json:"bases,omitempty"` + Keywords []*Node `protobuf:"bytes,3,rep,name=keywords,proto3" json:"keywords,omitempty"` + Body []*Node `protobuf:"bytes,4,rep,name=body,proto3" json:"body,omitempty"` + DecoratorList []*Node `protobuf:"bytes,5,rep,name=decorator_list,proto3" json:"decorator_list,omitempty"` +} + +func (x *ClassDef) Reset() { + *x = ClassDef{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ClassDef) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ClassDef) ProtoMessage() {} + +func (x *ClassDef) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ClassDef.ProtoReflect.Descriptor instead. +func (*ClassDef) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{1} +} + +func (x *ClassDef) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *ClassDef) GetBases() []*Node { + if x != nil { + return x.Bases + } + return nil +} + +func (x *ClassDef) GetKeywords() []*Node { + if x != nil { + return x.Keywords + } + return nil +} + +func (x *ClassDef) GetBody() []*Node { + if x != nil { + return x.Body + } + return nil +} + +func (x *ClassDef) GetDecoratorList() []*Node { + if x != nil { + return x.DecoratorList + } + return nil +} + +type Import struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Names []*Node `protobuf:"bytes,1,rep,name=names,proto3" json:"names,omitempty"` +} + +func (x *Import) Reset() { + *x = Import{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Import) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Import) ProtoMessage() {} + +func (x *Import) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Import.ProtoReflect.Descriptor instead. +func (*Import) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{2} +} + +func (x *Import) GetNames() []*Node { + if x != nil { + return x.Names + } + return nil +} + +type ImportFrom struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Module string `protobuf:"bytes,1,opt,name=module,proto3" json:"module,omitempty"` + Names []*Node `protobuf:"bytes,2,rep,name=names,proto3" json:"names,omitempty"` + Level int32 `protobuf:"varint,3,opt,name=level,proto3" json:"level,omitempty"` +} + +func (x *ImportFrom) Reset() { + *x = ImportFrom{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ImportFrom) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ImportFrom) ProtoMessage() {} + +func (x *ImportFrom) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ImportFrom.ProtoReflect.Descriptor instead. +func (*ImportFrom) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{3} +} + +func (x *ImportFrom) GetModule() string { + if x != nil { + return x.Module + } + return "" +} + +func (x *ImportFrom) GetNames() []*Node { + if x != nil { + return x.Names + } + return nil +} + +func (x *ImportFrom) GetLevel() int32 { + if x != nil { + return x.Level + } + return 0 +} + +type Module struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Body []*Node `protobuf:"bytes,1,rep,name=body,proto3" json:"body,omitempty"` +} + +func (x *Module) Reset() { + *x = Module{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Module) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Module) ProtoMessage() {} + +func (x *Module) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Module.ProtoReflect.Descriptor instead. +func (*Module) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{4} +} + +func (x *Module) GetBody() []*Node { + if x != nil { + return x.Body + } + return nil +} + +var File_python_ast_proto protoreflect.FileDescriptor + +var file_python_ast_proto_rawDesc = []byte{ + 0x0a, 0x10, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xbe, 0x01, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, + 0x12, 0x2c, 0x0a, 0x09, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, 0x64, 0x65, 0x66, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, + 0x65, 0x66, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x25, + 0x0a, 0x06, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x48, 0x00, 0x52, 0x06, 0x49, + 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x32, 0x0a, 0x0b, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x5f, + 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x61, 0x73, 0x74, + 0x2e, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x48, 0x00, 0x52, 0x0a, 0x49, + 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x25, 0x0a, 0x06, 0x6d, 0x6f, 0x64, + 0x75, 0x6c, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x48, 0x00, 0x52, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, + 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, + 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, + 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, + 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, + 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, + 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, + 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, + 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, + 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, + 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, + 0x69, 0x73, 0x74, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, + 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, + 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, + 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, + 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, + 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, + 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, + 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, + 0x62, 0x6f, 0x64, 0x79, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, + 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, + 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_python_ast_proto_rawDescOnce sync.Once + file_python_ast_proto_rawDescData = file_python_ast_proto_rawDesc +) + +func file_python_ast_proto_rawDescGZIP() []byte { + file_python_ast_proto_rawDescOnce.Do(func() { + file_python_ast_proto_rawDescData = protoimpl.X.CompressGZIP(file_python_ast_proto_rawDescData) + }) + return file_python_ast_proto_rawDescData +} + +var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_python_ast_proto_goTypes = []interface{}{ + (*Node)(nil), // 0: ast.Node + (*ClassDef)(nil), // 1: ast.ClassDef + (*Import)(nil), // 2: ast.Import + (*ImportFrom)(nil), // 3: ast.ImportFrom + (*Module)(nil), // 4: ast.Module +} +var file_python_ast_proto_depIdxs = []int32{ + 1, // 0: ast.Node.class_def:type_name -> ast.ClassDef + 2, // 1: ast.Node.import:type_name -> ast.Import + 3, // 2: ast.Node.import_from:type_name -> ast.ImportFrom + 4, // 3: ast.Node.module:type_name -> ast.Module + 0, // 4: ast.ClassDef.bases:type_name -> ast.Node + 0, // 5: ast.ClassDef.keywords:type_name -> ast.Node + 0, // 6: ast.ClassDef.body:type_name -> ast.Node + 0, // 7: ast.ClassDef.decorator_list:type_name -> ast.Node + 0, // 8: ast.Import.names:type_name -> ast.Node + 0, // 9: ast.ImportFrom.names:type_name -> ast.Node + 0, // 10: ast.Module.body:type_name -> ast.Node + 11, // [11:11] is the sub-list for method output_type + 11, // [11:11] is the sub-list for method input_type + 11, // [11:11] is the sub-list for extension type_name + 11, // [11:11] is the sub-list for extension extendee + 0, // [0:11] is the sub-list for field type_name +} + +func init() { file_python_ast_proto_init() } +func file_python_ast_proto_init() { + if File_python_ast_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_python_ast_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Node); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ClassDef); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Import); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ImportFrom); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Module); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_python_ast_proto_msgTypes[0].OneofWrappers = []interface{}{ + (*Node_ClassDef)(nil), + (*Node_Import)(nil), + (*Node_ImportFrom)(nil), + (*Node_Module)(nil), + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_python_ast_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_python_ast_proto_goTypes, + DependencyIndexes: file_python_ast_proto_depIdxs, + MessageInfos: file_python_ast_proto_msgTypes, + }.Build() + File_python_ast_proto = out.File + file_python_ast_proto_rawDesc = nil + file_python_ast_proto_goTypes = nil + file_python_ast_proto_depIdxs = nil +} diff --git a/internal/python/dump.py b/internal/python/dump.py new file mode 100644 index 0000000000..b6f6e5bf7a --- /dev/null +++ b/internal/python/dump.py @@ -0,0 +1,4 @@ +import ast + +print(ast.dump(ast.parse(open('out.py').read(), filename='out.py', type_comments=True), + indent=4)) diff --git a/internal/python/out.py b/internal/python/out.py new file mode 100644 index 0000000000..dfa9e5aa64 --- /dev/null +++ b/internal/python/out.py @@ -0,0 +1,19 @@ +import dataclasses +from typing import Optional + +# Comment +# Longer Comment + +@dataclasses.dataclass() +class InventoryItem: + """Class for keeping track of an item in inventory.""" + name: Optional[str] = None + unit_price: Optional[float] = None + quantity_on_hand: Optional[int] = None + + def total_cost(self) -> float: + return self.unit_price * self.quantity_on_hand + +if __name__ == "__main__": + print(InventoryItem()) + diff --git a/protos/python/ast.proto b/protos/python/ast.proto new file mode 100644 index 0000000000..46002a3810 --- /dev/null +++ b/protos/python/ast.proto @@ -0,0 +1,42 @@ +syntax = "proto3"; + +package ast; + +option go_package = "github.com/kyleconroy/sqlc/internal/python/ast"; + +message Node { + oneof node { + ClassDef class_def = 1 [json_name="ClassDef"]; + Import import = 2 [json_name="Import"]; + ImportFrom import_from = 3 [json_name="ImportFrom"]; + Module module = 4 [json_name="Module"]; + } +} + +message ClassDef +{ + string name = 1 [json_name="name"]; + repeated Node bases = 2 [json_name="bases"]; + repeated Node keywords = 3 [json_name="keywords"]; + repeated Node body = 4 [json_name="body"]; + repeated Node decorator_list = 5 [json_name="decorator_list"]; +} + +message Import +{ + repeated Node names = 1 [json_name="names"]; +} + +message ImportFrom +{ + string module = 1 [json_name="module"]; + repeated Node names = 2 [json_name="names"]; + int32 level = 3 [json_name="level"]; +} + +message Module +{ + repeated Node body = 1 [json_name="body"]; +} + + From ca5279a60910c02befd16bc4e7ff82ccf56f9d1b Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 8 Dec 2021 09:35:03 -0800 Subject: [PATCH 02/12] Add Alias ast --- internal/python/ast/ast.pb.go | 210 ++++++++++++++++-------- internal/python/printer/printer.go | 73 ++++++++ internal/python/printer/printer_test.go | 40 +++++ protos/python/ast.proto | 6 + 4 files changed, 264 insertions(+), 65 deletions(-) create mode 100644 internal/python/printer/printer.go create mode 100644 internal/python/printer/printer_test.go diff --git a/internal/python/ast/ast.pb.go b/internal/python/ast/ast.pb.go index 0cdbe4ff58..fe6833cb69 100644 --- a/internal/python/ast/ast.pb.go +++ b/internal/python/ast/ast.pb.go @@ -30,6 +30,7 @@ type Node struct { // *Node_Import // *Node_ImportFrom // *Node_Module + // *Node_Alias Node isNode_Node `protobuf_oneof:"node"` } @@ -100,6 +101,13 @@ func (x *Node) GetModule() *Module { return nil } +func (x *Node) GetAlias() *Alias { + if x, ok := x.GetNode().(*Node_Alias); ok { + return x.Alias + } + return nil +} + type isNode_Node interface { isNode_Node() } @@ -120,6 +128,10 @@ type Node_Module struct { Module *Module `protobuf:"bytes,4,opt,name=module,json=Module,proto3,oneof"` } +type Node_Alias struct { + Alias *Alias `protobuf:"bytes,5,opt,name=alias,json=Alias,proto3,oneof"` +} + func (*Node_ClassDef) isNode_Node() {} func (*Node_Import) isNode_Node() {} @@ -128,6 +140,55 @@ func (*Node_ImportFrom) isNode_Node() {} func (*Node_Module) isNode_Node() {} +func (*Node_Alias) isNode_Node() {} + +type Alias struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *Alias) Reset() { + *x = Alias{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Alias) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Alias) ProtoMessage() {} + +func (x *Alias) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Alias.ProtoReflect.Descriptor instead. +func (*Alias) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{1} +} + +func (x *Alias) GetName() string { + if x != nil { + return x.Name + } + return "" +} + type ClassDef struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -143,7 +204,7 @@ type ClassDef struct { func (x *ClassDef) Reset() { *x = ClassDef{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[1] + mi := &file_python_ast_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -156,7 +217,7 @@ func (x *ClassDef) String() string { func (*ClassDef) ProtoMessage() {} func (x *ClassDef) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[1] + mi := &file_python_ast_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -169,7 +230,7 @@ func (x *ClassDef) ProtoReflect() protoreflect.Message { // Deprecated: Use ClassDef.ProtoReflect.Descriptor instead. func (*ClassDef) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{1} + return file_python_ast_proto_rawDescGZIP(), []int{2} } func (x *ClassDef) GetName() string { @@ -218,7 +279,7 @@ type Import struct { func (x *Import) Reset() { *x = Import{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[2] + mi := &file_python_ast_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -231,7 +292,7 @@ func (x *Import) String() string { func (*Import) ProtoMessage() {} func (x *Import) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[2] + mi := &file_python_ast_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -244,7 +305,7 @@ func (x *Import) ProtoReflect() protoreflect.Message { // Deprecated: Use Import.ProtoReflect.Descriptor instead. func (*Import) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{2} + return file_python_ast_proto_rawDescGZIP(), []int{3} } func (x *Import) GetNames() []*Node { @@ -267,7 +328,7 @@ type ImportFrom struct { func (x *ImportFrom) Reset() { *x = ImportFrom{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[3] + mi := &file_python_ast_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -280,7 +341,7 @@ func (x *ImportFrom) String() string { func (*ImportFrom) ProtoMessage() {} func (x *ImportFrom) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[3] + mi := &file_python_ast_proto_msgTypes[4] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -293,7 +354,7 @@ func (x *ImportFrom) ProtoReflect() protoreflect.Message { // Deprecated: Use ImportFrom.ProtoReflect.Descriptor instead. func (*ImportFrom) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{3} + return file_python_ast_proto_rawDescGZIP(), []int{4} } func (x *ImportFrom) GetModule() string { @@ -328,7 +389,7 @@ type Module struct { func (x *Module) Reset() { *x = Module{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[4] + mi := &file_python_ast_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -341,7 +402,7 @@ func (x *Module) String() string { func (*Module) ProtoMessage() {} func (x *Module) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[4] + mi := &file_python_ast_proto_msgTypes[5] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -354,7 +415,7 @@ func (x *Module) ProtoReflect() protoreflect.Message { // Deprecated: Use Module.ProtoReflect.Descriptor instead. func (*Module) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{4} + return file_python_ast_proto_rawDescGZIP(), []int{5} } func (x *Module) GetBody() []*Node { @@ -368,7 +429,7 @@ var File_python_ast_proto protoreflect.FileDescriptor var file_python_ast_proto_rawDesc = []byte{ 0x0a, 0x10, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xbe, 0x01, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, + 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xe2, 0x01, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x2c, 0x0a, 0x09, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, 0x64, 0x65, 0x66, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x25, @@ -380,33 +441,37 @@ var file_python_ast_proto_rawDesc = []byte{ 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x25, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x48, 0x00, 0x52, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, - 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, - 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, - 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, - 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, - 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, - 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, - 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, - 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, - 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, - 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, - 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, - 0x69, 0x73, 0x74, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, - 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, - 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, - 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, - 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, - 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, - 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, - 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, - 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, - 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, - 0x62, 0x6f, 0x64, 0x79, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, - 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, - 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x12, 0x22, 0x0a, 0x05, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x0a, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x48, 0x00, 0x52, 0x05, 0x41, + 0x6c, 0x69, 0x61, 0x73, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0x1b, 0x0a, 0x05, + 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, + 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x62, 0x61, + 0x73, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x08, 0x6b, + 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, + 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, + 0x64, 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, + 0x79, 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, + 0x69, 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, + 0x6c, 0x69, 0x73, 0x74, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, + 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, + 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, + 0x5b, 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, + 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, + 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, + 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, + 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, + 0x04, 0x62, 0x6f, 0x64, 0x79, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, + 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, + 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -421,31 +486,33 @@ func file_python_ast_proto_rawDescGZIP() []byte { return file_python_ast_proto_rawDescData } -var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_python_ast_proto_goTypes = []interface{}{ (*Node)(nil), // 0: ast.Node - (*ClassDef)(nil), // 1: ast.ClassDef - (*Import)(nil), // 2: ast.Import - (*ImportFrom)(nil), // 3: ast.ImportFrom - (*Module)(nil), // 4: ast.Module + (*Alias)(nil), // 1: ast.Alias + (*ClassDef)(nil), // 2: ast.ClassDef + (*Import)(nil), // 3: ast.Import + (*ImportFrom)(nil), // 4: ast.ImportFrom + (*Module)(nil), // 5: ast.Module } var file_python_ast_proto_depIdxs = []int32{ - 1, // 0: ast.Node.class_def:type_name -> ast.ClassDef - 2, // 1: ast.Node.import:type_name -> ast.Import - 3, // 2: ast.Node.import_from:type_name -> ast.ImportFrom - 4, // 3: ast.Node.module:type_name -> ast.Module - 0, // 4: ast.ClassDef.bases:type_name -> ast.Node - 0, // 5: ast.ClassDef.keywords:type_name -> ast.Node - 0, // 6: ast.ClassDef.body:type_name -> ast.Node - 0, // 7: ast.ClassDef.decorator_list:type_name -> ast.Node - 0, // 8: ast.Import.names:type_name -> ast.Node - 0, // 9: ast.ImportFrom.names:type_name -> ast.Node - 0, // 10: ast.Module.body:type_name -> ast.Node - 11, // [11:11] is the sub-list for method output_type - 11, // [11:11] is the sub-list for method input_type - 11, // [11:11] is the sub-list for extension type_name - 11, // [11:11] is the sub-list for extension extendee - 0, // [0:11] is the sub-list for field type_name + 2, // 0: ast.Node.class_def:type_name -> ast.ClassDef + 3, // 1: ast.Node.import:type_name -> ast.Import + 4, // 2: ast.Node.import_from:type_name -> ast.ImportFrom + 5, // 3: ast.Node.module:type_name -> ast.Module + 1, // 4: ast.Node.alias:type_name -> ast.Alias + 0, // 5: ast.ClassDef.bases:type_name -> ast.Node + 0, // 6: ast.ClassDef.keywords:type_name -> ast.Node + 0, // 7: ast.ClassDef.body:type_name -> ast.Node + 0, // 8: ast.ClassDef.decorator_list:type_name -> ast.Node + 0, // 9: ast.Import.names:type_name -> ast.Node + 0, // 10: ast.ImportFrom.names:type_name -> ast.Node + 0, // 11: ast.Module.body:type_name -> ast.Node + 12, // [12:12] is the sub-list for method output_type + 12, // [12:12] is the sub-list for method input_type + 12, // [12:12] is the sub-list for extension type_name + 12, // [12:12] is the sub-list for extension extendee + 0, // [0:12] is the sub-list for field type_name } func init() { file_python_ast_proto_init() } @@ -467,7 +534,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ClassDef); i { + switch v := v.(*Alias); i { case 0: return &v.state case 1: @@ -479,7 +546,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Import); i { + switch v := v.(*ClassDef); i { case 0: return &v.state case 1: @@ -491,7 +558,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ImportFrom); i { + switch v := v.(*Import); i { case 0: return &v.state case 1: @@ -503,6 +570,18 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ImportFrom); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Module); i { case 0: return &v.state @@ -520,6 +599,7 @@ func file_python_ast_proto_init() { (*Node_Import)(nil), (*Node_ImportFrom)(nil), (*Node_Module)(nil), + (*Node_Alias)(nil), } type x struct{} out := protoimpl.TypeBuilder{ @@ -527,7 +607,7 @@ func file_python_ast_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_python_ast_proto_rawDesc, NumEnums: 0, - NumMessages: 5, + NumMessages: 6, NumExtensions: 0, NumServices: 0, }, diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go new file mode 100644 index 0000000000..9a94bb2de7 --- /dev/null +++ b/internal/python/printer/printer.go @@ -0,0 +1,73 @@ +package printer + +import "github.com/kyleconroy/sqlc/internal/python/ast" + +type writer struct { + options Options + src []byte +} + +type Options struct { +} + +type PrintResult struct { + Code []byte +} + +func Print(node *ast.Node, options Options) PrintResult { + w := writer{options: options} + w.printNode(node, 0) + return PrintResult{ + Code: w.src, + } +} + +func (w *writer) print(text string) { + w.src = append(w.src, text...) +} + +func (w *writer) printNode(node *ast.Node, indent int32) { + switch n := node.Node.(type) { + + case *ast.Node_Alias: + w.print(n.Alias.Name) + + case *ast.Node_ClassDef: + w.printClassDef(n.ClassDef, indent) + + case *ast.Node_Import: + w.printImport(n.Import, indent) + + case *ast.Node_ImportFrom: + // w.printImport(n.Import, indent) + + case *ast.Node_Module: + w.printModule(n.Module, indent) + + } +} + +func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) { + w.print("\n\n") + w.print("class ") + w.print(cd.Name) + w.print(":\n") + w.print(" pass\n") +} + +func (w *writer) printImport(imp *ast.Import, indent int32) { + w.print("import ") + for i, node := range imp.Names { + w.printNode(node, indent) + if i != len(imp.Names)-1 { + w.print(", ") + } + } + w.print("\n") +} + +func (w *writer) printModule(mod *ast.Module, indent int32) { + for _, node := range mod.Body { + w.printNode(node, indent) + } +} diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go new file mode 100644 index 0000000000..f844909c38 --- /dev/null +++ b/internal/python/printer/printer_test.go @@ -0,0 +1,40 @@ +package printer + +import ( + "testing" + + "github.com/kyleconroy/sqlc/internal/python/ast" +) + +func TestPrinter(t *testing.T) { + node := &ast.Node{ + Node: &ast.Node_Module{ + Module: &ast.Module{ + Body: []*ast.Node{ + { + Node: &ast.Node_Import{ + Import: &ast.Import{ + Names: []*ast.Node{ + { + Node: &ast.Node_Alias{ + Alias: &ast.Alias{ + Name: "foo", + }, + }, + }, + }, + }, + }, + }, + { + Node: &ast.Node_ClassDef{ + ClassDef: &ast.ClassDef{Name: "Foo"}, + }, + }, + }, + }, + }, + } + result := Print(node, Options{}) + t.Log(string(result.Code)) +} diff --git a/protos/python/ast.proto b/protos/python/ast.proto index 46002a3810..0e56e37f51 100644 --- a/protos/python/ast.proto +++ b/protos/python/ast.proto @@ -10,9 +10,15 @@ message Node { Import import = 2 [json_name="Import"]; ImportFrom import_from = 3 [json_name="ImportFrom"]; Module module = 4 [json_name="Module"]; + Alias alias = 5 [json_name="Alias"]; } } +message Alias +{ + string name = 1 [json_name="name"]; +} + message ClassDef { string name = 1 [json_name="name"]; From 55c8f2be592ff76014407725b2f8af98c84fd68d Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 8 Dec 2021 21:24:04 -0800 Subject: [PATCH 03/12] Add more Python ast nodes --- internal/python/ast/ast.pb.go | 409 ++++++++++++++++++++---- internal/python/dump.py | 7 +- internal/python/printer/printer.go | 15 +- internal/python/printer/printer_test.go | 62 ++-- protos/python/ast.proto | 19 ++ 5 files changed, 423 insertions(+), 89 deletions(-) diff --git a/internal/python/ast/ast.pb.go b/internal/python/ast/ast.pb.go index fe6833cb69..41544908cc 100644 --- a/internal/python/ast/ast.pb.go +++ b/internal/python/ast/ast.pb.go @@ -31,6 +31,9 @@ type Node struct { // *Node_ImportFrom // *Node_Module // *Node_Alias + // *Node_AnnAssign + // *Node_Name + // *Node_Subscript Node isNode_Node `protobuf_oneof:"node"` } @@ -108,6 +111,27 @@ func (x *Node) GetAlias() *Alias { return nil } +func (x *Node) GetAnnAssign() *AnnAssign { + if x, ok := x.GetNode().(*Node_AnnAssign); ok { + return x.AnnAssign + } + return nil +} + +func (x *Node) GetName() *Name { + if x, ok := x.GetNode().(*Node_Name); ok { + return x.Name + } + return nil +} + +func (x *Node) GetSubscript() *Subscript { + if x, ok := x.GetNode().(*Node_Subscript); ok { + return x.Subscript + } + return nil +} + type isNode_Node interface { isNode_Node() } @@ -132,6 +156,18 @@ type Node_Alias struct { Alias *Alias `protobuf:"bytes,5,opt,name=alias,json=Alias,proto3,oneof"` } +type Node_AnnAssign struct { + AnnAssign *AnnAssign `protobuf:"bytes,6,opt,name=ann_assign,json=AnnAssign,proto3,oneof"` +} + +type Node_Name struct { + Name *Name `protobuf:"bytes,7,opt,name=name,json=Name,proto3,oneof"` +} + +type Node_Subscript struct { + Subscript *Subscript `protobuf:"bytes,8,opt,name=subscript,json=Subscript,proto3,oneof"` +} + func (*Node_ClassDef) isNode_Node() {} func (*Node_Import) isNode_Node() {} @@ -142,6 +178,12 @@ func (*Node_Module) isNode_Node() {} func (*Node_Alias) isNode_Node() {} +func (*Node_AnnAssign) isNode_Node() {} + +func (*Node_Name) isNode_Node() {} + +func (*Node_Subscript) isNode_Node() {} + type Alias struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -189,6 +231,69 @@ func (x *Alias) GetName() string { return "" } +type AnnAssign struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Target *Name `protobuf:"bytes,1,opt,name=target,proto3" json:"target,omitempty"` + Annotation *Node `protobuf:"bytes,2,opt,name=annotation,proto3" json:"annotation,omitempty"` + Simple int32 `protobuf:"varint,3,opt,name=simple,proto3" json:"simple,omitempty"` +} + +func (x *AnnAssign) Reset() { + *x = AnnAssign{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AnnAssign) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AnnAssign) ProtoMessage() {} + +func (x *AnnAssign) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AnnAssign.ProtoReflect.Descriptor instead. +func (*AnnAssign) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{2} +} + +func (x *AnnAssign) GetTarget() *Name { + if x != nil { + return x.Target + } + return nil +} + +func (x *AnnAssign) GetAnnotation() *Node { + if x != nil { + return x.Annotation + } + return nil +} + +func (x *AnnAssign) GetSimple() int32 { + if x != nil { + return x.Simple + } + return 0 +} + type ClassDef struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -204,7 +309,7 @@ type ClassDef struct { func (x *ClassDef) Reset() { *x = ClassDef{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[2] + mi := &file_python_ast_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -217,7 +322,7 @@ func (x *ClassDef) String() string { func (*ClassDef) ProtoMessage() {} func (x *ClassDef) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[2] + mi := &file_python_ast_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -230,7 +335,7 @@ func (x *ClassDef) ProtoReflect() protoreflect.Message { // Deprecated: Use ClassDef.ProtoReflect.Descriptor instead. func (*ClassDef) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{2} + return file_python_ast_proto_rawDescGZIP(), []int{3} } func (x *ClassDef) GetName() string { @@ -279,7 +384,7 @@ type Import struct { func (x *Import) Reset() { *x = Import{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[3] + mi := &file_python_ast_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -292,7 +397,7 @@ func (x *Import) String() string { func (*Import) ProtoMessage() {} func (x *Import) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[3] + mi := &file_python_ast_proto_msgTypes[4] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -305,7 +410,7 @@ func (x *Import) ProtoReflect() protoreflect.Message { // Deprecated: Use Import.ProtoReflect.Descriptor instead. func (*Import) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{3} + return file_python_ast_proto_rawDescGZIP(), []int{4} } func (x *Import) GetNames() []*Node { @@ -328,7 +433,7 @@ type ImportFrom struct { func (x *ImportFrom) Reset() { *x = ImportFrom{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[4] + mi := &file_python_ast_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -341,7 +446,7 @@ func (x *ImportFrom) String() string { func (*ImportFrom) ProtoMessage() {} func (x *ImportFrom) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[4] + mi := &file_python_ast_proto_msgTypes[5] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -354,7 +459,7 @@ func (x *ImportFrom) ProtoReflect() protoreflect.Message { // Deprecated: Use ImportFrom.ProtoReflect.Descriptor instead. func (*ImportFrom) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{4} + return file_python_ast_proto_rawDescGZIP(), []int{5} } func (x *ImportFrom) GetModule() string { @@ -389,7 +494,7 @@ type Module struct { func (x *Module) Reset() { *x = Module{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[5] + mi := &file_python_ast_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -402,7 +507,7 @@ func (x *Module) String() string { func (*Module) ProtoMessage() {} func (x *Module) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[5] + mi := &file_python_ast_proto_msgTypes[6] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -415,7 +520,7 @@ func (x *Module) ProtoReflect() protoreflect.Message { // Deprecated: Use Module.ProtoReflect.Descriptor instead. func (*Module) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{5} + return file_python_ast_proto_rawDescGZIP(), []int{6} } func (x *Module) GetBody() []*Node { @@ -425,11 +530,113 @@ func (x *Module) GetBody() []*Node { return nil } +type Name struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` +} + +func (x *Name) Reset() { + *x = Name{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Name) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Name) ProtoMessage() {} + +func (x *Name) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[7] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Name.ProtoReflect.Descriptor instead. +func (*Name) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{7} +} + +func (x *Name) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +type Subscript struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` + Slice string `protobuf:"bytes,2,opt,name=slice,proto3" json:"slice,omitempty"` +} + +func (x *Subscript) Reset() { + *x = Subscript{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[8] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Subscript) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Subscript) ProtoMessage() {} + +func (x *Subscript) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[8] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Subscript.ProtoReflect.Descriptor instead. +func (*Subscript) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{8} +} + +func (x *Subscript) GetValue() string { + if x != nil { + return x.Value + } + return "" +} + +func (x *Subscript) GetSlice() string { + if x != nil { + return x.Slice + } + return "" +} + var File_python_ast_proto protoreflect.FileDescriptor var file_python_ast_proto_rawDesc = []byte{ 0x0a, 0x10, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xe2, 0x01, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, + 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xe4, 0x02, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x2c, 0x0a, 0x09, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, 0x64, 0x65, 0x66, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x25, @@ -443,35 +650,56 @@ var file_python_ast_proto_rawDesc = []byte{ 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x48, 0x00, 0x52, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x22, 0x0a, 0x05, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0a, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x48, 0x00, 0x52, 0x05, 0x41, - 0x6c, 0x69, 0x61, 0x73, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0x1b, 0x0a, 0x05, - 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, - 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x62, 0x61, - 0x73, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, - 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x08, 0x6b, - 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, - 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, - 0x64, 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, - 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, - 0x79, 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, - 0x69, 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, - 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, - 0x6c, 0x69, 0x73, 0x74, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, - 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, - 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, - 0x5b, 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, - 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, - 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, - 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, - 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, - 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, - 0x04, 0x62, 0x6f, 0x64, 0x79, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, - 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, - 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, - 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6c, 0x69, 0x61, 0x73, 0x12, 0x2f, 0x0a, 0x0a, 0x61, 0x6e, 0x6e, 0x5f, 0x61, 0x73, 0x73, 0x69, + 0x67, 0x6e, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x41, + 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x48, 0x00, 0x52, 0x09, 0x41, 0x6e, 0x6e, 0x41, + 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x1f, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x07, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x48, 0x00, + 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x2e, 0x0a, 0x09, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, + 0x69, 0x70, 0x74, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x48, 0x00, 0x52, 0x09, 0x53, 0x75, 0x62, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0x1b, + 0x0a, 0x05, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x71, 0x0a, 0x09, 0x41, + 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x21, 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, + 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, + 0x61, 0x6d, 0x65, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x29, 0x0a, 0x0a, 0x61, + 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, + 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x22, 0xb8, + 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, + 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, + 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, + 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, + 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, + 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, + 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, + 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, + 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, + 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, + 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, + 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, + 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, + 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, + 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, 0x61, + 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, + 0x69, 0x64, 0x22, 0x37, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, + 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, + 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, + 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, + 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -486,33 +714,41 @@ func file_python_ast_proto_rawDescGZIP() []byte { return file_python_ast_proto_rawDescData } -var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 6) +var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 9) var file_python_ast_proto_goTypes = []interface{}{ (*Node)(nil), // 0: ast.Node (*Alias)(nil), // 1: ast.Alias - (*ClassDef)(nil), // 2: ast.ClassDef - (*Import)(nil), // 3: ast.Import - (*ImportFrom)(nil), // 4: ast.ImportFrom - (*Module)(nil), // 5: ast.Module + (*AnnAssign)(nil), // 2: ast.AnnAssign + (*ClassDef)(nil), // 3: ast.ClassDef + (*Import)(nil), // 4: ast.Import + (*ImportFrom)(nil), // 5: ast.ImportFrom + (*Module)(nil), // 6: ast.Module + (*Name)(nil), // 7: ast.Name + (*Subscript)(nil), // 8: ast.Subscript } var file_python_ast_proto_depIdxs = []int32{ - 2, // 0: ast.Node.class_def:type_name -> ast.ClassDef - 3, // 1: ast.Node.import:type_name -> ast.Import - 4, // 2: ast.Node.import_from:type_name -> ast.ImportFrom - 5, // 3: ast.Node.module:type_name -> ast.Module + 3, // 0: ast.Node.class_def:type_name -> ast.ClassDef + 4, // 1: ast.Node.import:type_name -> ast.Import + 5, // 2: ast.Node.import_from:type_name -> ast.ImportFrom + 6, // 3: ast.Node.module:type_name -> ast.Module 1, // 4: ast.Node.alias:type_name -> ast.Alias - 0, // 5: ast.ClassDef.bases:type_name -> ast.Node - 0, // 6: ast.ClassDef.keywords:type_name -> ast.Node - 0, // 7: ast.ClassDef.body:type_name -> ast.Node - 0, // 8: ast.ClassDef.decorator_list:type_name -> ast.Node - 0, // 9: ast.Import.names:type_name -> ast.Node - 0, // 10: ast.ImportFrom.names:type_name -> ast.Node - 0, // 11: ast.Module.body:type_name -> ast.Node - 12, // [12:12] is the sub-list for method output_type - 12, // [12:12] is the sub-list for method input_type - 12, // [12:12] is the sub-list for extension type_name - 12, // [12:12] is the sub-list for extension extendee - 0, // [0:12] is the sub-list for field type_name + 2, // 5: ast.Node.ann_assign:type_name -> ast.AnnAssign + 7, // 6: ast.Node.name:type_name -> ast.Name + 8, // 7: ast.Node.subscript:type_name -> ast.Subscript + 7, // 8: ast.AnnAssign.target:type_name -> ast.Name + 0, // 9: ast.AnnAssign.annotation:type_name -> ast.Node + 0, // 10: ast.ClassDef.bases:type_name -> ast.Node + 0, // 11: ast.ClassDef.keywords:type_name -> ast.Node + 0, // 12: ast.ClassDef.body:type_name -> ast.Node + 0, // 13: ast.ClassDef.decorator_list:type_name -> ast.Node + 0, // 14: ast.Import.names:type_name -> ast.Node + 0, // 15: ast.ImportFrom.names:type_name -> ast.Node + 0, // 16: ast.Module.body:type_name -> ast.Node + 17, // [17:17] is the sub-list for method output_type + 17, // [17:17] is the sub-list for method input_type + 17, // [17:17] is the sub-list for extension type_name + 17, // [17:17] is the sub-list for extension extendee + 0, // [0:17] is the sub-list for field type_name } func init() { file_python_ast_proto_init() } @@ -546,7 +782,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ClassDef); i { + switch v := v.(*AnnAssign); i { case 0: return &v.state case 1: @@ -558,7 +794,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Import); i { + switch v := v.(*ClassDef); i { case 0: return &v.state case 1: @@ -570,7 +806,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ImportFrom); i { + switch v := v.(*Import); i { case 0: return &v.state case 1: @@ -582,6 +818,18 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ImportFrom); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Module); i { case 0: return &v.state @@ -593,6 +841,30 @@ func file_python_ast_proto_init() { return nil } } + file_python_ast_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Name); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Subscript); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } file_python_ast_proto_msgTypes[0].OneofWrappers = []interface{}{ (*Node_ClassDef)(nil), @@ -600,6 +872,9 @@ func file_python_ast_proto_init() { (*Node_ImportFrom)(nil), (*Node_Module)(nil), (*Node_Alias)(nil), + (*Node_AnnAssign)(nil), + (*Node_Name)(nil), + (*Node_Subscript)(nil), } type x struct{} out := protoimpl.TypeBuilder{ @@ -607,7 +882,7 @@ func file_python_ast_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_python_ast_proto_rawDesc, NumEnums: 0, - NumMessages: 6, + NumMessages: 9, NumExtensions: 0, NumServices: 0, }, diff --git a/internal/python/dump.py b/internal/python/dump.py index b6f6e5bf7a..68726aef26 100644 --- a/internal/python/dump.py +++ b/internal/python/dump.py @@ -1,4 +1,9 @@ import ast +import argparse -print(ast.dump(ast.parse(open('out.py').read(), filename='out.py', type_comments=True), +parser = argparse.ArgumentParser(description='Process some integers.') +parser.add_argument('file', type=argparse.FileType('r', encoding='UTF-8'), help='file to parse') +args = parser.parse_args() + +print(ast.dump(ast.parse(args.file.read(), filename='out.py', type_comments=True), indent=4)) diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index 9a94bb2de7..540cd77931 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -39,7 +39,7 @@ func (w *writer) printNode(node *ast.Node, indent int32) { w.printImport(n.Import, indent) case *ast.Node_ImportFrom: - // w.printImport(n.Import, indent) + w.printImportFrom(n.ImportFrom, indent) case *ast.Node_Module: w.printModule(n.Module, indent) @@ -63,7 +63,18 @@ func (w *writer) printImport(imp *ast.Import, indent int32) { w.print(", ") } } - w.print("\n") +} + +func (w *writer) printImportFrom(imp *ast.ImportFrom, indent int32) { + w.print("from ") + w.print(imp.Module) + w.print(" import ") + for i, node := range imp.Names { + w.printNode(node, indent) + if i != len(imp.Names)-1 { + w.print(", ") + } + } } func (w *writer) printModule(mod *ast.Module, indent int32) { diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go index f844909c38..563c67afe2 100644 --- a/internal/python/printer/printer_test.go +++ b/internal/python/printer/printer_test.go @@ -3,38 +3,62 @@ package printer import ( "testing" + "github.com/google/go-cmp/cmp" + "github.com/kyleconroy/sqlc/internal/python/ast" ) +type testcase struct { + Node *ast.Node + Expected string +} + func TestPrinter(t *testing.T) { - node := &ast.Node{ - Node: &ast.Node_Module{ - Module: &ast.Module{ - Body: []*ast.Node{ - { - Node: &ast.Node_Import{ - Import: &ast.Import{ - Names: []*ast.Node{ - { - Node: &ast.Node_Alias{ - Alias: &ast.Alias{ - Name: "foo", - }, - }, + for name, tc := range map[string]testcase{ + "import": { + Node: &ast.Node{ + Node: &ast.Node_Import{ + Import: &ast.Import{ + Names: []*ast.Node{ + { + Node: &ast.Node_Alias{ + Alias: &ast.Alias{ + Name: "foo", }, }, }, }, }, - { - Node: &ast.Node_ClassDef{ - ClassDef: &ast.ClassDef{Name: "Foo"}, + }, + }, + Expected: `import foo`, + }, + "import-from": { + Node: &ast.Node{ + Node: &ast.Node_ImportFrom{ + ImportFrom: &ast.ImportFrom{ + Module: "dataclasses", + Names: []*ast.Node{ + { + Node: &ast.Node_Alias{ + Alias: &ast.Alias{ + Name: "dataclass", + }, + }, + }, }, }, }, }, + Expected: `from dataclasses import dataclass`, }, + } { + tc := tc + t.Run(name, func(t *testing.T) { + result := Print(tc.Node, Options{}) + if diff := cmp.Diff(tc.Expected, string(result.Code)); diff != "" { + t.Errorf("print mismatch (-want +got):\n%s", diff) + } + }) } - result := Print(node, Options{}) - t.Log(string(result.Code)) } diff --git a/protos/python/ast.proto b/protos/python/ast.proto index 0e56e37f51..1c181a081e 100644 --- a/protos/python/ast.proto +++ b/protos/python/ast.proto @@ -11,6 +11,9 @@ message Node { ImportFrom import_from = 3 [json_name="ImportFrom"]; Module module = 4 [json_name="Module"]; Alias alias = 5 [json_name="Alias"]; + AnnAssign ann_assign = 6 [json_name="AnnAssign"]; + Name name = 7 [json_name="Name"]; + Subscript subscript = 8 [json_name="Subscript"]; } } @@ -19,6 +22,13 @@ message Alias string name = 1 [json_name="name"]; } +message AnnAssign +{ + Name target = 1 [json_name="target"]; + Node annotation = 2 [json_name="annotation"]; + int32 simple = 3 [json_name="simple"]; +} + message ClassDef { string name = 1 [json_name="name"]; @@ -45,4 +55,13 @@ message Module repeated Node body = 1 [json_name="body"]; } +message Name +{ + string id = 1 [json_name="id"]; +} +message Subscript +{ + string value = 1 [json_name="value"]; + string slice = 2 [json_name="slice"]; +} From ac9afbfc47ba63631d997274643501df34d8e486 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Thu, 9 Dec 2021 09:26:36 -0800 Subject: [PATCH 04/12] Print class defs --- internal/python/printer/printer.go | 34 +++++++++++++++++++-- internal/python/printer/printer_test.go | 40 ++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index 540cd77931..7d9559b8b5 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -26,12 +26,21 @@ func (w *writer) print(text string) { w.src = append(w.src, text...) } +func (w *writer) printIndent(indent int32) { + for i, n := 0, int(indent); i < n; i++ { + w.src = append(w.src, " "...) + } +} + func (w *writer) printNode(node *ast.Node, indent int32) { switch n := node.Node.(type) { case *ast.Node_Alias: w.print(n.Alias.Name) + case *ast.Node_AnnAssign: + w.printAnnAssign(n.AnnAssign, indent) + case *ast.Node_ClassDef: w.printClassDef(n.ClassDef, indent) @@ -44,15 +53,35 @@ func (w *writer) printNode(node *ast.Node, indent int32) { case *ast.Node_Module: w.printModule(n.Module, indent) + case *ast.Node_Name: + w.print(n.Name.Id) + + default: + panic(n) + } } +func (w *writer) printAnnAssign(aa *ast.AnnAssign, indent int32) { + w.print(aa.Target.Id) + w.print(": ") + w.printNode(aa.Annotation, indent) +} + func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) { - w.print("\n\n") + for _, node := range cd.DecoratorList { + w.print("@") + w.printNode(node, indent) + w.print("\n") + } w.print("class ") w.print(cd.Name) w.print(":\n") - w.print(" pass\n") + for _, node := range cd.Body { + w.printIndent(indent + 1) + w.printNode(node, indent+1) + w.print("\n") + } } func (w *writer) printImport(imp *ast.Import, indent int32) { @@ -75,6 +104,7 @@ func (w *writer) printImportFrom(imp *ast.ImportFrom, indent int32) { w.print(", ") } } + w.print("\n") } func (w *writer) printModule(mod *ast.Module, indent int32) { diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go index 563c67afe2..7830ad5eaa 100644 --- a/internal/python/printer/printer_test.go +++ b/internal/python/printer/printer_test.go @@ -1,6 +1,7 @@ package printer import ( + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -15,6 +16,43 @@ type testcase struct { func TestPrinter(t *testing.T) { for name, tc := range map[string]testcase{ + "dataclass": { + Node: &ast.Node{ + Node: &ast.Node_ClassDef{ + ClassDef: &ast.ClassDef{ + Name: "Foo", + DecoratorList: []*ast.Node{ + { + Node: &ast.Node_Name{ + Name: &ast.Name{ + Id: "dataclass", + }, + }, + }, + }, + Body: []*ast.Node{ + { + Node: &ast.Node_AnnAssign{ + AnnAssign: &ast.AnnAssign{ + Target: &ast.Name{Id: "bar"}, + Annotation: &ast.Node{ + Node: &ast.Node_Name{ + Name: &ast.Name{Id: "int"}, + }, + }, + }, + }, + }, + }, + }, + }, + }, + Expected: ` +@dataclass +class Foo: + bar: int +`, + }, "import": { Node: &ast.Node{ Node: &ast.Node_Import{ @@ -56,7 +94,7 @@ func TestPrinter(t *testing.T) { tc := tc t.Run(name, func(t *testing.T) { result := Print(tc.Node, Options{}) - if diff := cmp.Diff(tc.Expected, string(result.Code)); diff != "" { + if diff := cmp.Diff(strings.TrimSpace(tc.Expected), strings.TrimSpace(string(result.Code))); diff != "" { t.Errorf("print mismatch (-want +got):\n%s", diff) } }) From 77d9e169a96d1b5ddd7b6ba3c7ac5aa9c8a60f44 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Thu, 9 Dec 2021 09:46:03 -0800 Subject: [PATCH 05/12] Print type annontations --- internal/python/ast/ast.pb.go | 41 +++++++++++++------------ internal/python/printer/printer.go | 17 +++++++++- internal/python/printer/printer_test.go | 16 ++++++++++ protos/python/ast.proto | 4 +-- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/internal/python/ast/ast.pb.go b/internal/python/ast/ast.pb.go index 41544908cc..fdbf886a8a 100644 --- a/internal/python/ast/ast.pb.go +++ b/internal/python/ast/ast.pb.go @@ -582,8 +582,8 @@ type Subscript struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` - Slice string `protobuf:"bytes,2,opt,name=slice,proto3" json:"slice,omitempty"` + Value *Name `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` + Slice *Name `protobuf:"bytes,2,opt,name=slice,proto3" json:"slice,omitempty"` } func (x *Subscript) Reset() { @@ -618,18 +618,18 @@ func (*Subscript) Descriptor() ([]byte, []int) { return file_python_ast_proto_rawDescGZIP(), []int{8} } -func (x *Subscript) GetValue() string { +func (x *Subscript) GetValue() *Name { if x != nil { return x.Value } - return "" + return nil } -func (x *Subscript) GetSlice() string { +func (x *Subscript) GetSlice() *Name { if x != nil { return x.Slice } - return "" + return nil } var File_python_ast_proto protoreflect.FileDescriptor @@ -692,14 +692,15 @@ var file_python_ast_proto_rawDesc = []byte{ 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, - 0x69, 0x64, 0x22, 0x37, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, - 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, - 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, - 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, - 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, - 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x69, 0x64, 0x22, 0x4d, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, + 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x12, 0x1f, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x73, 0x6c, 0x69, 0x63, + 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, + 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, + 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -744,11 +745,13 @@ var file_python_ast_proto_depIdxs = []int32{ 0, // 14: ast.Import.names:type_name -> ast.Node 0, // 15: ast.ImportFrom.names:type_name -> ast.Node 0, // 16: ast.Module.body:type_name -> ast.Node - 17, // [17:17] is the sub-list for method output_type - 17, // [17:17] is the sub-list for method input_type - 17, // [17:17] is the sub-list for extension type_name - 17, // [17:17] is the sub-list for extension extendee - 0, // [0:17] is the sub-list for field type_name + 7, // 17: ast.Subscript.value:type_name -> ast.Name + 7, // 18: ast.Subscript.slice:type_name -> ast.Name + 19, // [19:19] is the sub-list for method output_type + 19, // [19:19] is the sub-list for method input_type + 19, // [19:19] is the sub-list for extension type_name + 19, // [19:19] is the sub-list for extension extendee + 0, // [0:19] is the sub-list for field type_name } func init() { file_python_ast_proto_init() } diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index 7d9559b8b5..f71f5675fd 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -56,6 +56,9 @@ func (w *writer) printNode(node *ast.Node, indent int32) { case *ast.Node_Name: w.print(n.Name.Id) + case *ast.Node_Subscript: + w.printSubscript(n.Subscript, indent) + default: panic(n) @@ -63,7 +66,7 @@ func (w *writer) printNode(node *ast.Node, indent int32) { } func (w *writer) printAnnAssign(aa *ast.AnnAssign, indent int32) { - w.print(aa.Target.Id) + w.printName(aa.Target, indent) w.print(": ") w.printNode(aa.Annotation, indent) } @@ -112,3 +115,15 @@ func (w *writer) printModule(mod *ast.Module, indent int32) { w.printNode(node, indent) } } + +func (w *writer) printName(n *ast.Name, indent int32) { + w.print(n.Id) +} + +func (w *writer) printSubscript(ss *ast.Subscript, indent int32) { + w.printName(ss.Value, indent) + w.print("[") + w.printName(ss.Slice, indent) + w.print("]") + +} diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go index 7830ad5eaa..d19eb1795c 100644 --- a/internal/python/printer/printer_test.go +++ b/internal/python/printer/printer_test.go @@ -43,6 +43,21 @@ func TestPrinter(t *testing.T) { }, }, }, + { + Node: &ast.Node_AnnAssign{ + AnnAssign: &ast.AnnAssign{ + Target: &ast.Name{Id: "bat"}, + Annotation: &ast.Node{ + Node: &ast.Node_Subscript{ + Subscript: &ast.Subscript{ + Value: &ast.Name{Id: "Optional"}, + Slice: &ast.Name{Id: "int"}, + }, + }, + }, + }, + }, + }, }, }, }, @@ -51,6 +66,7 @@ func TestPrinter(t *testing.T) { @dataclass class Foo: bar: int + bat: Optional[int] `, }, "import": { diff --git a/protos/python/ast.proto b/protos/python/ast.proto index 1c181a081e..1a23472cfe 100644 --- a/protos/python/ast.proto +++ b/protos/python/ast.proto @@ -62,6 +62,6 @@ message Name message Subscript { - string value = 1 [json_name="value"]; - string slice = 2 [json_name="slice"]; + Name value = 1 [json_name="value"]; + Name slice = 2 [json_name="slice"]; } From a835e0aea0a241b585d8f36bff18145fe5f4f3b8 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Thu, 9 Dec 2021 10:11:48 -0800 Subject: [PATCH 06/12] Add more Python AST nodes --- internal/python/ast/ast.pb.go | 473 ++++++++++++++++++------ internal/python/out.py | 19 +- internal/python/printer/printer.go | 42 +++ internal/python/printer/printer_test.go | 44 +++ protos/python/ast.proto | 20 + 5 files changed, 477 insertions(+), 121 deletions(-) diff --git a/internal/python/ast/ast.pb.go b/internal/python/ast/ast.pb.go index fdbf886a8a..3b5d3501e8 100644 --- a/internal/python/ast/ast.pb.go +++ b/internal/python/ast/ast.pb.go @@ -34,6 +34,9 @@ type Node struct { // *Node_AnnAssign // *Node_Name // *Node_Subscript + // *Node_Attribute + // *Node_Constant + // *Node_Assign Node isNode_Node `protobuf_oneof:"node"` } @@ -132,6 +135,27 @@ func (x *Node) GetSubscript() *Subscript { return nil } +func (x *Node) GetAttribute() *Attribute { + if x, ok := x.GetNode().(*Node_Attribute); ok { + return x.Attribute + } + return nil +} + +func (x *Node) GetConstant() *Constant { + if x, ok := x.GetNode().(*Node_Constant); ok { + return x.Constant + } + return nil +} + +func (x *Node) GetAssign() *Assign { + if x, ok := x.GetNode().(*Node_Assign); ok { + return x.Assign + } + return nil +} + type isNode_Node interface { isNode_Node() } @@ -168,6 +192,18 @@ type Node_Subscript struct { Subscript *Subscript `protobuf:"bytes,8,opt,name=subscript,json=Subscript,proto3,oneof"` } +type Node_Attribute struct { + Attribute *Attribute `protobuf:"bytes,9,opt,name=attribute,json=Attribute,proto3,oneof"` +} + +type Node_Constant struct { + Constant *Constant `protobuf:"bytes,10,opt,name=constant,json=Constant,proto3,oneof"` +} + +type Node_Assign struct { + Assign *Assign `protobuf:"bytes,11,opt,name=assign,json=Assign,proto3,oneof"` +} + func (*Node_ClassDef) isNode_Node() {} func (*Node_Import) isNode_Node() {} @@ -184,6 +220,12 @@ func (*Node_Name) isNode_Node() {} func (*Node_Subscript) isNode_Node() {} +func (*Node_Attribute) isNode_Node() {} + +func (*Node_Constant) isNode_Node() {} + +func (*Node_Assign) isNode_Node() {} + type Alias struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -231,6 +273,61 @@ func (x *Alias) GetName() string { return "" } +type Attribute struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Value *Name `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` + Attr string `protobuf:"bytes,2,opt,name=attr,proto3" json:"attr,omitempty"` +} + +func (x *Attribute) Reset() { + *x = Attribute{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Attribute) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Attribute) ProtoMessage() {} + +func (x *Attribute) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Attribute.ProtoReflect.Descriptor instead. +func (*Attribute) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{2} +} + +func (x *Attribute) GetValue() *Name { + if x != nil { + return x.Value + } + return nil +} + +func (x *Attribute) GetAttr() string { + if x != nil { + return x.Attr + } + return "" +} + type AnnAssign struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -244,7 +341,7 @@ type AnnAssign struct { func (x *AnnAssign) Reset() { *x = AnnAssign{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[2] + mi := &file_python_ast_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -257,7 +354,7 @@ func (x *AnnAssign) String() string { func (*AnnAssign) ProtoMessage() {} func (x *AnnAssign) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[2] + mi := &file_python_ast_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -270,7 +367,7 @@ func (x *AnnAssign) ProtoReflect() protoreflect.Message { // Deprecated: Use AnnAssign.ProtoReflect.Descriptor instead. func (*AnnAssign) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{2} + return file_python_ast_proto_rawDescGZIP(), []int{3} } func (x *AnnAssign) GetTarget() *Name { @@ -294,6 +391,61 @@ func (x *AnnAssign) GetSimple() int32 { return 0 } +type Assign struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Targets []*Name `protobuf:"bytes,1,rep,name=targets,proto3" json:"targets,omitempty"` + Value *Node `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *Assign) Reset() { + *x = Assign{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Assign) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Assign) ProtoMessage() {} + +func (x *Assign) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Assign.ProtoReflect.Descriptor instead. +func (*Assign) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{4} +} + +func (x *Assign) GetTargets() []*Name { + if x != nil { + return x.Targets + } + return nil +} + +func (x *Assign) GetValue() *Node { + if x != nil { + return x.Value + } + return nil +} + type ClassDef struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -309,7 +461,7 @@ type ClassDef struct { func (x *ClassDef) Reset() { *x = ClassDef{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[3] + mi := &file_python_ast_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -322,7 +474,7 @@ func (x *ClassDef) String() string { func (*ClassDef) ProtoMessage() {} func (x *ClassDef) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[3] + mi := &file_python_ast_proto_msgTypes[5] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -335,7 +487,7 @@ func (x *ClassDef) ProtoReflect() protoreflect.Message { // Deprecated: Use ClassDef.ProtoReflect.Descriptor instead. func (*ClassDef) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{3} + return file_python_ast_proto_rawDescGZIP(), []int{5} } func (x *ClassDef) GetName() string { @@ -373,6 +525,53 @@ func (x *ClassDef) GetDecoratorList() []*Node { return nil } +type Constant struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *Constant) Reset() { + *x = Constant{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Constant) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Constant) ProtoMessage() {} + +func (x *Constant) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Constant.ProtoReflect.Descriptor instead. +func (*Constant) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{6} +} + +func (x *Constant) GetValue() string { + if x != nil { + return x.Value + } + return "" +} + type Import struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -384,7 +583,7 @@ type Import struct { func (x *Import) Reset() { *x = Import{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[4] + mi := &file_python_ast_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -397,7 +596,7 @@ func (x *Import) String() string { func (*Import) ProtoMessage() {} func (x *Import) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[4] + mi := &file_python_ast_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -410,7 +609,7 @@ func (x *Import) ProtoReflect() protoreflect.Message { // Deprecated: Use Import.ProtoReflect.Descriptor instead. func (*Import) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{4} + return file_python_ast_proto_rawDescGZIP(), []int{7} } func (x *Import) GetNames() []*Node { @@ -433,7 +632,7 @@ type ImportFrom struct { func (x *ImportFrom) Reset() { *x = ImportFrom{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[5] + mi := &file_python_ast_proto_msgTypes[8] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -446,7 +645,7 @@ func (x *ImportFrom) String() string { func (*ImportFrom) ProtoMessage() {} func (x *ImportFrom) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[5] + mi := &file_python_ast_proto_msgTypes[8] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -459,7 +658,7 @@ func (x *ImportFrom) ProtoReflect() protoreflect.Message { // Deprecated: Use ImportFrom.ProtoReflect.Descriptor instead. func (*ImportFrom) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{5} + return file_python_ast_proto_rawDescGZIP(), []int{8} } func (x *ImportFrom) GetModule() string { @@ -494,7 +693,7 @@ type Module struct { func (x *Module) Reset() { *x = Module{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[6] + mi := &file_python_ast_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -507,7 +706,7 @@ func (x *Module) String() string { func (*Module) ProtoMessage() {} func (x *Module) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[6] + mi := &file_python_ast_proto_msgTypes[9] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -520,7 +719,7 @@ func (x *Module) ProtoReflect() protoreflect.Message { // Deprecated: Use Module.ProtoReflect.Descriptor instead. func (*Module) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{6} + return file_python_ast_proto_rawDescGZIP(), []int{9} } func (x *Module) GetBody() []*Node { @@ -541,7 +740,7 @@ type Name struct { func (x *Name) Reset() { *x = Name{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[7] + mi := &file_python_ast_proto_msgTypes[10] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -554,7 +753,7 @@ func (x *Name) String() string { func (*Name) ProtoMessage() {} func (x *Name) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[7] + mi := &file_python_ast_proto_msgTypes[10] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -567,7 +766,7 @@ func (x *Name) ProtoReflect() protoreflect.Message { // Deprecated: Use Name.ProtoReflect.Descriptor instead. func (*Name) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{7} + return file_python_ast_proto_rawDescGZIP(), []int{10} } func (x *Name) GetId() string { @@ -589,7 +788,7 @@ type Subscript struct { func (x *Subscript) Reset() { *x = Subscript{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[8] + mi := &file_python_ast_proto_msgTypes[11] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -602,7 +801,7 @@ func (x *Subscript) String() string { func (*Subscript) ProtoMessage() {} func (x *Subscript) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[8] + mi := &file_python_ast_proto_msgTypes[11] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -615,7 +814,7 @@ func (x *Subscript) ProtoReflect() protoreflect.Message { // Deprecated: Use Subscript.ProtoReflect.Descriptor instead. func (*Subscript) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{8} + return file_python_ast_proto_rawDescGZIP(), []int{11} } func (x *Subscript) GetValue() *Name { @@ -636,7 +835,7 @@ var File_python_ast_proto protoreflect.FileDescriptor var file_python_ast_proto_rawDesc = []byte{ 0x0a, 0x10, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xe4, 0x02, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, + 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xe8, 0x03, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x2c, 0x0a, 0x09, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, 0x64, 0x65, 0x66, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x25, @@ -658,49 +857,69 @@ var file_python_ast_proto_rawDesc = []byte{ 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x2e, 0x0a, 0x09, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x48, 0x00, 0x52, 0x09, 0x53, 0x75, 0x62, - 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0x1b, - 0x0a, 0x05, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x71, 0x0a, 0x09, 0x41, - 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x21, 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, - 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, - 0x61, 0x6d, 0x65, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x29, 0x0a, 0x0a, 0x61, - 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, - 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, - 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x22, 0xb8, - 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, - 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, - 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, - 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, - 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, - 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, - 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, - 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, - 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, - 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, - 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, - 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, - 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, - 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, - 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, - 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, - 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, - 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, - 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, - 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, - 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, 0x61, - 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, - 0x69, 0x64, 0x22, 0x4d, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, - 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, - 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, - 0x12, 0x1f, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x73, 0x6c, 0x69, 0x63, - 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, - 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, - 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, - 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, 0x2e, 0x0a, 0x09, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, + 0x75, 0x74, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x48, 0x00, 0x52, 0x09, 0x41, 0x74, 0x74, + 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x12, 0x2b, 0x0a, 0x08, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x61, + 0x6e, 0x74, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, + 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6f, 0x6e, 0x73, 0x74, + 0x61, 0x6e, 0x74, 0x12, 0x25, 0x0a, 0x06, 0x61, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x18, 0x0b, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, + 0x48, 0x00, 0x52, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, + 0x64, 0x65, 0x22, 0x1b, 0x0a, 0x05, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, + 0x40, 0x0a, 0x09, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x12, 0x1f, 0x0a, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, + 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x12, 0x0a, + 0x04, 0x61, 0x74, 0x74, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x74, 0x74, + 0x72, 0x22, 0x71, 0x0a, 0x09, 0x41, 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x21, + 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, + 0x74, 0x12, 0x29, 0x0a, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, + 0x52, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, + 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x69, + 0x6d, 0x70, 0x6c, 0x65, 0x22, 0x4e, 0x0a, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x23, + 0x0a, 0x07, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x07, 0x74, 0x61, 0x72, 0x67, + 0x65, 0x74, 0x73, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x76, + 0x61, 0x6c, 0x75, 0x65, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, + 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x18, 0x02, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, + 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, + 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, + 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x1d, 0x0a, + 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, + 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x31, 0x0a, 0x0e, + 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x05, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, + 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x22, + 0x20, 0x0a, 0x08, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76, + 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x6e, + 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, + 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, 0x0a, 0x0a, + 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, + 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x64, 0x75, + 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, + 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, 0x6f, 0x64, + 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, + 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, + 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x4d, 0x0a, 0x09, 0x53, 0x75, + 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, + 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, + 0x6d, 0x65, 0x52, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, + 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, + 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, + 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( @@ -715,43 +934,52 @@ func file_python_ast_proto_rawDescGZIP() []byte { return file_python_ast_proto_rawDescData } -var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 9) +var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 12) var file_python_ast_proto_goTypes = []interface{}{ (*Node)(nil), // 0: ast.Node (*Alias)(nil), // 1: ast.Alias - (*AnnAssign)(nil), // 2: ast.AnnAssign - (*ClassDef)(nil), // 3: ast.ClassDef - (*Import)(nil), // 4: ast.Import - (*ImportFrom)(nil), // 5: ast.ImportFrom - (*Module)(nil), // 6: ast.Module - (*Name)(nil), // 7: ast.Name - (*Subscript)(nil), // 8: ast.Subscript + (*Attribute)(nil), // 2: ast.Attribute + (*AnnAssign)(nil), // 3: ast.AnnAssign + (*Assign)(nil), // 4: ast.Assign + (*ClassDef)(nil), // 5: ast.ClassDef + (*Constant)(nil), // 6: ast.Constant + (*Import)(nil), // 7: ast.Import + (*ImportFrom)(nil), // 8: ast.ImportFrom + (*Module)(nil), // 9: ast.Module + (*Name)(nil), // 10: ast.Name + (*Subscript)(nil), // 11: ast.Subscript } var file_python_ast_proto_depIdxs = []int32{ - 3, // 0: ast.Node.class_def:type_name -> ast.ClassDef - 4, // 1: ast.Node.import:type_name -> ast.Import - 5, // 2: ast.Node.import_from:type_name -> ast.ImportFrom - 6, // 3: ast.Node.module:type_name -> ast.Module + 5, // 0: ast.Node.class_def:type_name -> ast.ClassDef + 7, // 1: ast.Node.import:type_name -> ast.Import + 8, // 2: ast.Node.import_from:type_name -> ast.ImportFrom + 9, // 3: ast.Node.module:type_name -> ast.Module 1, // 4: ast.Node.alias:type_name -> ast.Alias - 2, // 5: ast.Node.ann_assign:type_name -> ast.AnnAssign - 7, // 6: ast.Node.name:type_name -> ast.Name - 8, // 7: ast.Node.subscript:type_name -> ast.Subscript - 7, // 8: ast.AnnAssign.target:type_name -> ast.Name - 0, // 9: ast.AnnAssign.annotation:type_name -> ast.Node - 0, // 10: ast.ClassDef.bases:type_name -> ast.Node - 0, // 11: ast.ClassDef.keywords:type_name -> ast.Node - 0, // 12: ast.ClassDef.body:type_name -> ast.Node - 0, // 13: ast.ClassDef.decorator_list:type_name -> ast.Node - 0, // 14: ast.Import.names:type_name -> ast.Node - 0, // 15: ast.ImportFrom.names:type_name -> ast.Node - 0, // 16: ast.Module.body:type_name -> ast.Node - 7, // 17: ast.Subscript.value:type_name -> ast.Name - 7, // 18: ast.Subscript.slice:type_name -> ast.Name - 19, // [19:19] is the sub-list for method output_type - 19, // [19:19] is the sub-list for method input_type - 19, // [19:19] is the sub-list for extension type_name - 19, // [19:19] is the sub-list for extension extendee - 0, // [0:19] is the sub-list for field type_name + 3, // 5: ast.Node.ann_assign:type_name -> ast.AnnAssign + 10, // 6: ast.Node.name:type_name -> ast.Name + 11, // 7: ast.Node.subscript:type_name -> ast.Subscript + 2, // 8: ast.Node.attribute:type_name -> ast.Attribute + 6, // 9: ast.Node.constant:type_name -> ast.Constant + 4, // 10: ast.Node.assign:type_name -> ast.Assign + 10, // 11: ast.Attribute.value:type_name -> ast.Name + 10, // 12: ast.AnnAssign.target:type_name -> ast.Name + 0, // 13: ast.AnnAssign.annotation:type_name -> ast.Node + 10, // 14: ast.Assign.targets:type_name -> ast.Name + 0, // 15: ast.Assign.value:type_name -> ast.Node + 0, // 16: ast.ClassDef.bases:type_name -> ast.Node + 0, // 17: ast.ClassDef.keywords:type_name -> ast.Node + 0, // 18: ast.ClassDef.body:type_name -> ast.Node + 0, // 19: ast.ClassDef.decorator_list:type_name -> ast.Node + 0, // 20: ast.Import.names:type_name -> ast.Node + 0, // 21: ast.ImportFrom.names:type_name -> ast.Node + 0, // 22: ast.Module.body:type_name -> ast.Node + 10, // 23: ast.Subscript.value:type_name -> ast.Name + 10, // 24: ast.Subscript.slice:type_name -> ast.Name + 25, // [25:25] is the sub-list for method output_type + 25, // [25:25] is the sub-list for method input_type + 25, // [25:25] is the sub-list for extension type_name + 25, // [25:25] is the sub-list for extension extendee + 0, // [0:25] is the sub-list for field type_name } func init() { file_python_ast_proto_init() } @@ -785,7 +1013,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*AnnAssign); i { + switch v := v.(*Attribute); i { case 0: return &v.state case 1: @@ -797,7 +1025,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ClassDef); i { + switch v := v.(*AnnAssign); i { case 0: return &v.state case 1: @@ -809,7 +1037,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Import); i { + switch v := v.(*Assign); i { case 0: return &v.state case 1: @@ -821,7 +1049,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ImportFrom); i { + switch v := v.(*ClassDef); i { case 0: return &v.state case 1: @@ -833,7 +1061,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Module); i { + switch v := v.(*Constant); i { case 0: return &v.state case 1: @@ -845,7 +1073,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Name); i { + switch v := v.(*Import); i { case 0: return &v.state case 1: @@ -857,6 +1085,42 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ImportFrom); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Module); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Name); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Subscript); i { case 0: return &v.state @@ -878,6 +1142,9 @@ func file_python_ast_proto_init() { (*Node_AnnAssign)(nil), (*Node_Name)(nil), (*Node_Subscript)(nil), + (*Node_Attribute)(nil), + (*Node_Constant)(nil), + (*Node_Assign)(nil), } type x struct{} out := protoimpl.TypeBuilder{ @@ -885,7 +1152,7 @@ func file_python_ast_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_python_ast_proto_rawDesc, NumEnums: 0, - NumMessages: 9, + NumMessages: 12, NumExtensions: 0, NumServices: 0, }, diff --git a/internal/python/out.py b/internal/python/out.py index dfa9e5aa64..4aa8e84941 100644 --- a/internal/python/out.py +++ b/internal/python/out.py @@ -1,19 +1,2 @@ -import dataclasses -from typing import Optional - -# Comment -# Longer Comment - -@dataclasses.dataclass() class InventoryItem: - """Class for keeping track of an item in inventory.""" - name: Optional[str] = None - unit_price: Optional[float] = None - quantity_on_hand: Optional[int] = None - - def total_cost(self) -> float: - return self.unit_price * self.quantity_on_hand - -if __name__ == "__main__": - print(InventoryItem()) - + pass diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index f71f5675fd..d0df62ba1c 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -41,9 +41,18 @@ func (w *writer) printNode(node *ast.Node, indent int32) { case *ast.Node_AnnAssign: w.printAnnAssign(n.AnnAssign, indent) + case *ast.Node_Assign: + w.printAssign(n.Assign, indent) + + case *ast.Node_Attribute: + w.printAttribute(n.Attribute, indent) + case *ast.Node_ClassDef: w.printClassDef(n.ClassDef, indent) + case *ast.Node_Constant: + w.printConstant(n.Constant, indent) + case *ast.Node_Import: w.printImport(n.Import, indent) @@ -71,6 +80,23 @@ func (w *writer) printAnnAssign(aa *ast.AnnAssign, indent int32) { w.printNode(aa.Annotation, indent) } +func (w *writer) printAssign(a *ast.Assign, indent int32) { + for i, name := range a.Targets { + w.printName(name, indent) + if i != len(a.Targets)-1 { + w.print(", ") + } + } + w.print(" = ") + w.printNode(a.Value, indent) +} + +func (w *writer) printAttribute(a *ast.Attribute, indent int32) { + w.printName(a.Value, indent) + w.print(".") + w.print(a.Attr) +} + func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) { for _, node := range cd.DecoratorList { w.print("@") @@ -79,6 +105,16 @@ func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) { } w.print("class ") w.print(cd.Name) + if len(cd.Bases) > 0 { + w.print("(") + for i, node := range cd.Bases { + w.printNode(node, indent) + if i != len(cd.Bases)-1 { + w.print(", ") + } + } + w.print(")") + } w.print(":\n") for _, node := range cd.Body { w.printIndent(indent + 1) @@ -87,6 +123,12 @@ func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) { } } +func (w *writer) printConstant(c *ast.Constant, indent int32) { + w.print("\"") + w.print(c.Value) + w.print("\"") +} + func (w *writer) printImport(imp *ast.Import, indent int32) { w.print("import ") for i, node := range imp.Names { diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go index d19eb1795c..ade29340dd 100644 --- a/internal/python/printer/printer_test.go +++ b/internal/python/printer/printer_test.go @@ -16,6 +16,50 @@ type testcase struct { func TestPrinter(t *testing.T) { for name, tc := range map[string]testcase{ + "assign": { + Node: &ast.Node{ + Node: &ast.Node_Assign{ + Assign: &ast.Assign{ + Targets: []*ast.Name{ + {Id: "FICTION"}, + }, + Value: &ast.Node{ + Node: &ast.Node_Constant{ + Constant: &ast.Constant{ + Value: "FICTION", + }, + }, + }, + }, + }, + }, + Expected: `FICTION = "FICTION"`, + }, + "class-base": { + Node: &ast.Node{ + Node: &ast.Node_ClassDef{ + ClassDef: &ast.ClassDef{ + Name: "Foo", + Bases: []*ast.Node{ + { + Node: &ast.Node_Name{ + Name: &ast.Name{Id: "str"}, + }, + }, + { + Node: &ast.Node_Attribute{ + Attribute: &ast.Attribute{ + Value: &ast.Name{Id: "enum"}, + Attr: "Enum", + }, + }, + }, + }, + }, + }, + }, + Expected: `class Foo(str, enum.Enum):`, + }, "dataclass": { Node: &ast.Node{ Node: &ast.Node_ClassDef{ diff --git a/protos/python/ast.proto b/protos/python/ast.proto index 1a23472cfe..3fbb74ef93 100644 --- a/protos/python/ast.proto +++ b/protos/python/ast.proto @@ -14,6 +14,9 @@ message Node { AnnAssign ann_assign = 6 [json_name="AnnAssign"]; Name name = 7 [json_name="Name"]; Subscript subscript = 8 [json_name="Subscript"]; + Attribute attribute = 9 [json_name="Attribute"]; + Constant constant = 10 [json_name="Constant"]; + Assign assign = 11 [json_name="Assign"]; } } @@ -22,6 +25,12 @@ message Alias string name = 1 [json_name="name"]; } +message Attribute +{ + Name value = 1 [json_name="value"]; + string attr = 2 [json_name="attr"]; +} + message AnnAssign { Name target = 1 [json_name="target"]; @@ -29,6 +38,12 @@ message AnnAssign int32 simple = 3 [json_name="simple"]; } +message Assign +{ + repeated Name targets = 1 [json_name="targets"]; + Node value = 2 [json_name="value"]; +} + message ClassDef { string name = 1 [json_name="name"]; @@ -38,6 +53,11 @@ message ClassDef repeated Node decorator_list = 5 [json_name="decorator_list"]; } +message Constant +{ + string value = 1 [json_name="value"]; +} + message Import { repeated Node names = 1 [json_name="names"]; From 74737b721294baef820565cf2b10b79fa5a2c6fa Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Thu, 9 Dec 2021 10:15:20 -0800 Subject: [PATCH 07/12] Add new line after imports --- internal/python/printer/printer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index d0df62ba1c..178b1a58a0 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -137,6 +137,7 @@ func (w *writer) printImport(imp *ast.Import, indent int32) { w.print(", ") } } + w.print("\n") } func (w *writer) printImportFrom(imp *ast.ImportFrom, indent int32) { From 2c440bea210f5bf738916cd19867db06dae8e527 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 10 Dec 2021 09:53:49 -0800 Subject: [PATCH 08/12] Start experimenting with model definitions --- examples/python/src/authors/models.py | 3 +- examples/python/src/booktest/models.py | 3 +- examples/python/src/booktest/query.py | 2 +- examples/python/src/jets/models.py | 2 +- examples/python/src/ondeck/models.py | 3 +- examples/python/src/ondeck/venue.py | 2 +- internal/codegen/python/gen.go | 164 ++++++++++++- internal/codegen/python/imports.go | 51 +++- internal/python/ast/ast.pb.go | 306 +++++++++++++++--------- internal/python/out.py | 3 +- internal/python/printer/printer.go | 12 + internal/python/printer/printer_test.go | 13 +- protos/python/ast.proto | 8 + 13 files changed, 440 insertions(+), 132 deletions(-) diff --git a/examples/python/src/authors/models.py b/examples/python/src/authors/models.py index 42d945abe1..298d5324bd 100644 --- a/examples/python/src/authors/models.py +++ b/examples/python/src/authors/models.py @@ -1,11 +1,12 @@ # Code generated by sqlc. DO NOT EDIT. from typing import Optional - import dataclasses + + @dataclasses.dataclass() class Author: id: int diff --git a/examples/python/src/booktest/models.py b/examples/python/src/booktest/models.py index 7367c14720..e03d914fec 100644 --- a/examples/python/src/booktest/models.py +++ b/examples/python/src/booktest/models.py @@ -1,9 +1,10 @@ # Code generated by sqlc. DO NOT EDIT. from typing import List +import dataclasses import datetime import enum -import dataclasses + diff --git a/examples/python/src/booktest/query.py b/examples/python/src/booktest/query.py index ea8255237f..24fc9ded4f 100644 --- a/examples/python/src/booktest/query.py +++ b/examples/python/src/booktest/query.py @@ -1,9 +1,9 @@ # Code generated by sqlc. DO NOT EDIT. from typing import AsyncIterator, List, Optional +import dataclasses import datetime -import dataclasses import sqlalchemy import sqlalchemy.ext.asyncio diff --git a/examples/python/src/jets/models.py b/examples/python/src/jets/models.py index 13ff1a122c..5abcfd13a7 100644 --- a/examples/python/src/jets/models.py +++ b/examples/python/src/jets/models.py @@ -1,7 +1,7 @@ # Code generated by sqlc. DO NOT EDIT. +import dataclasses -import dataclasses diff --git a/examples/python/src/ondeck/models.py b/examples/python/src/ondeck/models.py index 6e2de33b5e..97be8762e5 100644 --- a/examples/python/src/ondeck/models.py +++ b/examples/python/src/ondeck/models.py @@ -1,9 +1,10 @@ # Code generated by sqlc. DO NOT EDIT. from typing import List, Optional +import dataclasses import datetime import enum -import dataclasses + # Venues can be either open or closed diff --git a/examples/python/src/ondeck/venue.py b/examples/python/src/ondeck/venue.py index c0e31be474..2d7273da5e 100644 --- a/examples/python/src/ondeck/venue.py +++ b/examples/python/src/ondeck/venue.py @@ -1,8 +1,8 @@ # Code generated by sqlc. DO NOT EDIT. from typing import AsyncIterator, List, Optional - import dataclasses + import sqlalchemy import sqlalchemy.ext.asyncio diff --git a/internal/codegen/python/gen.go b/internal/codegen/python/gen.go index 3173bc6d8f..436d9750b3 100644 --- a/internal/codegen/python/gen.go +++ b/internal/codegen/python/gen.go @@ -4,18 +4,21 @@ import ( "bufio" "bytes" "fmt" + "log" + "regexp" + "sort" + "strings" + "text/template" + "github.com/kyleconroy/sqlc/internal/codegen" "github.com/kyleconroy/sqlc/internal/compiler" "github.com/kyleconroy/sqlc/internal/config" "github.com/kyleconroy/sqlc/internal/core" "github.com/kyleconroy/sqlc/internal/inflection" + pyast "github.com/kyleconroy/sqlc/internal/python/ast" + pyprint "github.com/kyleconroy/sqlc/internal/python/printer" "github.com/kyleconroy/sqlc/internal/sql/ast" "github.com/kyleconroy/sqlc/internal/sql/catalog" - "log" - "regexp" - "sort" - "strings" - "text/template" ) type Constant struct { @@ -481,6 +484,154 @@ func buildQueries(r *compiler.Result, settings config.CombinedSettings, structs return qs } +func importNode(name string) *pyast.Node { + return &pyast.Node{ + Node: &pyast.Node_Import{ + Import: &pyast.Import{ + Names: []*pyast.Node{ + { + Node: &pyast.Node_Alias{ + Alias: &pyast.Alias{ + Name: name, + }, + }, + }, + }, + }, + }, + } +} + +func classDefNode(name string, bases ...*pyast.Node) *pyast.Node { + return &pyast.Node{ + Node: &pyast.Node_ClassDef{ + ClassDef: &pyast.ClassDef{ + Name: name, + Bases: bases, + }, + }, + } +} + +func nameNode(id string) *pyast.Node { + return &pyast.Node{ + Node: &pyast.Node_Name{ + Name: &pyast.Name{Id: id}, + }, + } +} + +func attributeNode(value, attr string) *pyast.Node { + return &pyast.Node{ + Node: &pyast.Node_Attribute{ + Attribute: &pyast.Attribute{ + Value: &pyast.Name{Id: value}, + Attr: attr, + }, + }, + } +} + +func assignNode(target, value string) *pyast.Node { + return &pyast.Node{ + Node: &pyast.Node_Assign{ + Assign: &pyast.Assign{ + Targets: []*pyast.Name{ + {Id: target}, + }, + Value: &pyast.Node{ + Node: &pyast.Node_Constant{ + Constant: &pyast.Constant{ + Value: value, + }, + }, + }, + }, + }, + } +} + +func subscriptNode(value, slice string) *pyast.Node { + return &pyast.Node{ + Node: &pyast.Node_Subscript{ + Subscript: &pyast.Subscript{ + Value: &pyast.Name{Id: value}, + Slice: &pyast.Name{Id: slice}, + }, + }, + } +} + +func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { + mod := &pyast.Module{ + Body: []*pyast.Node{ + { + Node: &pyast.Node_Comment{ + Comment: &pyast.Comment{ + Text: "Code generated by sqlc. DO NOT EDIT.", + }, + }, + }, + }, + } + std, pkg := i.modelImportSpecs() + for _, spec := range buildImportBlock2(std) { + mod.Body = append(mod.Body, importNode(spec.Module)) + } + for _, spec := range buildImportBlock2(pkg) { + mod.Body = append(mod.Body, importNode(spec.Module)) + } + + for _, e := range ctx.Enums { + def := &pyast.ClassDef{ + Name: e.Name, + Bases: []*pyast.Node{ + nameNode("str"), + attributeNode("enum", "Enum"), + }, + } + for _, c := range e.Constants { + def.Body = append(def.Body, assignNode(c.Name, c.Value)) + } + mod.Body = append(mod.Body, &pyast.Node{ + Node: &pyast.Node_ClassDef{ + ClassDef: def, + }, + }) + } + + for _, m := range ctx.Models { + def := &pyast.ClassDef{ + Name: m.Name, + } + for _, f := range m.Fields { + var ann *pyast.Node + if f.Type.IsArray { + ann = subscriptNode("List", f.Type.InnerType) + } else if f.Type.IsNull { + ann = subscriptNode("Optional", f.Type.InnerType) + } else { + ann = nameNode(f.Type.InnerType) + } + def.Body = append(def.Body, &pyast.Node{ + Node: &pyast.Node_AnnAssign{ + AnnAssign: &pyast.AnnAssign{ + Target: &pyast.Name{Id: f.Name}, + Annotation: ann, + }, + }, + }) + } + mod.Body = append(mod.Body, &pyast.Node{ + Node: &pyast.Node_ClassDef{ + ClassDef: def, + }, + }) + } + + return &pyast.Node{Node: &pyast.Node_Module{Module: mod}} +} + var modelsTmpl = `# Code generated by sqlc. DO NOT EDIT. {{- range imports .SourceName}} {{.}} @@ -703,6 +854,9 @@ func Generate(r *compiler.Result, settings config.CombinedSettings) (map[string] return nil } + result := pyprint.Print(buildModelsTree(&tctx, i), pyprint.Options{}) + fmt.Println(string(result.Code)) + if err := execute("models.py", modelsFile); err != nil { return nil, err } diff --git a/internal/codegen/python/imports.go b/internal/codegen/python/imports.go index dfce83a085..4947e52840 100644 --- a/internal/codegen/python/imports.go +++ b/internal/codegen/python/imports.go @@ -2,9 +2,10 @@ package python import ( "fmt" - "github.com/kyleconroy/sqlc/internal/config" "sort" "strings" + + "github.com/kyleconroy/sqlc/internal/config" ) type importSpec struct { @@ -76,7 +77,7 @@ func (i *importer) Imports(fileName string) []string { return i.queryImports(fileName) } -func (i *importer) modelImports() []string { +func (i *importer) modelImportSpecs() (map[string]importSpec, map[string]importSpec) { modelUses := func(name string) bool { for _, model := range i.Models { if structUses(name, model) { @@ -87,12 +88,12 @@ func (i *importer) modelImports() []string { } std := stdImports(modelUses) + std["dataclasses"] = importSpec{Module: "dataclasses"} if len(i.Enums) > 0 { std["enum"] = importSpec{Module: "enum"} } pkg := make(map[string]importSpec) - pkg["dataclasses"] = importSpec{Module: "dataclasses"} for _, o := range i.Settings.Overrides { if o.PythonType.IsSet() && o.PythonType.Module != "" { @@ -101,7 +102,11 @@ func (i *importer) modelImports() []string { } } } + return std, pkg +} +func (i *importer) modelImports() []string { + std, pkg := i.modelImportSpecs() importLines := []string{ buildImportBlock(std), "", @@ -146,7 +151,7 @@ func (i *importer) queryImports(fileName string) []string { queryValueModelImports := func(qv QueryValue) { if qv.IsStruct() && qv.EmitStruct() { - pkg["dataclasses"] = importSpec{Module: "dataclasses"} + std["dataclasses"] = importSpec{Module: "dataclasses"} } } @@ -183,6 +188,44 @@ func (i *importer) queryImports(fileName string) []string { return importLines } +type importFromSpec struct { + Module string + Names []string + Alias string +} + +func buildImportBlock2(pkgs map[string]importSpec) []importFromSpec { + pkgImports := make([]importFromSpec, 0) + fromImports := make(map[string][]string) + for _, is := range pkgs { + if is.Name == "" || is.Alias != "" { + pkgImports = append(pkgImports, importFromSpec{ + Module: is.Module, + Names: []string{is.Name}, + Alias: is.Alias, + }) + } else { + names, ok := fromImports[is.Module] + if !ok { + names = make([]string, 0, 1) + } + names = append(names, is.Name) + fromImports[is.Module] = names + } + } + for modName, names := range fromImports { + sort.Strings(names) + pkgImports = append(pkgImports, importFromSpec{ + Module: modName, + Names: names, + }) + } + sort.Slice(pkgImports, func(i, j int) bool { + return pkgImports[i].Module < pkgImports[j].Module || pkgImports[i].Names[0] < pkgImports[j].Names[0] + }) + return pkgImports +} + func buildImportBlock(pkgs map[string]importSpec) string { pkgImports := make([]importSpec, 0) fromImports := make(map[string][]string) diff --git a/internal/python/ast/ast.pb.go b/internal/python/ast/ast.pb.go index 3b5d3501e8..4e9269fb43 100644 --- a/internal/python/ast/ast.pb.go +++ b/internal/python/ast/ast.pb.go @@ -37,6 +37,7 @@ type Node struct { // *Node_Attribute // *Node_Constant // *Node_Assign + // *Node_Comment Node isNode_Node `protobuf_oneof:"node"` } @@ -156,6 +157,13 @@ func (x *Node) GetAssign() *Assign { return nil } +func (x *Node) GetComment() *Comment { + if x, ok := x.GetNode().(*Node_Comment); ok { + return x.Comment + } + return nil +} + type isNode_Node interface { isNode_Node() } @@ -204,6 +212,10 @@ type Node_Assign struct { Assign *Assign `protobuf:"bytes,11,opt,name=assign,json=Assign,proto3,oneof"` } +type Node_Comment struct { + Comment *Comment `protobuf:"bytes,12,opt,name=comment,json=Comment,proto3,oneof"` +} + func (*Node_ClassDef) isNode_Node() {} func (*Node_Import) isNode_Node() {} @@ -226,6 +238,8 @@ func (*Node_Constant) isNode_Node() {} func (*Node_Assign) isNode_Node() {} +func (*Node_Comment) isNode_Node() {} + type Alias struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -525,6 +539,55 @@ func (x *ClassDef) GetDecoratorList() []*Node { return nil } +// The Python ast module does not parse comments. It's not clear if this is the +// best way to support them in the AST +type Comment struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Text string `protobuf:"bytes,1,opt,name=text,proto3" json:"text,omitempty"` +} + +func (x *Comment) Reset() { + *x = Comment{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Comment) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Comment) ProtoMessage() {} + +func (x *Comment) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Comment.ProtoReflect.Descriptor instead. +func (*Comment) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{6} +} + +func (x *Comment) GetText() string { + if x != nil { + return x.Text + } + return "" +} + type Constant struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -536,7 +599,7 @@ type Constant struct { func (x *Constant) Reset() { *x = Constant{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[6] + mi := &file_python_ast_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -549,7 +612,7 @@ func (x *Constant) String() string { func (*Constant) ProtoMessage() {} func (x *Constant) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[6] + mi := &file_python_ast_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -562,7 +625,7 @@ func (x *Constant) ProtoReflect() protoreflect.Message { // Deprecated: Use Constant.ProtoReflect.Descriptor instead. func (*Constant) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{6} + return file_python_ast_proto_rawDescGZIP(), []int{7} } func (x *Constant) GetValue() string { @@ -583,7 +646,7 @@ type Import struct { func (x *Import) Reset() { *x = Import{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[7] + mi := &file_python_ast_proto_msgTypes[8] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -596,7 +659,7 @@ func (x *Import) String() string { func (*Import) ProtoMessage() {} func (x *Import) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[7] + mi := &file_python_ast_proto_msgTypes[8] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -609,7 +672,7 @@ func (x *Import) ProtoReflect() protoreflect.Message { // Deprecated: Use Import.ProtoReflect.Descriptor instead. func (*Import) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{7} + return file_python_ast_proto_rawDescGZIP(), []int{8} } func (x *Import) GetNames() []*Node { @@ -632,7 +695,7 @@ type ImportFrom struct { func (x *ImportFrom) Reset() { *x = ImportFrom{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[8] + mi := &file_python_ast_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -645,7 +708,7 @@ func (x *ImportFrom) String() string { func (*ImportFrom) ProtoMessage() {} func (x *ImportFrom) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[8] + mi := &file_python_ast_proto_msgTypes[9] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -658,7 +721,7 @@ func (x *ImportFrom) ProtoReflect() protoreflect.Message { // Deprecated: Use ImportFrom.ProtoReflect.Descriptor instead. func (*ImportFrom) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{8} + return file_python_ast_proto_rawDescGZIP(), []int{9} } func (x *ImportFrom) GetModule() string { @@ -693,7 +756,7 @@ type Module struct { func (x *Module) Reset() { *x = Module{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[9] + mi := &file_python_ast_proto_msgTypes[10] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -706,7 +769,7 @@ func (x *Module) String() string { func (*Module) ProtoMessage() {} func (x *Module) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[9] + mi := &file_python_ast_proto_msgTypes[10] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -719,7 +782,7 @@ func (x *Module) ProtoReflect() protoreflect.Message { // Deprecated: Use Module.ProtoReflect.Descriptor instead. func (*Module) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{9} + return file_python_ast_proto_rawDescGZIP(), []int{10} } func (x *Module) GetBody() []*Node { @@ -740,7 +803,7 @@ type Name struct { func (x *Name) Reset() { *x = Name{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[10] + mi := &file_python_ast_proto_msgTypes[11] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -753,7 +816,7 @@ func (x *Name) String() string { func (*Name) ProtoMessage() {} func (x *Name) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[10] + mi := &file_python_ast_proto_msgTypes[11] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -766,7 +829,7 @@ func (x *Name) ProtoReflect() protoreflect.Message { // Deprecated: Use Name.ProtoReflect.Descriptor instead. func (*Name) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{10} + return file_python_ast_proto_rawDescGZIP(), []int{11} } func (x *Name) GetId() string { @@ -788,7 +851,7 @@ type Subscript struct { func (x *Subscript) Reset() { *x = Subscript{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[11] + mi := &file_python_ast_proto_msgTypes[12] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -801,7 +864,7 @@ func (x *Subscript) String() string { func (*Subscript) ProtoMessage() {} func (x *Subscript) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[11] + mi := &file_python_ast_proto_msgTypes[12] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -814,7 +877,7 @@ func (x *Subscript) ProtoReflect() protoreflect.Message { // Deprecated: Use Subscript.ProtoReflect.Descriptor instead. func (*Subscript) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{11} + return file_python_ast_proto_rawDescGZIP(), []int{12} } func (x *Subscript) GetValue() *Name { @@ -835,7 +898,7 @@ var File_python_ast_proto protoreflect.FileDescriptor var file_python_ast_proto_rawDesc = []byte{ 0x0a, 0x10, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xe8, 0x03, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, + 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0x92, 0x04, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x2c, 0x0a, 0x09, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, 0x64, 0x65, 0x66, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x25, @@ -865,61 +928,65 @@ var file_python_ast_proto_rawDesc = []byte{ 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x12, 0x25, 0x0a, 0x06, 0x61, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, - 0x48, 0x00, 0x52, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, - 0x64, 0x65, 0x22, 0x1b, 0x0a, 0x05, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, - 0x40, 0x0a, 0x09, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x12, 0x1f, 0x0a, 0x05, - 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, - 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x12, 0x0a, - 0x04, 0x61, 0x74, 0x74, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x74, 0x74, - 0x72, 0x22, 0x71, 0x0a, 0x09, 0x41, 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x21, - 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, - 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, - 0x74, 0x12, 0x29, 0x0a, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, - 0x52, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, - 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x69, - 0x6d, 0x70, 0x6c, 0x65, 0x22, 0x4e, 0x0a, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x23, - 0x0a, 0x07, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, - 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x07, 0x74, 0x61, 0x72, 0x67, - 0x65, 0x74, 0x73, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x76, - 0x61, 0x6c, 0x75, 0x65, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, - 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x18, 0x02, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, - 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, - 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, - 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x1d, 0x0a, - 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, - 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x31, 0x0a, 0x0e, - 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x05, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, - 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x22, - 0x20, 0x0a, 0x08, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76, - 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x6e, - 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, - 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, 0x0a, 0x0a, - 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, - 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x64, 0x75, - 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, - 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, - 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, - 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, 0x6f, 0x64, - 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, - 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, - 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x22, 0x4d, 0x0a, 0x09, 0x53, 0x75, - 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, - 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, - 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, - 0x6d, 0x65, 0x52, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, - 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, - 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, - 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x48, 0x00, 0x52, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x28, 0x0a, 0x07, 0x63, 0x6f, + 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x61, 0x73, + 0x74, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x48, 0x00, 0x52, 0x07, 0x43, 0x6f, 0x6d, + 0x6d, 0x65, 0x6e, 0x74, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0x1b, 0x0a, 0x05, + 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x40, 0x0a, 0x09, 0x41, 0x74, 0x74, + 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, + 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x74, 0x74, 0x72, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x74, 0x74, 0x72, 0x22, 0x71, 0x0a, 0x09, 0x41, + 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x21, 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, + 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, + 0x61, 0x6d, 0x65, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x29, 0x0a, 0x0a, 0x61, + 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, + 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, + 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x22, 0x4e, + 0x0a, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x23, 0x0a, 0x07, 0x74, 0x61, 0x72, 0x67, + 0x65, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x07, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x73, 0x12, 0x1f, 0x0a, + 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, + 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0xb8, + 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, + 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, + 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, + 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, + 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, + 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, + 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, + 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x22, 0x1d, 0x0a, 0x07, 0x43, 0x6f, 0x6d, + 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x22, 0x20, 0x0a, 0x08, 0x43, 0x6f, 0x6e, 0x73, + 0x74, 0x61, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, + 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, + 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, + 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, + 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, + 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, + 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, + 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, + 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, + 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, + 0x61, 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x02, 0x69, 0x64, 0x22, 0x4d, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, + 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x73, 0x6c, 0x69, + 0x63, 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, + 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, + 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -934,7 +1001,7 @@ func file_python_ast_proto_rawDescGZIP() []byte { return file_python_ast_proto_rawDescData } -var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 12) +var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 13) var file_python_ast_proto_goTypes = []interface{}{ (*Node)(nil), // 0: ast.Node (*Alias)(nil), // 1: ast.Alias @@ -942,44 +1009,46 @@ var file_python_ast_proto_goTypes = []interface{}{ (*AnnAssign)(nil), // 3: ast.AnnAssign (*Assign)(nil), // 4: ast.Assign (*ClassDef)(nil), // 5: ast.ClassDef - (*Constant)(nil), // 6: ast.Constant - (*Import)(nil), // 7: ast.Import - (*ImportFrom)(nil), // 8: ast.ImportFrom - (*Module)(nil), // 9: ast.Module - (*Name)(nil), // 10: ast.Name - (*Subscript)(nil), // 11: ast.Subscript + (*Comment)(nil), // 6: ast.Comment + (*Constant)(nil), // 7: ast.Constant + (*Import)(nil), // 8: ast.Import + (*ImportFrom)(nil), // 9: ast.ImportFrom + (*Module)(nil), // 10: ast.Module + (*Name)(nil), // 11: ast.Name + (*Subscript)(nil), // 12: ast.Subscript } var file_python_ast_proto_depIdxs = []int32{ 5, // 0: ast.Node.class_def:type_name -> ast.ClassDef - 7, // 1: ast.Node.import:type_name -> ast.Import - 8, // 2: ast.Node.import_from:type_name -> ast.ImportFrom - 9, // 3: ast.Node.module:type_name -> ast.Module + 8, // 1: ast.Node.import:type_name -> ast.Import + 9, // 2: ast.Node.import_from:type_name -> ast.ImportFrom + 10, // 3: ast.Node.module:type_name -> ast.Module 1, // 4: ast.Node.alias:type_name -> ast.Alias 3, // 5: ast.Node.ann_assign:type_name -> ast.AnnAssign - 10, // 6: ast.Node.name:type_name -> ast.Name - 11, // 7: ast.Node.subscript:type_name -> ast.Subscript + 11, // 6: ast.Node.name:type_name -> ast.Name + 12, // 7: ast.Node.subscript:type_name -> ast.Subscript 2, // 8: ast.Node.attribute:type_name -> ast.Attribute - 6, // 9: ast.Node.constant:type_name -> ast.Constant + 7, // 9: ast.Node.constant:type_name -> ast.Constant 4, // 10: ast.Node.assign:type_name -> ast.Assign - 10, // 11: ast.Attribute.value:type_name -> ast.Name - 10, // 12: ast.AnnAssign.target:type_name -> ast.Name - 0, // 13: ast.AnnAssign.annotation:type_name -> ast.Node - 10, // 14: ast.Assign.targets:type_name -> ast.Name - 0, // 15: ast.Assign.value:type_name -> ast.Node - 0, // 16: ast.ClassDef.bases:type_name -> ast.Node - 0, // 17: ast.ClassDef.keywords:type_name -> ast.Node - 0, // 18: ast.ClassDef.body:type_name -> ast.Node - 0, // 19: ast.ClassDef.decorator_list:type_name -> ast.Node - 0, // 20: ast.Import.names:type_name -> ast.Node - 0, // 21: ast.ImportFrom.names:type_name -> ast.Node - 0, // 22: ast.Module.body:type_name -> ast.Node - 10, // 23: ast.Subscript.value:type_name -> ast.Name - 10, // 24: ast.Subscript.slice:type_name -> ast.Name - 25, // [25:25] is the sub-list for method output_type - 25, // [25:25] is the sub-list for method input_type - 25, // [25:25] is the sub-list for extension type_name - 25, // [25:25] is the sub-list for extension extendee - 0, // [0:25] is the sub-list for field type_name + 6, // 11: ast.Node.comment:type_name -> ast.Comment + 11, // 12: ast.Attribute.value:type_name -> ast.Name + 11, // 13: ast.AnnAssign.target:type_name -> ast.Name + 0, // 14: ast.AnnAssign.annotation:type_name -> ast.Node + 11, // 15: ast.Assign.targets:type_name -> ast.Name + 0, // 16: ast.Assign.value:type_name -> ast.Node + 0, // 17: ast.ClassDef.bases:type_name -> ast.Node + 0, // 18: ast.ClassDef.keywords:type_name -> ast.Node + 0, // 19: ast.ClassDef.body:type_name -> ast.Node + 0, // 20: ast.ClassDef.decorator_list:type_name -> ast.Node + 0, // 21: ast.Import.names:type_name -> ast.Node + 0, // 22: ast.ImportFrom.names:type_name -> ast.Node + 0, // 23: ast.Module.body:type_name -> ast.Node + 11, // 24: ast.Subscript.value:type_name -> ast.Name + 11, // 25: ast.Subscript.slice:type_name -> ast.Name + 26, // [26:26] is the sub-list for method output_type + 26, // [26:26] is the sub-list for method input_type + 26, // [26:26] is the sub-list for extension type_name + 26, // [26:26] is the sub-list for extension extendee + 0, // [0:26] is the sub-list for field type_name } func init() { file_python_ast_proto_init() } @@ -1061,7 +1130,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Constant); i { + switch v := v.(*Comment); i { case 0: return &v.state case 1: @@ -1073,7 +1142,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Import); i { + switch v := v.(*Constant); i { case 0: return &v.state case 1: @@ -1085,7 +1154,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ImportFrom); i { + switch v := v.(*Import); i { case 0: return &v.state case 1: @@ -1097,7 +1166,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Module); i { + switch v := v.(*ImportFrom); i { case 0: return &v.state case 1: @@ -1109,7 +1178,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Name); i { + switch v := v.(*Module); i { case 0: return &v.state case 1: @@ -1121,6 +1190,18 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Name); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Subscript); i { case 0: return &v.state @@ -1145,6 +1226,7 @@ func file_python_ast_proto_init() { (*Node_Attribute)(nil), (*Node_Constant)(nil), (*Node_Assign)(nil), + (*Node_Comment)(nil), } type x struct{} out := protoimpl.TypeBuilder{ @@ -1152,7 +1234,7 @@ func file_python_ast_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_python_ast_proto_rawDesc, NumEnums: 0, - NumMessages: 12, + NumMessages: 13, NumExtensions: 0, NumServices: 0, }, diff --git a/internal/python/out.py b/internal/python/out.py index 4aa8e84941..2a088b19a9 100644 --- a/internal/python/out.py +++ b/internal/python/out.py @@ -1,2 +1 @@ -class InventoryItem: - pass +from collections.abc import Mapping, Sequence diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index 178b1a58a0..055dc8d007 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -50,6 +50,9 @@ func (w *writer) printNode(node *ast.Node, indent int32) { case *ast.Node_ClassDef: w.printClassDef(n.ClassDef, indent) + case *ast.Node_Comment: + w.printComment(n.Comment, indent) + case *ast.Node_Constant: w.printConstant(n.Constant, indent) @@ -129,6 +132,12 @@ func (w *writer) printConstant(c *ast.Constant, indent int32) { w.print("\"") } +func (w *writer) printComment(c *ast.Comment, indent int32) { + w.print("# ") + w.print(c.Text) + w.print("\n") +} + func (w *writer) printImport(imp *ast.Import, indent int32) { w.print("import ") for i, node := range imp.Names { @@ -155,6 +164,9 @@ func (w *writer) printImportFrom(imp *ast.ImportFrom, indent int32) { func (w *writer) printModule(mod *ast.Module, indent int32) { for _, node := range mod.Body { + if _, ok := node.Node.(*ast.Node_ClassDef); ok { + w.print("\n\n") + } w.printNode(node, indent) } } diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go index ade29340dd..1c9d76a8fa 100644 --- a/internal/python/printer/printer_test.go +++ b/internal/python/printer/printer_test.go @@ -135,12 +135,19 @@ class Foo: Node: &ast.Node{ Node: &ast.Node_ImportFrom{ ImportFrom: &ast.ImportFrom{ - Module: "dataclasses", + Module: "pkg", Names: []*ast.Node{ { Node: &ast.Node_Alias{ Alias: &ast.Alias{ - Name: "dataclass", + Name: "foo", + }, + }, + }, + { + Node: &ast.Node_Alias{ + Alias: &ast.Alias{ + Name: "bar", }, }, }, @@ -148,7 +155,7 @@ class Foo: }, }, }, - Expected: `from dataclasses import dataclass`, + Expected: `from pkg import foo, bar`, }, } { tc := tc diff --git a/protos/python/ast.proto b/protos/python/ast.proto index 3fbb74ef93..f70c7c9df5 100644 --- a/protos/python/ast.proto +++ b/protos/python/ast.proto @@ -17,6 +17,7 @@ message Node { Attribute attribute = 9 [json_name="Attribute"]; Constant constant = 10 [json_name="Constant"]; Assign assign = 11 [json_name="Assign"]; + Comment comment = 12 [json_name="Comment"]; } } @@ -53,6 +54,13 @@ message ClassDef repeated Node decorator_list = 5 [json_name="decorator_list"]; } +// The Python ast module does not parse comments. It's not clear if this is the +// best way to support them in the AST +message Comment +{ + string text = 1 [json_name="text"]; +} + message Constant { string value = 1 [json_name="value"]; From f11c61aed7af552538c235c83d2e89a074b52adc Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 10 Dec 2021 18:22:24 -0800 Subject: [PATCH 09/12] models.py --- internal/codegen/python/gen.go | 66 ++++- internal/python/ast/ast.pb.go | 321 ++++++++++++++++-------- internal/python/out.py | 8 +- internal/python/printer/printer.go | 30 ++- internal/python/printer/printer_test.go | 2 +- protos/python/ast.proto | 8 + 6 files changed, 314 insertions(+), 121 deletions(-) diff --git a/internal/codegen/python/gen.go b/internal/codegen/python/gen.go index 436d9750b3..9e467dc7cd 100644 --- a/internal/codegen/python/gen.go +++ b/internal/codegen/python/gen.go @@ -483,6 +483,15 @@ func buildQueries(r *compiler.Result, settings config.CombinedSettings, structs sort.Slice(qs, func(i, j int) bool { return qs[i].MethodName < qs[j].MethodName }) return qs } +func aliasNode(name string) *pyast.Node { + return &pyast.Node{ + Node: &pyast.Node_Alias{ + Alias: &pyast.Alias{ + Name: name, + }, + }, + } +} func importNode(name string) *pyast.Node { return &pyast.Node{ @@ -575,11 +584,25 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { }, } std, pkg := i.modelImportSpecs() - for _, spec := range buildImportBlock2(std) { - mod.Body = append(mod.Body, importNode(spec.Module)) - } - for _, spec := range buildImportBlock2(pkg) { - mod.Body = append(mod.Body, importNode(spec.Module)) + + for _, specs := range []map[string]importSpec{std, pkg} { + for _, spec := range buildImportBlock2(specs) { + if len(spec.Names) > 0 && spec.Names[0] != "" { + imp := &pyast.ImportFrom{ + Module: spec.Module, + } + for _, name := range spec.Names { + imp.Names = append(imp.Names, aliasNode(name)) + } + mod.Body = append(mod.Body, &pyast.Node{ + Node: &pyast.Node_ImportFrom{ + ImportFrom: imp, + }, + }) + } else { + mod.Body = append(mod.Body, importNode(spec.Module)) + } + } } for _, e := range ctx.Enums { @@ -590,6 +613,21 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { attributeNode("enum", "Enum"), }, } + if e.Comment != "" { + def.Body = append(def.Body, &pyast.Node{ + Node: &pyast.Node_Expr{ + Expr: &pyast.Expr{ + Value: &pyast.Node{ + Node: &pyast.Node_Constant{ + Constant: &pyast.Constant{ + Value: e.Comment, + }, + }, + }, + }, + }, + }) + } for _, c := range e.Constants { def.Body = append(def.Body, assignNode(c.Name, c.Value)) } @@ -604,6 +642,21 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { def := &pyast.ClassDef{ Name: m.Name, } + if m.Comment != "" { + def.Body = append(def.Body, &pyast.Node{ + Node: &pyast.Node_Expr{ + Expr: &pyast.Expr{ + Value: &pyast.Node{ + Node: &pyast.Node_Constant{ + Constant: &pyast.Constant{ + Value: m.Comment, + }, + }, + }, + }, + }, + }) + } for _, f := range m.Fields { var ann *pyast.Node if f.Type.IsArray { @@ -618,6 +671,7 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { AnnAssign: &pyast.AnnAssign{ Target: &pyast.Name{Id: f.Name}, Annotation: ann, + Comment: f.Comment, }, }, }) @@ -855,7 +909,7 @@ func Generate(r *compiler.Result, settings config.CombinedSettings) (map[string] } result := pyprint.Print(buildModelsTree(&tctx, i), pyprint.Options{}) - fmt.Println(string(result.Code)) + output["models.py"] = string(result.Python) if err := execute("models.py", modelsFile); err != nil { return nil, err diff --git a/internal/python/ast/ast.pb.go b/internal/python/ast/ast.pb.go index 4e9269fb43..25d2b7a901 100644 --- a/internal/python/ast/ast.pb.go +++ b/internal/python/ast/ast.pb.go @@ -38,6 +38,7 @@ type Node struct { // *Node_Constant // *Node_Assign // *Node_Comment + // *Node_Expr Node isNode_Node `protobuf_oneof:"node"` } @@ -164,6 +165,13 @@ func (x *Node) GetComment() *Comment { return nil } +func (x *Node) GetExpr() *Expr { + if x, ok := x.GetNode().(*Node_Expr); ok { + return x.Expr + } + return nil +} + type isNode_Node interface { isNode_Node() } @@ -216,6 +224,10 @@ type Node_Comment struct { Comment *Comment `protobuf:"bytes,12,opt,name=comment,json=Comment,proto3,oneof"` } +type Node_Expr struct { + Expr *Expr `protobuf:"bytes,13,opt,name=expr,json=Expr,proto3,oneof"` +} + func (*Node_ClassDef) isNode_Node() {} func (*Node_Import) isNode_Node() {} @@ -240,6 +252,8 @@ func (*Node_Assign) isNode_Node() {} func (*Node_Comment) isNode_Node() {} +func (*Node_Expr) isNode_Node() {} + type Alias struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -347,9 +361,10 @@ type AnnAssign struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Target *Name `protobuf:"bytes,1,opt,name=target,proto3" json:"target,omitempty"` - Annotation *Node `protobuf:"bytes,2,opt,name=annotation,proto3" json:"annotation,omitempty"` - Simple int32 `protobuf:"varint,3,opt,name=simple,proto3" json:"simple,omitempty"` + Target *Name `protobuf:"bytes,1,opt,name=target,proto3" json:"target,omitempty"` + Annotation *Node `protobuf:"bytes,2,opt,name=annotation,proto3" json:"annotation,omitempty"` + Simple int32 `protobuf:"varint,3,opt,name=simple,proto3" json:"simple,omitempty"` + Comment string `protobuf:"bytes,4,opt,name=Comment,json=comment,proto3" json:"Comment,omitempty"` } func (x *AnnAssign) Reset() { @@ -405,6 +420,13 @@ func (x *AnnAssign) GetSimple() int32 { return 0 } +func (x *AnnAssign) GetComment() string { + if x != nil { + return x.Comment + } + return "" +} + type Assign struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -412,6 +434,7 @@ type Assign struct { Targets []*Name `protobuf:"bytes,1,rep,name=targets,proto3" json:"targets,omitempty"` Value *Node `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` + Comment string `protobuf:"bytes,3,opt,name=Comment,json=comment,proto3" json:"Comment,omitempty"` } func (x *Assign) Reset() { @@ -460,6 +483,13 @@ func (x *Assign) GetValue() *Node { return nil } +func (x *Assign) GetComment() string { + if x != nil { + return x.Comment + } + return "" +} + type ClassDef struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -635,6 +665,53 @@ func (x *Constant) GetValue() string { return "" } +type Expr struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Value *Node `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *Expr) Reset() { + *x = Expr{} + if protoimpl.UnsafeEnabled { + mi := &file_python_ast_proto_msgTypes[8] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Expr) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Expr) ProtoMessage() {} + +func (x *Expr) ProtoReflect() protoreflect.Message { + mi := &file_python_ast_proto_msgTypes[8] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Expr.ProtoReflect.Descriptor instead. +func (*Expr) Descriptor() ([]byte, []int) { + return file_python_ast_proto_rawDescGZIP(), []int{8} +} + +func (x *Expr) GetValue() *Node { + if x != nil { + return x.Value + } + return nil +} + type Import struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -646,7 +723,7 @@ type Import struct { func (x *Import) Reset() { *x = Import{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[8] + mi := &file_python_ast_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -659,7 +736,7 @@ func (x *Import) String() string { func (*Import) ProtoMessage() {} func (x *Import) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[8] + mi := &file_python_ast_proto_msgTypes[9] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -672,7 +749,7 @@ func (x *Import) ProtoReflect() protoreflect.Message { // Deprecated: Use Import.ProtoReflect.Descriptor instead. func (*Import) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{8} + return file_python_ast_proto_rawDescGZIP(), []int{9} } func (x *Import) GetNames() []*Node { @@ -695,7 +772,7 @@ type ImportFrom struct { func (x *ImportFrom) Reset() { *x = ImportFrom{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[9] + mi := &file_python_ast_proto_msgTypes[10] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -708,7 +785,7 @@ func (x *ImportFrom) String() string { func (*ImportFrom) ProtoMessage() {} func (x *ImportFrom) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[9] + mi := &file_python_ast_proto_msgTypes[10] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -721,7 +798,7 @@ func (x *ImportFrom) ProtoReflect() protoreflect.Message { // Deprecated: Use ImportFrom.ProtoReflect.Descriptor instead. func (*ImportFrom) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{9} + return file_python_ast_proto_rawDescGZIP(), []int{10} } func (x *ImportFrom) GetModule() string { @@ -756,7 +833,7 @@ type Module struct { func (x *Module) Reset() { *x = Module{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[10] + mi := &file_python_ast_proto_msgTypes[11] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -769,7 +846,7 @@ func (x *Module) String() string { func (*Module) ProtoMessage() {} func (x *Module) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[10] + mi := &file_python_ast_proto_msgTypes[11] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -782,7 +859,7 @@ func (x *Module) ProtoReflect() protoreflect.Message { // Deprecated: Use Module.ProtoReflect.Descriptor instead. func (*Module) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{10} + return file_python_ast_proto_rawDescGZIP(), []int{11} } func (x *Module) GetBody() []*Node { @@ -803,7 +880,7 @@ type Name struct { func (x *Name) Reset() { *x = Name{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[11] + mi := &file_python_ast_proto_msgTypes[12] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -816,7 +893,7 @@ func (x *Name) String() string { func (*Name) ProtoMessage() {} func (x *Name) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[11] + mi := &file_python_ast_proto_msgTypes[12] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -829,7 +906,7 @@ func (x *Name) ProtoReflect() protoreflect.Message { // Deprecated: Use Name.ProtoReflect.Descriptor instead. func (*Name) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{11} + return file_python_ast_proto_rawDescGZIP(), []int{12} } func (x *Name) GetId() string { @@ -851,7 +928,7 @@ type Subscript struct { func (x *Subscript) Reset() { *x = Subscript{} if protoimpl.UnsafeEnabled { - mi := &file_python_ast_proto_msgTypes[12] + mi := &file_python_ast_proto_msgTypes[13] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -864,7 +941,7 @@ func (x *Subscript) String() string { func (*Subscript) ProtoMessage() {} func (x *Subscript) ProtoReflect() protoreflect.Message { - mi := &file_python_ast_proto_msgTypes[12] + mi := &file_python_ast_proto_msgTypes[13] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -877,7 +954,7 @@ func (x *Subscript) ProtoReflect() protoreflect.Message { // Deprecated: Use Subscript.ProtoReflect.Descriptor instead. func (*Subscript) Descriptor() ([]byte, []int) { - return file_python_ast_proto_rawDescGZIP(), []int{12} + return file_python_ast_proto_rawDescGZIP(), []int{13} } func (x *Subscript) GetValue() *Name { @@ -898,7 +975,7 @@ var File_python_ast_proto protoreflect.FileDescriptor var file_python_ast_proto_rawDesc = []byte{ 0x0a, 0x10, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, 0x61, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0x92, 0x04, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, + 0x74, 0x6f, 0x12, 0x03, 0x61, 0x73, 0x74, 0x22, 0xb3, 0x04, 0x0a, 0x04, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x2c, 0x0a, 0x09, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x5f, 0x64, 0x65, 0x66, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x48, 0x00, 0x52, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x25, @@ -931,62 +1008,70 @@ var file_python_ast_proto_rawDesc = []byte{ 0x48, 0x00, 0x52, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x28, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x48, 0x00, 0x52, 0x07, 0x43, 0x6f, 0x6d, - 0x6d, 0x65, 0x6e, 0x74, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0x1b, 0x0a, 0x05, - 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x40, 0x0a, 0x09, 0x41, 0x74, 0x74, - 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, - 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x74, 0x74, 0x72, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x74, 0x74, 0x72, 0x22, 0x71, 0x0a, 0x09, 0x41, - 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x21, 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, - 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, - 0x61, 0x6d, 0x65, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x29, 0x0a, 0x0a, 0x61, - 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x61, 0x6e, 0x6e, 0x6f, - 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, - 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x22, 0x4e, - 0x0a, 0x06, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x23, 0x0a, 0x07, 0x74, 0x61, 0x72, 0x67, - 0x65, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, - 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x07, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x73, 0x12, 0x1f, 0x0a, - 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, - 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0xb8, - 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, - 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, - 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, - 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, - 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, - 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x1d, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, - 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, - 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x31, 0x0a, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, - 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, - 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, - 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x22, 0x1d, 0x0a, 0x07, 0x43, 0x6f, 0x6d, - 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x22, 0x20, 0x0a, 0x08, 0x43, 0x6f, 0x6e, 0x73, - 0x74, 0x61, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, - 0x70, 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, - 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, - 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, - 0x72, 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, - 0x61, 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, - 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, - 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, - 0x65, 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, - 0x62, 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, - 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, - 0x61, 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x02, 0x69, 0x64, 0x22, 0x4d, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, - 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, - 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, - 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x73, 0x6c, 0x69, - 0x63, 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, - 0x2f, 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, - 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, - 0x2f, 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x1f, 0x0a, 0x04, 0x65, 0x78, 0x70, 0x72, 0x18, 0x0d, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x45, 0x78, 0x70, 0x72, 0x48, 0x00, 0x52, + 0x04, 0x45, 0x78, 0x70, 0x72, 0x42, 0x06, 0x0a, 0x04, 0x6e, 0x6f, 0x64, 0x65, 0x22, 0x1b, 0x0a, + 0x05, 0x41, 0x6c, 0x69, 0x61, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x40, 0x0a, 0x09, 0x41, 0x74, + 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, + 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x74, 0x74, 0x72, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x74, 0x74, 0x72, 0x22, 0x8b, 0x01, 0x0a, + 0x09, 0x41, 0x6e, 0x6e, 0x41, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x12, 0x21, 0x0a, 0x06, 0x74, 0x61, + 0x72, 0x67, 0x65, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, + 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x29, 0x0a, + 0x0a, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x0a, 0x61, 0x6e, + 0x6e, 0x6f, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x69, 0x6d, 0x70, + 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, + 0x12, 0x18, 0x0a, 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x68, 0x0a, 0x06, 0x41, 0x73, + 0x73, 0x69, 0x67, 0x6e, 0x12, 0x23, 0x0a, 0x07, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x73, 0x18, + 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, + 0x52, 0x07, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x73, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, + 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, + 0x6f, 0x64, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x43, 0x6f, + 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, + 0x6d, 0x65, 0x6e, 0x74, 0x22, 0xb8, 0x01, 0x0a, 0x08, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x44, 0x65, + 0x66, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x18, 0x02, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, + 0x05, 0x62, 0x61, 0x73, 0x65, 0x73, 0x12, 0x25, 0x0a, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, + 0x64, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, + 0x6f, 0x64, 0x65, 0x52, 0x08, 0x6b, 0x65, 0x79, 0x77, 0x6f, 0x72, 0x64, 0x73, 0x12, 0x1d, 0x0a, + 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, + 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x31, 0x0a, 0x0e, + 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x18, 0x05, + 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, + 0x0e, 0x64, 0x65, 0x63, 0x6f, 0x72, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x6c, 0x69, 0x73, 0x74, 0x22, + 0x1d, 0x0a, 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, + 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x22, 0x20, + 0x0a, 0x08, 0x43, 0x6f, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x22, 0x27, 0x0a, 0x04, 0x45, 0x78, 0x70, 0x72, 0x12, 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, + 0x64, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x29, 0x0a, 0x06, 0x49, 0x6d, 0x70, + 0x6f, 0x72, 0x74, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, + 0x61, 0x6d, 0x65, 0x73, 0x22, 0x5b, 0x0a, 0x0a, 0x49, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x46, 0x72, + 0x6f, 0x6d, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x06, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1f, 0x0a, 0x05, 0x6e, 0x61, + 0x6d, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x73, 0x12, 0x14, 0x0a, 0x05, 0x6c, + 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, + 0x6c, 0x22, 0x27, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x04, 0x62, + 0x6f, 0x64, 0x79, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, + 0x4e, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x22, 0x16, 0x0a, 0x04, 0x4e, 0x61, + 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, + 0x69, 0x64, 0x22, 0x4d, 0x0a, 0x09, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x12, + 0x1f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, + 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x12, 0x1f, 0x0a, 0x05, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x09, 0x2e, 0x61, 0x73, 0x74, 0x2e, 0x4e, 0x61, 0x6d, 0x65, 0x52, 0x05, 0x73, 0x6c, 0x69, 0x63, + 0x65, 0x42, 0x30, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x6b, 0x79, 0x6c, 0x65, 0x63, 0x6f, 0x6e, 0x72, 0x6f, 0x79, 0x2f, 0x73, 0x71, 0x6c, 0x63, 0x2f, + 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x70, 0x79, 0x74, 0x68, 0x6f, 0x6e, 0x2f, + 0x61, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -1001,7 +1086,7 @@ func file_python_ast_proto_rawDescGZIP() []byte { return file_python_ast_proto_rawDescData } -var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 13) +var file_python_ast_proto_msgTypes = make([]protoimpl.MessageInfo, 14) var file_python_ast_proto_goTypes = []interface{}{ (*Node)(nil), // 0: ast.Node (*Alias)(nil), // 1: ast.Alias @@ -1011,44 +1096,47 @@ var file_python_ast_proto_goTypes = []interface{}{ (*ClassDef)(nil), // 5: ast.ClassDef (*Comment)(nil), // 6: ast.Comment (*Constant)(nil), // 7: ast.Constant - (*Import)(nil), // 8: ast.Import - (*ImportFrom)(nil), // 9: ast.ImportFrom - (*Module)(nil), // 10: ast.Module - (*Name)(nil), // 11: ast.Name - (*Subscript)(nil), // 12: ast.Subscript + (*Expr)(nil), // 8: ast.Expr + (*Import)(nil), // 9: ast.Import + (*ImportFrom)(nil), // 10: ast.ImportFrom + (*Module)(nil), // 11: ast.Module + (*Name)(nil), // 12: ast.Name + (*Subscript)(nil), // 13: ast.Subscript } var file_python_ast_proto_depIdxs = []int32{ 5, // 0: ast.Node.class_def:type_name -> ast.ClassDef - 8, // 1: ast.Node.import:type_name -> ast.Import - 9, // 2: ast.Node.import_from:type_name -> ast.ImportFrom - 10, // 3: ast.Node.module:type_name -> ast.Module + 9, // 1: ast.Node.import:type_name -> ast.Import + 10, // 2: ast.Node.import_from:type_name -> ast.ImportFrom + 11, // 3: ast.Node.module:type_name -> ast.Module 1, // 4: ast.Node.alias:type_name -> ast.Alias 3, // 5: ast.Node.ann_assign:type_name -> ast.AnnAssign - 11, // 6: ast.Node.name:type_name -> ast.Name - 12, // 7: ast.Node.subscript:type_name -> ast.Subscript + 12, // 6: ast.Node.name:type_name -> ast.Name + 13, // 7: ast.Node.subscript:type_name -> ast.Subscript 2, // 8: ast.Node.attribute:type_name -> ast.Attribute 7, // 9: ast.Node.constant:type_name -> ast.Constant 4, // 10: ast.Node.assign:type_name -> ast.Assign 6, // 11: ast.Node.comment:type_name -> ast.Comment - 11, // 12: ast.Attribute.value:type_name -> ast.Name - 11, // 13: ast.AnnAssign.target:type_name -> ast.Name - 0, // 14: ast.AnnAssign.annotation:type_name -> ast.Node - 11, // 15: ast.Assign.targets:type_name -> ast.Name - 0, // 16: ast.Assign.value:type_name -> ast.Node - 0, // 17: ast.ClassDef.bases:type_name -> ast.Node - 0, // 18: ast.ClassDef.keywords:type_name -> ast.Node - 0, // 19: ast.ClassDef.body:type_name -> ast.Node - 0, // 20: ast.ClassDef.decorator_list:type_name -> ast.Node - 0, // 21: ast.Import.names:type_name -> ast.Node - 0, // 22: ast.ImportFrom.names:type_name -> ast.Node - 0, // 23: ast.Module.body:type_name -> ast.Node - 11, // 24: ast.Subscript.value:type_name -> ast.Name - 11, // 25: ast.Subscript.slice:type_name -> ast.Name - 26, // [26:26] is the sub-list for method output_type - 26, // [26:26] is the sub-list for method input_type - 26, // [26:26] is the sub-list for extension type_name - 26, // [26:26] is the sub-list for extension extendee - 0, // [0:26] is the sub-list for field type_name + 8, // 12: ast.Node.expr:type_name -> ast.Expr + 12, // 13: ast.Attribute.value:type_name -> ast.Name + 12, // 14: ast.AnnAssign.target:type_name -> ast.Name + 0, // 15: ast.AnnAssign.annotation:type_name -> ast.Node + 12, // 16: ast.Assign.targets:type_name -> ast.Name + 0, // 17: ast.Assign.value:type_name -> ast.Node + 0, // 18: ast.ClassDef.bases:type_name -> ast.Node + 0, // 19: ast.ClassDef.keywords:type_name -> ast.Node + 0, // 20: ast.ClassDef.body:type_name -> ast.Node + 0, // 21: ast.ClassDef.decorator_list:type_name -> ast.Node + 0, // 22: ast.Expr.value:type_name -> ast.Node + 0, // 23: ast.Import.names:type_name -> ast.Node + 0, // 24: ast.ImportFrom.names:type_name -> ast.Node + 0, // 25: ast.Module.body:type_name -> ast.Node + 12, // 26: ast.Subscript.value:type_name -> ast.Name + 12, // 27: ast.Subscript.slice:type_name -> ast.Name + 28, // [28:28] is the sub-list for method output_type + 28, // [28:28] is the sub-list for method input_type + 28, // [28:28] is the sub-list for extension type_name + 28, // [28:28] is the sub-list for extension extendee + 0, // [0:28] is the sub-list for field type_name } func init() { file_python_ast_proto_init() } @@ -1154,7 +1242,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Import); i { + switch v := v.(*Expr); i { case 0: return &v.state case 1: @@ -1166,7 +1254,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ImportFrom); i { + switch v := v.(*Import); i { case 0: return &v.state case 1: @@ -1178,7 +1266,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Module); i { + switch v := v.(*ImportFrom); i { case 0: return &v.state case 1: @@ -1190,7 +1278,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Name); i { + switch v := v.(*Module); i { case 0: return &v.state case 1: @@ -1202,6 +1290,18 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Name); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[13].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Subscript); i { case 0: return &v.state @@ -1227,6 +1327,7 @@ func file_python_ast_proto_init() { (*Node_Constant)(nil), (*Node_Assign)(nil), (*Node_Comment)(nil), + (*Node_Expr)(nil), } type x struct{} out := protoimpl.TypeBuilder{ @@ -1234,7 +1335,7 @@ func file_python_ast_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_python_ast_proto_rawDesc, NumEnums: 0, - NumMessages: 13, + NumMessages: 14, NumExtensions: 0, NumServices: 0, }, diff --git a/internal/python/out.py b/internal/python/out.py index 2a088b19a9..e021beb1fc 100644 --- a/internal/python/out.py +++ b/internal/python/out.py @@ -1 +1,7 @@ -from collections.abc import Mapping, Sequence +class Foo: + """ + Hello + World + """ + + HELLO = "FOO" diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index 055dc8d007..0d41c29a3c 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -11,14 +11,14 @@ type Options struct { } type PrintResult struct { - Code []byte + Python []byte } func Print(node *ast.Node, options Options) PrintResult { w := writer{options: options} w.printNode(node, 0) return PrintResult{ - Code: w.src, + Python: w.src, } } @@ -56,6 +56,9 @@ func (w *writer) printNode(node *ast.Node, indent int32) { case *ast.Node_Constant: w.printConstant(n.Constant, indent) + case *ast.Node_Expr: + w.printNode(n.Expr.Value, indent) + case *ast.Node_Import: w.printImport(n.Import, indent) @@ -78,6 +81,12 @@ func (w *writer) printNode(node *ast.Node, indent int32) { } func (w *writer) printAnnAssign(aa *ast.AnnAssign, indent int32) { + if aa.Comment != "" { + w.print("# ") + w.print(aa.Comment) + w.print("\n") + w.printIndent(indent) + } w.printName(aa.Target, indent) w.print(": ") w.printNode(aa.Annotation, indent) @@ -119,8 +128,23 @@ func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) { w.print(")") } w.print(":\n") - for _, node := range cd.Body { + for i, node := range cd.Body { w.printIndent(indent + 1) + // A docstring is a string literal that occurs as the first + // statement in a module, function, class, or method + // definition. Such a docstring becomes the __doc__ special + // attribute of that object. + if i == 0 { + if e, ok := node.Node.(*ast.Node_Expr); ok { + if c, ok := e.Expr.Value.Node.(*ast.Node_Constant); ok { + w.print(`"""`) + w.print(c.Constant.Value) + w.print(`"""`) + w.print("\n") + continue + } + } + } w.printNode(node, indent+1) w.print("\n") } diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go index 1c9d76a8fa..a956cda8f6 100644 --- a/internal/python/printer/printer_test.go +++ b/internal/python/printer/printer_test.go @@ -161,7 +161,7 @@ class Foo: tc := tc t.Run(name, func(t *testing.T) { result := Print(tc.Node, Options{}) - if diff := cmp.Diff(strings.TrimSpace(tc.Expected), strings.TrimSpace(string(result.Code))); diff != "" { + if diff := cmp.Diff(strings.TrimSpace(tc.Expected), strings.TrimSpace(string(result.Python))); diff != "" { t.Errorf("print mismatch (-want +got):\n%s", diff) } }) diff --git a/protos/python/ast.proto b/protos/python/ast.proto index f70c7c9df5..da85f238de 100644 --- a/protos/python/ast.proto +++ b/protos/python/ast.proto @@ -18,6 +18,7 @@ message Node { Constant constant = 10 [json_name="Constant"]; Assign assign = 11 [json_name="Assign"]; Comment comment = 12 [json_name="Comment"]; + Expr expr = 13 [json_name="Expr"]; } } @@ -37,12 +38,14 @@ message AnnAssign Name target = 1 [json_name="target"]; Node annotation = 2 [json_name="annotation"]; int32 simple = 3 [json_name="simple"]; + string Comment = 4 [json_name="comment"]; } message Assign { repeated Name targets = 1 [json_name="targets"]; Node value = 2 [json_name="value"]; + string Comment = 3 [json_name="comment"]; } message ClassDef @@ -66,6 +69,11 @@ message Constant string value = 1 [json_name="value"]; } +message Expr +{ + Node value = 1 [json_name="value"]; +} + message Import { repeated Node names = 1 [json_name="names"]; From 03a90c39ce01992e98fdb7587997b981367dc276 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 10 Dec 2021 18:38:10 -0800 Subject: [PATCH 10/12] Generate models.py via AST --- internal/codegen/python/gen.go | 56 +-- .../python/__pycache__/ast.cpython-39.pyc | Bin 0 -> 278 bytes internal/python/ast/ast.pb.go | 360 +++++++++++------- internal/python/models.py | 12 + internal/python/out.py | 7 +- internal/python/out.txt | 35 ++ internal/python/printer/printer.go | 10 +- internal/python/printer/printer_test.go | 23 +- protos/python/ast.proto | 8 +- 9 files changed, 323 insertions(+), 188 deletions(-) create mode 100644 internal/python/__pycache__/ast.cpython-39.pyc create mode 100644 internal/python/models.py create mode 100644 internal/python/out.txt diff --git a/internal/codegen/python/gen.go b/internal/codegen/python/gen.go index 9e467dc7cd..97633c8e98 100644 --- a/internal/codegen/python/gen.go +++ b/internal/codegen/python/gen.go @@ -560,12 +560,12 @@ func assignNode(target, value string) *pyast.Node { } } -func subscriptNode(value, slice string) *pyast.Node { +func subscriptNode(value string, slice *pyast.Node) *pyast.Node { return &pyast.Node{ Node: &pyast.Node_Subscript{ Subscript: &pyast.Subscript{ Value: &pyast.Name{Id: value}, - Slice: &pyast.Name{Id: slice}, + Slice: slice, }, }, } @@ -641,6 +641,15 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { for _, m := range ctx.Models { def := &pyast.ClassDef{ Name: m.Name, + DecoratorList: []*pyast.Node{ + { + Node: &pyast.Node_Call{ + Call: &pyast.Call{ + Func: attributeNode("dataclasses", "dataclass"), + }, + }, + }, + }, } if m.Comment != "" { def.Body = append(def.Body, &pyast.Node{ @@ -658,13 +667,12 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { }) } for _, f := range m.Fields { - var ann *pyast.Node + ann := nameNode(f.Type.InnerType) if f.Type.IsArray { - ann = subscriptNode("List", f.Type.InnerType) - } else if f.Type.IsNull { - ann = subscriptNode("Optional", f.Type.InnerType) - } else { - ann = nameNode(f.Type.InnerType) + ann = subscriptNode("List", ann) + } + if f.Type.IsNull { + ann = subscriptNode("Optional", ann) } def.Body = append(def.Body, &pyast.Node{ Node: &pyast.Node_AnnAssign{ @@ -686,33 +694,6 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node { return &pyast.Node{Node: &pyast.Node_Module{Module: mod}} } -var modelsTmpl = `# Code generated by sqlc. DO NOT EDIT. -{{- range imports .SourceName}} -{{.}} -{{- end}} - - -{{range .Enums}} -{{- if .Comment}}{{comment .Comment}}{{- end}} -class {{.Name}}(str, enum.Enum): - {{- range .Constants}} - {{.Name}} = "{{.Value}}" - {{- end}} -{{end}} - -{{- range .Models}} -{{if .Comment}}{{comment .Comment}}{{- end}} -@dataclasses.dataclass() -class {{.Name}}: {{- range .Fields}} - {{- if .Comment}} - {{comment .Comment}}{{else}} - {{- end}} - {{.Name}}: {{.Type}} - {{- end}} - -{{end}} -` - var queriesTmpl = ` {{- define "dataclassParse"}} @@ -878,7 +859,6 @@ func Generate(r *compiler.Result, settings config.CombinedSettings) (map[string] "imports": i.Imports, } - modelsFile := template.Must(template.New("table").Funcs(funcMap).Parse(modelsTmpl)) queriesFile := template.Must(template.New("table").Funcs(funcMap).Parse(queriesTmpl)) tctx := pyTmplCtx{ @@ -911,10 +891,6 @@ func Generate(r *compiler.Result, settings config.CombinedSettings) (map[string] result := pyprint.Print(buildModelsTree(&tctx, i), pyprint.Options{}) output["models.py"] = string(result.Python) - if err := execute("models.py", modelsFile); err != nil { - return nil, err - } - files := map[string]struct{}{} for _, q := range queries { files[q.SourceName] = struct{}{} diff --git a/internal/python/__pycache__/ast.cpython-39.pyc b/internal/python/__pycache__/ast.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..01398c6f218d446b0876b38b07cae52fefa1f756 GIT binary patch literal 278 zcmYk0u};J=42F|5?G;3KUZKl{2SA7?U;!4EQ&juJ-K9y0)1j=)_aH1hP*$eC0t@&M z1D5<-pXAT7?YeWpwOu~!f#>H5{@04(mYdcbAV50tBv7K*EA9$4s5c1}XsC}BG_X1{ zOWwN|eY>zD?p_;9c^&6+ww?U2$#cYqUKKqCz|x&?GlX$OpJ~x7^s+QN56pdCDt1d9 z)6VCjVHzWYO@u{&iS5CNhWFp@}K%amZxo#SMmSOx`*Z=lmSJ`Fp6n T<{u`9H ast.ClassDef - 9, // 1: ast.Node.import:type_name -> ast.Import - 10, // 2: ast.Node.import_from:type_name -> ast.ImportFrom - 11, // 3: ast.Node.module:type_name -> ast.Module + 6, // 0: ast.Node.class_def:type_name -> ast.ClassDef + 10, // 1: ast.Node.import:type_name -> ast.Import + 11, // 2: ast.Node.import_from:type_name -> ast.ImportFrom + 12, // 3: ast.Node.module:type_name -> ast.Module 1, // 4: ast.Node.alias:type_name -> ast.Alias 3, // 5: ast.Node.ann_assign:type_name -> ast.AnnAssign - 12, // 6: ast.Node.name:type_name -> ast.Name - 13, // 7: ast.Node.subscript:type_name -> ast.Subscript + 13, // 6: ast.Node.name:type_name -> ast.Name + 14, // 7: ast.Node.subscript:type_name -> ast.Subscript 2, // 8: ast.Node.attribute:type_name -> ast.Attribute - 7, // 9: ast.Node.constant:type_name -> ast.Constant + 8, // 9: ast.Node.constant:type_name -> ast.Constant 4, // 10: ast.Node.assign:type_name -> ast.Assign - 6, // 11: ast.Node.comment:type_name -> ast.Comment - 8, // 12: ast.Node.expr:type_name -> ast.Expr - 12, // 13: ast.Attribute.value:type_name -> ast.Name - 12, // 14: ast.AnnAssign.target:type_name -> ast.Name - 0, // 15: ast.AnnAssign.annotation:type_name -> ast.Node - 12, // 16: ast.Assign.targets:type_name -> ast.Name - 0, // 17: ast.Assign.value:type_name -> ast.Node - 0, // 18: ast.ClassDef.bases:type_name -> ast.Node - 0, // 19: ast.ClassDef.keywords:type_name -> ast.Node - 0, // 20: ast.ClassDef.body:type_name -> ast.Node - 0, // 21: ast.ClassDef.decorator_list:type_name -> ast.Node - 0, // 22: ast.Expr.value:type_name -> ast.Node - 0, // 23: ast.Import.names:type_name -> ast.Node - 0, // 24: ast.ImportFrom.names:type_name -> ast.Node - 0, // 25: ast.Module.body:type_name -> ast.Node - 12, // 26: ast.Subscript.value:type_name -> ast.Name - 12, // 27: ast.Subscript.slice:type_name -> ast.Name - 28, // [28:28] is the sub-list for method output_type - 28, // [28:28] is the sub-list for method input_type - 28, // [28:28] is the sub-list for extension type_name - 28, // [28:28] is the sub-list for extension extendee - 0, // [0:28] is the sub-list for field type_name + 7, // 11: ast.Node.comment:type_name -> ast.Comment + 9, // 12: ast.Node.expr:type_name -> ast.Expr + 5, // 13: ast.Node.call:type_name -> ast.Call + 13, // 14: ast.Attribute.value:type_name -> ast.Name + 13, // 15: ast.AnnAssign.target:type_name -> ast.Name + 0, // 16: ast.AnnAssign.annotation:type_name -> ast.Node + 13, // 17: ast.Assign.targets:type_name -> ast.Name + 0, // 18: ast.Assign.value:type_name -> ast.Node + 0, // 19: ast.Call.func:type_name -> ast.Node + 0, // 20: ast.ClassDef.bases:type_name -> ast.Node + 0, // 21: ast.ClassDef.keywords:type_name -> ast.Node + 0, // 22: ast.ClassDef.body:type_name -> ast.Node + 0, // 23: ast.ClassDef.decorator_list:type_name -> ast.Node + 0, // 24: ast.Expr.value:type_name -> ast.Node + 0, // 25: ast.Import.names:type_name -> ast.Node + 0, // 26: ast.ImportFrom.names:type_name -> ast.Node + 0, // 27: ast.Module.body:type_name -> ast.Node + 13, // 28: ast.Subscript.value:type_name -> ast.Name + 0, // 29: ast.Subscript.slice:type_name -> ast.Node + 30, // [30:30] is the sub-list for method output_type + 30, // [30:30] is the sub-list for method input_type + 30, // [30:30] is the sub-list for extension type_name + 30, // [30:30] is the sub-list for extension extendee + 0, // [0:30] is the sub-list for field type_name } func init() { file_python_ast_proto_init() } @@ -1206,7 +1275,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ClassDef); i { + switch v := v.(*Call); i { case 0: return &v.state case 1: @@ -1218,7 +1287,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Comment); i { + switch v := v.(*ClassDef); i { case 0: return &v.state case 1: @@ -1230,7 +1299,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Constant); i { + switch v := v.(*Comment); i { case 0: return &v.state case 1: @@ -1242,7 +1311,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Expr); i { + switch v := v.(*Constant); i { case 0: return &v.state case 1: @@ -1254,7 +1323,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Import); i { + switch v := v.(*Expr); i { case 0: return &v.state case 1: @@ -1266,7 +1335,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ImportFrom); i { + switch v := v.(*Import); i { case 0: return &v.state case 1: @@ -1278,7 +1347,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Module); i { + switch v := v.(*ImportFrom); i { case 0: return &v.state case 1: @@ -1290,7 +1359,7 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Name); i { + switch v := v.(*Module); i { case 0: return &v.state case 1: @@ -1302,6 +1371,18 @@ func file_python_ast_proto_init() { } } file_python_ast_proto_msgTypes[13].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Name); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_python_ast_proto_msgTypes[14].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Subscript); i { case 0: return &v.state @@ -1328,6 +1409,7 @@ func file_python_ast_proto_init() { (*Node_Assign)(nil), (*Node_Comment)(nil), (*Node_Expr)(nil), + (*Node_Call)(nil), } type x struct{} out := protoimpl.TypeBuilder{ @@ -1335,7 +1417,7 @@ func file_python_ast_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_python_ast_proto_rawDesc, NumEnums: 0, - NumMessages: 14, + NumMessages: 15, NumExtensions: 0, NumServices: 0, }, diff --git a/internal/python/models.py b/internal/python/models.py new file mode 100644 index 0000000000..2a676f90de --- /dev/null +++ b/internal/python/models.py @@ -0,0 +1,12 @@ +# Code generated by sqlc. DO NOT EDIT. +from dataclasses import dataclass +from typing import Optional + + +@dataclass +class Author: + id: int + name: str + bio: Optional[str] + + diff --git a/internal/python/out.py b/internal/python/out.py index e021beb1fc..a1bd3dfcb8 100644 --- a/internal/python/out.py +++ b/internal/python/out.py @@ -1,7 +1,2 @@ class Foo: - """ - Hello - World - """ - - HELLO = "FOO" + foo: Optional[List[str]] diff --git a/internal/python/out.txt b/internal/python/out.txt new file mode 100644 index 0000000000..d2bb8f8c52 --- /dev/null +++ b/internal/python/out.txt @@ -0,0 +1,35 @@ +Module( + body=[ + ImportFrom( + module='dataclasses', + names=[ + alias(name='dataclass')], + level=0), + ImportFrom( + module='typing', + names=[ + alias(name='Optional')], + level=0), + ClassDef( + name='Author', + bases=[], + keywords=[], + body=[ + AnnAssign( + target=Name(id='id', ctx=Store()), + annotation=Name(id='int', ctx=Load()), + simple=1), + AnnAssign( + target=Name(id='name', ctx=Store()), + annotation=Name(id='str', ctx=Load()), + simple=1), + AnnAssign( + target=Name(id='bio', ctx=Store()), + annotation=Subscript( + value=Name(id='Optional', ctx=Load()), + slice=Name(id='str', ctx=Load()), + ctx=Load()), + simple=1)], + decorator_list=[ + Name(id='dataclass', ctx=Load())])], + type_ignores=[]) diff --git a/internal/python/printer/printer.go b/internal/python/printer/printer.go index 0d41c29a3c..977dd86b05 100644 --- a/internal/python/printer/printer.go +++ b/internal/python/printer/printer.go @@ -47,6 +47,9 @@ func (w *writer) printNode(node *ast.Node, indent int32) { case *ast.Node_Attribute: w.printAttribute(n.Attribute, indent) + case *ast.Node_Call: + w.printCall(n.Call, indent) + case *ast.Node_ClassDef: w.printClassDef(n.ClassDef, indent) @@ -109,6 +112,11 @@ func (w *writer) printAttribute(a *ast.Attribute, indent int32) { w.print(a.Attr) } +func (w *writer) printCall(c *ast.Call, indent int32) { + w.printNode(c.Func, indent) + w.print("()") +} + func (w *writer) printClassDef(cd *ast.ClassDef, indent int32) { for _, node := range cd.DecoratorList { w.print("@") @@ -202,7 +210,7 @@ func (w *writer) printName(n *ast.Name, indent int32) { func (w *writer) printSubscript(ss *ast.Subscript, indent int32) { w.printName(ss.Value, indent) w.print("[") - w.printName(ss.Slice, indent) + w.printNode(ss.Slice, indent) w.print("]") } diff --git a/internal/python/printer/printer_test.go b/internal/python/printer/printer_test.go index a956cda8f6..6237819b95 100644 --- a/internal/python/printer/printer_test.go +++ b/internal/python/printer/printer_test.go @@ -95,7 +95,11 @@ func TestPrinter(t *testing.T) { Node: &ast.Node_Subscript{ Subscript: &ast.Subscript{ Value: &ast.Name{Id: "Optional"}, - Slice: &ast.Name{Id: "int"}, + Slice: &ast.Node{ + Node: &ast.Node_Name{ + Name: &ast.Name{Id: "int"}, + }, + }, }, }, }, @@ -113,6 +117,23 @@ class Foo: bat: Optional[int] `, }, + "call": { + Node: &ast.Node{ + Node: &ast.Node_Call{ + Call: &ast.Call{ + Func: &ast.Node{ + Node: &ast.Node_Alias{ + Alias: &ast.Alias{ + Name: "foo", + }, + }, + }, + }, + }, + }, + Expected: `foo()`, + }, + "import": { Node: &ast.Node{ Node: &ast.Node_Import{ diff --git a/protos/python/ast.proto b/protos/python/ast.proto index da85f238de..2f1d8fac7f 100644 --- a/protos/python/ast.proto +++ b/protos/python/ast.proto @@ -19,6 +19,7 @@ message Node { Assign assign = 11 [json_name="Assign"]; Comment comment = 12 [json_name="Comment"]; Expr expr = 13 [json_name="Expr"]; + Call call = 14 [json_name="Call"]; } } @@ -48,6 +49,11 @@ message Assign string Comment = 3 [json_name="comment"]; } +message Call +{ + Node func = 1 [json_name="func"]; +} + message ClassDef { string name = 1 [json_name="name"]; @@ -99,5 +105,5 @@ message Name message Subscript { Name value = 1 [json_name="value"]; - Name slice = 2 [json_name="slice"]; + Node slice = 2 [json_name="slice"]; } From 0101a370510ea6549accc3fded264d14729b32fa Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 10 Dec 2021 18:41:26 -0800 Subject: [PATCH 11/12] Update examples with new formatting --- examples/python/src/authors/models.py | 8 +------- examples/python/src/booktest/models.py | 8 +------- examples/python/src/jets/models.py | 9 --------- examples/python/src/ondeck/models.py | 10 +++------- 4 files changed, 5 insertions(+), 30 deletions(-) diff --git a/examples/python/src/authors/models.py b/examples/python/src/authors/models.py index 298d5324bd..9cac9b2798 100644 --- a/examples/python/src/authors/models.py +++ b/examples/python/src/authors/models.py @@ -1,10 +1,6 @@ # Code generated by sqlc. DO NOT EDIT. -from typing import Optional import dataclasses - - - - +from typing import Optional @dataclasses.dataclass() @@ -12,5 +8,3 @@ class Author: id: int name: str bio: Optional[str] - - diff --git a/examples/python/src/booktest/models.py b/examples/python/src/booktest/models.py index e03d914fec..4ab5c6a28b 100644 --- a/examples/python/src/booktest/models.py +++ b/examples/python/src/booktest/models.py @@ -1,11 +1,8 @@ # Code generated by sqlc. DO NOT EDIT. -from typing import List import dataclasses import datetime import enum - - - +from typing import List class BookType(str, enum.Enum): @@ -19,7 +16,6 @@ class Author: name: str - @dataclasses.dataclass() class Book: book_id: int @@ -30,5 +26,3 @@ class Book: year: int available: datetime.datetime tags: List[str] - - diff --git a/examples/python/src/jets/models.py b/examples/python/src/jets/models.py index 5abcfd13a7..67e2d4bffb 100644 --- a/examples/python/src/jets/models.py +++ b/examples/python/src/jets/models.py @@ -2,10 +2,6 @@ import dataclasses - - - - @dataclasses.dataclass() class Jet: id: int @@ -15,24 +11,19 @@ class Jet: color: str - @dataclasses.dataclass() class Language: id: int language: str - @dataclasses.dataclass() class Pilot: id: int name: str - @dataclasses.dataclass() class PilotLanguage: pilot_id: int language_id: int - - diff --git a/examples/python/src/ondeck/models.py b/examples/python/src/ondeck/models.py index 97be8762e5..8ea4e6835a 100644 --- a/examples/python/src/ondeck/models.py +++ b/examples/python/src/ondeck/models.py @@ -1,14 +1,12 @@ # Code generated by sqlc. DO NOT EDIT. -from typing import List, Optional import dataclasses import datetime import enum +from typing import List, Optional - - -# Venues can be either open or closed class Status(str, enum.Enum): + """Venues can be either open or closed""" OPEN = "op!en" CLOSED = "clo@sed" @@ -19,9 +17,9 @@ class City: name: str -# Venues are places where muisc happens @dataclasses.dataclass() class Venue: + """Venues are places where muisc happens""" id: int status: Status statuses: Optional[List[Status]] @@ -33,5 +31,3 @@ class Venue: songkick_id: Optional[str] tags: Optional[List[str]] created_at: datetime.datetime - - From abc8f8281c645c7734180ae659c3957aef713892 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Fri, 10 Dec 2021 18:45:50 -0800 Subject: [PATCH 12/12] Remove temp files --- .../python/__pycache__/ast.cpython-39.pyc | Bin 278 -> 0 bytes internal/python/dump.py | 9 ----- internal/python/models.py | 12 ------ internal/python/out.py | 2 - internal/python/out.txt | 35 ------------------ 5 files changed, 58 deletions(-) delete mode 100644 internal/python/__pycache__/ast.cpython-39.pyc delete mode 100644 internal/python/dump.py delete mode 100644 internal/python/models.py delete mode 100644 internal/python/out.py delete mode 100644 internal/python/out.txt diff --git a/internal/python/__pycache__/ast.cpython-39.pyc b/internal/python/__pycache__/ast.cpython-39.pyc deleted file mode 100644 index 01398c6f218d446b0876b38b07cae52fefa1f756..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 278 zcmYk0u};J=42F|5?G;3KUZKl{2SA7?U;!4EQ&juJ-K9y0)1j=)_aH1hP*$eC0t@&M z1D5<-pXAT7?YeWpwOu~!f#>H5{@04(mYdcbAV50tBv7K*EA9$4s5c1}XsC}BG_X1{ zOWwN|eY>zD?p_;9c^&6+ww?U2$#cYqUKKqCz|x&?GlX$OpJ~x7^s+QN56pdCDt1d9 z)6VCjVHzWYO@u{&iS5CNhWFp@}K%amZxo#SMmSOx`*Z=lmSJ`Fp6n T<{u`9H