Skip to content

feat(py_test): Allows passing tags to generated py_pytest_main. #580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ElanHR
Copy link

@ElanHR ElanHR commented Jun 6, 2025

Passing tags to py_pytest_main allows them to be excluded from mypy type checking.

Current implementation of pytest-generated code does not adhere to strict type safety and gives the following mypy errors:

bazel-out/.../my_test.pytest_main.py:37: error: Argument 1 to "open" has incompatible type "str | None"; expected "int | str | bytes | PathLike[str] | PathLike[bytes]"  [arg-type]
bazel-out/.../my_test.pytest_main.py:78: error: Argument 1 to "int" has incompatible type "str | None"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
bazel-out/.../my_test.pytest_main.py:84: error: Argument 1 to "Path" has incompatible type "str | None"; expected "str | PathLike[str]"  [arg-type]
bazel-out/.../my_test.pytest_main.py:109: error: Unsupported left operand type for + ("None")  [operator]
bazel-out/.../my_test.pytest_main.py:109: note: Left operand is of type "str | None"
bazel-out/.../my_test.pytest_main.py:114: error: Argument 1 to "open" has incompatible type "str | None"; expected "int | str | bytes | PathLike[str] | PathLike[bytes]"  [arg-type]

The current workaround to exclude these tests via tags however these are not currently passed to generated py_pytest_main.


Changes are visible to end-users: yes

  • Searched for relevant documentation and updated as needed: yes
  • Breaking change (forces users to change their own code or config): no
  • Suggested release notes appear below: yes

Enables py_test tags passing to generated py_pytest_main

Test plan

Build/Unit tests

bazel build //...
bazel test //...
  • Manual testing; please provide instructions so we can reproduce:

tools/aspects.bzl:

load("@pip_types//:types.bzl", "types")
load("@rules_mypy//mypy:mypy.bzl", "mypy")

mypy_aspect = mypy(
	mypy_ini = "@@//:mypy.ini",
	types = types,
	suppression_tags = ["no-mypy", "no-checks"],
)

...<path_to_my_test>/BUILD.bazel:

py_test (
  name = "my_test",
  srcs = ["my_test.py"],
  pytest_main = True,
  deps = [
     ...
  ],
  tags = ["no-mypy"],
)

No mypy errors are raised.

@CLAassistant
Copy link

CLAassistant commented Jun 6, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

aspect-workflows bot commented Jun 6, 2025

Test

1 test target passed

Targets
//docs:update_2_test [k8-fastbuild]                 80ms

Total test execution time was 80ms. 38 tests (97.4%) were fully cached saving 1m 9s.

@arrdem
Copy link
Collaborator

arrdem commented Jun 6, 2025

Is this required with #577? In general I'm not convinced there's correct behavior for macros to pass tags down to the other rules/macros they invoke, so this feels like the wrong approach to me.

@arrdem arrdem self-requested a review June 6, 2025 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants