Skip to main content

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.

  1. Check that all attributes from the class other that should adhere to the protocol are present in it.
  2. Attributes, input arguments and return arguments need ot have the same type annotations between protocol and other.

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

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

annotation_protocol-1.4.0.dev1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

annotation_protocol-1.4.0.dev1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file annotation_protocol-1.4.0.dev1.tar.gz.

File metadata

  • Download URL: annotation_protocol-1.4.0.dev1.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

Hashes for annotation_protocol-1.4.0.dev1.tar.gz
Algorithm Hash digest
SHA256 6f1254413d340970d05cc925665f303a1f065dd42f6c057061732aa42509c76b
MD5 d9cbec9ef3b438e375d7ce746e35f609
BLAKE2b-256 7e65934b0cc558da91fa44b48e0ac2aa9069f45adbf3a5af128172bf5a470cbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for annotation_protocol-1.4.0.dev1.tar.gz:

Publisher: publish-to-pypi.yml on RoyalHaskoningDHV/annotation-protocol

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file annotation_protocol-1.4.0.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for annotation_protocol-1.4.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 21f4f70d7c97b0a8398c34fa03669f74e77b0869509b4b1ab6f814b0ff120283
MD5 d0ab168b1320000a6027578754745754
BLAKE2b-256 d5fc5461833010b4343f3aae1f0dc7ad6a9cf51175b3a20a4a754a90e5c6cf91

See more details on using hashes here.

Provenance

The following attestation bundles were made for annotation_protocol-1.4.0.dev1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on RoyalHaskoningDHV/annotation-protocol

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page