Skip to main content
Version: 1.2.4

Customize an Expectation Class

Existing Expectation Classes can be customized to include additional information such as business logic, more descriptive naming conventions, and specialized rendering for Data Docs. This is done by subclassing an existing Expectation class and populating the subclass with default values and customized attributes.

Advantages of subclassing an Expectation and providing customized attributes rather than creating an instance of the parent Expectation and passing in parameters include:

  • All instances of the Expectation that use the default values will be updated if changes are made to the class definition.
  • More descriptive Expectation names can be provided that indicate the business logic behind the Expectation.
  • Customized text can be provided to describe the Expectation when Data Docs are generated from Validation Results.

Prerequisites

Procedure

  1. Choose a base Expectation class.

    You can customize any of the core Expectation classes in GX. You can view the available Expectations and their functionality in the Expectation Gallery.

    In this example, ExpectColumnValuesToBeBetween will be customized.

  2. Create a new Expectation class that inherits the base Expectation class.

    The core Expectations in GX have names describing their functionality. When you create a customized Expectation class you can provide a class name that is more indicative of your specific use case:

    Python
    class ExpectValidPassengerCount(gx.expectations.ExpectColumnValuesToBeBetween):
  3. Override the Expectation's attributes with new default values.

    The attributes that can be overriden correspond to the parameters required by the base Expectation. These can be referenced from the Expectation Gallery.

    In this example, the default column for ExpectValidPassengerCount is set to passenger_count and the default value range for the column is defined as between 1 and 6:

    Python
    class ExpectValidPassengerCount(gx.expectations.ExpectColumnValuesToBeBetween):
    column: str = "passenger_count"
    min_value: int = 1
    max_value: int = 6
  4. Customize the rendering of the new Expectation when displayed in Data Docs.

    The description attribute of a customized Expectation class contains the text describing the customized Expectation when its results are rendered into Data Docs. You can format the description string with Markdown syntax:

    Python
    class ExpectValidPassengerCount(gx.expectations.ExpectColumnValuesToBeBetween):
    column: str = "passenger_count"
    min_value: int = 1
    max_value: int = 6
    description: str = "There should be between **1** and **6** passengers."
  5. Use the customized subclass as an Expectation.

    It is best not to overwrite the predefined default values by passing in parameters when a customized Expectation is created. This ensures that the description remains accurate to the values that the customized Expectation uses. It also allows you to update all instances of the customized Expectation by editing the default values in the customized Expectation's class definition rather than having to update each instance individually in their Expectation Suites:

    Python
    expectation = ExpectValidPassengerCount()  # Uses the predefined default values

    A customized Expectation instance can be added to Expectation Suites and validated just like any other Expectation.