Skip to main content

🧹 kirei-py

モジュラモノリス + ヘキサゴナルアーキテクチャ + DDD の規約を、import-linter と Pylint で検査する。

  • import-linter: 層とモジュールの間の import の向き
  • Pylint: application・domain.model・port に書くコードの形

app.fastship.jp の backend/ の構成 (src/<モジュール>/{application,domain/model,port/adapter,exception}) を前提にする。

uv add --dev kirei-py

🏛 import の規約 (import-linter)

[tool.importlinter]
root_packages = ["billing"]
contract_types = [
    "architecture.hexagonal: kirei.importlinter.Hexagonal",
    "architecture.modularmonolithic: kirei.importlinter.ModularMonolithic",
]

[[tool.importlinter.contracts]]
id = "hexagonal"
name = "ヘキサゴナルアーキテクチャ"
type = "architecture.hexagonal"

[[tool.importlinter.contracts]]
id = "modularmonolithic"
name = "モジュラモノリス"
type = "architecture.modularmonolithic"
uv run lint-imports
  • 規約の中身 (層の順序や許可する import) は kirei のコード (kirei.importlinter) にあり、kirei-py を更新すると変わる
  • root_packages には src 直下のモジュールをすべて並べる (足し忘れは lint-imports が検出する)。空だと import-linter が動かない
  • lint-imports は pyproject.toml のあるディレクトリで実行する

プロジェクト独自の contract も [tool.importlinter] に足せる。

[tool.importlinter]
root_packages = ["billing"]
include_external_packages = true  # sqlalchemy などの外部パッケージを forbidden_modules に書くときに要る

[[tool.importlinter.contracts]]
id = "no-sqlalchemy-in-domain"
name = "ドメイン層は永続化の技術を知らない"
type = "forbidden"
source_modules = ["billing.domain"]
forbidden_modules = ["sqlalchemy"]

ヘキサゴナルアーキテクチャ (モジュール内部の依存)

core → middleware → port → application → domain → exception の向きにだけ import できる。domain を import できるのは core / application / port.adapter.persistence / port.adapter.service だけ

モジュラモノリス (モジュール間の依存)

モジュール間の import は ACL アダプター (port.adapter.service.〇〇.adapter) から相手の公開 IF (port.adapter.resource) へだけ

🧭 コードの規約 (Pylint)

[tool.pylint.main]
load-plugins = ["kirei.pylint"]
# モジュールの名前を billing.application.… にする (層はモジュールの名前で決める)
source-roots = ["src"]

[tool.pylint."messages control"]
# 一般のルールは ruff が見るので、kirei の規約だけを有効にする
disable = ["all"]
enable = ["kirei-application", "kirei-domain-model", "kirei-port", "kirei-module"]
uv run pylint src
  • 層は、モジュールの名前で決める: <モジュール>.application.**・<モジュール>.domain.model.**・<モジュール>.port.adapter.**。ほかの場所 (core.py・exception/ など) は見ない
  • fastship-py の部品 (@transactional・DIContainer・DomainEvent など) は import 文から見分けるので、kirei-py は fastship-py に依存しない
  • 親クラスを推論できないとき (親のパッケージが入っていないときなど) は、@override などの確認をしない
  • 1 か所だけ外すときは、その行に # pylint: disable=<規約の名前> を書く

設計の原則

規約は、次の原則と、fastship-py の実行時の決まりから作っている。

原則 理由
エンティティ・値オブジェクトは I/O をしない。ほかの集約を読む確認はアプリケーションサービスが行い、要るものをメソッドに渡す。async にしてよいのは、アダプターが実装する抽象 (リポジトリ・外部サービスの interface) だけ DB や event loop なしでテストでき、DB を読む場所がアプリケーションサービスに集まる (『実践ドメイン駆動設計』の集約の章: 集約にリポジトリを注入しない)
アプリケーションサービスは Dpo を返す。Dpo はドメインのオブジェクトを運んでよい 『実践ドメイン駆動設計』14 章の DPO。port が domain を import しないことは import-linter が見る
集約からほかの集約は ID で参照する。メソッドの引数で受け取るのは構わない 1 つのトランザクションで変える集約が広がらない
アプリケーションサービスを入れ子にしない 1 メソッド = 1 ユースケース = 1 トランザクション。入れ子は fastship-py が実行時に RuntimeError にする
エンティティか値オブジェクトかは比べ方で見分ける。frozen なら値オブジェクト、eq=False で id で比べるならエンティティ ほかのコンテキストの写し (id を持つ利用者など) は、値オブジェクトとして持ってよい

規約の一覧

kirei-application (<モジュール>.application.**)

規約 指摘すること
application-service-without-inject *ApplicationService の __init__ に引数があるのに @inject が無い
sync-application-service-method *ApplicationService の公開メソッドが def
application-service-returns-non-dpo *ApplicationService の公開メソッドの戻り値の型が、*Dpo・None・それらの list などでない
nested-application-service *ApplicationService の __init__ が *ApplicationService を受け取る
di-container-in-application application 層で DIContainer を使う
transactional-without-command @transactional のメソッドの引数が、*Command 型の 1 つだけでない
public-collaborator *ApplicationService が協力者を self.__x でなく公開の属性に持つ
invalid-command-class command.py のクラスが、@dataclass(frozen=True) の *Command でない
invalid-dpo-class dpo.py のクラスが、@dataclass(frozen=True) の *Dpo でない

kirei-domain-model (<モジュール>.domain.model.**)

規約 指摘すること
async-domain-model @abc.abstractmethod でないメソッド・関数の async def
dependency-lookup-in-domain-model DomainRegistry・DIContainer を使う
entity-references-aggregate エンティティの属性の型が、ほかのパッケージ (集約) のエンティティ。同じパッケージの中のエンティティは持ってよい
entity-without-identity-equality dataclass が frozen でなく、eq=False で __eq__・__hash__ を書いてもいない
repository-not-abstract *Repository が abc.ABC を継承していないか、@abc.abstractmethod でないメソッドを持つ
domain-event-without-to-dict DomainEvent の子に to_dict が無い
domain-event-suffix DomainEvent の子の名前が Event で終わる

kirei-port (<モジュール>.port.adapter.**)

規約 指摘すること
di-resolved-outside-property DIContainer.instance().resolve(...) を @property の外で呼ぶか、結果を属性に持つ (アダプターは DI を作り直す lifespan をまたぐ)
sync-adapter-handler *Resource・*Tool の公開メソッド (property を除く) が def
invalid-repository-implementation persistence/repository/inmem のクラスが、InMem<X>Repository の名前で <X>Repository を継承していない (sqlalchemy は SQLAlchemy<X>Repository)
invalid-adapter-model request.py の *Request・response.py の *Json・output.py の *Output が BaseModel を継承していない。*Json・*Output に classmethod from_ が無い
invalid-table-row driver/ のクラスが、DataBase を継承した *TableRow で、__tablename__・of・to_entity を持つものでない
public-adapter-attribute __init__ で router・tools 以外を公開の属性に持つ

kirei-module (上の 3 つの層)

規約 指摘すること
missing-override 親のメソッドを上書きするメソッド (dunder を除く) に @override が無い
module-name-mismatch ファイル名と同じ名前のクラスが無い (大文字と小文字、_ は区別しない。InMemUserRepository は in_mem_user_repository.py)
multiple-classes-in-module トップレベルのクラスが 1 つのファイルに 2 つ以上ある。command.py・dpo.py・request.py・response.py・output.py・__init__.py と、_ で始まるクラス・TypedDict は除く

Metadata

Release files for kirei-py 0.1.0

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

Source distribution (sdist)

Source distribution for kirei-py 0.1.0
File Size Uploaded Provenance Actions
kirei_py-0.1.0.tar.gz 53.2 kB With provenance Details for kirei_py-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kirei-py 0.1.0
File Interpreter ABI Platform Actions
kirei_py-0.1.0-py3-none-any.whl Python 3 none any Details for kirei_py-0.1.0-py3-none-any.whl

Total release size: 73.0 kB

Release files / kirei_py-0.1.0.tar.gz

Download URL kirei_py-0.1.0.tar.gz
Size 53.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9ae15715ea48a1308420e8eba54b2797d234a03be9de2b23b80c417f7110865e
BLAKE2b-256 checksum
How to use checksums
943f7343f2926f7604768034c6982412a55afab4b00b03a1d23e1d57f344b9b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / kirei_py-0.1.0-py3-none-any.whl

Download URL kirei_py-0.1.0-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eba0b340f2dff4abfe18f4328d8b1ac5883b3cec3b9da0b794f53563cf231a6b
BLAKE2b-256 checksum
How to use checksums
c9cb5629f0765599a475ca5cad0a9f61b0be6a7a8a3f36fdec7923c0fb6d782a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for kirei-py 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for kirei_py-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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