Skip to content

Commit b5debea

Browse files
authored
V1 - more typing and cleanup (#3296)
* More cleanup of unused code * Add more typing for validate functions
1 parent 8b595f5 commit b5debea

31 files changed

+190
-71
lines changed

setup.cfg

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
[metadata]
22
description_file = README.md
3-
4-
[flake8]
5-
ignore = F401,E722,E203,W503,E713,F402,F841,E302
6-
max-line-length = 230
7-
exclude = .git,.hg,.svn,test,setup.py,__pycache__

src/cfnlint/jsonschema/_keywords.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def properties(
489489
yield from validator.descend(
490490
instance[p],
491491
subschema,
492-
path=k[0] if len(k) > 0 else p,
492+
path=k[0] if k else p,
493493
schema_path=p,
494494
property_path=p,
495495
)

src/cfnlint/rules/jsonschema/CfnLintJsonSchema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any, Sequence
1010

1111
from cfnlint.helpers import load_resource
12-
from cfnlint.jsonschema import ValidationError
12+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
1313
from cfnlint.jsonschema.exceptions import best_match
1414
from cfnlint.rules.jsonschema.Base import BaseJsonSchema
1515

@@ -58,7 +58,9 @@ def _iter_errors(self, validator, instance):
5858
for err in errs:
5959
yield self._clean_error(err)
6060

61-
def validate(self, validator, keywords, instance, schema):
61+
def validate(
62+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
63+
) -> ValidationResult:
6264
# if the schema has a description will only replace the message with that
6365
# description and use the best error for the location information
6466
if not self._use_schema_arg:

src/cfnlint/rules/jsonschema/CfnLintJsonSchemaRegional.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77

88
from typing import Any
99

10-
from cfnlint.jsonschema import ValidationError
10+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
1111
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema
1212

1313

1414
class CfnLintJsonSchemaRegional(CfnLintJsonSchema):
1515
def message(self, instance: Any, err: ValidationError) -> str:
1616
return err.message
1717

18-
def validate(self, validator, keywords, instance, schema):
18+
def validate(
19+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
20+
) -> ValidationResult:
1921
for region in validator.context.regions:
2022
region_validator = validator.evolve(
2123
context=validator.context.evolve(regions=[region]),

src/cfnlint/rules/resources/BothUpdateReplacePolicyDeletionPolicyNeeded.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
68
from typing import Any
79

810
import cfnlint.data.schemas.other.resources as resources_schemas
9-
from cfnlint.jsonschema import ValidationError
11+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
1012
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
1113

1214

@@ -37,7 +39,9 @@ def message(self, instance: Any, err: ValidationError) -> str:
3739
"are needed to protect resource from deletion"
3840
)
3941

40-
def validate(self, validator, keywords, instance, schema):
42+
def validate(
43+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
44+
) -> ValidationResult:
4145
resource_type = instance.get("Type")
4246
if resource_type in [
4347
"AWS::Lambda::Version",

src/cfnlint/rules/resources/CfnInit.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
8+
from typing import Any
9+
610
import cfnlint.data.schemas.other.resources
711
from cfnlint.helpers import FUNCTIONS
12+
from cfnlint.jsonschema import ValidationResult, Validator
813
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
914

1015

@@ -26,7 +31,9 @@ def __init__(self) -> None:
2631
all_matches=True,
2732
)
2833

29-
def validate(self, validator, keywords, instance, schema):
34+
def validate(
35+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
36+
) -> ValidationResult:
3037
cfn_validator = self.extend_validator(
3138
validator=validator,
3239
schema=self._schema,

src/cfnlint/rules/resources/RetentionPeriodOnResourceTypesWithAutoExpiringContent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
68
from typing import Any, Dict, List
79

810
import cfnlint.data.schemas.extensions.aws_rds_dbinstance
9-
from cfnlint.jsonschema import ValidationError
11+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
1012
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails
1113

1214

@@ -100,7 +102,9 @@ def message(self, instance: Any, err: ValidationError) -> str:
100102
"values to avoid data loss on resource)"
101103
)
102104

103-
def validate(self, validator, keywords, instance, schema):
105+
def validate(
106+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
107+
) -> ValidationResult:
104108
if len(validator.context.path.cfn_path) < 1:
105109
return
106110

src/cfnlint/rules/resources/backup/BackupPlanLifecycleRule.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
68
from collections import deque
9+
from typing import Any
710

8-
from cfnlint.jsonschema import ValidationError
11+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
912
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
1013

1114

@@ -32,7 +35,9 @@ def __init__(self) -> None:
3235
]
3336
)
3437

35-
def validate(self, validator, uI, instance, schema):
38+
def validate(
39+
self, validator: Validator, uI: Any, instance: Any, schema: dict[str, Any]
40+
) -> ValidationResult:
3641
delete_after_days = instance.get("DeleteAfterDays")
3742
move_to_cold_storage_after_days = instance.get("MoveToColdStorageAfterDays")
3843

src/cfnlint/rules/resources/certificatemanager/DomainValidationOptions.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
68
from collections import deque
9+
from typing import Any
710

8-
from cfnlint.jsonschema import ValidationError
11+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
912
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
1013

1114

@@ -34,7 +37,9 @@ def __init__(self):
3437
]
3538
)
3639

37-
def validate(self, validator, _, instance, schema):
40+
def validate(
41+
self, validator: Validator, _, instance: Any, schema: dict[str, Any]
42+
) -> ValidationResult:
3843
if not isinstance(instance, dict):
3944
return
4045

src/cfnlint/rules/resources/cloudfront/Aliases.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
8+
from typing import Any
9+
610
import regex as re
711

812
from cfnlint.helpers import REGEX_DYN_REF
13+
from cfnlint.jsonschema import ValidationResult, Validator
914
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
1015

1116

@@ -26,7 +31,9 @@ def __init__(self):
2631
]
2732
)
2833

29-
def validate(self, validator, _, instance, schema):
34+
def validate(
35+
self, validator: Validator, _, instance: Any, schema: dict[str, Any]
36+
) -> ValidationResult:
3037

3138
if isinstance(instance, str):
3239
if re.match(REGEX_DYN_REF, instance):

src/cfnlint/rules/resources/events/RuleScheduleExpression.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6-
from cfnlint.jsonschema import ValidationError
6+
from __future__ import annotations
7+
8+
from typing import Any
9+
10+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
711
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
812

913

@@ -20,7 +24,9 @@ def initialize(self, cfn):
2024
"""Initialize the rule"""
2125
self.__init__(["Resources/AWS::Events::Rule/Properties/ScheduleExpression"])
2226

23-
def validate(self, validator, keywords, instance, schema):
27+
def validate(
28+
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any]
29+
) -> ValidationResult:
2430
# Value is either "cron()" or "rate()"
2531
if not validator.is_type(instance, "string"):
2632
return

src/cfnlint/rules/resources/iam/IdentityPolicy.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,3 @@ def __init__(self):
3131
"identity",
3232
"policy_identity.json",
3333
)
34-
35-
def validate(self, validator, policy_type, policy, schema):
36-
yield from super().validate(validator, policy_type, policy, schema)

src/cfnlint/rules/resources/iam/Permissions.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
8+
from typing import Any
9+
610
from cfnlint.data import AdditionalSpecs
711
from cfnlint.helpers import ensure_list, load_resource
8-
from cfnlint.jsonschema import ValidationError
12+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
913
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
1014

1115

@@ -26,7 +30,9 @@ def __init__(self):
2630
)
2731
self.service_map = self.load_service_map()
2832

29-
def validate(self, validator, _, instance, schema):
33+
def validate(
34+
self, validator: Validator, _, instance: Any, schema: dict[str, Any]
35+
) -> ValidationResult:
3036
actions = ensure_list(instance)
3137

3238
for action in actions:

src/cfnlint/rules/resources/iam/Policy.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any, List
1010

1111
from cfnlint.helpers import load_resource
12-
from cfnlint.jsonschema import Validator
12+
from cfnlint.jsonschema import ValidationResult, Validator
1313
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema
1414
from cfnlint.schema.resolver import RefResolver
1515

@@ -50,7 +50,13 @@ def __init__(
5050
self.resolver = RefResolver.from_schema(self.identity_schema, store=store)
5151

5252
# pylint: disable=unused-argument
53-
def validate(self, validator, policy_type, policy, schema):
53+
def validate(
54+
self,
55+
validator: Validator,
56+
policy_type: Any,
57+
policy: Any,
58+
schema: dict[str, Any],
59+
) -> ValidationResult:
5460
# First time child rules are configured against the rule
5561
# so we can run this now
5662
if validator.is_type(policy, "string"):

src/cfnlint/rules/resources/iam/RoleArnPattern.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
8+
from typing import Any
9+
610
import regex as re
711

8-
from cfnlint.jsonschema import ValidationError
12+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
913
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
1014

1115

@@ -33,7 +37,9 @@ def __init__(self) -> None:
3337
)
3438

3539
# pylint: disable=unused-argument
36-
def validate(self, validator, aZ, arn, schema):
40+
def validate(
41+
self, validator: Validator, aZ: Any, arn: Any, schema: dict[str, Any]
42+
) -> ValidationResult:
3743
if not validator.is_type(arn, "string"):
3844
return
3945

src/cfnlint/rules/resources/lmbd/DeprecatedRuntimeEnd.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6+
from __future__ import annotations
7+
68
from datetime import datetime
9+
from typing import Any
710

811
from cfnlint.data import AdditionalSpecs
912
from cfnlint.helpers import load_resource
10-
from cfnlint.jsonschema import ValidationError
13+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
1114
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
1215

1316

@@ -31,7 +34,9 @@ def __init__(self):
3134
)
3235

3336
# pylint: disable=unused-argument
34-
def validate(self, validator, v, runtime, schema):
37+
def validate(
38+
self, validator: Validator, v: Any, runtime: Any, schema: dict[str, Any]
39+
) -> ValidationResult:
3540
runtime_data = self.deprecated_runtimes.get(runtime)
3641
if not runtime_data:
3742
return
@@ -49,4 +54,4 @@ def validate(self, validator, v, runtime, schema):
4954
)
5055

5156
if self.child_rules["W2531"]:
52-
yield from self.child_rules["W2531"].lambdaruntime(runtime, runtime_data)
57+
yield from self.child_rules["W2531"].lambdaruntime(runtime, runtime_data) # type: ignore

src/cfnlint/rules/resources/lmbd/SnapStart.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55

6-
from cfnlint.jsonschema import ValidationError
6+
from __future__ import annotations
7+
8+
from typing import Any
9+
10+
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
711
from cfnlint.rules.jsonschema.CfnLintKeyword import CfnLintKeyword
812

913

@@ -24,14 +28,16 @@ def __init__(self):
2428
["Resources/AWS::Lambda::Function/Properties/SnapStart/ApplyOn"]
2529
)
2630

27-
def validate(self, validator, _, instance, schema):
31+
def validate(
32+
self, validator: Validator, _, instance: Any, schema: dict[str, Any]
33+
) -> ValidationResult:
2834
if not validator.is_type(instance, "string"):
2935
return
3036

3137
if instance != "PublishedVersions":
3238
return
3339

34-
resource_name = validator.context.path.path[1]
40+
resource_name: str = str(validator.context.path.path[1])
3541
lambda_version_type = "AWS::Lambda::Version"
3642
if list(
3743
validator.cfn.get_resource_children(resource_name, [lambda_version_type])

0 commit comments

Comments
 (0)