Hi, We have a Python project, a restful API client for a web service. Alongside the API client, we provided a mock package for users to test their project with the service without actually connecting to the external service.
The mock package contains some test data, so users may want to leave them out of their production environment. So we want to provide the mock package using extras.
We have the following pyproject.toml file:
[project]
name = "gcp"
dependencies = [
...
]
dynamic = ["version"]
[project.optional-dependencies]
dev = [
...
]
mock = []
[options]
packages = ["gcp"]
[options.extras_require]
mock = ["gcpmock"]
[build-system]
requires = ["setuptools>=45", "setuptools_scm>=7.0.0"]
build-backend = "setuptools.build_meta"
[tool.setuptools]
package-dir = {"" = "src"}
[tool.setuptools.package-data]
gcp = ["py.typed"]
gcpmock = ["py.typed"]
We hope pip install gcp
will leave out the gcpmock
package. If users want to install gcpmock for testing, they can do pip install "gcp[mock]"
. If we use python -m build
to build the package and then upload it to a PyPI server, pip install gcp
will also install the gcpmock
package.
I think one way to do this is: during the build, we generate the gcpmock
as a separate package and then add it to the optional dependencies of gcp
. So we will end up with two separate packages on the PyPI server. But those two packages can share a single repository and, more importantly, a single pyproject.toml. In this way, we can easily ensure the API methods’ signatures in gcpmock
are matched with those in the real gcp
package. And we can share a lot of tool configs in the same pyproject.toml file.
There is an old topic discussing about the similar needs. I don’t know if I should bring up an old topic so I created a new one.
I think this can also help this topic. If the WebSocket support related package is not installed without the extra, Unicorn won’t need to worry about the user accidentally installing an unmatched webscockets
package.