Skip to main content

Hatch Build Hook for Polylith

A plugin for Hatch and the Polylith Architecture.

This build hook will look for Polylith bricks in pyproject.toml and optionally re-write the imports made in the source code.

Installation

[build-system]
requires = ["hatchling", "hatch-polylith-bricks"]
build-backend = "hatchling.build"

[tool.hatch.build.hooks.polylith-bricks]
# NOTE: this section is needed to enable the hook in the build process, even if empty

This Build Hook has two main usages:

  • identify the included Polylith bricks from the pyproject.toml, and hand them over to the Hatch build process.
  • add support for building Python libraries by re-writing source code with a custom top namespace.

Bricks are added to a project with relative paths, from the bases and components folders in a Polylith Workspace. The hook will add the bricks to the Hatch in-memory build config (force-include) provided by the Hatch build process. This will make the built wheel and sdist include proper paths to the source code.

Polylith Bricks are defined in the tool.polylith.bricks section of the pyproject.toml:

[tool.polylith.bricks]
"../../bases/my_namespace/my_base" = "my_namespace/my_base"
"../../components/my_namespace/my_component" = "my_namespace/my_component

Polylith documentation

the Python tools for the Polylith Architecture

Why re-write code?

Building libraries is supported in the Python tools for the Polylith Architecture, but you will need to consider that code will share the same top namespace with any other library built from the same monorepo.

This can be a problem when more than one of your libraries are installed into the same virtual environment. Python libraries by default are installed in a "flat" folder structure, two libraries with the same top namespace will collide.

A Solution: add a custom top namespace during packaging of the library with Hatch and this build hook plugin.

How is this done?

The code in this repo uses AST (Abstract Syntax Tree) parsing to modify source code. The Python built-in ast module is used to parse and un-parse Python code.

What's the output from this plugin?

Without any custom namespace in the configuration: no changes in the code. Building and packaging as-is.

With a Top Namespace configuration

[tool.polylith.build]
top-namespace = "my_custom_namespace"
my_custom_namespace/
    my_namespace/
        /my_package
           __init__.py
           my_module.py

Before:

from my_namespace.my_package import my_function

After:

from my_custom_namespace.my_namespace.my_package import my_function

Usage

Key Default Description
work-dir .polylith_tmp The temporary working directory for copying and re-writing source code.

Polylith documentation

the Python tools for the Polylith Architecture

Metadata

Release files for hatch-polylith-bricks 1.5.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hatch-polylith-bricks 1.5.7
File Size Uploaded
hatch_polylith_bricks-1.5.7.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hatch-polylith-bricks 1.5.7
File Interpreter ABI Platform
hatch_polylith_bricks-1.5.7-py3-none-any.whl Python 3 none any Details

Total release size: 22.2 kB

Release files / hatch_polylith_bricks-1.5.7.tar.gz

Download URL hatch_polylith_bricks-1.5.7.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0195a8b9ce72aef1a4ef3836aa20e8cd853c1634cf4dd8dd5aabccab7436aeee
BLAKE2b-256 checksum
How to use checksums
17c87360e0a5ba40d1e7b18c09974858917f5204b49bd8d1262e079e445e16a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.1

Release files / hatch_polylith_bricks-1.5.7-py3-none-any.whl

Download URL hatch_polylith_bricks-1.5.7-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a854e16d022f591516c5afc85905c4bab4adbd4a59b517b7cb9fef3e6869e10
BLAKE2b-256 checksum
How to use checksums
57b907b999da994900ae2a2fefa2ed1e9d7b887aa076b4bdc849ee1432e75a27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.1

Release history Release notifications | RSS feed

This release

1.5.7 This release

2 release files

1.5.6

2 release files

1.5.5

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.9

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page