Skip to content

Commit 175a336

Browse files
yushan26yushan8
andauthored
refactor(gazelle) Types for exposed members of python.ParserOutput are now all public (#2959)
Export the members of `python.ParserOutput` struct to make it publicly accessible. This allows other `py` extensions to leverage the Python resolver logic for resolving Python imports, instead of have to duplicate the resolving logic. --------- Co-authored-by: yushan <[email protected]>
1 parent 107a878 commit 175a336

File tree

9 files changed

+56
-35
lines changed

9 files changed

+56
-35
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ BEGIN_UNRELEASED_TEMPLATE
4747
END_UNRELEASED_TEMPLATE
4848
-->
4949

50+
{#v0-0-0}
51+
## Unreleased
52+
53+
[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0
54+
55+
{#v0-0-0-changed}
56+
### Changed
57+
* (gazelle) Types for exposed members of `python.ParserOutput` are now all public.
58+
59+
{#v0-0-0-fixed}
60+
### Fixed
61+
* Nothing fixed.
62+
63+
{#v0-0-0-added}
64+
### Added
65+
* Nothing added.
66+
67+
{#v0-0-0-removed}
68+
### Removed
69+
* Nothing removed.
70+
5071
{#1-5-0}
5172
## [1.5.0] - 2025-06-11
5273

gazelle/python/file_parser.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const (
4141

4242
type ParserOutput struct {
4343
FileName string
44-
Modules []module
45-
Comments []comment
44+
Modules []Module
45+
Comments []Comment
4646
HasMain bool
4747
}
4848

@@ -127,24 +127,24 @@ func (p *FileParser) parseMain(ctx context.Context, node *sitter.Node) bool {
127127
return false
128128
}
129129

130-
// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
130+
// parseImportStatement parses a node for an import statement, returning a `Module` and a boolean
131131
// representing if the parse was OK or not.
132-
func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
132+
func parseImportStatement(node *sitter.Node, code []byte) (Module, bool) {
133133
switch node.Type() {
134134
case sitterNodeTypeDottedName:
135-
return module{
135+
return Module{
136136
Name: node.Content(code),
137137
LineNumber: node.StartPoint().Row + 1,
138138
}, true
139139
case sitterNodeTypeAliasedImport:
140140
return parseImportStatement(node.Child(0), code)
141141
case sitterNodeTypeWildcardImport:
142-
return module{
142+
return Module{
143143
Name: "*",
144144
LineNumber: node.StartPoint().Row + 1,
145145
}, true
146146
}
147-
return module{}, false
147+
return Module{}, false
148148
}
149149

150150
// parseImportStatements parses a node for import statements, returning true if the node is
@@ -188,7 +188,7 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
188188
// It updates FileParser.output.Comments with the parsed comment.
189189
func (p *FileParser) parseComments(node *sitter.Node) bool {
190190
if node.Type() == sitterNodeTypeComment {
191-
p.output.Comments = append(p.output.Comments, comment(node.Content(p.code)))
191+
p.output.Comments = append(p.output.Comments, Comment(node.Content(p.code)))
192192
return true
193193
}
194194
return false

gazelle/python/file_parser_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestParseImportStatements(t *testing.T) {
2727
name string
2828
code string
2929
filepath string
30-
result []module
30+
result []Module
3131
}{
3232
{
3333
name: "not has import",
@@ -39,7 +39,7 @@ func TestParseImportStatements(t *testing.T) {
3939
name: "has import",
4040
code: "import unittest\nimport os.path\nfrom foo.bar import abc.xyz",
4141
filepath: "abc.py",
42-
result: []module{
42+
result: []Module{
4343
{
4444
Name: "unittest",
4545
LineNumber: 1,
@@ -66,7 +66,7 @@ func TestParseImportStatements(t *testing.T) {
6666
import unittest
6767
`,
6868
filepath: "abc.py",
69-
result: []module{
69+
result: []Module{
7070
{
7171
Name: "unittest",
7272
LineNumber: 2,
@@ -79,7 +79,7 @@ func TestParseImportStatements(t *testing.T) {
7979
name: "invalid syntax",
8080
code: "import os\nimport",
8181
filepath: "abc.py",
82-
result: []module{
82+
result: []Module{
8383
{
8484
Name: "os",
8585
LineNumber: 1,
@@ -92,7 +92,7 @@ func TestParseImportStatements(t *testing.T) {
9292
name: "import as",
9393
code: "import os as b\nfrom foo import bar as c# 123",
9494
filepath: "abc.py",
95-
result: []module{
95+
result: []Module{
9696
{
9797
Name: "os",
9898
LineNumber: 1,
@@ -111,7 +111,7 @@ func TestParseImportStatements(t *testing.T) {
111111
{
112112
name: "complex import",
113113
code: "from unittest import *\nfrom foo import (bar as c, baz, qux as d)\nfrom . import abc",
114-
result: []module{
114+
result: []Module{
115115
{
116116
Name: "unittest.*",
117117
LineNumber: 1,
@@ -152,7 +152,7 @@ func TestParseComments(t *testing.T) {
152152
units := []struct {
153153
name string
154154
code string
155-
result []comment
155+
result []Comment
156156
}{
157157
{
158158
name: "not has comment",
@@ -162,17 +162,17 @@ func TestParseComments(t *testing.T) {
162162
{
163163
name: "has comment",
164164
code: "# a = 1\n# b = 2",
165-
result: []comment{"# a = 1", "# b = 2"},
165+
result: []Comment{"# a = 1", "# b = 2"},
166166
},
167167
{
168168
name: "has comment in if",
169169
code: "if True:\n # a = 1\n # b = 2",
170-
result: []comment{"# a = 1", "# b = 2"},
170+
result: []Comment{"# a = 1", "# b = 2"},
171171
},
172172
{
173173
name: "has comment inline",
174174
code: "import os# 123\nfrom pathlib import Path as b#456",
175-
result: []comment{"# 123", "#456"},
175+
result: []Comment{"# 123", "#456"},
176176
},
177177
}
178178
for _, u := range units {
@@ -248,7 +248,7 @@ func TestParseFull(t *testing.T) {
248248
output, err := p.Parse(context.Background())
249249
assert.NoError(t, err)
250250
assert.Equal(t, ParserOutput{
251-
Modules: []module{{Name: "bar.abc", LineNumber: 1, Filepath: "foo/a.py", From: "bar"}},
251+
Modules: []Module{{Name: "bar.abc", LineNumber: 1, Filepath: "foo/a.py", From: "bar"}},
252252
Comments: nil,
253253
HasMain: false,
254254
FileName: "a.py",

gazelle/python/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
471471

472472
for _, pyTestTarget := range pyTestTargets {
473473
if conftest != nil {
474-
pyTestTarget.addModuleDependency(module{Name: strings.TrimSuffix(conftestFilename, ".py")})
474+
pyTestTarget.addModuleDependency(Module{Name: strings.TrimSuffix(conftestFilename, ".py")})
475475
}
476476
pyTest := pyTestTarget.build()
477477

gazelle/python/parser.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ func removeDupesFromStringTreeSetSlice(array []string) []string {
145145
return dedupe
146146
}
147147

148-
// module represents a fully-qualified, dot-separated, Python module as seen on
148+
// Module represents a fully-qualified, dot-separated, Python module as seen on
149149
// the import statement, alongside the line number where it happened.
150-
type module struct {
150+
type Module struct {
151151
// The fully-qualified, dot-separated, Python module name as seen on import
152152
// statements.
153153
Name string `json:"name"`
@@ -162,7 +162,7 @@ type module struct {
162162

163163
// moduleComparator compares modules by name.
164164
func moduleComparator(a, b interface{}) int {
165-
return godsutils.StringComparator(a.(module).Name, b.(module).Name)
165+
return godsutils.StringComparator(a.(Module).Name, b.(Module).Name)
166166
}
167167

168168
// annotationKind represents Gazelle annotation kinds.
@@ -176,12 +176,12 @@ const (
176176
annotationKindIncludeDep annotationKind = "include_dep"
177177
)
178178

179-
// comment represents a Python comment.
180-
type comment string
179+
// Comment represents a Python comment.
180+
type Comment string
181181

182182
// asAnnotation returns an annotation object if the comment has the
183183
// annotationPrefix.
184-
func (c *comment) asAnnotation() (*annotation, error) {
184+
func (c *Comment) asAnnotation() (*annotation, error) {
185185
uncomment := strings.TrimLeft(string(*c), "# ")
186186
if !strings.HasPrefix(uncomment, annotationPrefix) {
187187
return nil, nil
@@ -215,7 +215,7 @@ type annotations struct {
215215

216216
// annotationsFromComments returns all the annotations parsed out of the
217217
// comments of a Python module.
218-
func annotationsFromComments(comments []comment) (*annotations, error) {
218+
func annotationsFromComments(comments []Comment) (*annotations, error) {
219219
ignore := make(map[string]struct{})
220220
includeDeps := []string{}
221221
for _, comment := range comments {

gazelle/python/resolve.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (py *Resolver) Resolve(
151151
hasFatalError := false
152152
MODULES_LOOP:
153153
for it.Next() {
154-
mod := it.Value().(module)
154+
mod := it.Value().(Module)
155155
moduleParts := strings.Split(mod.Name, ".")
156156
possibleModules := []string{mod.Name}
157157
for len(moduleParts) > 1 {
@@ -214,7 +214,7 @@ func (py *Resolver) Resolve(
214214
matches := ix.FindRulesByImportWithConfig(c, imp, languageName)
215215
if len(matches) == 0 {
216216
// Check if the imported module is part of the standard library.
217-
if isStdModule(module{Name: moduleName}) {
217+
if isStdModule(Module{Name: moduleName}) {
218218
continue MODULES_LOOP
219219
} else if cfg.ValidateImportStatements() {
220220
err := fmt.Errorf(

gazelle/python/std_modules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func init() {
3434
}
3535
}
3636

37-
func isStdModule(m module) bool {
37+
func isStdModule(m Module) bool {
3838
_, ok := stdModules[m.Name]
3939
return ok
4040
}

gazelle/python/std_modules_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestIsStdModule(t *testing.T) {
24-
assert.True(t, isStdModule(module{Name: "unittest"}))
25-
assert.True(t, isStdModule(module{Name: "os.path"}))
26-
assert.False(t, isStdModule(module{Name: "foo"}))
24+
assert.True(t, isStdModule(Module{Name: "unittest"}))
25+
assert.True(t, isStdModule(Module{Name: "os.path"}))
26+
assert.False(t, isStdModule(Module{Name: "foo"}))
2727
}

gazelle/python/target.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (t *targetBuilder) addSrcs(srcs *treeset.Set) *targetBuilder {
6969
}
7070

7171
// addModuleDependency adds a single module dep to the target.
72-
func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
72+
func (t *targetBuilder) addModuleDependency(dep Module) *targetBuilder {
7373
fileName := dep.Name + ".py"
7474
if dep.From != "" {
7575
fileName = dep.From + ".py"
@@ -87,7 +87,7 @@ func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
8787
func (t *targetBuilder) addModuleDependencies(deps *treeset.Set) *targetBuilder {
8888
it := deps.Iterator()
8989
for it.Next() {
90-
t.addModuleDependency(it.Value().(module))
90+
t.addModuleDependency(it.Value().(Module))
9191
}
9292
return t
9393
}

0 commit comments

Comments
 (0)