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

Techdebt: Improve type annotations #7124

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions moto/core/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
from collections import OrderedDict, defaultdict
from typing import (
TYPE_CHECKING,
Any,
Callable,
ClassVar,
Expand Down Expand Up @@ -41,6 +42,13 @@

JINJA_ENVS: Dict[type, Environment] = {}

if TYPE_CHECKING:
from typing_extensions import ParamSpec

P = ParamSpec("P")

T = TypeVar("T")


ResponseShape = TypeVar("ResponseShape", bound="BaseResponse")

Expand Down Expand Up @@ -173,13 +181,13 @@ def _authenticate_and_authorize_s3_action(
self._authenticate_and_authorize_action(S3IAMRequest, resource)

@staticmethod
def set_initial_no_auth_action_count(initial_no_auth_action_count: int) -> Callable[..., Callable[..., TYPE_RESPONSE]]: # type: ignore[misc]
def set_initial_no_auth_action_count(
initial_no_auth_action_count: int,
) -> "Callable[[Callable[P, T]], Callable[P, T]]":
_test_server_mode_endpoint = settings.test_server_mode_endpoint()

def decorator(
function: Callable[..., TYPE_RESPONSE]
) -> Callable[..., TYPE_RESPONSE]:
def wrapper(*args: Any, **kwargs: Any) -> TYPE_RESPONSE:
def decorator(function: "Callable[P, T]") -> "Callable[P, T]":
def wrapper(*args: "P.args", **kwargs: "P.kwargs") -> T:
if settings.TEST_SERVER_MODE:
response = requests.post(
f"{_test_server_mode_endpoint}/moto-api/reset-auth",
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ disable = W,C,R,E
enable = anomalous-backslash-in-string, arguments-renamed, dangerous-default-value, deprecated-module, function-redefined, import-self, redefined-builtin, redefined-outer-name, reimported, pointless-statement, super-with-arguments, unused-argument, unused-import, unused-variable, useless-else-on-loop, wildcard-import

[mypy]
files= moto, tests/test_core/test_mock_all.py, tests/test_core/test_decorator_calls.py, tests/test_core/test_responses_module.py, tests/test_core/test_mypy.py
files= moto, tests/test_core/
show_column_numbers=True
show_error_codes = True
disable_error_code=abstract
Expand Down
13 changes: 8 additions & 5 deletions tests/test_core/test_account_id_resolution.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Dict, Optional
from unittest import SkipTest

import requests
Expand All @@ -13,18 +14,18 @@


class TestAccountIdResolution:
def setup_method(self):
def setup_method(self) -> None:
if settings.TEST_SERVER_MODE:
raise SkipTest(
"No point in testing this in ServerMode, as we already start our own server"
)
self.server = ThreadedMotoServer(port=SERVER_PORT, verbose=False)
self.server.start()

def teardown_method(self):
def teardown_method(self) -> None:
self.server.stop()

def test_environment_variable_takes_precedence(self):
def test_environment_variable_takes_precedence(self) -> None:
# Verify ACCOUNT ID is standard
resp = self._get_caller_identity()
assert self._get_account_id(resp) == ACCOUNT_ID
Expand All @@ -51,7 +52,9 @@ def test_environment_variable_takes_precedence(self):
resp = self._get_caller_identity()
assert self._get_account_id(resp) == ACCOUNT_ID

def _get_caller_identity(self, extra_headers=None):
def _get_caller_identity(
self, extra_headers: Optional[Dict[str, str]] = None
) -> requests.Response:
data = "Action=GetCallerIdentity&Version=2011-06-15"
headers = {
"Authorization": "AWS4-HMAC-SHA256 Credential=abcd/20010101/us-east-2/sts/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=...",
Expand All @@ -61,6 +64,6 @@ def _get_caller_identity(self, extra_headers=None):
headers.update(extra_headers or {})
return requests.post(f"{BASE_URL}", headers=headers, data=data)

def _get_account_id(self, resp):
def _get_account_id(self, resp: requests.Response) -> str:
data = xmltodict.parse(resp.content)
return data["GetCallerIdentityResponse"]["GetCallerIdentityResult"]["Account"]