Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 53f8a26

Browse filesBrowse files
committed
Add python directory structure and code. No bazel yet.
1 parent 24a910d commit 53f8a26
Copy full SHA for 53f8a26

File tree

6 files changed

+133
-0
lines changed
Filter options

6 files changed

+133
-0
lines changed
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Using a `src` dir and separate `tests` dir, with bzlmod
2+
3+
This example highlights how to set up `MODULE.bazel`, `BUILD.bazel`, and `gazelle` to work with
4+
a python `src` directory and a separate `tests` directory[^1].
5+
6+
7+
Run tests by first `cd`ing into this directory and then running `bazel test`:
8+
9+
```shell
10+
$ cd examples/bzlmod_python_src_dir_with_separate_tests_dir
11+
$ bazel test --test_output=errors //...
12+
```
13+
14+
Everything should pass.
15+
16+
Try changing `tests/test_my_python_module.py`'s assert to a different value and run
17+
`bazel test` again. You'll see a test failure, yay!
18+
19+
20+
[^1]: This is how the [Python Packaging User Guide][pypa-tutorial] recommends new python libraries
21+
be set up.
22+
23+
[pypa-tutorial]: https://github.com/pypa/packaging.python.org/blob/091e45c8f78614307ccfdc061a6e562d669b178b/source/tutorials/packaging-projects.rst
24+
25+
26+
## Details
27+
28+
The folder structure, prior to adding Bazel, is:
29+
30+
```
31+
./
32+
├── pyproject.toml
33+
├── README.md
34+
├── src/
35+
│ └── my_package/
36+
│ ├── __init__.py
37+
│ └── my_python_module.py
38+
└── tests/
39+
├── __init__.py
40+
└── test_my_python_module.py
41+
```
42+
43+
After adding files and configuration for Bazel and gazelle:
44+
45+
```
46+
packaging_tutorial/
47+
├── BUILD.bazel # New
48+
├── gazelle_python.yaml # New, empty
49+
├── MODULE.bazel # New
50+
├── pyproject.toml
51+
├── README.md
52+
├── requirements.lock # New, empty
53+
├── src/
54+
│ ├── BUILD.bazel # New
55+
│ └── mypackage/
56+
│ ├── __init__.py
57+
│ └── my_python_module.py
58+
└── tests/
59+
├── __init__.py
60+
└── test_my_python_module.py
61+
```
62+
63+
After running Gazelle:
64+
65+
```shell
66+
$ bazel run //:requirements.update
67+
$ bazel run //:gazelle_python_manifest.update
68+
$ bazel run //:gazelle
69+
```
70+
71+
```
72+
packaging_tutorial/
73+
├── BUILD.bazel
74+
├── gazelle_python.yaml # Updated by 'bazel run //:gazelle_python_manifest.update'
75+
├── MODULE.bazel
76+
├── MODULE.bazel.lock # New, not included in git repo
77+
├── pyproject.toml
78+
├── README.md
79+
├── requirements.lock # Updated by 'bazel run //:requirements.update'
80+
├── src/
81+
│ ├── BUILD.bazel
82+
│ └── mypackage/
83+
│ ├── __init__.py
84+
│ ├── BUILD.bazel # New
85+
│ └── my_python_module.py
86+
└── tests/
87+
├── __init__.py
88+
├── BUILD.bazel # New
89+
└── test_my_python_module.py
90+
```
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "my_package"
7+
version = "0.0.1"
8+
description = "Example of using Bazel with python `src` and `tests` dir"
9+
dependencies = [
10+
"pathspec==0.12.1",
11+
]
12+
13+
[tool.setuptools]
14+
package-dir = {"" = "src"}
15+
16+
[tool.setuptools.packages.find]
17+
where = ["src"]

‎examples/bzlmod_python_src_dir_with_separate_tests_dir/src/my_package/__init__.py

Copy file name to clipboardExpand all lines: examples/bzlmod_python_src_dir_with_separate_tests_dir/src/my_package/__init__.py
Whitespace-only changes.
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# A import something small and benign so that we can showcase installing packages from pip
2+
import pathspec
3+
4+
# Satisfy linters by ignoring the import.
5+
del pathspec
6+
7+
8+
def my_func(a: int) -> int:
9+
return a + 5

‎examples/bzlmod_python_src_dir_with_separate_tests_dir/tests/__init__.py

Copy file name to clipboardExpand all lines: examples/bzlmod_python_src_dir_with_separate_tests_dir/tests/__init__.py
Whitespace-only changes.
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
3+
from my_package import my_python_module
4+
5+
6+
class TestMyFunc(unittest.TestCase):
7+
def test_good_values(self) -> None:
8+
got = my_python_module.my_func(0)
9+
self.assertEqual(got, 5)
10+
11+
def test_bad_values(self) -> None:
12+
with self.assertRaises(TypeError):
13+
my_python_module.my_func(int)
14+
15+
16+
if __name__ == "__main__":
17+
unittest.main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.