Skip to main content

Nothing says "DO NOT USE" like a poison value.

Project description

pypoison

Nothing says "DO NOT USE" like a poison value.

Sometimes you need a value to represent absence of a value. However, sometimes you want to make absolutely sure this value is not used in any way, shape, form, or anything whatsoever. In these cases, values like None, float('NaN'), or False don't really fit, because you can still "touch" these values.

That's what poison values are for. Because nothing says "DO NOT USE" like a poison value.

This module provides an object that overrides all methods to raise an exception.

Table of Contents

Install

Just pip install pypoison. (As soon as I have uploaded it to PyPI.)

Or just copy pypoison/__init__.py to your project as pypoison.py.

There are no dependencies.

Usage

Just use the value returned by pypoison.get_poison() when you need a poison value.

The object is always the same object.

Custom behavior

By default, all accesses raise a ValueError with a human-readable explanation which attribute/method was accessed.

If you prefer otherwise, you can call pypoison.set_exception(my_exception), and the poison value now raises the object my_exception whenever anything is done with the poison value.

For extra fanciness, you can call pypoison.set_handler(my_fn), and the poison value will raise whatever my_fn(method_name) returns.

To reset to the default behavior, you can call pypoison.set_exception(None)

Examples

>>> 1 in pypoison.get_poison()
ValueError: Tried to access __contains__ on poison value.
>>> pypoison.get_poison() / 1
ValueError: Tried to access __truediv__ on poison value.
>>> 1 + Foo()
ValueError: Tried to access __radd__ on poison value.
>>> pypoison.get_poison() * 4
ValueError: Tried to access __mul__ on poison value.
>>> pypoison.get_poison() % 3
ValueError: Tried to access __mod__ on poison value.
>>> 3 % pypoison.get_poison()
ValueError: Tried to access __rmod__ on poison value.
>>> '{}'.format(pypoison.get_poison())
ValueError: Tried to access __format__ on poison value.
>>> '{!r}'.format(pypoison.get_poison())
ValueError: Tried to access __repr__ on poison value.
>>> list(pypoison.get_poison())
ValueError: Tried to access __iter__ on poison value.
>>> help(pypoison.get_poison())
ValueError: Tried to access __getattribute__ on poison value.
>>> pypoison.get_poison() == 42
ValueError: Tried to access __eq__ on poison value.

Caveat

Serious

Heavy use of the inspect module might fiddle around with the poison object, however safeguards are in place to make sure that this is not easy.

Passive use, such as is or storing it in a passive container (e.g. a list) cannot be prevented. However, this is intentionl. This module wants to provide a poisonous placeholder value, and not an actively malicious value.

I am not sure how to handle callable(), id(), and type().

Logically-null usage cannot be detected, such as in pypoison.get_poison() in [] or True or pypoison.get_poison() or even max([pypoison.get_poison()]).

This module may cause bugs in your debugger/IDE/editor. Or at least unexpected behavior.

Do not store the poison value in a global. Many tools, e.g. unittest, walk the global variables and inspect them.

Non-serious

The amount of black magic might poison your appreciation of Python.

Pythonidae are always nonvenomous; this module makes it poisonous!

TODOs

  • Do weird stuff with it

Contribute

Feel free to dive in! Open an issue or submit PRs.

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

pypoison-0.0.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

pypoison-0.0.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pypoison-0.0.2.tar.gz.

File metadata

  • Download URL: pypoison-0.0.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.5

File hashes

Hashes for pypoison-0.0.2.tar.gz
Algorithm Hash digest
SHA256 092d89306c3ea6320077abc79d35e47808582627fb7df3eb07199cedd6ca6426
MD5 b4a3b7e87618abacfd12a9f0466274fb
BLAKE2b-256 aa565e434fd7b8e185d3d2260c010d6587ad1b7bde1df396d6f2da4f79f8f074

See more details on using hashes here.

File details

Details for the file pypoison-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pypoison-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.5

File hashes

Hashes for pypoison-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4e0caf9f1fff5a2be014279248716992ff5f4a5e64fa1246e69c6b96700d8c2c
MD5 8e073e9186c496e3b7f2bf513aeb2e05
BLAKE2b-256 7d13ffbc58b2656a1e320a0a61a51cb6c9fedbbc4df13c26b853805c4db2994e

See more details on using hashes here.

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