Skip to content

types

AnnotatedType = type(ChemicalSymbol) module-attribute

ChemicalSymbol = Annotated[str, Field(pattern=EXTENDED_CHEMICAL_SYMBOLS_PATTERN)] module-attribute

ELEMENT_SYMBOLS_PATTERN = '(' + '|'.join(CHEMICAL_SYMBOLS) + ')' module-attribute

EXTENDED_CHEMICAL_SYMBOLS_PATTERN = '(' + '|'.join(CHEMICAL_SYMBOLS + EXTRA_SYMBOLS) + ')' module-attribute

ElementSymbol = Annotated[str, Field(pattern=ELEMENT_SYMBOLS_PATTERN)] module-attribute

NoneType = type(None) module-attribute

OptionalType = type(Optional[str]) module-attribute

SEMVER_PATTERN = '^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$' module-attribute

SemanticVersion = Annotated[str, Field(pattern=SEMVER_PATTERN, examples=['0.10.1', '1.0.0-rc.2', '1.2.3-rc.5+develop'])] module-attribute

UnionType = type(Union[str, int]) module-attribute

__all__ = ('ChemicalSymbol', 'SemanticVersion') module-attribute

_get_origin_type(annotation)

Get the origin type of a type annotation.

Parameters:

Name Type Description Default
annotation type

The type annotation.

required

Returns:

Type Description
type

The origin type.

Source code in optimade/models/types.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def _get_origin_type(annotation: type) -> type:
    """Get the origin type of a type annotation.

    Parameters:
        annotation: The type annotation.

    Returns:
        The origin type.

    """
    # If the annotation is a Union, get the first non-None type (this includes
    # Optional[T])
    if isinstance(annotation, (OptionalType, UnionType)):
        for arg in get_args(annotation):
            if arg not in (None, NoneType):
                annotation = arg
                break

    # If the annotation is an Annotated type, get the first type
    if isinstance(annotation, AnnotatedType):
        annotation = get_args(annotation)[0]

    # Recursively unpack annotation, if it is a Union, Optional, or Annotated type
    while isinstance(annotation, (OptionalType, UnionType, AnnotatedType)):
        annotation = _get_origin_type(annotation)

    # Special case for Literal
    # NOTE: Expecting Literal arguments to all be of a single type
    arg = get_args(annotation)
    if arg and not isinstance(arg, type):
        # Expect arg to be a Literal type argument
        annotation = type(arg)

    # Ensure that the annotation is a builtin type
    return getattr(annotation, "__origin__", annotation)