pyrit.identifiers.Identifier#
- class Identifier(class_name: str, class_module: str, class_description: str, identifier_type: ~typing.Literal['class', 'instance'], *, hash: str | None = None, pyrit_version: str = <factory>)[source]#
Bases:
objectBase dataclass for identifying PyRIT components.
This frozen dataclass provides a stable identifier for registry items, targets, scorers, attacks, converters, and other components. The hash is computed at creation time from the core fields and remains constant.
This class serves as: 1. Base for registry metadata (replacing RegistryItemMetadata) 2. Future replacement for get_identifier() dict patterns
All component-specific identifier types should extend this with additional fields.
- __init__(class_name: str, class_module: str, class_description: str, identifier_type: ~typing.Literal['class', 'instance'], *, hash: str | None = None, pyrit_version: str = <factory>) None#
Methods
__init__(class_name, class_module, ...[, ...])from_dict(data)Create an Identifier from a dictionary (e.g., retrieved from database).
normalize(value)Normalize a value to an Identifier instance.
to_dict()Return only fields suitable for DB storage.
Attributes
- classmethod from_dict(data: dict[str, Any]) T[source]#
Create an Identifier from a dictionary (e.g., retrieved from database).
Note
For fields with max_storage_length, stored values may be truncated strings like “<first N chars>… [sha256:<hash>]”. If a ‘hash’ key is present in the input dict, it will be preserved rather than recomputed, ensuring identity matching works correctly.
- Parameters:
data – The dictionary representation.
- Returns:
A new Identifier instance.
- classmethod normalize(value: T | dict[str, Any]) T[source]#
Normalize a value to an Identifier instance.
This method handles conversion from legacy dict format to Identifier, emitting a deprecation warning when a dict is passed. Existing Identifier instances are returned as-is.
- Parameters:
value – An Identifier instance or a dict (legacy format).
- Returns:
The normalized Identifier instance.
- Raises:
TypeError – If value is not an Identifier or dict.
- to_dict() dict[str, Any][source]#
Return only fields suitable for DB storage.
Fields with max_storage_length metadata are truncated to show the first N characters followed by the field’s hash, formatted as: “<first N chars>… [sha256:<hash[:16]>]”
Nested Identifier objects are recursively serialized to dicts.