Closed antspy closed 1 month ago
If I'm reading correctly, you tried:
py_pytest_main(
name = "__test__",
deps = ["@pip//pytest"],
tags = ["no-mypy"],
)
...
This will remove type-checking from this target, but not from mytest
-- you explicitly include the py_pytest_main
output as a source file in mytest
:
srcs = [
"mytest.py",
":__test__",
],
The mypy.ini exclude does not work, I think, because we explicitly pass all the source files as arguments (as far as I can tell we have to do things this way).
As a workaround, you can define a macro for your py_test that will type-check your test sources while suppressing type checking on the test itself, something like:
def custom_py_test(name, srcs = [], deps = [], tags = [], **kwargs):
py_library(
name = name + ".lib"
srcs = srcs,
deps = deps,
tags = tags,
)
py_test(
name = name,
size = "small",
srcs = [":__test__"] + srcs,
main = ":__test__.py",
package_collisions = "warning",
deps = [":__test__"] + deps,
tags = ["no-mypy"] + tags,
**kwargs
)
Aside from that, we'd need to look at adding source-level suppressions, but I think the API might end up pretty awkward.
Thanks a lot for your quick answer! Let me try it and I will report back :)
That worked! Thank you for the workaround :)
Hi,
I have setup
mypy
to automatically run on my bazel builds and tests. Thank you for the cool project! :)I am using the
py_pytest_main
rule for tests fromrules_py
like so:The problem is that the library generates this
__test.py__
file whichmypy
is not happy about. I have tried excluding it - this is the content ofmypy.ini
:but the build still fails with
bazel-out/k8-fastbuild/bin/mypath/__test__.py:42: error: Need type annotation for "user_args" (hint: "user_args: list[<type>] = ...") [var-annotated]
.I have verified that the
mypy.ini
file is taken into account, because if I changefollow_imports
fromsilent
tonormal
, a lot more errors show up. So the problem seems to be that the exclude doesn't work, for some reason.I believe the full invocation is:
I have also tried adding the
tags = ["no-mypy"],
to thepy_pytest_main
target above, but it results in the same error. (If I add the tag tomytest
, then it's respected and the build runs fine, but that will actually prevent the test from being checked. I only want to stop mypy from checking the__test__.py
file.Any help will be greatly appreciated. Thank you! :)