Annotation protocol to allow very thorough isinstance checks.
Project description
annotation-protocol
The AnnotationProtocol allows for more thorough isinstance checks in python. Specifically, it adds the following functionality beyond typing.Protocol.
- Check that all attributes from the class
otherthat should adhere to theprotocolare present in it. - Attributes, input arguments and return arguments need ot have the same type annotations between
protocolandother.
Author: Royal HaskoningDHV
Email: ruud.kassing@rhdhv.com, jesse.de.ruijter@rhdhv.com, miguel.hernandez@rhdhv.com, steffen.burgers@rhdhv.com, pierpaolo.lucarelli@rhdhv.com
Installation
Use the package manager pip to install annotation-protocol.
pip install annotation-protocol
Background
The Protocol class from the typing package can be used to create templates of classes with specific attributes and methods. We can check if a class adheres to a given Protocol by doing an isinstance check of the form isinstance(MyClass(), MyProtocol). The AnnotationProtocol extends this functionality by also checking if all type-hints are the same for each attribute, method argument and method output.
Usage
For clarity, in the examples below we compare the standard Protocol from the typing package against the AnnotationProtocol. Note that only the AnnotationProtocol returns False when there is a mismatch in type-hints.
from typing import Protocol, runtime_checkable
from annotation_protocol import AnnotationProtocol
@runtime_checkable
class MyProtocol(Protocol):
def testfun(my_arg: str | list) -> set:
...
class MyAnnotationProtocol(AnnotationProtocol):
def testfun(my_arg: str | list) -> set:
...
class ClassShouldPass:
def testfun(my_arg: str) -> set:
return set()
class ClassShouldFail:
def testfun(my_arg: dict) -> set:
return set()
print(f"Protocol: {isinstance(ClassShouldPass(), MyProtocol)}") # returns True
print(f"Protocol: {isinstance(ClassShouldFail(), MyProtocol)}") # returns True
print(f"AnnotationProtocol: {isinstance(ClassShouldPass(), MyAnnotationProtocol)}") # returns True
print(f"AnnotationProtocol: {isinstance(ClassShouldFail(), MyAnnotationProtocol)}") # returns False
Note that it is possible to have a subset of type annotations in the ClassShouldPass class compared to the MyAnnotationProtocol. In other words it is not necessary to have all types of a UnionType group of types from the protocol in the class that should adhere to the protocol.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file annotation_protocol-1.4.0.tar.gz.
File metadata
- Download URL: annotation_protocol-1.4.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15d846a4984339bab6cbf80a44623219b8cb06b4f4fee0f22c31a255d16900f8
|
|
| MD5 |
8d4325c6497ee9d858777a063e1fb066
|
|
| BLAKE2b-256 |
eafd612c96531b1c1d1c06e5d79547faea3f805785d67481b350f3f6a9cf6dc5
|
Provenance
The following attestation bundles were made for annotation_protocol-1.4.0.tar.gz:
Publisher:
publish-to-pypi.yml on RoyalHaskoningDHV/annotation-protocol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
annotation_protocol-1.4.0.tar.gz -
Subject digest:
15d846a4984339bab6cbf80a44623219b8cb06b4f4fee0f22c31a255d16900f8 - Sigstore transparency entry: 834393561
- Sigstore integration time:
-
Permalink:
RoyalHaskoningDHV/annotation-protocol@0071017bf9f4f40aa3d64de571d7ea65abf20fd0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/RoyalHaskoningDHV
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@0071017bf9f4f40aa3d64de571d7ea65abf20fd0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file annotation_protocol-1.4.0-py3-none-any.whl.
File metadata
- Download URL: annotation_protocol-1.4.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fc66f1506f015db16fdd50fad18520cbb126a7902b27257c9fa521eb5efec60
|
|
| MD5 |
992e15c03c1af2486e0c1383538eef5c
|
|
| BLAKE2b-256 |
258b71a5e1392dd3aca7ffeef0c3b10ea9b0e62959b5f39889702a06e11eda96
|
Provenance
The following attestation bundles were made for annotation_protocol-1.4.0-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on RoyalHaskoningDHV/annotation-protocol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
annotation_protocol-1.4.0-py3-none-any.whl -
Subject digest:
6fc66f1506f015db16fdd50fad18520cbb126a7902b27257c9fa521eb5efec60 - Sigstore transparency entry: 834393564
- Sigstore integration time:
-
Permalink:
RoyalHaskoningDHV/annotation-protocol@0071017bf9f4f40aa3d64de571d7ea65abf20fd0 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/RoyalHaskoningDHV
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@0071017bf9f4f40aa3d64de571d7ea65abf20fd0 -
Trigger Event:
push
-
Statement type: