You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义Great Expectations TableExpectation遇TypeError:缺少data参数

解决Great Expectations自定义TableExpectation的_validate参数缺失错误

错误核心原因

这个报错说明你自定义的ExpectUniqueIdsWithNullEnd类的_validate方法签名不符合Great Expectations(GE)框架的规范,或者实现时错误定义了参数,导致框架调用时无法正确传递所需参数。


逐步排查与修复步骤

1. 修正_validate方法的参数签名

GE的TableExpectation子类的_validate方法必须遵循固定参数规范,不能自定义非框架要求的参数(比如你代码中的data)。正确的签名如下:

from typing import Dict, Optional
from great_expectations.expectations.table_expectation import TableExpectation
from great_expectations.core import ExpectationConfiguration, ExpectationValidationResult
from great_expectations.execution_engine import ExecutionEngine

class ExpectUniqueIdsWithNullEnd(TableExpectation):
    # 必须定义的元数据
    expectation_type = "expect_unique_ids_with_null_end"
    domain_type = "table"
    # 根据需求添加其他元数据(如description、success_keys等)

    def _validate(
        self,
        configuration: ExpectationConfiguration,
        metrics: Dict,
        runtime_configuration: Optional[Dict] = None,
        execution_engine: Optional[ExecutionEngine] = None,
    ) -> ExpectationValidationResult:
        # 从metrics中获取所需数据,而非直接接收data参数
        # 示例:获取全表数据(根据验证逻辑调整所需metrics)
        table_rows = metrics.get("table.rows")
        
        # 编写自定义验证逻辑(检查ID唯一性+末尾是否为Null等)
        
        # 返回验证结果
        return ExpectationValidationResult(
            success=your_validation_result,
            result={"observed_value": observed_value}
        )

2. 配置正确的metrics依赖

如果验证逻辑需要特定数据集或计算指标,必须在类中声明metric_dependencies,让GE框架提前计算并传递这些指标到_validate的metrics参数中:

class ExpectUniqueIdsWithNullEnd(TableExpectation):
    # ...其他元数据
    metric_dependencies = ("table.rows",)  # 按需添加所需metrics,比如"column.values"等

3. 确保自定义期望注册正确

必须使用GE的注册机制将类纳入框架,否则调用时会出现不兼容问题:

from great_expectations.expectations import register_expectation

@register_expectation
class ExpectUniqueIdsWithNullEnd(TableExpectation):
    # ...类实现

4. 检查ExpectationConfiguration与Validator的使用

确认添加期望到Suite的代码无错误,示例如下:

from great_expectations.core import ExpectationConfiguration

# 配置自定义期望
expectation_config = ExpectationConfiguration(
    expectation_type="expect_unique_ids_with_null_end",
    kwargs={}  # 添加期望所需参数(如目标列名等)
)

# 添加到Suite并执行验证
validator.add_expectation(expectation_config)
validation_result = validator.validate()

内容的提问来源于stack exchange,提问作者Fenrir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 03:37:07