From 8ce0c144b668387bc7bdb35824aebbc2257796ac Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 15 Nov 2024 00:08:29 +0000 Subject: [PATCH 1/2] Add support for generated files with custom imports If the source files of a py_library are generated, and the library has custom `imports`, we need to add both the imports path and the generated dir's relative import path to the MYPYPATH --- examples/demo/generated_file_imports/BUILD.bazel | 7 +++++++ examples/demo/generated_file_imports/foo.py | 3 +++ .../demo/generated_file_imports/nested/BUILD.bazel | 12 ++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 examples/demo/generated_file_imports/BUILD.bazel create mode 100644 examples/demo/generated_file_imports/foo.py create mode 100644 examples/demo/generated_file_imports/nested/BUILD.bazel diff --git a/examples/demo/generated_file_imports/BUILD.bazel b/examples/demo/generated_file_imports/BUILD.bazel new file mode 100644 index 0000000..84e2adb --- /dev/null +++ b/examples/demo/generated_file_imports/BUILD.bazel @@ -0,0 +1,7 @@ +py_binary( + name = "foo", + srcs = ["foo.py"], + deps = [ + "//generated_file_imports/nested:bar", + ], +) diff --git a/examples/demo/generated_file_imports/foo.py b/examples/demo/generated_file_imports/foo.py new file mode 100644 index 0000000..8f62357 --- /dev/null +++ b/examples/demo/generated_file_imports/foo.py @@ -0,0 +1,3 @@ +from bar import foo + +foo() diff --git a/examples/demo/generated_file_imports/nested/BUILD.bazel b/examples/demo/generated_file_imports/nested/BUILD.bazel new file mode 100644 index 0000000..eb55570 --- /dev/null +++ b/examples/demo/generated_file_imports/nested/BUILD.bazel @@ -0,0 +1,12 @@ +genrule( + name = "generate_source", + outs = ["bar.py"], + cmd = "echo 'def foo(): print(\"hi\")' > $(OUTS)", +) + +py_library( + name = "bar", + srcs = ["bar.py"], + imports = ["."], + visibility = ["//visibility:public"], +) From 2bb9d3afec9819de379a4498800d2fbb1e5d65a8 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 15 Nov 2024 00:11:01 +0000 Subject: [PATCH 2/2] fix --- mypy/private/mypy.bzl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mypy/private/mypy.bzl b/mypy/private/mypy.bzl index 7ca94f2..36a330f 100644 --- a/mypy/private/mypy.bzl +++ b/mypy/private/mypy.bzl @@ -80,10 +80,14 @@ def _mypy_impl(target, ctx): # file roots? unique_generated_dirs = generated_dirs.keys() + generated_custom_imports = [] + for generated_dir in unique_generated_dirs: + for custom_import in custom_imports: + generated_custom_imports.append("{}/{}".format(generated_dir, custom_import)) # types need to appear first in the mypy path since the module directories # are the same and mypy resolves the first ones, first. - mypy_path = ":".join(types + external_deps + custom_imports + unique_generated_dirs) + mypy_path = ":".join(types + external_deps + custom_imports + unique_generated_dirs + generated_custom_imports) output_file = ctx.actions.declare_file(ctx.rule.attr.name + ".mypy_stdout")