bazel-contrib/bazel-mypy-integration

main_mypy.runfiles is not a valid Python package name

Opened this issue · 1 comments

This seems like it could be similar to #15 but I'm opening a new issue because it has nothing to do with protobuf. I have a plain Python application with no external dependencies that only uses rules_python and Bzlmod.

I am consistently getting the error

main_mypy.runfiles is not a valid Python package name

when running bazel test.

My source file is

main.py

def sayHello():
    return f"Hello, world!"

if __name__ == "__main__":
    print(sayHello())

and my BUILD.bazel contains:

py_binary(
    name = "main",
    srcs = ["main.py"],
    visibility = ["//:__subpackages__"],
)

mypy_test(
    name = "main_mypy",
    deps = [
        ":main",
    ],
)

Lines from relevant files:

MODULE.bazel

bazel_dep(
    name = "rules_python",
    version = "0.31.0",
)
bazel_dep(
    name = "rules_python_gazelle_plugin",
    version = "0.31.0",
)

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    configure_coverage_tool = True,
    python_version = "3.12",
)

bazel_dep(
    name = "mypy_integration",
    version = "0.0.0",
)

git_override(
    module_name = "mypy_integration",
    remote = "https://github.com/bazel-contrib/bazel-mypy-integration",
    commit = "c289c0cd30e53b5cc467b601295834dbc400f4b2",
)

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
    hub_name = "mypy_pip",
    python_version = "3.12",
    requirements_lock = "//third_party/python/mypy:requirements_lock.txt",
)
use_repo(pip, "mypy_pip")

.bazelrc

build --@mypy_integration//:mypy=//third_party/python/mypy:mypy

third_party/python/mypy/BUILD.bazel

load("@mypy_pip//:requirements.bzl", "requirement")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

compile_pip_requirements(
    name = "requirements",
    src = "requirements.txt",
    requirements_txt = "requirements_lock.txt",
)

alias(
    name = "mypy",
    actual = requirement("mypy"),
    visibility = ["//visibility:public"],
)

I was able to work around this by adding

build --incompatible_default_to_explicit_init_py

to .bazelrc, which requires me to manually create __init__.py for my Python modules. Not ideal, but workable.