2016-10-30 07:38:30 -04:00
|
|
|
PEP: 532
|
2016-12-11 09:11:36 -05:00
|
|
|
Title: A circuit breaking protocol and binary operators
|
2016-10-30 07:38:30 -04:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2023-10-11 08:05:51 -04:00
|
|
|
Author: Alyssa Coghlan <ncoghlan@gmail.com>,
|
2016-12-11 09:11:36 -05:00
|
|
|
Mark E. Haase <mehaase@gmail.com>
|
2017-11-29 04:46:18 -05:00
|
|
|
Status: Deferred
|
2016-10-30 07:38:30 -04:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 30-Oct-2016
|
2017-11-29 04:46:18 -05:00
|
|
|
Python-Version: 3.8
|
2022-03-09 11:04:44 -05:00
|
|
|
Post-History: 05-Nov-2016
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2017-11-29 04:46:18 -05:00
|
|
|
PEP Deferral
|
|
|
|
============
|
|
|
|
|
|
|
|
Further consideration of this PEP has been deferred until Python 3.8 at the
|
|
|
|
earliest.
|
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
Inspired by :pep:`335`, :pep:`505`, :pep:`531`, and the related discussions, this PEP
|
2016-12-11 09:11:36 -05:00
|
|
|
proposes the definition of a new circuit breaking protocol (using the
|
|
|
|
method names ``__then__`` and ``__else__``) that provides a common underlying
|
|
|
|
semantic foundation for:
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
* conditional expressions: ``LHS if COND else RHS``
|
|
|
|
* logical conjunction: ``LHS and RHS``
|
|
|
|
* logical disjunction: ``LHS or RHS``
|
2022-01-21 06:03:51 -05:00
|
|
|
* the None-aware operators proposed in :pep:`505`
|
|
|
|
* the rich comparison chaining model proposed in :pep:`535`
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Taking advantage of the new protocol, it further proposes that the definition
|
|
|
|
of conditional expressions be revised to also permit the use of ``if`` and
|
|
|
|
``else`` respectively as right-associative and left-associative general
|
|
|
|
purpose short-circuiting operators:
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
* Right-associative short-circuiting: ``LHS if RHS``
|
|
|
|
* Left-associative short-circuiting: ``LHS else RHS``
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
In order to make logical inversion (``not EXPR``) consistent with the above
|
|
|
|
changes, it also proposes the introduction of a new logical inversion protocol
|
|
|
|
(using the method name ``__not__``).
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
To force short-circuiting of a circuit breaker without having to evaluate
|
|
|
|
the expression creating it twice, a new ``operator.short_circuit(obj)``
|
|
|
|
helper function will be added to the operator module.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Finally, a new standard ``types.CircuitBreaker`` type is proposed to decouple
|
|
|
|
an object's truth value (as used to determine control flow) from the value
|
|
|
|
it returns from short-circuited circuit breaking expressions, with the
|
|
|
|
following factory functions added to the operator module to represent
|
|
|
|
particularly common switching idioms:
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
* switching on ``bool(obj)``: ``operator.true(obj)``
|
|
|
|
* switching on ``not bool(obj)``: ``operator.false(obj)``
|
|
|
|
* switching on ``obj is value``: ``operator.is_sentinel(obj, value)``
|
|
|
|
* switching on ``obj is not value``: ``operator.is_not_sentinel(obj, value)``
|
2016-11-02 11:34:50 -04:00
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Relationship with other PEPs
|
|
|
|
============================
|
2016-11-02 11:34:50 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
This PEP builds on an extended history of work in other proposals. Some of
|
|
|
|
the key proposals are discussed below.
|
2016-11-02 11:34:50 -04:00
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
PEP 531: Existence checking protocol
|
|
|
|
------------------------------------
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
This PEP is a direct successor to :pep:`531`, replacing the existence checking
|
2016-12-11 09:11:36 -05:00
|
|
|
protocol and the new ``?then`` and ``?else`` syntactic operators defined there
|
|
|
|
with the new circuit breaking protocol and adjustments to conditional
|
|
|
|
expressions and the ``not`` operator.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
PEP 505: None-aware operators
|
|
|
|
-----------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
This PEP complements the None-aware operator proposals in :pep:`505`, by offering
|
2016-12-11 09:11:36 -05:00
|
|
|
an underlying protocol-driven semantic framework that explains their
|
|
|
|
short-circuiting behaviour as highly optimised syntactic sugar for particular
|
|
|
|
uses of conditional expressions.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Given the changes proposed by this PEP:
|
|
|
|
|
|
|
|
* ``LHS ?? RHS`` would roughly be ``is_not_sentinel(LHS, None) else RHS``
|
|
|
|
* ``EXPR?.attr`` would roughly be ``EXPR.attr if is_not_sentinel(EXPR, None)``
|
|
|
|
* ``EXPR?[key]`` would roughly be ``EXPR[key] if is_not_sentinel(EXPR, None)``
|
|
|
|
|
|
|
|
In all three cases, the dedicated syntactic form would be optimised to avoid
|
|
|
|
actually creating the circuit breaker instance and instead implement the
|
|
|
|
underlying control flow directly. In the latter two cases, the syntactic form
|
|
|
|
would also avoid evaluating ``EXPR`` twice.
|
|
|
|
|
|
|
|
This means that while the None-aware operators would remain highly specialised
|
|
|
|
and specific to None, other sentinel values would still be usable through the
|
|
|
|
more general protocol-driven proposal in this PEP.
|
|
|
|
|
|
|
|
|
|
|
|
PEP 335: Overloadable Boolean operators
|
|
|
|
---------------------------------------
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
:pep:`335` proposed the ability to overload the short-circuiting ``and`` and
|
2016-12-11 09:11:36 -05:00
|
|
|
``or`` operators directly, with the ability to overload the semantics of
|
|
|
|
comparison chaining being one of the consequences of that change. The
|
|
|
|
proposal in an earlier version of this PEP to instead handle the element-wise
|
2016-11-03 01:13:00 -04:00
|
|
|
comparison use case by changing the semantic definition of comparison chaining
|
2022-09-14 04:35:00 -04:00
|
|
|
is drawn directly from Guido's rejection of :pep:`335` [1]_.
|
2016-12-11 09:11:36 -05:00
|
|
|
|
|
|
|
However, initial feedback on this PEP indicated that the number of different
|
|
|
|
proposals that it covered made it difficult to read, so that part of the
|
2022-01-21 06:03:51 -05:00
|
|
|
proposal has been separated out as :pep:`535`.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
PEP 535: Rich comparison chaining
|
|
|
|
---------------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
As noted above, :pep:`535` is a proposal to build on the circuit breaking protocol
|
2016-12-11 09:11:36 -05:00
|
|
|
defined in this PEP in order to expand the rich comparison support introduced
|
2022-01-21 06:03:51 -05:00
|
|
|
in :pep:`207` to also handle comparison chaining operations like
|
2016-12-11 09:11:36 -05:00
|
|
|
``LEFT_BOUND < VALUE < RIGHT_BOUND``.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
Specification
|
|
|
|
=============
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The circuit breaking protocol (``if-else``)
|
|
|
|
-------------------------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Conditional expressions (``LHS if COND else RHS``) are currently interpreted
|
|
|
|
as an expression level equivalent to::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
if COND:
|
|
|
|
_expr_result = LHS
|
|
|
|
else:
|
|
|
|
_expr_result = RHS
|
|
|
|
|
|
|
|
This PEP proposes changing that expansion to allow the checked condition to
|
|
|
|
implement a new "circuit breaking" protocol that allows it to see, and
|
|
|
|
potentially alter, the result of either or both branches of the expression::
|
|
|
|
|
|
|
|
_cb = COND
|
|
|
|
_type_cb = type(cb)
|
|
|
|
if _cb:
|
|
|
|
_expr_result = LHS
|
|
|
|
if hasattr(_type_cb, "__then__"):
|
|
|
|
_expr_result = _type_cb.__then__(_cb, _expr_result)
|
|
|
|
else:
|
|
|
|
_expr_result = RHS
|
|
|
|
if hasattr(_type_cb, "__else__"):
|
|
|
|
_expr_result = _type_cb.__else__(_cb, _expr_result)
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
As shown, interpreter implementations would be required to access only the
|
|
|
|
protocol method needed for the branch of the conditional expression that is
|
|
|
|
actually executed. Consistent with other protocol methods, the special methods
|
|
|
|
would be looked up via the circuit breaker's type, rather than directly on the
|
|
|
|
instance.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Circuit breaking operators (binary ``if`` and binary ``else``)
|
|
|
|
--------------------------------------------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The proposed name of the protocol doesn't come from the proposed changes to
|
|
|
|
the semantics of conditional expressions. Rather, it comes from the proposed
|
|
|
|
addition of ``if`` and ``else`` as general purpose protocol driven
|
|
|
|
short-circuiting operators to complement the existing ``True`` and ``False``
|
|
|
|
based short-circuiting operators (``or`` and ``and``, respectively) as well
|
2022-01-21 06:03:51 -05:00
|
|
|
as the ``None`` based short-circuiting operator proposed in :pep:`505` (``??``).
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Together, these two operators would be known as the circuit breaking operators.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
In order to support this usage, the definition of conditional expressions in
|
|
|
|
the language grammar would be updated to make both the ``if`` clause and
|
|
|
|
the ``else`` clause optional::
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
test: else_test ['if' or_test ['else' test]] | lambdef
|
2016-11-05 05:21:05 -04:00
|
|
|
else_test: or_test ['else' test]
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Note that we would need to avoid the apparent simplification to
|
|
|
|
``else_test ('if' else_test)*`` in order to make it easier for compiler
|
|
|
|
implementations to correctly preserve the semantics of normal conditional
|
|
|
|
expressions.
|
|
|
|
|
|
|
|
The definition of the ``test_nocond`` node in the grammar (which deliberately
|
|
|
|
excludes conditional expressions) would remain unchanged, so the circuit
|
|
|
|
breaking operators would require parentheses when used in the ``if``
|
2016-11-05 05:21:05 -04:00
|
|
|
clause of comprehensions and generator expressions just as conditional
|
|
|
|
expressions themselves do.
|
|
|
|
|
|
|
|
This grammar definition means precedence/associativity in the otherwise
|
2016-12-11 09:11:36 -05:00
|
|
|
ambiguous case of ``expr1 if cond else expr2 else expr3`` resolves as
|
|
|
|
``(expr1 if cond else expr2) else epxr3``. However, a guideline will also be
|
2022-01-21 06:03:51 -05:00
|
|
|
added to :pep:`8` to say "don't do that", as such a construct will be inherently
|
2016-12-11 09:11:36 -05:00
|
|
|
confusing for readers, regardless of how the interpreter executes it.
|
|
|
|
|
|
|
|
The right-associative circuit breaking operator (``LHS if RHS``) would then
|
|
|
|
be expanded as follows::
|
|
|
|
|
|
|
|
_cb = RHS
|
|
|
|
_expr_result = LHS if _cb else _cb
|
|
|
|
|
|
|
|
While the left-associative circuit breaking operator (``LHS else RHS``) would
|
|
|
|
be expanded as::
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
_cb = LHS
|
|
|
|
_expr_result = _cb if _cb else RHS
|
|
|
|
|
|
|
|
The key point to note in both cases is that when the circuit breaking
|
|
|
|
expression short-circuits, the condition expression is used as the result of
|
|
|
|
the expression *unless* the condition is a circuit breaker. In the latter
|
|
|
|
case, the appropriate circuit breaker protocol method is called as usual, but
|
|
|
|
the circuit breaker itself is supplied as the method argument.
|
|
|
|
|
|
|
|
This allows circuit breakers to reliably detect short-circuiting by checking
|
|
|
|
for cases when the argument passed in as the candidate expression result is
|
|
|
|
``self``.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
Overloading logical inversion (``not``)
|
|
|
|
---------------------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
Any circuit breaker definition will have a logical inverse that is still a
|
2016-12-11 09:11:36 -05:00
|
|
|
circuit breaker, but inverts the answer as to when to short circuit the
|
|
|
|
expression evaluation. For example, the ``operator.true`` and
|
|
|
|
``operator.false`` circuit breakers proposed in this PEP are each other's
|
|
|
|
logical inverse.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
A new protocol method, ``__not__(self)``, will be introduced to permit circuit
|
|
|
|
breakers and other types to override ``not`` expressions to return their
|
|
|
|
logical inverse rather than a coerced boolean result.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
To preserve the semantics of existing language optimisations (such as
|
|
|
|
eliminating double negations directly in a boolean context as redundant),
|
|
|
|
``__not__`` implementations will be required to respect the following
|
|
|
|
invariant::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
assert not bool(obj) == bool(not obj)
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
However, symmetric circuit breakers (those that implement all of ``__bool__``,
|
|
|
|
``__not__``, ``__then__`` and ``__else__``) would only be expected to respect
|
|
|
|
the full semantics of boolean logic when all circuit breakers involved in the
|
|
|
|
expression are using a consistent definition of "truth". This is covered
|
|
|
|
further in `Respecting De Morgan's Laws`_.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Forcing short-circuiting behaviour
|
|
|
|
----------------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Invocation of a circuit breaker's short-circuiting behaviour can be forced by
|
|
|
|
using it as all three operands in a conditional expression::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
obj if obj else obj
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Or, equivalently, as both operands in a circuit breaking expression::
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
obj if obj
|
|
|
|
obj else obj
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Rather than requiring the using of any of these patterns, this PEP proposes
|
|
|
|
to add a dedicated function to the ``operator`` to explicitly short-circuit
|
|
|
|
a circuit breaker, while passing other objects through unmodified::
|
|
|
|
|
|
|
|
def short_circuit(obj)
|
|
|
|
"""Replace circuit breakers with their short-circuited result
|
|
|
|
|
|
|
|
Passes other input values through unmodified.
|
|
|
|
"""
|
|
|
|
return obj if obj else obj
|
|
|
|
|
|
|
|
|
|
|
|
Circuit breaking identity comparisons (``is`` and ``is not``)
|
|
|
|
-------------------------------------------------------------
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
In the absence of any standard circuit breakers, the proposed ``if`` and
|
|
|
|
``else`` operators would largely just be unusual spellings of the existing
|
|
|
|
``and`` and ``or`` logical operators.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
However, this PEP further proposes to provide a new general purpose
|
|
|
|
``types.CircuitBreaker`` type that implements the appropriate short
|
|
|
|
circuiting logic, as well as factory functions in the operator module
|
|
|
|
that correspond to the ``is`` and ``is not`` operators.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
These would be defined in such a way that the following expressions produce
|
|
|
|
``VALUE`` rather than ``False`` when the conditional check fails::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
EXPR if is_sentinel(VALUE, SENTINEL)
|
|
|
|
EXPR if is_not_sentinel(VALUE, SENTINEL)
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
And similarly, these would produce ``VALUE`` rather than ``True`` when the
|
|
|
|
conditional check succeeds::
|
|
|
|
|
|
|
|
is_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
is_not_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
|
|
|
|
In effect, these comparisons would be defined such that the leading
|
|
|
|
``VALUE if`` and trailing ``else VALUE`` clauses can be omitted as implied in
|
|
|
|
expressions of the following forms::
|
|
|
|
|
|
|
|
# To handle "if" expressions, " else VALUE" is implied when omitted
|
|
|
|
EXPR if is_sentinel(VALUE, SENTINEL) else VALUE
|
|
|
|
EXPR if is_not_sentinel(VALUE, SENTINEL) else VALUE
|
|
|
|
# To handle "else" expressions, "VALUE if " is implied when omitted
|
|
|
|
VALUE if is_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
VALUE if is_not_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
|
|
|
|
The proposed ``types.CircuitBreaker`` type would represent this behaviour
|
|
|
|
programmatically as follows::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
class CircuitBreaker:
|
2016-12-11 09:11:36 -05:00
|
|
|
"""Simple circuit breaker type"""
|
|
|
|
def __init__(self, value, bool_value):
|
2016-10-30 07:38:30 -04:00
|
|
|
self.value = value
|
2016-12-11 09:11:36 -05:00
|
|
|
self.bool_value = bool(bool_value)
|
2016-10-30 07:38:30 -04:00
|
|
|
def __bool__(self):
|
2016-12-11 09:11:36 -05:00
|
|
|
return self.bool_value
|
2016-11-02 11:34:50 -04:00
|
|
|
def __not__(self):
|
2016-12-11 09:11:36 -05:00
|
|
|
return CircuitBreaker(self.value, not self.bool_value)
|
|
|
|
def __then__(self, result):
|
|
|
|
if result is self:
|
2016-11-03 01:13:00 -04:00
|
|
|
return self.value
|
2016-12-11 09:11:36 -05:00
|
|
|
return result
|
|
|
|
def __else__(self, result):
|
|
|
|
if result is self:
|
|
|
|
return self.value
|
|
|
|
return result
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The key characteristic of these circuit breakers is that they are *ephemeral*:
|
|
|
|
when they are told that short circuiting has taken place (by receiving a
|
|
|
|
reference to themselves as the candidate expression result), they return the
|
|
|
|
original value, rather than the circuit breaking wrapper.
|
2016-11-02 11:34:50 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The short-circuiting detection is defined such that the wrapper will always
|
|
|
|
be removed if you explicitly pass the same circuit breaker instance to both
|
|
|
|
sides of a circuit breaking operator or use one as all three operands in a
|
|
|
|
conditional expression::
|
2016-11-02 11:34:50 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
breaker = types.CircuitBreaker(foo, foo is None)
|
|
|
|
assert operator.short_circuit(breaker) is foo
|
|
|
|
assert (breaker if breaker) is foo
|
|
|
|
assert (breaker else breaker) is foo
|
|
|
|
assert (breaker if breaker else breaker) is foo
|
|
|
|
breaker = types.CircuitBreaker(foo, foo is not None)
|
|
|
|
assert operator.short_circuit(breaker) is foo
|
|
|
|
assert (breaker if breaker) is foo
|
|
|
|
assert (breaker else breaker) is foo
|
|
|
|
assert (breaker if breaker else breaker) is foo
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The factory functions in the ``operator`` module would then make it
|
|
|
|
straightforward to create circuit breakers that correspond to identity
|
2017-08-11 18:54:20 -04:00
|
|
|
checks using the ``is`` and ``is not`` operators::
|
2016-11-02 11:34:50 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
def is_sentinel(value, sentinel):
|
|
|
|
"""Returns a circuit breaker switching on 'value is sentinel'"""
|
|
|
|
return types.CircuitBreaker(value, value is sentinel)
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
def is_not_sentinel(value, sentinel):
|
|
|
|
"""Returns a circuit breaker switching on 'value is not sentinel'"""
|
|
|
|
return types.CircuitBreaker(value, value is not sentinel)
|
|
|
|
|
|
|
|
|
|
|
|
Truth checking comparisons
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
Due to their short-circuiting nature, the runtime logic underlying the ``and``
|
|
|
|
and ``or`` operators has never previously been accessible through the
|
|
|
|
``operator`` or ``types`` modules.
|
|
|
|
|
|
|
|
The introduction of circuit breaking operators and circuit breakers allows
|
|
|
|
that logic to be captured in the operator module as follows::
|
|
|
|
|
|
|
|
def true(value):
|
|
|
|
"""Returns a circuit breaker switching on 'bool(value)'"""
|
|
|
|
return types.CircuitBreaker(value, bool(value))
|
|
|
|
|
|
|
|
def false(value):
|
|
|
|
"""Returns a circuit breaker switching on 'not bool(value)'"""
|
|
|
|
return types.CircuitBreaker(value, not bool(value))
|
|
|
|
|
|
|
|
* ``LHS or RHS`` would be effectively ``true(LHS) else RHS``
|
|
|
|
* ``LHS and RHS`` would be effectively ``false(LHS) else RHS``
|
|
|
|
|
|
|
|
No actual change would take place in these operator definitions, the new
|
|
|
|
circuit breaking protocol and operators would just provide a way to make the
|
|
|
|
control flow logic programmable, rather than hardcoding the sense of the check
|
|
|
|
at development time.
|
|
|
|
|
|
|
|
Respecting the rules of boolean logic, these expressions could also be
|
|
|
|
expanded in their inverted form by using the right-associative circuit
|
|
|
|
breaking operator instead:
|
|
|
|
|
|
|
|
* ``LHS or RHS`` would be effectively ``RHS if false(LHS)``
|
|
|
|
* ``LHS and RHS`` would be effectively ``RHS if true(LHS)``
|
|
|
|
|
|
|
|
|
|
|
|
None-aware operators
|
|
|
|
--------------------
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
If both this PEP and :pep:`505`'s None-aware operators were accepted, then the
|
2016-12-11 09:11:36 -05:00
|
|
|
proposed ``is_sentinel`` and ``is_not_sentinel`` circuit breaker factories
|
|
|
|
would be used to encapsulate the notion of "None checking": seeing if a value
|
|
|
|
is ``None`` and either falling back to an alternative value (an operation known
|
|
|
|
as "None-coalescing") or passing it through as the result of the overall
|
|
|
|
expression (an operation known as "None-severing" or "None-propagating").
|
|
|
|
|
|
|
|
Given these circuit breakers, ``LHS ?? RHS`` would be roughly equivalent to
|
|
|
|
both of the following:
|
|
|
|
|
|
|
|
* ``is_not_sentinel(LHS, None) else RHS``
|
|
|
|
* ``RHS if is_sentinel(LHS, None)``
|
|
|
|
|
|
|
|
Due to the way they inject control flow into attribute lookup and subscripting
|
|
|
|
operations, None-aware attribute access and None-aware subscripting can't be
|
|
|
|
expressed directly in terms of the circuit breaking operators, but they can
|
|
|
|
still be defined in terms of the underlying circuit breaking protocol.
|
|
|
|
|
|
|
|
In those terms, ``EXPR?.ATTR[KEY].SUBATTR()`` would be semantically
|
|
|
|
equivalent to::
|
|
|
|
|
|
|
|
_lookup_base = EXPR
|
|
|
|
_circuit_breaker = is_not_sentinel(_lookup_base, None)
|
|
|
|
_expr_result = _lookup_base.ATTR[KEY].SUBATTR() if _circuit_breaker
|
|
|
|
|
|
|
|
Similarly, ``EXPR?[KEY].ATTR.SUBATTR()`` would be semantically equivalent
|
|
|
|
to::
|
|
|
|
|
|
|
|
_lookup_base = EXPR
|
|
|
|
_circuit_breaker = is_not_sentinel(_lookup_base, None)
|
|
|
|
_expr_result = _lookup_base[KEY].ATTR.SUBATTR() if _circuit_breaker
|
|
|
|
|
|
|
|
The actual implementations of the None-aware operators would presumably be
|
|
|
|
optimised to skip actually creating the circuit breaker instance, but the
|
|
|
|
above expansions would still provide an accurate description of the observable
|
|
|
|
behaviour of the operators at runtime.
|
|
|
|
|
|
|
|
|
|
|
|
Rich chained comparisons
|
|
|
|
------------------------
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
Refer to :pep:`535` for a detailed discussion of this possible use case.
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
Other conditional constructs
|
|
|
|
----------------------------
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
No changes are proposed to if statements, while statements, comprehensions,
|
|
|
|
or generator expressions, as the boolean clauses they contain are used
|
|
|
|
entirely for control flow purposes and never return a result as such.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-11-02 11:34:50 -04:00
|
|
|
However, it's worth noting that while such proposals are outside the scope of
|
2016-12-11 09:11:36 -05:00
|
|
|
this PEP, the circuit breaking protocol defined here would already be
|
|
|
|
sufficient to support constructs like::
|
2016-11-02 11:34:50 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
def is_not_none(obj):
|
|
|
|
return is_sentinel(obj, None)
|
|
|
|
|
|
|
|
while is_not_none(dynamic_query()) as result:
|
2016-11-02 11:34:50 -04:00
|
|
|
... # Code using result
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
and::
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
if is_not_none(re.search(pattern, text)) as match:
|
2016-11-03 01:13:00 -04:00
|
|
|
... # Code using match
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
This could be done by assigning the result of
|
|
|
|
``operator.short_circuit(CONDITION)`` to the name given in the ``as`` clause,
|
|
|
|
rather than assigning ``CONDITION`` to the given name directly.
|
2016-11-03 01:13:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
Style guide recommendations
|
|
|
|
---------------------------
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
The following additions to :pep:`8` are proposed in relation to the new features
|
2016-11-03 01:13:00 -04:00
|
|
|
introduced by this PEP:
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
* Avoid combining conditional expressions (``if-else``) and the standalone
|
|
|
|
circuit breaking operators (``if`` and ``else``) in a single expression -
|
|
|
|
use one or the other depending on the situation, but not both.
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
* Avoid using conditional expressions (``if-else``) and the standalone
|
|
|
|
circuit breaking operators (``if`` and ``else``) as part of ``if``
|
|
|
|
conditions in ``if`` statements and the filter clauses of comprehensions
|
|
|
|
and generator expressions.
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Adding new operators
|
|
|
|
--------------------
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
Similar to :pep:`335`, early drafts of this PEP focused on making the existing
|
2016-11-03 01:13:00 -04:00
|
|
|
``and`` and ``or`` operators less rigid in their interpretation, rather than
|
2016-12-11 09:11:36 -05:00
|
|
|
proposing new operators. However, this proved to be problematic for a few key
|
2016-11-03 01:13:00 -04:00
|
|
|
reasons:
|
|
|
|
|
|
|
|
* the ``and`` and ``or`` operators have a long established and stable meaning,
|
|
|
|
so readers would inevitably be surprised if their meaning now became
|
|
|
|
dependent on the type of the left operand. Even new users would be confused
|
|
|
|
by this change due to 25+ years of teaching material that assumes the
|
|
|
|
current well-known semantics for these operators
|
|
|
|
* Python interpreter implementations, including CPython, have taken advantage
|
|
|
|
of the existing semantics of ``and`` and ``or`` when defining runtime and
|
|
|
|
compile time optimisations, which would all need to be reviewed and
|
|
|
|
potentially discarded if the semantics of those operations changed
|
2016-12-11 09:11:36 -05:00
|
|
|
* it isn't clear what names would be appropriate for the new methods needed
|
|
|
|
to define the protocol
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Proposing short-circuiting binary variants of the existing ``if-else`` ternary
|
|
|
|
operator instead resolves all of those issues:
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
* the runtime semantics of ``and`` and ``or`` remain entirely unchanged
|
|
|
|
* while the semantics of the unary ``not`` operator do change, the invariant
|
|
|
|
required of ``__not__`` implementations means that existing expression
|
|
|
|
optimisations in boolean contexts will remain valid.
|
|
|
|
* ``__else__`` is the short-circuiting outcome for ``if`` expressions due to
|
|
|
|
the absence of a trailing ``else`` clause
|
|
|
|
* ``__then__`` is the short-circuiting outcome for ``else`` expressions due to
|
|
|
|
the absence of a leading ``if`` clause (this connection would be even clearer
|
|
|
|
if the method name was ``__if__``, but that would be ambiguous given the
|
|
|
|
other uses of the ``if`` keyword that won't invoke the circuit breaking
|
|
|
|
protocol)
|
2016-11-03 01:13:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
Naming the operator and protocol
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
The names "circuit breaking operator", "circuit breaking protocol" and
|
|
|
|
"circuit breaker" are all inspired by the phrase "short circuiting operator":
|
|
|
|
the general language design term for operators that only conditionally
|
|
|
|
evaluate their right operand.
|
|
|
|
|
|
|
|
The electrical analogy is that circuit breakers in Python detect and handle
|
|
|
|
short circuits in expressions before they trigger any exceptions similar to the
|
|
|
|
way that circuit breakers detect and handle short circuits in electrical
|
|
|
|
systems before they damage any equipment or harm any humans.
|
|
|
|
|
|
|
|
The Python level analogy is that just as a ``break`` statement lets you
|
|
|
|
terminate a loop before it reaches its natural conclusion, a circuit breaking
|
|
|
|
expression lets you terminate evaluation of the expression and produce a result
|
|
|
|
immediately.
|
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Using existing keywords
|
|
|
|
-----------------------
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Using existing keywords has the benefit of allowing the new operators to
|
2016-11-03 01:13:00 -04:00
|
|
|
be introduced without a ``__future__`` statement.
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
``if`` and ``else`` are semantically appropriate for the proposed new protocol,
|
|
|
|
and the only additional syntactic ambiguity introduced arises when the new
|
|
|
|
operators are combined with the explicit ``if-else`` conditional expression
|
|
|
|
syntax.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The PEP handles that ambiguity by explicitly specifying how it should be
|
2022-01-21 06:03:51 -05:00
|
|
|
handled by interpreter implementers, but proposing to point out in :pep:`8`
|
2016-12-11 09:11:36 -05:00
|
|
|
that even though interpreters will understand it, human readers probably
|
|
|
|
won't, and hence it won't be a good idea to use both conditional expressions
|
|
|
|
and the circuit breaking operators in a single expression.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Naming the protocol methods
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
Naming the ``__else__`` method was straightforward, as reusing the operator
|
|
|
|
keyword name results in a special method name that is both obvious and
|
|
|
|
unambiguous.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Naming the ``__then__`` method was less straightforward, as there was another
|
|
|
|
possible option in using the keyword-based name ``__if__``.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The problem with ``__if__`` is that there would continue to be many cases
|
|
|
|
where the ``if`` keyword appeared, with an expression to its immediate right,
|
|
|
|
but the ``__if__`` special method would not be invoked. Instead, the
|
|
|
|
``bool()`` builtin and its underlying special methods (``__bool__``,
|
|
|
|
``__len__``) would be invoked, while ``__if__`` had no effect.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
With the boolean protocol already playing a part in conditional expressions and
|
|
|
|
the new circuit breaking protocol, the less ambiguous name ``__then__`` was
|
|
|
|
chosen based on the terminology commonly used in computer science and
|
|
|
|
programming language design to describe the first clause of an ``if``
|
|
|
|
statement.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Making binary ``if`` right-associative
|
|
|
|
--------------------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The precedent set by conditional expressions means that a binary
|
|
|
|
short-circuiting ``if`` expression must necessarily have the condition on the
|
|
|
|
right as a matter of consistency.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
With the right operand always being evaluated first, and the left operand not
|
|
|
|
being evaluated at all if the right operand is true in a boolean context,
|
|
|
|
the natural outcome is a right-associative operator.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Naming the standard circuit breakers
|
|
|
|
------------------------------------
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
When used solely with the left-associative circuit breaking operator,
|
|
|
|
explicit circuit breaker names for unary checks read well if they start with
|
|
|
|
the preposition ``if_``::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
operator.if_true(LHS) else RHS
|
|
|
|
operator.if_false(LHS) else RHS
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
However, incorporating the ``if_`` doesn't read as well when performing
|
|
|
|
logical inversion::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
not operator.if_true(LHS) else RHS
|
|
|
|
not operator.if_false(LHS) else RHS
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Or when using the right-associative circuit breaking operator::
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
LHS if operator.if_true(RHS)
|
|
|
|
LHS if operator.if_false(RHS)
|
|
|
|
|
|
|
|
Or when naming a binary comparison operation::
|
|
|
|
|
|
|
|
operator.if_is_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
operator.if_is_not_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
|
|
|
|
By contrast, omitting the preposition from the circuit breaker name gives a
|
|
|
|
result that reads reasonably well in all forms for unary checks::
|
|
|
|
|
|
|
|
operator.true(LHS) else RHS # Preceding "LHS if " implied
|
|
|
|
operator.false(LHS) else RHS # Preceding "LHS if " implied
|
|
|
|
not operator.true(LHS) else RHS # Preceding "LHS if " implied
|
|
|
|
not operator.false(LHS) else RHS # Preceding "LHS if " implied
|
|
|
|
LHS if operator.true(RHS) # Trailing " else RHS" implied
|
|
|
|
LHS if operator.false(RHS) # Trailing " else RHS" implied
|
|
|
|
LHS if not operator.true(RHS) # Trailing " else RHS" implied
|
|
|
|
LHS if not operator.false(RHS) # Trailing " else RHS" implied
|
|
|
|
|
|
|
|
And also reads well for binary checks::
|
|
|
|
|
|
|
|
operator.is_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
operator.is_not_sentinel(VALUE, SENTINEL) else EXPR
|
|
|
|
EXPR if operator.is_sentinel(VALUE, SENTINEL)
|
|
|
|
EXPR if operator.is_not_sentinel(VALUE, SENTINEL)
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
Risks and concerns
|
|
|
|
==================
|
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
This PEP has been designed specifically to address the risks and concerns
|
|
|
|
raised when discussing PEPs 335, 505 and 531.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
* it defines new operators and adjusts the definition of chained comparison
|
|
|
|
(in a separate PEP) rather than impacting the existing ``and`` and ``or``
|
|
|
|
operators
|
|
|
|
* the proposed new operators are general purpose short-circuiting binary
|
|
|
|
operators that can even be used to express the existing semantics of ``and``
|
|
|
|
and ``or`` rather than focusing solely and inflexibly on identity checking
|
|
|
|
against ``None``
|
|
|
|
* the changes to the ``not`` unary operator and the ``is`` and ``is not``
|
|
|
|
binary comparison operators are defined in such a way that control flow
|
|
|
|
optimisations based on the existing semantics remain valid
|
|
|
|
|
|
|
|
One consequence of this approach is that this PEP *on its own* doesn't produce
|
|
|
|
much in the way of direct benefits to end users aside from making it possible
|
2022-01-21 06:03:51 -05:00
|
|
|
to omit some common ``None if`` prefixes and ``else None`` suffixes from
|
2016-12-11 09:11:36 -05:00
|
|
|
particular forms of conditional expression.
|
|
|
|
|
|
|
|
Instead, what it mainly provides is a common foundation that would allow the
|
2022-01-21 06:03:51 -05:00
|
|
|
None-aware operator proposals in :pep:`505` and the rich comparison chaining
|
|
|
|
proposal in :pep:`535` to be pursued atop a common underlying semantic framework
|
2016-12-11 09:11:36 -05:00
|
|
|
that would also be shared with conditional expressions and the existing ``and``
|
|
|
|
and ``or`` operators.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Design Discussion
|
|
|
|
=================
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Protocol walk-through
|
|
|
|
---------------------
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The following diagram illustrates the core concepts behind the circuit
|
|
|
|
breaking protocol (although it glosses over the technical detail of looking
|
|
|
|
up the special methods via the type rather than the instance):
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
.. image:: pep-0532/circuit-breaking-protocol.svg
|
2022-03-11 15:41:57 -05:00
|
|
|
:class: invert-in-dark-mode
|
2016-12-11 09:11:36 -05:00
|
|
|
:alt: diagram of circuit breaking protocol applied to ternary expression
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
We will work through the following expression::
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
>>> def is_not_none(obj):
|
|
|
|
... return operator.is_not_sentinel(obj, None)
|
|
|
|
>>> x if is_not_none(data.get("key")) else y
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
``is_not_none`` is a helper function that invokes the proposed
|
|
|
|
``operator.is_not_sentinel`` ``types.CircuitBreaker`` factory with ``None`` as
|
|
|
|
the sentinel value. ``data`` is a container (such as a builtin ``dict``
|
|
|
|
instance) that returns ``None`` when the ``get()`` method is called with an
|
|
|
|
unknown key.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
We can rewrite the example to give a name to the circuit breaker instance::
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
>>> maybe_value = is_not_none(data.get("key"))
|
|
|
|
>>> x if maybe_value else y
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Here the ``maybe_value`` circuit breaker instance corresponds to ``breaker``
|
|
|
|
in the diagram.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The ternary condition is evaluated by calling ``bool(maybe_value)``, which is
|
|
|
|
the same as Python's existing behavior. The change in behavior is that instead
|
|
|
|
of directly returning one of the operands ``x`` or ``y``, the circuit breaking
|
|
|
|
protocol passes the relevant operand to the circuit breaker used in the
|
|
|
|
condition.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
If ``bool(maybe_value)`` evaluates to ``True`` (i.e. the requested
|
|
|
|
key exists and its value is not ``None``) then the interpreter calls
|
|
|
|
``type(maybe_value).__then__(maybe_value, x)``. Otherwise, it calls
|
|
|
|
``type(maybe_value).__else__(maybe_value, y)``.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The protocol also applies to the new ``if`` and ``else`` binary operators,
|
|
|
|
but in these cases, the interpreter needs a way to indicate the missing third
|
|
|
|
operand. It does this by re-using the circuit breaker itself in that role.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Consider these two expressions::
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
>>> x if data.get("key") is None
|
|
|
|
>>> x if operator.is_sentinel(data.get("key"), None)
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
The first form of this expression returns ``x`` if ``data.get("key") is None``,
|
|
|
|
but otherwise returns ``False``, which almost certainly isn't what we want.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
By contrast, the second form of this expression still returns ``x`` if
|
|
|
|
``data.get("key") is None``, but otherwise returns ``data.get("key")``, which
|
|
|
|
is significantly more useful behaviour.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
We can understand this behavior by rewriting it as a ternary expression with
|
|
|
|
an explicitly named circuit breaker instance::
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
>>> maybe_value = operator.is_sentinel(data.get("key"), None)
|
|
|
|
>>> x if maybe_value else maybe_value
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
If ``bool(maybe_value)`` is ``True`` (i.e. ``data.get("key")`` is ``None``),
|
|
|
|
then the interpreter calls ``type(maybe_value).__then__(maybe_value, x)``. The
|
|
|
|
implementation of ``types.CircuitBreaker.__then__`` doesn't see anything that
|
|
|
|
indicates short-circuiting has taken place, and hence returns ``x``.
|
|
|
|
|
|
|
|
By contrast, if ``bool(maybe_value)`` is ``False`` (i.e. ``data.get("key")``
|
|
|
|
is *not* ``None``), the interpreter calls
|
|
|
|
``type(maybe_value).__else__(maybe_value, maybe_value)``. The implementation of
|
|
|
|
``types.CircuitBreaker.__else__`` detects that the instance method has received
|
|
|
|
itself as its argument and returns the wrapped value (i.e. ``data.get("key")``)
|
|
|
|
rather than the circuit breaker.
|
|
|
|
|
|
|
|
The same logic applies to ``else``, only reversed::
|
|
|
|
|
|
|
|
>>> is_not_none(data.get("key")) else y
|
|
|
|
|
|
|
|
This expression returns ``data.get("key")`` if it is not ``None``, otherwise it
|
2019-07-03 14:20:45 -04:00
|
|
|
evaluates and returns ``y``. To understand the mechanics, we rewrite the
|
2016-12-11 09:11:36 -05:00
|
|
|
expression as follows::
|
|
|
|
|
|
|
|
>>> maybe_value = is_not_none(data.get("key"))
|
|
|
|
>>> maybe_value if maybe_value else y
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
If ``bool(maybe_value)`` is ``True``, then the expression short-circuits and
|
2016-12-11 09:11:36 -05:00
|
|
|
the interpreter calls ``type(maybe_value).__else__(maybe_value, maybe_value)``.
|
|
|
|
The implementation of ``types.CircuitBreaker.__then__`` detects that the
|
|
|
|
instance method has received itself as its argument and returns the wrapped
|
|
|
|
value (i.e. ``data.get("key")``) rather than the circuit breaker.
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
If ``bool(maybe_value)`` is ``True``, the interpreter calls
|
2016-12-11 09:11:36 -05:00
|
|
|
``type(maybe_value).__else__(maybe_value, y)``. The implementation of
|
|
|
|
``types.CircuitBreaker.__else__`` doesn't see anything that indicates
|
|
|
|
short-circuiting has taken place, and hence returns ``y``.
|
|
|
|
|
|
|
|
|
|
|
|
Respecting De Morgan's Laws
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
Similar to ``and`` and ``or``, the binary short-circuiting operators will
|
|
|
|
permit multiple ways of writing essentially the same expression. This
|
|
|
|
seeming redundancy is unfortunately an implied consequence of defining the
|
|
|
|
protocol as a full boolean algebra, as boolean algebras respect a pair of
|
|
|
|
properties known as "De Morgan's Laws": the ability to express the results
|
|
|
|
of ``and`` and ``or`` operations in terms of each other and a suitable
|
|
|
|
combination of ``not`` operations.
|
|
|
|
|
|
|
|
For ``and`` and ``or`` in Python, these invariants can be described as follows::
|
|
|
|
|
2016-12-12 01:03:39 -05:00
|
|
|
assert bool(A and B) == bool(not (not A or not B))
|
|
|
|
assert bool(A or B) == bool(not (not A and not B))
|
2016-12-11 09:11:36 -05:00
|
|
|
|
|
|
|
That is, if you take one of the operators, invert both operands, switch to the
|
|
|
|
other operator, and then invert the overall result, you'll get the same
|
2016-12-12 01:03:39 -05:00
|
|
|
answer (in a boolean sense) as you did from the original operator. (This may
|
|
|
|
seem redundant, but in many situations it actually lets you eliminate double
|
|
|
|
negatives and find tautologically true or false subexpressions, thus reducing
|
|
|
|
the overall expression size).
|
2016-12-11 09:11:36 -05:00
|
|
|
|
|
|
|
For circuit breakers, defining a suitable invariant is complicated by the
|
2016-12-12 01:03:39 -05:00
|
|
|
fact that they're often going to be designed to eliminate themselves from the
|
|
|
|
expression result when they're short-circuited, which is an inherently
|
|
|
|
asymmetric behaviour. Accordingly, that inherent asymmetry needs to be
|
|
|
|
accounted for when mapping De Morgan's Laws to the expected behaviour of
|
|
|
|
symmetric circuit breakers.
|
|
|
|
|
|
|
|
One way this complication can be addressed is to wrap the operand that would
|
|
|
|
otherwise short-circuit in ``operator.true``, ensuring that when ``bool`` is
|
|
|
|
applied to the overall result, it uses the same definition of truth that was
|
|
|
|
used to decide which branch to evaluate, rather than applying ``bool`` directly
|
|
|
|
to the circuit breaker's input value.
|
2016-12-11 09:11:36 -05:00
|
|
|
|
|
|
|
Specifically, for the new short-circuiting operators, the following properties
|
|
|
|
would be reasonably expected to hold for any well-behaved symmetric circuit
|
2016-12-12 01:03:39 -05:00
|
|
|
breaker that implements both ``__bool__`` and ``__not__``::
|
2016-12-11 09:11:36 -05:00
|
|
|
|
2016-12-12 01:03:39 -05:00
|
|
|
assert bool(B if true(A)) == bool(not (true(not A) else not B))
|
|
|
|
assert bool(true(A) else B) == bool(not (not B if true(not A)))
|
2016-12-11 09:11:36 -05:00
|
|
|
|
2016-12-12 01:03:39 -05:00
|
|
|
Note the order of operations on the right hand side (applying ``true``
|
|
|
|
*after* inverting the input circuit breaker) - this ensures that an
|
|
|
|
assertion is actually being made about ``type(A).__not__``, rather than
|
|
|
|
merely being about the behaviour of ``type(true(A)).__not__``.
|
2016-12-11 09:11:36 -05:00
|
|
|
|
|
|
|
At the very least, ``types.CircuitBreaker`` instances would respect this
|
|
|
|
logic, allowing existing boolean expression optimisations (like double
|
|
|
|
negative elimination) to continue to be applied.
|
2016-11-05 05:21:05 -04:00
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
Arbitrary sentinel objects
|
|
|
|
--------------------------
|
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
Unlike PEPs 505 and 531, the proposal in this PEP readily handles custom
|
|
|
|
sentinel objects::
|
2016-11-03 01:13:00 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
_MISSING = object()
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
# Using the sentinel to check whether or not an argument was supplied
|
2016-12-11 09:11:36 -05:00
|
|
|
def my_func(arg=_MISSING):
|
|
|
|
arg = make_default() if is_sentinel(arg, _MISSING) # "else arg" implied
|
|
|
|
|
|
|
|
|
|
|
|
Implicitly defined circuit breakers in circuit breaking expressions
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
|
|
|
A never-posted draft of this PEP explored the idea of special casing the
|
|
|
|
``is`` and ``is not`` binary operators such that they were automatically
|
|
|
|
treated as circuit breakers when used in the context of a circuit breaking
|
|
|
|
expression. Unfortunately, it turned out that this approach necessarily
|
|
|
|
resulted in one of two highly undesirable outcomes:
|
|
|
|
|
|
|
|
A. the return type of these expressions changed universally from ``bool`` to
|
|
|
|
``types.CircuitBreaker``, potentially creating a backwards compatibility
|
|
|
|
problem (especially when working with extension module APIs that
|
|
|
|
specifically look for a builtin boolean value with ``PyBool_Check`` rather
|
|
|
|
than passing the supplied value through ``PyObject_IsTrue`` or using
|
|
|
|
the ``p`` (predicate) format in one of the argument parsing functions)
|
|
|
|
B. the return type of these expressions became *context dependent*, meaning
|
|
|
|
that other routine refactorings (like pulling a comparison operation out
|
|
|
|
into a local variable) could have a significant impact on the runtime
|
|
|
|
semantics of a piece of code
|
|
|
|
|
|
|
|
Neither of those possible outcomes seems warranted by the proposal in this PEP,
|
|
|
|
so it reverted to the current design where circuit breaker instances must be
|
|
|
|
created explicitly via API calls, and are never produced implicitly.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
|
|
|
==============
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
As with :pep:`505`, actual implementation has been deferred pending in-principle
|
2016-12-11 09:11:36 -05:00
|
|
|
interest in the idea of making these changes.
|
2016-10-30 07:38:30 -04:00
|
|
|
|
|
|
|
...TBD...
|
|
|
|
|
|
|
|
|
2016-11-03 01:13:00 -04:00
|
|
|
Acknowledgements
|
|
|
|
================
|
|
|
|
|
2022-09-14 04:35:00 -04:00
|
|
|
Thanks go to Steven D'Aprano for his detailed critique [2]_ of the initial
|
2016-12-11 09:11:36 -05:00
|
|
|
draft of this PEP that inspired many of the changes in the second draft, as
|
2022-09-14 04:35:00 -04:00
|
|
|
well as to all of the other participants in that discussion thread [3]_.
|
2016-11-03 01:13:00 -04:00
|
|
|
|
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
|
|
|
.. [1] PEP 335 rejection notification
|
2017-06-11 15:02:39 -04:00
|
|
|
(https://mail.python.org/pipermail/python-dev/2012-March/117510.html)
|
2016-10-30 07:38:30 -04:00
|
|
|
|
2016-12-11 09:11:36 -05:00
|
|
|
.. [2] Steven D'Aprano's critique of the initial draft
|
|
|
|
(https://mail.python.org/pipermail/python-ideas/2016-November/043615.html)
|
|
|
|
|
|
|
|
.. [3] python-ideas thread discussing initial draft
|
|
|
|
(https://mail.python.org/pipermail/python-ideas/2016-November/043563.html)
|
|
|
|
|
2016-10-30 07:38:30 -04:00
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain under the terms of the
|
|
|
|
CC0 1.0 license: https://creativecommons.org/publicdomain/zero/1.0/
|