Skip to content

Gazelle seems to not like PEP 695 – Type Parameter Syntax #2396

Closed
@dougthor42

Description

@dougthor42

TL;DR: I think we need to bump go-tree-sitter for python 3.12 support.

🐞 bug report

Affected Rule

Gazelle

Is this a regression?

I could argue it either way, lol. I'm going to go with "probably" because it's related to #1895.

Description

PEP 695 added a Type Parameter syntax that looks like:

# Before
from typing import TypeVar

_T = TypeVar("_T")

def func(a: _T, b: _T) -> _T:
    ...

# After
def func[T](a: T, b: T) -> T:
    ...

If a python file uses this new def func[T](...) syntax, and the file has if __name__ == "__main__", then Gazelle will incorrectly generate a py_library target rather than py_binary.

🔬 Minimal Reproduction

  1. Make a python file:
# foo.py
def func[T](a: T, b: T) -> T:
    ...

if __name__ == "__main__":
    print("hi")
  1. Run gazelle bazel run //:gazelle

Expected Result

py_binary(
    name = "foo",
    srcs = ["foo.py"],
)

Actual Result

py_library(
    name = "foo",
    srcs = ["foo.py"],
)

🔥 Exception or Error

None

🌍 Your Environment

Operating System:

gLinux

Output of bazel version:

$ bazel version
Bazelisk version: v1.20.0
Build label: 7.3.2
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Tue Oct 1 17:46:05 2024 (1727804765)
Build timestamp: 1727804765
Build timestamp as int: 1727804765

Rules_python version:

$ bazel mod graph                                                                                                       
<root> (pyle@0.0.0)                    
├───aspect_bazel_lib@2.7.1            
│   └─── ...
├───bazel_skylib@1.7.1                 
│   └─── ...
├───buildifier_prebuilt@6.4.0             
│   └─── ...     
├───gazelle@0.35.0                 
│   └─── ...
├───rules_helm@0.7.0 
│   └─── ...
├───rules_python@0.37.2 
│   ├───bazel_skylib@1.7.1 (*) 
│   ├───platforms@0.0.9 (*) 
│   ├───protobuf@24.4 (*) 
│   ├───rules_proto@6.0.0-rc1 (*) 
│   ├───stardoc@0.6.2 (*) 
│   ├───bazel_features@1.11.0 
│   │   └───bazel_skylib@1.7.1 (*) 
│   └───rules_cc@0.0.9 
│       └───platforms@0.0.9 (*) 
└───rules_python_gazelle_plugin@0.37.2 
    ├───bazel_skylib@1.7.1 (*) 
    ├───gazelle@0.35.0 (*) 
    ├───rules_go@0.45.1 (*) 
    └───rules_python@0.37.2 (*)

Anything else relevant?

I haven't been able to find a human-readable changelog of tree-sitter or go-tree-sitter that explicitly says when it added support for python 3.12...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions