Repository: great-expectations/great_expectations

Issue:
Use cleaner solution for non-truncating division in python 2
Prefer `from __future__ import division` to `1.*x/y`


Discussion:
Done inline during improve_partitions push.

Resolving patch:
diff --git a/examples/expectations/column_pair_map_expectation_template.py b/examples/expectations/column_pair_map_expectation_template.py
new file mode 100644
--- /dev/null
+++ b/examples/expectations/column_pair_map_expectation_template.py
@@ -0,0 +1,127 @@
+"""
+This is a template for creating custom ColumnPairMapExpectations.
+For detailed instructions on how to use it, please see:
+    https://docs.greatexpectations.io/docs/guides/expectations/creating_custom_expectations/how_to_create_custom_column_pair_map_expectations
+"""
+
+from typing import Optional
+
+from great_expectations.core.expectation_configuration import ExpectationConfiguration
+from great_expectations.exceptions import InvalidExpectationConfigurationError
+from great_expectations.execution_engine import (
+    PandasExecutionEngine,
+    SparkDFExecutionEngine,
+    SqlAlchemyExecutionEngine,
+)
+from great_expectations.expectations.expectation import ColumnPairMapExpectation
+from great_expectations.expectations.metrics.map_metric_provider import (
+    ColumnPairMapMetricProvider,
+    column_pair_condition_partial,
+)
+
+
+# This class defines a Metric to support your Expectation.
+# For most ColumnPairMapExpectations, the main business logic for calculation will live in this class.
+# <snippet>
+class ColumnPairValuesMatchSomeCriteria(ColumnPairMapMetricProvider):
+    # </snippet>
+    # This is the id string that will be used to reference your metric.
+    # <snippet>
+    condition_metric_name = "METRIC NAME GOES HERE"
+    # </snippet>
+    # These point your metric at the provided keys to facilitate calculation
+    condition_domain_keys = (
+        "column_A",
+        "column_B",
+    )
+    condition_value_keys = ()
+
+    # This method implements the core logic for the PandasExecutionEngine
+    # <snippet>
+    @column_pair_condition_partial(engine=PandasExecutionEngine)
+    def _pandas(cls, column_A, column_B, **kwargs):
+        raise NotImplementedError
+
+    # </snippet>
+
+    # This method defines the business logic for evaluating your metric when using a SqlAlchemyExecutionEngine
+    # @column_pair_condition_partial(engine=SqlAlchemyExecutionEngine)
+    # def _sqlalchemy(cls, column_A, column_B, _dialect, **kwargs):
+    #     raise NotImplementedError
+
+    # This method defines the business logic for evaluating your metric when using a SparkDFExecutionEngine
+    # @column_pair_condition_partial(engine=SparkDFExecutionEngine)
+    # def _spark(cls, column_A, column_B, **kwargs):
+    #     raise NotImplementedError
+
+
+# This class defines the Expectation itself
+# <snippet>
+class ExpectColumnPairValuesToMatchSomeCriteria(ColumnPairMapExpectation):
+    # </snippet>
+    # <snippet>
+    """TODO: Add a docstring here"""
+    # </snippet>
+
+    # These examples will be shown in the public gallery.
+    # They will also be executed as unit tests for your Expectation.
+    examples = []
+
+    # This is the id string of the Metric used by this Expectation.
+    # For most Expectations, it will be the same as the `condition_metric_name` defined in your Metric class above.
+    # <snippet>
+    map_metric = "METRIC NAME GOES HERE"
+    # </snippet>
+
+    # This is a list of parameter names that can affect whether the Expectation evaluates to True or False
+    success_keys = (
+        "column_A",
+        "column_B",
+        "mostly",
+    )
+
+    # This dictionary contains default values for any parameters that should have default values
+    default_kwarg_values = {}
+
+    def validate_configuration(
+        self, configuration: Optional[ExpectationConfiguration]
+    ) -> None:
+        """
+        Validates that a configuration has been set, and sets a configuration if it has yet to be set. Ensures that
+        necessary configuration arguments have been provided for the validation of the expectation.
+
+        Args:
+            configuration (OPTIONAL[ExpectationConfiguration]): \
+                An optional Expectation Configuration entry that will be used to configure the expectation
+        Returns:
+            None. Raises InvalidExpectationConfigurationError if the config is not validated successfully
+        """
+
+        super().validate_configuration(configuration)
+        if configuration is None:
+            configuration = self.configuration
+
+        # # Check other things in configuration.kwargs and raise Exceptions if needed
+        # try:
+        #     assert (
+        #         ...
+        #     ), "message"
+        #     assert (
+        #         ...
+        #     ), "message"
+        # except AssertionError as e:
+        #     raise InvalidExpectationConfigurationError(str(e))
+
+    # This object contains metadata for display in the public Gallery
+    # <snippet>
+    library_metadata = {
+        "tags": [],  # Tags for this Expectation in the Gallery
+        "contributors": [  # Github handles for all contributors to this Expectation.
+            "@your_name_here",  # Don't forget to add your github handle here!
+        ],
+    }
+    # </snippet>
+
+
+if __name__ == "__main__":
+    ExpectColumnPairValuesToMatchSomeCriteria().print_diagnostic_checklist()


Test patch:
diff --git a/tests/integration/docusaurus/expectations/creating_custom_expectations/expect_column_pair_values_to_have_a_difference_of_three.py b/tests/integration/docusaurus/expectations/creating_custom_expectations/expect_column_pair_values_to_have_a_difference_of_three.py
new file mode 100644
--- /dev/null
+++ b/tests/integration/docusaurus/expectations/creating_custom_expectations/expect_column_pair_values_to_have_a_difference_of_three.py
@@ -0,0 +1,163 @@
+from typing import Dict, Optional
+
+from great_expectations.core.expectation_configuration import ExpectationConfiguration
+from great_expectations.exceptions.exceptions import (
+    InvalidExpectationConfigurationError,
+)
+from great_expectations.execution_engine import (
+    ExecutionEngine,
+    PandasExecutionEngine,
+    SparkDFExecutionEngine,
+    SqlAlchemyExecutionEngine,
+)
+from great_expectations.expectations.expectation import (
+    ColumnPairMapExpectation,
+    ExpectationValidationResult,
+)
+from great_expectations.expectations.metrics.import_manager import F, sa
+from great_expectations.expectations.metrics.map_metric_provider import (
+    ColumnPairMapMetricProvider,
+    column_pair_condition_partial,
+)
+from great_expectations.validator.metric_configuration import MetricConfiguration
+
+
+# <snippet>
+class ColumnPairValuesDiffThree(ColumnPairMapMetricProvider):
+    # </snippet>
+    """MetricProvider Class for Pair Values Diff Three MetricProvider"""
+
+    # <snippet>
+    condition_metric_name = "column_pair_values.diff_three"
+    # </snippet>
+    condition_domain_keys = (
+        "column_A",
+        "column_B",
+    )
+    condition_value_keys = ()
+    # <snippet>
+    @column_pair_condition_partial(engine=PandasExecutionEngine)
+    def _pandas(cls, column_A, column_B, **kwargs):
+        return abs(column_A - column_B) == 3
+
+    # </snippet>
+    @column_pair_condition_partial(engine=SqlAlchemyExecutionEngine)
+    def _sqlalchemy(cls, column_A, column_B, **kwargs):
+        row_wise_cond = sa.and_(
+            sa.func.abs(column_A - column_B) == 3,
+            sa.not_(sa.or_(column_A == None, column_B == None)),
+        )
+        return row_wise_cond
+
+    @column_pair_condition_partial(engine=SparkDFExecutionEngine)
+    def _spark(cls, column_A, column_B, **kwargs):
+        row_wise_cond = F.abs(column_A - column_B) == 3
+        return row_wise_cond
+
+
+# <snippet>
+class ExpectColumnPairValuesToHaveADifferenceOfThree(ColumnPairMapExpectation):
+    # </snippet>
+    # <snippet>
+    """Expect two columns to have a row-wise difference of three."""
+    # </snippet>
+    # <snippet>
+    map_metric = "column_pair_values.diff_three"
+    # </snippet>
+    # These examples will be shown in the public gallery.
+    # They will also be executed as unit tests for your Expectation.
+    # <snippet>
+    examples = [
+        {
+            "data": {
+                "col_a": [3, 0, 1, 2, 3, 2],
+                "col_b": [0, -3, 4, -1, 0, 1],
+            },
+            "tests": [
+                {
+                    "title": "basic_positive_test",
+                    "exact_match_out": False,
+                    "include_in_gallery": True,
+                    "in": {"column_A": "col_a", "column_B": "col_b", "mostly": 0.8},
+                    "out": {
+                        "success": True,
+                    },
+                },
+                {
+                    "title": "basic_negative_test",
+                    "exact_match_out": False,
+                    "include_in_gallery": True,
+                    "in": {"column_A": "col_a", "column_B": "col_b", "mostly": 1},
+                    "out": {
+                        "success": False,
+                    },
+                },
+            ],
+        }
+    ]
+    # </snippet>
+    # Setting necessary computation metric dependencies and defining kwargs, as well as assigning kwargs default values
+    success_keys = (
+        "column_A",
+        "column_B",
+        "mostly",
+    )
+
+    default_kwarg_values = {
+        "row_condition": None,
+        "condition_parser": None,  # we expect this to be explicitly set whenever a row_condition is passed
+        "mostly": 1.0,
+        "result_format": "BASIC",
+        "include_config": True,
+        "catch_exceptions": False,
+    }
+    args_keys = (
+        "column_A",
+        "column_B",
+    )
+
+    def validate_configuration(
+        self, configuration: Optional[ExpectationConfiguration]
+    ) -> None:
+        super().validate_configuration(configuration)
+        if configuration is None:
+            configuration = self.configuration
+        try:
+            assert (
+                "column_A" in configuration.kwargs
+                and "column_B" in configuration.kwargs
+            ), "both columns must be provided"
+        except AssertionError as e:
+            raise InvalidExpectationConfigurationError(str(e))
+
+    # This dictionary contains metadata for display in the public gallery
+    # <snippet>
+    library_metadata = {
+        "tags": [
+            "basic math",
+            "multi-column expectation",
+        ],
+        "contributors": ["@joegargery"],
+    }
+    # </snippet>
+
+
+if __name__ == "__main__":
+    # <snippet>
+    ExpectColumnPairValuesToHaveADifferenceOfThree().print_diagnostic_checklist()
+    # </snippet>
+# Note to users: code below this line is only for integration testing -- ignore!
+
+diagnostics = ExpectColumnPairValuesToHaveADifferenceOfThree().run_diagnostics()
+
+for check in diagnostics["tests"]:
+    assert check["test_passed"] is True
+    assert check["error_diagnostics"] is None
+
+for check in diagnostics["errors"]:
+    assert check is None
+
+for check in diagnostics["maturity_checklist"]["experimental"]:
+    if check["message"] == "Passes all linting checks":
+        continue
+    assert check["passed"] is True
diff --git a/tests/integration/test_script_runner.py b/tests/integration/test_script_runner.py
--- a/tests/integration/test_script_runner.py
+++ b/tests/integration/test_script_runner.py
@@ -259,6 +259,10 @@ class IntegrationTestFixture:
         name="expect_table_columns_to_be_unique",
         user_flow_script="tests/integration/docusaurus/expectations/creating_custom_expectations/expect_table_columns_to_be_unique.py",
     ),
+    IntegrationTestFixture(
+        name="expect_column_pair_values_to_have_a_difference_of_three",
+        user_flow_script="tests/integration/docusaurus/expectations/creating_custom_expectations/expect_column_pair_values_to_have_a_difference_of_three.py",
+    ),
     IntegrationTestFixture(
         name="cross_table_comparisons_from_query",
         user_flow_script="tests/integration/docusaurus/expectations/advanced/user_configurable_profiler_cross_table_comparison_from_query.py",