Skip to content

refactor(gazelle) Types for exposed members of python.ParserOutput are now all public #2959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ BEGIN_UNRELEASED_TEMPLATE
END_UNRELEASED_TEMPLATE
-->

{#v0-0-0}
## Unreleased

[0.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.0.0

{#v0-0-0-changed}
### Changed
* (gazelle) Types for exposed members of `python.ParserOutput` are now all public.

{#v0-0-0-fixed}
### Fixed
* Nothing fixed.

{#v0-0-0-added}
### Added
* Nothing added.

{#v0-0-0-removed}
### Removed
* Nothing removed.

{#1-5-0}
## [1.5.0] - 2025-06-11

Expand Down
16 changes: 8 additions & 8 deletions gazelle/python/file_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ const (

type ParserOutput struct {
FileName string
Modules []module
Comments []comment
Modules []Module
Comments []Comment
HasMain bool
}

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

// parseImportStatement parses a node for an import statement, returning a `module` and a boolean
// parseImportStatement parses a node for an import statement, returning a `Module` and a boolean
// representing if the parse was OK or not.
func parseImportStatement(node *sitter.Node, code []byte) (module, bool) {
func parseImportStatement(node *sitter.Node, code []byte) (Module, bool) {
switch node.Type() {
case sitterNodeTypeDottedName:
return module{
return Module{
Name: node.Content(code),
LineNumber: node.StartPoint().Row + 1,
}, true
case sitterNodeTypeAliasedImport:
return parseImportStatement(node.Child(0), code)
case sitterNodeTypeWildcardImport:
return module{
return Module{
Name: "*",
LineNumber: node.StartPoint().Row + 1,
}, true
}
return module{}, false
return Module{}, false
}

// parseImportStatements parses a node for import statements, returning true if the node is
Expand Down Expand Up @@ -188,7 +188,7 @@ func (p *FileParser) parseImportStatements(node *sitter.Node) bool {
// It updates FileParser.output.Comments with the parsed comment.
func (p *FileParser) parseComments(node *sitter.Node) bool {
if node.Type() == sitterNodeTypeComment {
p.output.Comments = append(p.output.Comments, comment(node.Content(p.code)))
p.output.Comments = append(p.output.Comments, Comment(node.Content(p.code)))
return true
}
return false
Expand Down
22 changes: 11 additions & 11 deletions gazelle/python/file_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestParseImportStatements(t *testing.T) {
name string
code string
filepath string
result []module
result []Module
}{
{
name: "not has import",
Expand All @@ -39,7 +39,7 @@ func TestParseImportStatements(t *testing.T) {
name: "has import",
code: "import unittest\nimport os.path\nfrom foo.bar import abc.xyz",
filepath: "abc.py",
result: []module{
result: []Module{
{
Name: "unittest",
LineNumber: 1,
Expand All @@ -66,7 +66,7 @@ func TestParseImportStatements(t *testing.T) {
import unittest
`,
filepath: "abc.py",
result: []module{
result: []Module{
{
Name: "unittest",
LineNumber: 2,
Expand All @@ -79,7 +79,7 @@ func TestParseImportStatements(t *testing.T) {
name: "invalid syntax",
code: "import os\nimport",
filepath: "abc.py",
result: []module{
result: []Module{
{
Name: "os",
LineNumber: 1,
Expand All @@ -92,7 +92,7 @@ func TestParseImportStatements(t *testing.T) {
name: "import as",
code: "import os as b\nfrom foo import bar as c# 123",
filepath: "abc.py",
result: []module{
result: []Module{
{
Name: "os",
LineNumber: 1,
Expand All @@ -111,7 +111,7 @@ func TestParseImportStatements(t *testing.T) {
{
name: "complex import",
code: "from unittest import *\nfrom foo import (bar as c, baz, qux as d)\nfrom . import abc",
result: []module{
result: []Module{
{
Name: "unittest.*",
LineNumber: 1,
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestParseComments(t *testing.T) {
units := []struct {
name string
code string
result []comment
result []Comment
}{
{
name: "not has comment",
Expand All @@ -162,17 +162,17 @@ func TestParseComments(t *testing.T) {
{
name: "has comment",
code: "# a = 1\n# b = 2",
result: []comment{"# a = 1", "# b = 2"},
result: []Comment{"# a = 1", "# b = 2"},
},
{
name: "has comment in if",
code: "if True:\n # a = 1\n # b = 2",
result: []comment{"# a = 1", "# b = 2"},
result: []Comment{"# a = 1", "# b = 2"},
},
{
name: "has comment inline",
code: "import os# 123\nfrom pathlib import Path as b#456",
result: []comment{"# 123", "#456"},
result: []Comment{"# 123", "#456"},
},
}
for _, u := range units {
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestParseFull(t *testing.T) {
output, err := p.Parse(context.Background())
assert.NoError(t, err)
assert.Equal(t, ParserOutput{
Modules: []module{{Name: "bar.abc", LineNumber: 1, Filepath: "foo/a.py", From: "bar"}},
Modules: []Module{{Name: "bar.abc", LineNumber: 1, Filepath: "foo/a.py", From: "bar"}},
Comments: nil,
HasMain: false,
FileName: "a.py",
Expand Down
2 changes: 1 addition & 1 deletion gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes

for _, pyTestTarget := range pyTestTargets {
if conftest != nil {
pyTestTarget.addModuleDependency(module{Name: strings.TrimSuffix(conftestFilename, ".py")})
pyTestTarget.addModuleDependency(Module{Name: strings.TrimSuffix(conftestFilename, ".py")})
}
pyTest := pyTestTarget.build()

Expand Down
14 changes: 7 additions & 7 deletions gazelle/python/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ func removeDupesFromStringTreeSetSlice(array []string) []string {
return dedupe
}

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

// moduleComparator compares modules by name.
func moduleComparator(a, b interface{}) int {
return godsutils.StringComparator(a.(module).Name, b.(module).Name)
return godsutils.StringComparator(a.(Module).Name, b.(Module).Name)
}

// annotationKind represents Gazelle annotation kinds.
Expand All @@ -176,12 +176,12 @@ const (
annotationKindIncludeDep annotationKind = "include_dep"
)

// comment represents a Python comment.
type comment string
// Comment represents a Python comment.
type Comment string

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

// annotationsFromComments returns all the annotations parsed out of the
// comments of a Python module.
func annotationsFromComments(comments []comment) (*annotations, error) {
func annotationsFromComments(comments []Comment) (*annotations, error) {
ignore := make(map[string]struct{})
includeDeps := []string{}
for _, comment := range comments {
Expand Down
4 changes: 2 additions & 2 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (py *Resolver) Resolve(
hasFatalError := false
MODULES_LOOP:
for it.Next() {
mod := it.Value().(module)
mod := it.Value().(Module)
moduleParts := strings.Split(mod.Name, ".")
possibleModules := []string{mod.Name}
for len(moduleParts) > 1 {
Expand Down Expand Up @@ -214,7 +214,7 @@ func (py *Resolver) Resolve(
matches := ix.FindRulesByImportWithConfig(c, imp, languageName)
if len(matches) == 0 {
// Check if the imported module is part of the standard library.
if isStdModule(module{Name: moduleName}) {
if isStdModule(Module{Name: moduleName}) {
continue MODULES_LOOP
} else if cfg.ValidateImportStatements() {
err := fmt.Errorf(
Expand Down
2 changes: 1 addition & 1 deletion gazelle/python/std_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
}
}

func isStdModule(m module) bool {
func isStdModule(m Module) bool {
_, ok := stdModules[m.Name]
return ok
}
6 changes: 3 additions & 3 deletions gazelle/python/std_modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestIsStdModule(t *testing.T) {
assert.True(t, isStdModule(module{Name: "unittest"}))
assert.True(t, isStdModule(module{Name: "os.path"}))
assert.False(t, isStdModule(module{Name: "foo"}))
assert.True(t, isStdModule(Module{Name: "unittest"}))
assert.True(t, isStdModule(Module{Name: "os.path"}))
assert.False(t, isStdModule(Module{Name: "foo"}))
}
4 changes: 2 additions & 2 deletions gazelle/python/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (t *targetBuilder) addSrcs(srcs *treeset.Set) *targetBuilder {
}

// addModuleDependency adds a single module dep to the target.
func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
func (t *targetBuilder) addModuleDependency(dep Module) *targetBuilder {
fileName := dep.Name + ".py"
if dep.From != "" {
fileName = dep.From + ".py"
Expand All @@ -87,7 +87,7 @@ func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
func (t *targetBuilder) addModuleDependencies(deps *treeset.Set) *targetBuilder {
it := deps.Iterator()
for it.Next() {
t.addModuleDependency(it.Value().(module))
t.addModuleDependency(it.Value().(Module))
}
return t
}
Expand Down