From c3ca538604ecde8c5c200c51825742d3d0146a18 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Thu, 14 Nov 2024 23:02:32 +0000 Subject: [PATCH] Add imports from current target to MYPYPATH The previous handling I added only applied to a target's deps, if you have a standalone py_library with custom imports, this is necessary for the same reason --- examples/demo/standalone_imports/BUILD.bazel | 5 +++++ examples/demo/standalone_imports/a.py | 2 ++ examples/demo/standalone_imports/b.py | 1 + mypy/private/mypy.bzl | 3 +++ 4 files changed, 11 insertions(+) create mode 100644 examples/demo/standalone_imports/BUILD.bazel create mode 100644 examples/demo/standalone_imports/a.py create mode 100644 examples/demo/standalone_imports/b.py diff --git a/examples/demo/standalone_imports/BUILD.bazel b/examples/demo/standalone_imports/BUILD.bazel new file mode 100644 index 0000000..3279c76 --- /dev/null +++ b/examples/demo/standalone_imports/BUILD.bazel @@ -0,0 +1,5 @@ +py_library( + name = "foo", + srcs = glob(["*.py"]), + imports = ["."], +) diff --git a/examples/demo/standalone_imports/a.py b/examples/demo/standalone_imports/a.py new file mode 100644 index 0000000..9332a27 --- /dev/null +++ b/examples/demo/standalone_imports/a.py @@ -0,0 +1,2 @@ +def foo(): + pass diff --git a/examples/demo/standalone_imports/b.py b/examples/demo/standalone_imports/b.py new file mode 100644 index 0000000..b280e74 --- /dev/null +++ b/examples/demo/standalone_imports/b.py @@ -0,0 +1 @@ +from a import foo diff --git a/mypy/private/mypy.bzl b/mypy/private/mypy.bzl index 7ca94f2..8f813b8 100644 --- a/mypy/private/mypy.bzl +++ b/mypy/private/mypy.bzl @@ -56,6 +56,9 @@ def _mypy_impl(target, ctx): if dep.label in type_mapping ] + if PyInfo in target: + custom_imports.extend([x.split("/", 1)[-1] for x in target[PyInfo].imports.to_list()]) + for dep in (ctx.rule.attr.deps + additional_types): depsets.append(dep.default_runfiles.files)