ABCReg
ABCReg adds AnnotatedABC, AnnotatedABCMeta, and AnnotatedProtocolMeta, which provide additional interface checking at registration time to ensure that registered virtual and ordinary subclasses are valid.
Use ABCReg if you are designing a plugin API or any other interface that might be implemented by an end user.
Defining the Interface
Use AnnotatedABC just like an ordinary ABC. Make sure to mark your API methods as abstract and provide type hints:
from abc import abstractmethod
from abcreg import AnnotatedABC
class Queryable(AnnotatedABC):
@abstractmethod
def query(self, **kwargs: object) -> str: ...
We recommend you use virtual inheritance:
@Queryable.register
class VirtualQueryProvider:
def query(self, **kwargs: object) -> str:
return "query result"
assert issubclass(VirtualQueryProvider, Queryable)
assert isinstance(VirtualQueryProvider(), Queryable)
But, regular inheritance will also work:
class QueryProvider(Queryable):
def query(self, **kwargs: object) -> str:
return "query result"
assert issubclass(QueryProvider, Queryable)
Protocols (structural subtyping)
For structural subtyping with mypy, define a Protocol that uses AnnotatedProtocolMeta:
from typing import Protocol, runtime_checkable
from abcreg import AnnotatedProtocolMeta
@runtime_checkable
class LogHook(Protocol, metaclass=AnnotatedProtocolMeta):
def handlemsg(self, msg: str) -> bool: ...
@LogHook.register
class LogWriter:
def handlemsg(self, msg: str) -> bool:
return True
Mypy accepts structural matches without registration. @LogHook.register still validates annotations at runtime and registers a virtual subclass.
Requirements and Usage
Requires Python 3.10+. With UV, require with uv add abcreg.
Development
Pre-build checks:
uv sync --all-groups
uv run ruff check --fix
uv run ruff format --preview
uv run mypy --strict abcreg main.py tests/
Release files for abcreg 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| abcreg-0.2.0.tar.gz | 29.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| abcreg-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 33.9 kB
Release files / abcreg-0.2.0.tar.gz
| Download URL | abcreg-0.2.0.tar.gz |
|---|---|
| Size | 29.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
395bc16b3b38b2b809f8671b29ac6ee4237e1061bf64b8db89f27cd39c90baa1
|
|
BLAKE2b-256 checksum How to use checksums |
847e0cb65c23d70101fa6a6c22f1d2f89513e7b6545d033fd80bab6a751b4cda
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / abcreg-0.2.0-py3-none-any.whl
| Download URL | abcreg-0.2.0-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
be03b70e4c770c76d882c8b7a2615701c2cf9c0f2fca70f8b3e4e604313a1ebb
|
|
BLAKE2b-256 checksum How to use checksums |
928c7ec9007e22d71be02512387046cc0b8995f953ce0dd058a1b09b9d0486c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|