Skip to main content

mock_protocol

A unittest mock library than understands protocols and annotations

A drop-in replacement for the builtin python 3.7 unittest.mock module where annotations on spec objects are used to spec mocked attributes and methods.

In addition to the standard unittest.mock functionality, a new from_protocol function is added.

import mock_protocol

m = mock_protocol.from_protocol(C, **kwargs)

is an alias for

m = mock_protocol.create_autospec(C, spec_set=True, instance=True, **kwargs)

which is essentially the same as the unittest.mock method of the same name

m = unittest.mock.create_autospec(C, spec_set=True, instance=True, **kwargs)

except that type-hints are used to spec the mocked attributes.

When you have type-hinted code like this

class A(object):
    b: str

    def __init__(self, b: str):
        self.b = b

    def foo(self, bar: int) -> list:
        return ['x']*bar


class C(object):
    a: int

    def __init__(self, a: int):
        self.a = a

    def bar(self, foo: str) -> A:
        return A(foo)

Then mocking them with mock_protocol will use the type hints as specs for child mocks.

m = mock_protocol.from_protocol(C)
a = m.bar('hello')

# the following are all True
isinstance(a, A)
isinstance(a.b, str)
isinstance(m.a, int)
isinstance(a.foo(4), list)

mock_protocol also understands common typing types such as Dict and Tuple and Optional

import typing

class D(object):
    a: typing.Optional[int]
    d: typing.Dict[str, int]
    e: typing.Optional[typing.AnyStr]
    t: typing.Tuple[int, float]

    def __init__(self, a: int):
        self.a = a

    def bar(self, foo: str) -> typing.Optional[A]:
        return A(foo)

m = mock_protocol.from_protocol(D)
a = m.bar('hello')

# the following are all True
isinstance(m, D)
isinstance(a, A)
isinstance(m.a, int)
isinstance(m.d, dict)
isinstance(m.e, str)
isinstance(m.t, tuple)

Metadata

Release files for mock-protocol 1.0.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 mock-protocol 1.0.0
File Size Uploaded Provenance Actions
mock_protocol-1.0.0.tar.gz 23.2 kB Without provenance Details for mock_protocol-1.0.0.tar.gz

Release files / mock_protocol-1.0.0.tar.gz

Download URL mock_protocol-1.0.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2ecf2d7664c3e5de2cc570a108fcbd1b27dad82b40e639f6be7df51bad3e95dc
BLAKE2b-256 checksum
How to use checksums
81e0d43526598fcf67cbe4f8c4366b7dcf77018a60bb8cd3440a9dd6ed264aad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.6

Security for mock-protocol 1.0.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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

1.0.0 This release

1 release file No 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