Skip to content

Commit

Permalink
[p] Enable mypy checking of untyped defs (#6821)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-ucsc committed Feb 3, 2025
1 parent 77e6fb9 commit 9ab3ac9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
warn_unused_configs = True
allow_redefinition = True
explicit_package_bases = True
check_untyped_defs = True
modules =
azul.types,
azul.collections,
Expand Down
6 changes: 5 additions & 1 deletion src/azul/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,9 @@ def from_json(cls, spec: JSON) -> Self:
def __attrs_post_init__(self):
self.validate_name(self.name)
# Import locally to avoid cyclical import
from azul.indexer import (
Bundle,
)
from azul.plugins import (
MetadataPlugin,
Plugin,
Expand All @@ -938,11 +941,12 @@ def __attrs_post_init__(self):
if self.internal:
assert self.is_integration_test_catalog is True, self

repository_bundle_cls: type[Bundle]
metadata_bundle_cls: type[Bundle]
repository_bundle_cls, metadata_bundle_cls = (
plugin_type.bundle_cls(self.plugins[plugin_type.type_name()].name)
for plugin_type in [RepositoryPlugin, MetadataPlugin]
)

require(issubclass(repository_bundle_cls, metadata_bundle_cls),
'Catalog combines incompatible metadata and repository plugins',
self.name, repository_bundle_cls, metadata_bundle_cls)
Expand Down
19 changes: 13 additions & 6 deletions src/azul/chalice.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from chalice.app import (
BadRequestError,
CaseInsensitiveMapping,
HeadersType,
MultiDict,
NotFoundError,
Request,
Expand Down Expand Up @@ -190,7 +191,9 @@ def patched_event_source_handler(self_, event, context):

old_handler = chalice.app.EventSourceHandler.__call__
if old_handler.__code__ != patched_event_source_handler.__code__:
chalice.app.EventSourceHandler.__call__ = patched_event_source_handler
setattr(chalice.app.EventSourceHandler,
'__call__',
patched_event_source_handler)

def _logging_middleware(self, event, get_response):
self._log_request()
Expand Down Expand Up @@ -461,6 +464,7 @@ def __authenticate(self):

def _log_request(self):
if log.isEnabledFor(logging.INFO):
assert self.current_request is not None
context = self.current_request.context
request_info = {
'query': self.current_request.query_params,
Expand Down Expand Up @@ -615,7 +619,8 @@ def __call__(self, f):
try:
metric_alarms = getattr(f, 'metric_alarms')
except AttributeError:
metric_alarms = f.metric_alarms = []
metric_alarms = []
setattr(f, 'metric_alarms', metric_alarms)
metric_alarms.append(self)
return f

Expand Down Expand Up @@ -663,7 +668,7 @@ class retry(HandlerDecorator):

def __call__(self, f):
assert isinstance(f, chalice.app.EventSourceHandler), f
f.retry = self
setattr(f, 'retry', self)
return f

@property
Expand Down Expand Up @@ -693,7 +698,9 @@ def default_routes(self):
}
)
def swagger_redirect():
headers = {'Location': str(self.base_url.set(path='swagger/index.html'))}
headers: HeadersType = {
'Location': str(self.base_url.set(path='swagger/index.html'))
}
return Response(status_code=301, body='', headers=headers)

@self.route(
Expand Down Expand Up @@ -744,7 +751,7 @@ def swagger_initializer():
for path, method in self.non_interactive_routes
])
})
headers = {'Content-Type': 'application/javascript'}
headers: HeadersType = {'Content-Type': 'application/javascript'}
return Response(status_code=200, body=body, headers=headers)

@self.route(
Expand Down Expand Up @@ -861,7 +868,7 @@ def robots_txt():
('Allow', '/$'),
('Allow', '/swagger/')
])
headers = {'Content-Type': 'text/plain'}
headers: HeadersType = {'Content-Type': 'text/plain'}
return Response(status_code=200, headers=headers, body=body)

return locals()
Expand Down
4 changes: 3 additions & 1 deletion src/azul/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def __init__(self, *args, **kwargs):
# envhook.py to use cached credentials for the AssumeRoleProvider. This
# avoids repeated entry of MFA tokens when running this code locally.
# noinspection PyProtectedMember
self._refreshable_credentials = aws.boto3_session.get_credentials()
credentials = aws.boto3_session.get_credentials()
assert credentials is not None, R'Need credentials'
self._refreshable_credentials = credentials


class AzulConnection(Connection):
Expand Down
3 changes: 2 additions & 1 deletion src/azul/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
)
from azul.types import (
JSON,
MutableJSON,
)

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -214,7 +215,7 @@ def queues(self):
Returns information about the SQS queues used by the indexer.
"""
sqs = aws.resource('sqs', azul_logging=True)
response = {'up': True}
response: MutableJSON = {'up': True}
for queue in config.all_queue_names:
try:
queue_instance = sqs.get_queue_by_name(QueueName=queue).attributes
Expand Down
6 changes: 5 additions & 1 deletion src/azul/hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
import requests
import requests.sessions
import requests.structures

from azul import (
cached_property,
Expand Down Expand Up @@ -92,7 +93,10 @@ def sign_and_send(self, request: requests.Request) -> requests.Response:
return response

def sign(self, request: requests.PreparedRequest):
digest = hashlib.sha256(request.body).digest()
body = request.body
assert body is not None
digest = hashlib.sha256(body).digest()
assert isinstance(request.headers, requests.structures.CaseInsensitiveDict)
request.headers['Content-Digest'] = str(http_sfv.Dictionary({'sha-256': digest}))
key, key_id = aws.get_hmac_key_and_id()
self.signer.sign(request,
Expand Down
2 changes: 1 addition & 1 deletion src/azul/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def validate(self):
# Actual service account credentials
require(token_info['email_verified'] == 'true',
'Service account email is not verified')
require(token_info['email'] == self.credentials.service_account_email,
require(token_info['email'] == credentials.service_account_email,
'Service account email does not match')
elif isinstance(credentials, TokenCredentials):
authorized_party = token_info['azp']
Expand Down

0 comments on commit 9ab3ac9

Please sign in to comment.