Skip to content

Commit

Permalink
mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Sep 8, 2023
1 parent 656bc1b commit 3af5fc5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FILES=bugme.py

.PHONY: all
all: flake8 pylint
all: flake8 pylint mypy

.PHONY: flake8
flake8:
Expand All @@ -10,3 +10,7 @@ flake8:
.PHONY: pylint
pylint:
@pylint --disable=line-too-long $(FILES)

.PHONY: mypy
mypy:
@mypy --disable-error-code=attr-defined $(FILES)
41 changes: 20 additions & 21 deletions bugme.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
from concurrent.futures import ThreadPoolExecutor
from urllib.parse import urlparse, parse_qs
from typing import Any, Union
from typing import Any, Generator, List, Union

from datetime import datetime
from dateutil import parser
Expand All @@ -18,10 +18,10 @@
from github import Github, Auth, GithubException
from gitlab import Gitlab
from gitlab.exceptions import GitlabError
from bugzilla import Bugzilla
from bugzilla.exceptions import BugzillaError
from redminelib import Redmine
from redminelib.exceptions import BaseRedmineError
from bugzilla import Bugzilla # type: ignore
from bugzilla.exceptions import BugzillaError # type: ignore
from redminelib import Redmine # type: ignore
from redminelib.exceptions import BaseRedmineError # type: ignore

CREDENTIALS_FILE = os.path.expanduser("~/creds.json")

Expand Down Expand Up @@ -56,13 +56,14 @@ def __setitem__(self, item: str, value: Any):
setattr(self, item, value)


def get_item(string: str) -> Item:
def get_item(string: str) -> Union[Item, None]:
"""
Get Item from string
"""
if "#" not in string:
string = string if string.startswith("https://") else f"https://{string}"
url = urlparse(string)
assert url.hostname is not None
if url.hostname.startswith("git"):
return Item(
host=url.hostname,
Expand Down Expand Up @@ -113,13 +114,13 @@ def __exit__(self, exc_type, exc_value, traceback):
traceback,
)

def get_item(self, item: int) -> Item:
def get_item(self, item: Item) -> Union[Item, None]:
"""
This method must be overriden if get_items() isn't overriden
"""
raise NotImplementedError(f"{self.__class__.__name__}: get_item()")

def get_items(self, items: list) -> list:
def get_items(self, items: List[Item]) -> Generator[Union[Item, None], None, None]:
"""
Multithreaded get_items()
"""
Expand All @@ -144,7 +145,7 @@ def __exit__(self, exc_type, exc_value, traceback):
pass
super().__exit__(exc_type, exc_value, traceback)

def get_item(self, item: int) -> Item:
def get_item(self, item: Item) -> Union[Item, None]:
"""
Get Bugzilla item
"""
Expand All @@ -154,7 +155,7 @@ def get_item(self, item: int) -> Item:
logging.error("Bugzilla: %s: get_items(%d): %s", self.url, item, exc)
return None

def get_items(self, items: list) -> list:
def get_items(self, items: List[Item]) -> Generator[Union[Item, None], None, None]:
"""
Get Bugzilla items
"""
Expand All @@ -164,7 +165,7 @@ def get_items(self, items: list) -> list:
except BugzillaError as exc:
logging.error("Bugzilla: %s: get_items(): %s", self.url, exc)

def _to_item(self, info) -> Item:
def _to_item(self, info) -> Union[Item, None]:
return Item(
issue_id=info.id,
status=info.status,
Expand All @@ -185,7 +186,7 @@ def __init__(self, creds: dict):
auth = Auth.Token(**creds)
self.client = Github(auth=auth)

def get_item(self, item: Item) -> Item:
def get_item(self, item: Item) -> Union[Item, None]:
"""
Get Github issue
"""
Expand All @@ -196,7 +197,7 @@ def get_item(self, item: Item) -> Item:
return None
return self._to_item(info, item)

def _to_item(self, info, item) -> Item:
def _to_item(self, info, item) -> Union[Item, None]:
return Item(
issue_id=info.number,
status=info.state,
Expand All @@ -214,10 +215,8 @@ class MyGitlab(Service):

def __init__(self, url: str, creds: dict):
super().__init__(url)
ssl_verify = os.getenv("REQUESTS_CA_BUNDLE") if self.url else True
self.client = Gitlab(
url=self.url, ssl_verify=ssl_verify, retry_transient_errors=False, **creds
)
ssl_verify = os.environ.get("REQUESTS_CA_BUNDLE", False) if self.url else True
self.client = Gitlab(url=self.url, ssl_verify=ssl_verify, **creds)

def __exit__(self, exc_type, exc_value, traceback):
try:
Expand All @@ -226,7 +225,7 @@ def __exit__(self, exc_type, exc_value, traceback):
pass
super().__exit__(exc_type, exc_value, traceback)

def get_item(self, item: Item) -> Item:
def get_item(self, item: Item) -> Union[Item, None]:
"""
Get Gitlab issue
"""
Expand All @@ -237,7 +236,7 @@ def get_item(self, item: Item) -> Item:
return None
return self._to_item(info, item)

def _to_item(self, info, item) -> Item:
def _to_item(self, info, item) -> Union[Item, None]:
return Item(
issue_id=info.iid,
status=info.state,
Expand All @@ -257,7 +256,7 @@ def __init__(self, url: str, creds: dict):
super().__init__(url)
self.client = Redmine(url=self.url, raise_attr_exception=False, **creds)

def get_item(self, item: Item) -> Item:
def get_item(self, item: Item) -> Union[Item, None]:
"""
Get Redmine ticket
"""
Expand All @@ -268,7 +267,7 @@ def get_item(self, item: Item) -> Item:
return None
return self._to_item(info)

def _to_item(self, info) -> Item:
def _to_item(self, info) -> Union[Item, None]:
return Item(
issue_id=info.id,
status=info.status,
Expand Down
3 changes: 3 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

flake8
pylint
mypy
types-python-dateutil
types-pytz

0 comments on commit 3af5fc5

Please sign in to comment.