Skip to content

ActionInputs

Bases: EnvAttrDictVars

GitHub Action input variables.

Usage
class MyInputs(ActionInputs):
    my_input: str

action = ActionBase(inputs=MyInputs())
print(action.inputs.my_input)
print(action.inputs["my-input"])  # the same as above

With attributes, you can only access explicitly declared vars, with dict-like access you can access any var. This way you can find your balance between strictly defined vars and flexibility.

Attribute names are converted to kebab-case. So action.inputs.my_input is the same as action.inputs["my-input"].

If you need to access a snake_case named input my_input, you should use dict-style only: action.inputs["my_input"]. But it's common to use kebab-case in GitHub Actions input names.

By GitHub convention, all input names are upper-cased in the environment and prefixed with "INPUT_". So actions.inputs.my_input or actions.inputs['my-input'] will be the variable INPUT_MY-INPUT in the environment. The ActionInputs does the conversion automatically.

Uses lazy loading of the values. So the value is read from the environment only when accessed and only once, and saved in the object's internal dict.

Source code in src/github_custom_actions/inputs_outputs.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
class ActionInputs(EnvAttrDictVars):
    """GitHub Action input variables.

    Usage:
        ```python
        class MyInputs(ActionInputs):
            my_input: str

        action = ActionBase(inputs=MyInputs())
        print(action.inputs.my_input)
        print(action.inputs["my-input"])  # the same as above
        ```

    With attributes, you can only access explicitly declared vars, with dict-like access
    you can access any var.
    This way you can find your balance between strictly defined vars and flexibility.

    Attribute names are converted to `kebab-case`.
    So `action.inputs.my_input` is the same as `action.inputs["my-input"]`.

    If you need to access a `snake_case` named input `my_input`, you should
    use dict-style only: `action.inputs["my_input"]`.
    But it's common to use `kebab-case` in GitHub Actions input names.

    By GitHub convention, all input names are upper-cased in the environment
    and prefixed with "INPUT_".
    So `actions.inputs.my_input` or `actions.inputs['my-input']` will be the variable
    `INPUT_MY-INPUT` in the environment.
    The ActionInputs does the conversion automatically.

    Uses lazy loading of the values.
    So the value is read from the environment only when accessed and only once,
    and saved in the object's internal dict."""

    # pylint: disable=abstract-method  # we want RO implementation that raises NotImplementedError on write

    def _external_name(self, name: str) -> str:
        """Convert variable name to the external form."""
        return INPUT_PREFIX + name.upper()