From ab87b24112f196ea7677eaa80819cb6e8916cd16 Mon Sep 17 00:00:00 2001 From: _sam <3804518+aahung@users.noreply.github.com> Date: Tue, 28 May 2024 00:21:23 -0700 Subject: [PATCH] chore: Upgrade ruff to 0.4.5 --- Makefile | 4 ++-- integration/ruff.toml | 10 +++++----- requirements/dev.txt | 2 +- ruff.toml | 14 +++++++------- samtranslator/model/api/http_api_generator.py | 3 +-- samtranslator/model/eventsources/push.py | 12 ++++++------ samtranslator/model/intrinsics.py | 4 ++-- samtranslator/model/sam_resources.py | 4 ++-- samtranslator/sdk/parameter.py | 2 +- samtranslator/sdk/resource.py | 8 ++++---- samtranslator/third_party/py27hash/hash.py | 2 +- samtranslator/utils/py27hash_fix.py | 8 ++++---- tests/model/api/test_http_api_generator.py | 2 +- tests/ruff.toml | 10 +++++----- 14 files changed, 42 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 1f9df9a62..13db42bfd 100755 --- a/Makefile +++ b/Makefile @@ -50,14 +50,14 @@ black-check: make format-check lint: - ruff samtranslator bin schema_source integration tests + ruff check samtranslator bin schema_source integration tests # mypy performs type check mypy --strict samtranslator bin schema_source # cfn-lint to make sure generated CloudFormation makes sense bin/run_cfn_lint.sh lint-fix: - ruff --fix samtranslator bin schema_source integration tests + ruff check --fix samtranslator bin schema_source integration tests prepare-companion-stack: pytest -v --no-cov integration/setup -m setup diff --git a/integration/ruff.toml b/integration/ruff.toml index f295c2fe8..2ee0fd223 100644 --- a/integration/ruff.toml +++ b/integration/ruff.toml @@ -1,8 +1,11 @@ # black formatter takes care of the line length line-length = 999 +# Mininal python version we support is 3.8 +target-version = "py38" + # The code quality of tests can be a bit lower compared to samtranslator -select = [ +lint.select = [ "E", # Pyflakes "F", # Pyflakes "PL", # pylint @@ -15,10 +18,7 @@ select = [ "UP", # pyupgrade ] -# Mininal python version we support is 3.8 -target-version = "py38" - -[per-file-ignores] +[lint.per-file-ignores] # The code quality of tests can be a bit lower: "**/*.py" = [ diff --git a/requirements/dev.txt b/requirements/dev.txt index 79952c65e..5489cc574 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -4,7 +4,7 @@ pytest-xdist>=2.5,<4 pytest-env>=0.6,<1 pytest-rerunfailures>=9.1,<12 pyyaml~=6.0 -ruff~=0.1.0 +ruff~=0.4.5 # Test requirements pytest>=6.2,<8 diff --git a/ruff.toml b/ruff.toml index 48bf5e4eb..13e591534 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,7 +1,10 @@ # black formatter takes care of the line length line-length = 999 -select = [ +# Mininal python version we support is 3.8 +target-version = "py38" + +lint.select = [ "E", # pycodestyle "W", # pycodestyle "F", # Pyflakes @@ -27,7 +30,7 @@ select = [ "T20", # flake8-print ] -ignore = [ +lint.ignore = [ "UP006", # https://github.com/charliermarsh/ruff/pull/4427 "UP007", # https://github.com/charliermarsh/ruff/pull/4427 # Mutable class attributes should be annotated with `typing.ClassVar` @@ -37,10 +40,7 @@ ignore = [ "G004", ] -# Mininal python version we support is 3.8 -target-version = "py38" - -[per-file-ignores] +[lint.per-file-ignores] # python scripts in bin/ needs some python path configurations before import "bin/*.py" = [ # E402: module-import-not-at-top-of-file @@ -53,5 +53,5 @@ target-version = "py38" "T201", ] -[pylint] +[lint.pylint] max-args = 6 # We have many functions reaching 6 args diff --git a/samtranslator/model/api/http_api_generator.py b/samtranslator/model/api/http_api_generator.py index f97199037..6ce507137 100644 --- a/samtranslator/model/api/http_api_generator.py +++ b/samtranslator/model/api/http_api_generator.py @@ -633,8 +633,7 @@ def _get_authorizers( if "OpenIdConnectUrl" in authorizer: raise InvalidResourceException( self.logical_id, - "'OpenIdConnectUrl' is no longer a supported property for authorizer '%s'. Please refer to the AWS SAM documentation." - % (authorizer_name), + f"'OpenIdConnectUrl' is no longer a supported property for authorizer '{authorizer_name}'. Please refer to the AWS SAM documentation.", ) authorizers[authorizer_name] = ApiGatewayV2Authorizer( # type: ignore[no-untyped-call] api_logical_id=self.logical_id, diff --git a/samtranslator/model/eventsources/push.py b/samtranslator/model/eventsources/push.py index 07552cd8c..fdd2cf0da 100644 --- a/samtranslator/model/eventsources/push.py +++ b/samtranslator/model/eventsources/push.py @@ -429,7 +429,7 @@ def _depend_on_lambda_permissions_using_tag( dependency, so CloudFormation will automatically wait once it reaches that function, the same as if you were using a DependsOn. """ - properties = bucket.get("Properties", None) + properties = bucket.get("Properties") if properties is None: properties = {} bucket["Properties"] = properties @@ -576,14 +576,14 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] sqs_subscription: Dict[str, Any] = sam_expect( self.SqsSubscription, self.relative_id, "SqsSubscription", is_sam_event=True ).to_be_a_map() - queue_arn = sqs_subscription.get("QueueArn", None) - queue_url = sqs_subscription.get("QueueUrl", None) + queue_arn = sqs_subscription.get("QueueArn") + queue_url = sqs_subscription.get("QueueUrl") if not queue_arn or not queue_url: raise InvalidEventException(self.relative_id, "No QueueARN or QueueURL provided.") - queue_policy_logical_id = sqs_subscription.get("QueuePolicyLogicalId", None) - batch_size = sqs_subscription.get("BatchSize", None) - enabled = sqs_subscription.get("Enabled", None) + queue_policy_logical_id = sqs_subscription.get("QueuePolicyLogicalId") + batch_size = sqs_subscription.get("BatchSize") + enabled = sqs_subscription.get("Enabled") queue_policy = self._inject_sqs_queue_policy( # type: ignore[no-untyped-call] self.Topic, queue_arn, queue_url, function, queue_policy_logical_id diff --git a/samtranslator/model/intrinsics.py b/samtranslator/model/intrinsics.py index 543b7267a..db955994c 100644 --- a/samtranslator/model/intrinsics.py +++ b/samtranslator/model/intrinsics.py @@ -130,9 +130,9 @@ def make_shorthand(intrinsic_dict: Dict[str, Any]) -> str: :raises NotImplementedError: For intrinsic functions that don't support shorthands. """ if "Ref" in intrinsic_dict: - return "${%s}" % intrinsic_dict["Ref"] + return "${{{}}}".format(intrinsic_dict["Ref"]) if "Fn::GetAtt" in intrinsic_dict: - return "${%s}" % ".".join(intrinsic_dict["Fn::GetAtt"]) + return "${{{}}}".format(".".join(intrinsic_dict["Fn::GetAtt"])) raise NotImplementedError("Shorthanding is only supported for Ref and Fn::GetAtt") diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index 844ebafc2..fb018e208 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -253,7 +253,7 @@ def resources_to_link(self, resources: Dict[str, Any]) -> Dict[str, Any]: raise InvalidResourceException(self.logical_id, e.message) from e @cw_timer - def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: PLR0912, PLR0915 + def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: PLR0915 """Returns the Lambda function, role, and event resources to which this SAM Function corresponds. :param dict kwargs: already-converted resources that may need to be modified when converting this \ @@ -1914,7 +1914,7 @@ def to_cloudformation(self, **kwargs: Any) -> List[Resource]: raise InvalidResourceException(self.logical_id, "'Destination' is an empty list") - def generate_resources( # noqa: PLR0912 + def generate_resources( self, source: ConnectorResourceReference, destination: ConnectorResourceReference, diff --git a/samtranslator/sdk/parameter.py b/samtranslator/sdk/parameter.py index 57b7e735f..5e0218fc5 100644 --- a/samtranslator/sdk/parameter.py +++ b/samtranslator/sdk/parameter.py @@ -55,7 +55,7 @@ def add_default_parameter_values(self, sam_template: Dict[str, Any]) -> Any: :return dict: Merged parameter values """ - parameter_definition = sam_template.get("Parameters", None) + parameter_definition = sam_template.get("Parameters") if not parameter_definition or not isinstance(parameter_definition, dict): return self.parameter_values diff --git a/samtranslator/sdk/resource.py b/samtranslator/sdk/resource.py index 4beebca0b..fbadd006c 100644 --- a/samtranslator/sdk/resource.py +++ b/samtranslator/sdk/resource.py @@ -24,10 +24,10 @@ def __init__(self, resource_dict: Dict[str, Any]) -> None: self.resource_dict = resource_dict self.type = resource_dict.get("Type") - self.condition = resource_dict.get("Condition", None) - self.deletion_policy = resource_dict.get("DeletionPolicy", None) - self.update_replace_policy = resource_dict.get("UpdateReplacePolicy", None) - self.ignore_globals: Optional[Union[str, List[str]]] = resource_dict.get("IgnoreGlobals", None) + self.condition = resource_dict.get("Condition") + self.deletion_policy = resource_dict.get("DeletionPolicy") + self.update_replace_policy = resource_dict.get("UpdateReplacePolicy") + self.ignore_globals: Optional[Union[str, List[str]]] = resource_dict.get("IgnoreGlobals") # Properties is *not* required. Ex: SimpleTable resource has no required properties self.properties = resource_dict.get("Properties", {}) diff --git a/samtranslator/third_party/py27hash/hash.py b/samtranslator/third_party/py27hash/hash.py index 9c35551b0..3ea2f4473 100644 --- a/samtranslator/third_party/py27hash/hash.py +++ b/samtranslator/third_party/py27hash/hash.py @@ -53,7 +53,7 @@ def hash(value): # type: ignore[no-untyped-def] if isinstance(value, int): return hash(value) - raise TypeError("unhashable type: '%s'" % (type(value).__name__)) + raise TypeError(f"unhashable type: '{type(value).__name__}'") @staticmethod def thash(value): # type: ignore[no-untyped-def] diff --git a/samtranslator/utils/py27hash_fix.py b/samtranslator/utils/py27hash_fix.py index 9dd79acf1..17ee3b257 100644 --- a/samtranslator/utils/py27hash_fix.py +++ b/samtranslator/utils/py27hash_fix.py @@ -506,14 +506,14 @@ def __str__(self) -> str: for i, key in enumerate(self): string += ", " if i > 0 else "" if isinstance(key, ("".__class__, bytes)): - string += "%s: " % key.__repr__() + string += f"{key.__repr__()}: " else: - string += "%s: " % key + string += f"{key}: " if isinstance(self[key], ("".__class__, bytes)): - string += "%s" % self[key].__repr__() + string += str(self[key].__repr__()) else: - string += "%s" % self[key] + string += str(self[key]) string += "}" return string diff --git a/tests/model/api/test_http_api_generator.py b/tests/model/api/test_http_api_generator.py index 298889f06..c03fb2fda 100644 --- a/tests/model/api/test_http_api_generator.py +++ b/tests/model/api/test_http_api_generator.py @@ -170,7 +170,7 @@ def test_auth_iam_not_enabled_with_unsupported_values(self): } self.kwargs["definition_body"] = OpenApiEditor.gen_skeleton() http_api = HttpApiGenerator(**self.kwargs)._construct_http_api() - self.assertNotIn("components", http_api.Body, "EnableIamAuthorizer value: %s" % val) + self.assertNotIn("components", http_api.Body, f"EnableIamAuthorizer value: {val}") def test_auth_novalue_default_does_not_raise(self): self.kwargs["auth"] = self.authorizers diff --git a/tests/ruff.toml b/tests/ruff.toml index b9c75dbf3..79e96b2a1 100644 --- a/tests/ruff.toml +++ b/tests/ruff.toml @@ -1,8 +1,11 @@ # black formatter takes care of the line length line-length = 999 +# Mininal python version we support is 3.8 +target-version = "py38" + # The code quality of tests can be a bit lower compared to samtranslator -select = [ +lint.select = [ "E", # Pyflakes "F", # Pyflakes "PL", # pylint @@ -15,10 +18,7 @@ select = [ "UP", # pyupgrade ] -# Mininal python version we support is 3.8 -target-version = "py38" - -[per-file-ignores] +[lint.per-file-ignores] # The code quality of tests can be a bit lower: "**/*.py" = [