Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide typing (type annotations) for generated python rule files #489

Open
lktslionel opened this issue Apr 9, 2024 · 1 comment
Open

Comments

@lktslionel
Copy link

lktslionel commented Apr 9, 2024

Context

WHEN

I use rdk to create a rule rule. It generates a few python files for the rule

I WANT TO

The generated code to have types annotations

SO THAT

I could benefits from static type checker like mypy or other linter to guide the development of my rules.

Proposed solutions

Solution 1: Add mypy stub files in the template for every suppport python runtime version

The mypy stub file will provide all type annotations for the provided helpers and boilerplate code.

rdk/
└── template/
    └── runtime/
        └── pythonX/
            ├── rule_code.py
            ├── rule_test.py
            └── rule_code.pyi # <- Mypy Stub file 

Solution 2: Add type annotation to the provided template

Here is an example using libraries:

# ...
from typing import Union, cast, Dict
from mypy_boto3_config.type_defs import ComplianceTypeType, EvaluationTypeDef
from aws_lambda_powertools.utilities.data_classes import AWSConfigRuleEvent

# ....
def evaluate_compliance(
    event: AWSConfigRuleEvent,
    configuration_item: Dict[str, Any],
    valid_rule_parameters: Dict[str, Any],
) -> Union[
    None,
    ComplianceTypeType,
    EvaluationTypeDef,
    List[EvaluationTypeDef],
]:
    ...

# This generates an evaluation for config
def build_evaluation(
    resource_id: str,
    compliance_type: ComplianceTypeType,
    event: AWSConfigRuleEvent,
    resource_type: str=DEFAULT_RESOURCE_TYPE,
    annotation: str=None,
) -> EvaluationTypeDef:
    ...
    return cast(EvaluationTypeDef, eval_cc)

I coud help implement this but I need to comment to make sure I'm on the right path.
Thanks in advance

Regards

@bmorrissirromb
Copy link
Collaborator

I think this type of work would be useful. Typing is a useful feature for ensuring code accuracy. If you submitted a PR, I would review it and try to push it through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants