Skip to content

Commit

Permalink
CI: Verify types using mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Jan 30, 2023
1 parent bfc9632 commit 9059427
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/gating.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
tox_env:
- bandit
- lint
- mypy

runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion greenwave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
try:
import tomllib
except ImportError:
import toml as tomllib
import toml as tomllib # type: ignore

with open("pyproject.toml", "r") as f:
pyproject = tomllib.load(f)
Expand Down
4 changes: 3 additions & 1 deletion greenwave/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import greenwave.resources
import xmlrpc.client
from typing import Optional
from werkzeug.exceptions import BadRequest, NotFound
from flask import current_app
from greenwave.safe_yaml import (
Expand Down Expand Up @@ -534,6 +535,7 @@ class Rule(SafeYAMLObject):
This base class is not used directly.
"""

def check(self, policy, rule_context):
"""
Evaluate this policy rule for the given item.
Expand Down Expand Up @@ -796,7 +798,7 @@ class FedoraAtomicCi(PackageSpecificBuild):


class Policy(SafeYAMLObject):
root_yaml_tag = '!Policy'
root_yaml_tag: Optional[str] = '!Policy'

safe_yaml_attributes = {
'id': SafeYAMLString(),
Expand Down
25 changes: 15 additions & 10 deletions greenwave/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
import socket
import threading
from typing import List, Optional

from dateutil import tz
from dateutil.parser import parse
Expand All @@ -28,7 +29,7 @@
requests_session = threading.local().requests_session = get_requests_session()


def _koji(uri):
def _koji(uri: str):
"""
Returns per-thread cached XMLRPC server proxy object for Koji.
"""
Expand All @@ -49,7 +50,11 @@ def _requests_timeout():


class BaseRetriever:
def __init__(self, ignore_ids, when, url):
ignore_ids: List[int]
url: str
since: Optional[str]

def __init__(self, ignore_ids: List[int], when: str, url: str):
self.ignore_ids = ignore_ids
self.url = url

Expand Down Expand Up @@ -165,7 +170,7 @@ class NoSourceException(RuntimeError):


@cached
def retrieve_koji_build_target(nvr, koji_url):
def retrieve_koji_build_target(nvr: str, koji_url: str):
log.debug('Getting Koji task request ID %r', nvr)
proxy = _koji(koji_url)
task_request = proxy.getTaskRequest(nvr)
Expand All @@ -177,7 +182,7 @@ def retrieve_koji_build_target(nvr, koji_url):


@cached
def _retrieve_koji_build_attributes(nvr, koji_url):
def _retrieve_koji_build_attributes(nvr: str, koji_url: str):
log.debug('Getting Koji build %r', nvr)
proxy = _koji(koji_url)
build = proxy.getBuild(nvr)
Expand All @@ -200,15 +205,15 @@ def _retrieve_koji_build_attributes(nvr, koji_url):
return (task_id, source, creation_time)


def retrieve_koji_build_task_id(nvr, koji_url):
def retrieve_koji_build_task_id(nvr: str, koji_url: str):
return _retrieve_koji_build_attributes(nvr, koji_url)[0]


def retrieve_koji_build_source(nvr, koji_url):
def retrieve_koji_build_source(nvr: str, koji_url: str):
return _retrieve_koji_build_attributes(nvr, koji_url)[1]


def retrieve_koji_build_creation_time(nvr, koji_url):
def retrieve_koji_build_creation_time(nvr: str, koji_url: str):
creation_time = _retrieve_koji_build_attributes(nvr, koji_url)[2]
try:
time = parse(str(creation_time))
Expand All @@ -224,7 +229,7 @@ def retrieve_koji_build_creation_time(nvr, koji_url):
return datetime.datetime.now(tz.tzutc())


def retrieve_scm_from_koji(nvr):
def retrieve_scm_from_koji(nvr: str):
"""Retrieve cached rev and namespace from koji using the nvr"""
koji_url = current_app.config["KOJI_BASE_URL"]
try:
Expand All @@ -234,7 +239,7 @@ def retrieve_scm_from_koji(nvr):
return retrieve_scm_from_koji_build(nvr, source, koji_url)


def retrieve_scm_from_koji_build(nvr, source, koji_url):
def retrieve_scm_from_koji_build(nvr: str, source: str, koji_url: str):
if not source:
raise NoSourceException(
'Failed to retrieve SCM URL from Koji build "{}" at "{}" '
Expand Down Expand Up @@ -262,7 +267,7 @@ def retrieve_scm_from_koji_build(nvr, source, koji_url):


@cached
def retrieve_yaml_remote_rule(url):
def retrieve_yaml_remote_rule(url: str):
""" Retrieve a remote rule file content from the git web UI. """
response = requests_session.request('HEAD', url)
if response.status_code == 404:
Expand Down
7 changes: 6 additions & 1 deletion greenwave/safe_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"""
Provides a way of defining type-safe YAML parsing.
"""
from typing import Dict

from dateutil import tz
from dateutil.parser import parse
import yaml

safe_yaml_tag_to_class = {}
safe_yaml_tag_to_class: Dict[str, object] = {}


class SafeYAMLError(RuntimeError):
Expand All @@ -20,6 +22,7 @@ class SafeYAMLAttribute(object):
"""
Base class for SafeYAMLObject attributes (in SafeYAMLObject.safe_yaml_attributes dict).
"""

def __init__(self, optional=False):
self.optional = optional

Expand Down Expand Up @@ -240,6 +243,8 @@ class SafeYAMLObject(yaml.YAMLObject, metaclass=SafeYAMLObjectMetaclass):
"""
yaml_loader = yaml.SafeLoader

safe_yaml_attributes: Dict[str, SafeYAMLAttribute]

@classmethod
def __new__(cls, *args, **kwargs):
result = super().__new__(*args, **kwargs)
Expand Down
9 changes: 7 additions & 2 deletions greenwave/subjects/subject.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# SPDX-License-Identifier: GPL-2.0+

import re
from typing import Union

from greenwave.subjects.subject_type import GenericSubjectType, SubjectType


def _to_dict(format_dict, item):
Expand All @@ -25,10 +28,12 @@ class Subject:
Item or identifier should uniquely identify the artefact (test subject).
"""
_type: Union[GenericSubjectType, SubjectType]
item: str

def __init__(self, type_, item):
self.item = item
def __init__(self, type_: Union[GenericSubjectType, SubjectType], item: str):
self._type = type_
self.item = item

@property
def type(self):
Expand Down
3 changes: 2 additions & 1 deletion greenwave/tests/test_waivers_retriever.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# SPDX-License-Identifier: GPL-2.0+

import mock
from typing import Any, Dict

from greenwave.resources import WaiversRetriever

_DUMMY_RETRIEVER_ARGUMENTS = dict(
_DUMMY_RETRIEVER_ARGUMENTS: Dict[str, Any] = dict(
ignore_ids=[],
when=None,
url=None,
Expand Down
9 changes: 8 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = bandit,lint,py39,docs
envlist = bandit,lint,mypy,py39,docs
isolated_build = True

[testenv]
Expand Down Expand Up @@ -66,6 +66,13 @@ deps =
commands =
python -m flake8 {posargs}

[testenv:mypy]
skip_install = true
deps =
mypy
commands =
mypy -p greenwave --install-types --non-interactive --ignore-missing-imports

[flake8]
show-source = True
max-line-length = 100
Expand Down

0 comments on commit 9059427

Please sign in to comment.