Hub Python 庫文件

嚴格資料類

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

嚴格資料類

huggingface_hub 包提供了一個用於建立**嚴格資料類**的實用工具。它們是 Python 標準 dataclass 的增強版本,具有額外的驗證功能。嚴格資料類確保欄位在初始化和賦值時都經過驗證,這使得它們非常適合資料完整性至關重要的場景。

概述

嚴格資料類使用 @strict 裝飾器建立。它們透過以下方式擴充套件了常規資料類的功能:

  • 根據型別提示驗證欄位型別
  • 支援自定義驗證器以進行額外檢查
  • 可選地允許建構函式中的任意關鍵字引數
  • 在初始化和賦值時都驗證欄位

優點

  • 資料完整性:確保欄位始終包含有效資料
  • 易用性:與 Python 的 dataclass 模組無縫整合
  • 靈活性:支援自定義驗證器以實現複雜的驗證邏輯
  • 輕量級:無需 Pydantic、attrs 或類似庫等額外依賴

用法

基本示例

from dataclasses import dataclass
from huggingface_hub.dataclasses import strict, as_validated_field

# Custom validator to ensure a value is positive
@as_validated_field
def positive_int(value: int):
    if not value > 0:
        raise ValueError(f"Value must be positive, got {value}")

@strict
@dataclass
class Config:
    model_type: str
    hidden_size: int = positive_int(default=16)
    vocab_size: int = 32  # Default value

    # Methods named `validate_xxx` are treated as class-wise validators
    def validate_big_enough_vocab(self):
        if self.vocab_size < self.hidden_size:
            raise ValueError(f"vocab_size ({self.vocab_size}) must be greater than hidden_size ({self.hidden_size})")

欄位在初始化期間進行驗證

config = Config(model_type="bert", hidden_size=24)   # Valid
config = Config(model_type="bert", hidden_size=-1)   # Raises StrictDataclassFieldValidationError

欄位之間的一致性也在初始化期間進行驗證(類級驗證)

# `vocab_size` too small compared to `hidden_size`
config = Config(model_type="bert", hidden_size=32, vocab_size=16)   # Raises StrictDataclassClassValidationError

欄位在賦值期間也進行驗證

config.hidden_size = 512  # Valid
config.hidden_size = -1   # Raises StrictDataclassFieldValidationError

要在賦值後重新執行類級驗證,必須顯式呼叫 .validate

config.validate()  # Runs all class validators

自定義驗證器

您可以使用 validated_field 為欄位附加多個自定義驗證器。驗證器是一個可呼叫物件,它接受一個引數並在值無效時引發異常。

from dataclasses import dataclass
from huggingface_hub.dataclasses import strict, validated_field

def multiple_of_64(value: int):
    if value % 64 != 0:
        raise ValueError(f"Value must be a multiple of 64, got {value}")

@strict
@dataclass
class Config:
    hidden_size: int = validated_field(validator=[positive_int, multiple_of_64])

在此示例中,兩個驗證器都應用於 hidden_size 欄位。

附加關鍵字引數

預設情況下,嚴格資料類只接受類中定義的欄位。您可以透過在 @strict 裝飾器中設定 accept_kwargs=True 來允許附加關鍵字引數。

from dataclasses import dataclass
from huggingface_hub.dataclasses import strict

@strict(accept_kwargs=True)
@dataclass
class ConfigWithKwargs:
    model_type: str
    vocab_size: int = 16

config = ConfigWithKwargs(model_type="bert", vocab_size=30000, extra_field="extra_value")
print(config)  # ConfigWithKwargs(model_type='bert', vocab_size=30000, *extra_field='extra_value')

附加關鍵字引數會出現在資料類的字串表示中,但會以 * 為字首,以強調它們未經驗證。

與型別提示整合

嚴格資料類尊重型別提示並自動驗證它們。例如:

from typing import List
from dataclasses import dataclass
from huggingface_hub.dataclasses import strict

@strict
@dataclass
class Config:
    layers: List[int]

config = Config(layers=[64, 128])  # Valid
config = Config(layers="not_a_list")  # Raises StrictDataclassFieldValidationError

支援的型別包括

  • 任意
  • 聯合
  • 可選
  • 字面量
  • 列表
  • 字典
  • 元組
  • 集合

以及這些型別的任意組合。如果您需要更復雜的型別驗證,可以透過自定義驗證器實現。

類驗證器

名為 validate_xxx 的方法被視為類驗證器。這些方法必須只接受 self 作為引數。類驗證器在初始化期間執行一次,緊隨 __post_init__ 之後。您可以根據需要定義任意數量的類驗證器——它們將按照出現的順序依次執行。

請注意,當欄位在初始化後更新時,類驗證器不會自動重新執行。要手動重新驗證物件,您需要呼叫 obj.validate()

from dataclasses import dataclass
from huggingface_hub.dataclasses import strict

@strict
@dataclass
class Config:
    foo: str
    foo_length: int
    upper_case: bool = False

    def validate_foo_length(self):
        if len(self.foo) != self.foo_length:
            raise ValueError(f"foo must be {self.foo_length} characters long, got {len(self.foo)}")

    def validate_foo_casing(self):
        if self.upper_case and self.foo.upper() != self.foo:
            raise ValueError(f"foo must be uppercase, got {self.foo}")

config = Config(foo="bar", foo_length=3) # ok

config.upper_case = True
config.validate() # Raises StrictDataclassClassValidationError

Config(foo="abcd", foo_length=3) # Raises StrictDataclassFieldValidationError
Config(foo="Bar", foo_length=3, upper_case=True) # Raises StrictDataclassFieldValidationError

方法 .validate() 是嚴格資料類中的保留名稱。為防止意外行為,如果您的類已經定義了此方法,將引發 StrictDataclassDefinitionError 錯誤。

API 參考

@strict

@strict 裝飾器透過嚴格驗證增強了資料類。

huggingface_hub.dataclasses.strict

< >

( accept_kwargs: bool = False )

引數

  • cls — 要轉換為嚴格資料類的類。
  • accept_kwargs (bool, 可選) — 如果為 True,則允許在 __init__ 中使用任意關鍵字引數。預設為 False。

用於為資料類新增嚴格驗證的裝飾器。

此裝飾器必須在 @dataclass 之上使用,以確保 IDE 和靜態型別工具將該類識別為資料類。

可以帶引數或不帶引數使用

  • @strict
  • @strict(accept_kwargs=True)

示例

>>> from dataclasses import dataclass
>>> from huggingface_hub.dataclasses import as_validated_field, strict, validated_field

>>> @as_validated_field
>>> def positive_int(value: int):
...     if not value >= 0:
...         raise ValueError(f"Value must be positive, got {value}")

>>> @strict(accept_kwargs=True)
... @dataclass
... class User:
...     name: str
...     age: int = positive_int(default=10)

# Initialize
>>> User(name="John")
User(name='John', age=10)

# Extra kwargs are accepted
>>> User(name="John", age=30, lastname="Doe")
User(name='John', age=30, *lastname='Doe')

# Invalid type => raises
>>> User(name="John", age="30")
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
    TypeError: Field 'age' expected int, got str (value: '30')

# Invalid value => raises
>>> User(name="John", age=-1)
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
    ValueError: Value must be positive, got -1

as_validated_field

用於建立 validated_field 的裝飾器。建議用於只有一個驗證器的欄位,以避免樣板程式碼。

huggingface_hub.dataclasses.as_validated_field

< >

( validator: typing.Callable[[typing.Any], NoneType] )

引數

  • validator (可呼叫) — 一個方法,它接受一個值作為輸入,並在值無效時引發 ValueError/TypeError。

將驗證器函式裝飾為 validated_field(即帶自定義驗證器的資料類欄位)。

validated_field

建立帶有自定義驗證的資料類欄位。

huggingface_hub.dataclasses.validated_field

< >

( validator: typing.Union[typing.List[typing.Callable[[typing.Any], NoneType]], typing.Callable[[typing.Any], NoneType]] default: typing.Union[typing.Any, dataclasses._MISSING_TYPE] = <dataclasses._MISSING_TYPE object at 0x7fd63e62b910> default_factory: typing.Union[typing.Callable[[], typing.Any], dataclasses._MISSING_TYPE] = <dataclasses._MISSING_TYPE object at 0x7fd63e62b910> init: bool = True repr: bool = True hash: typing.Optional[bool] = None compare: bool = True metadata: typing.Optional[typing.Dict] = None **kwargs: typing.Any )

引數

  • validator (可呼叫List[可呼叫]) — 一個方法,它接受一個值作為輸入,並在值無效時引發 ValueError/TypeError。可以是驗證器列表,以應用多個檢查。
  • **kwargs — 要傳遞給 dataclasses.field() 的附加引數。

建立帶有自定義驗證器的資料類欄位。

對於對欄位應用多個檢查很有用。如果只應用一個規則,請檢視 as_validated_field 裝飾器。

錯誤

class huggingface_hub.errors.StrictDataclassError

< >

( )

嚴格資料類的基本異常。

class huggingface_hub.errors.StrictDataclassDefinitionError

< >

( )

當嚴格資料類定義不正確時丟擲的異常。

class huggingface_hub.errors.StrictDataclassFieldValidationError

< >

( field: str cause: Exception )

當嚴格資料類在給定欄位的驗證失敗時丟擲的異常。

為什麼不使用 pydantic?(或 attrs?或 marshmallow_dataclass?)

  • 請參閱 https://github.com/huggingface/transformers/issues/36329 中關於新增 Pydantic 作為依賴的討論。這將是一個沉重的新增,需要仔細的邏輯來支援 v1 和 v2。
  • 我們不需要 Pydantic 的大部分功能,特別是那些與自動型別轉換、jsonschema、序列化、別名等相關的功能。
  • 我們不需要從字典例項化類的能力。
  • 我們不想修改資料。在 @strict 中,“驗證”意味著“檢查值是否有效”。在 Pydantic 中,“驗證”意味著“型別轉換值,可能修改它,然後檢查它是否有效”。
  • 我們不需要極速驗證。@strict 並非為效能至關重要的高負載而設計。常見用例涉及驗證模型配置(執行一次,與執行模型相比可忽略不計)。這使我們能夠保持程式碼最少。
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.