Skip to content

type_bridge.attribute.string

string

String

String(value)

Bases: Attribute

String attribute type that accepts str values.

Example

class Name(String): pass

class Email(String): pass

With Literal for type safety

class Status(String): pass

status: Literal["active", "inactive"] | Status

Initialize String attribute with a string value.

Parameters:

Name Type Description Default
value str

The string value to store

required
Source code in type_bridge/attribute/string.py
def __init__(self, value: str):
    """Initialize String attribute with a string value.

    Args:
        value: The string value to store
    """
    super().__init__(value)

value property

value

Get the stored string value.

__str__

__str__()

Convert to string.

Source code in type_bridge/attribute/string.py
def __str__(self) -> str:
    """Convert to string."""
    return str(self.value)

__add__

__add__(other)

Concatenate strings.

Source code in type_bridge/attribute/string.py
def __add__(self, other: object) -> "String":
    """Concatenate strings."""
    if isinstance(other, str):
        return String(self.value + other)
    elif isinstance(other, String):
        return String(self.value + other.value)
    else:
        return NotImplemented

__radd__

__radd__(other)

Right-hand string concatenation.

Source code in type_bridge/attribute/string.py
def __radd__(self, other: object) -> "String":
    """Right-hand string concatenation."""
    if isinstance(other, str):
        return String(other + self.value)
    else:
        return NotImplemented

contains classmethod

contains(value)

Create contains string expression.

Parameters:

Name Type Description Default
value String

String value to search for

required

Returns:

Type Description
StringExpr

StringExpr for attr contains value

Example

Email.contains(Email("@company.com")) # email contains "@company.com"

Source code in type_bridge/attribute/string.py
@classmethod
def contains(cls, value: "String") -> "StringExpr":
    """Create contains string expression.

    Args:
        value: String value to search for

    Returns:
        StringExpr for attr contains value

    Example:
        Email.contains(Email("@company.com"))  # email contains "@company.com"
    """
    from type_bridge.expressions import StringExpr

    return StringExpr(attr_type=cls, operation="contains", pattern=value)

like classmethod

like(pattern)

Create regex pattern matching expression.

Parameters:

Name Type Description Default
pattern String

Regex pattern to match

required

Returns:

Type Description
StringExpr

StringExpr for attr like pattern

Example

Name.like(Name("^A.*")) # name starts with 'A'

Source code in type_bridge/attribute/string.py
@classmethod
def like(cls, pattern: "String") -> "StringExpr":
    """Create regex pattern matching expression.

    Args:
        pattern: Regex pattern to match

    Returns:
        StringExpr for attr like pattern

    Example:
        Name.like(Name("^A.*"))  # name starts with 'A'
    """
    from type_bridge.expressions import StringExpr

    return StringExpr(attr_type=cls, operation="like", pattern=pattern)

regex classmethod

regex(pattern)

Create regex pattern matching expression (alias for like).

Note

Automatically converts to TypeQL 'like' operator. Both 'like' and 'regex' perform regex pattern matching in TypeDB.

Parameters:

Name Type Description Default
pattern String

Regex pattern to match

required

Returns:

Type Description
StringExpr

StringExpr for attr like pattern

Example

Email.regex(Email(".@gmail.com")) # Generates TypeQL: $email like ".@gmail.com"

Source code in type_bridge/attribute/string.py
@classmethod
def regex(cls, pattern: "String") -> "StringExpr":
    """Create regex pattern matching expression (alias for like).

    Note:
        Automatically converts to TypeQL 'like' operator.
        Both 'like' and 'regex' perform regex pattern matching in TypeDB.

    Args:
        pattern: Regex pattern to match

    Returns:
        StringExpr for attr like pattern

    Example:
        Email.regex(Email(".*@gmail\\.com"))  # Generates TypeQL: $email like ".*@gmail\\.com"
    """
    from type_bridge.expressions import StringExpr

    return StringExpr(attr_type=cls, operation="regex", pattern=pattern)

startswith classmethod

startswith(prefix)

Create startswith string expression.

Parameters:

Name Type Description Default
prefix String

Prefix string to check for

required

Returns:

Type Description
StringExpr

StringExpr for attr like "^prefix.*"

Source code in type_bridge/attribute/string.py
@classmethod
def startswith(cls, prefix: "String") -> "StringExpr":
    """Create startswith string expression.

    Args:
        prefix: Prefix string to check for

    Returns:
        StringExpr for attr like "^prefix.*"
    """
    # Unwrap if it's an Attribute instance to get the raw string for regex construction
    # Note: Type-safe signature says "String", but we need the raw value
    raw_prefix = prefix.value if isinstance(prefix, String) else str(prefix)
    pattern = f"^{re.escape(raw_prefix)}.*"
    return cls.regex(cls(pattern))

endswith classmethod

endswith(suffix)

Create endswith string expression.

Parameters:

Name Type Description Default
suffix String

Suffix string to check for

required

Returns:

Type Description
StringExpr

StringExpr for attr like ".*suffix$"

Source code in type_bridge/attribute/string.py
@classmethod
def endswith(cls, suffix: "String") -> "StringExpr":
    """Create endswith string expression.

    Args:
        suffix: Suffix string to check for

    Returns:
        StringExpr for attr like ".*suffix$"
    """
    # Unwrap if it's an Attribute instance to get the raw string for regex construction
    raw_suffix = suffix.value if isinstance(suffix, String) else str(suffix)
    pattern = f".*{re.escape(raw_suffix)}$"
    return cls.regex(cls(pattern))

build_lookup classmethod

build_lookup(lookup, value)

Build an expression for string-specific lookups.

Overrides base method to handle contains, regex, startswith, endswith.

Source code in type_bridge/attribute/string.py
@classmethod
def build_lookup(cls, lookup: str, value: Any) -> "Expression":
    """Build an expression for string-specific lookups.

    Overrides base method to handle contains, regex, startswith, endswith.
    """
    if lookup in ("contains", "regex", "startswith", "endswith", "like"):
        # Ensure value is wrapped in String for method calls
        wrapped_val = value if isinstance(value, cls) else cls(str(value))

        if lookup == "contains":
            return cls.contains(wrapped_val)
        elif lookup == "regex" or lookup == "like":
            return cls.regex(wrapped_val)
        elif lookup == "startswith":
            return cls.startswith(wrapped_val)
        elif lookup == "endswith":
            return cls.endswith(wrapped_val)

    # Delegate to base for standard operators (eq, in, isnull, etc.)
    return super().build_lookup(lookup, value)