Skip to content

Commit

Permalink
ci: Add mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
WCY-dt committed Dec 18, 2024
1 parent 5afc412 commit f58a748
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Pylint
name: Pylint and MyPy

on: [push]

Expand All @@ -17,8 +17,11 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install -r requirements.txt
pip install pylint mypy types-requests types-pytz
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
- name: Type checking with mypy
run: |
mypy util generator
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[简体中文](README_zh-CN.md) | English

[![Deploy state](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml/badge.svg)](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml) [![Server Status](https://img.shields.io/badge/dynamic/json?logo=linux&color=brightgreen&label=Server%20status&query=%24.status&cacheSeconds=600&url=https%3A%2F%2F2024.ch3nyang.top%2Fstatus)](https://2024.ch3nyang.top)
[![Pylint](https://github.com/WCY-dt/my-github-2024/actions/workflows/pylint.yml/badge.svg)](https://github.com/WCY-dt/my-github-2024/actions/workflows/pylint.yml) [![Deploy state](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml/badge.svg)](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml) [![Server Status](https://img.shields.io/badge/dynamic/json?logo=linux&color=brightgreen&label=Server%20status&query=%24.status&cacheSeconds=600&url=https%3A%2F%2F2024.ch3nyang.top%2Fstatus)](https://2024.ch3nyang.top)

<strong style="font-size: 24px;">👉 Try it now: <a href="https://2024.ch3nyang.top">https://2024.ch3nyang.top</a></strong>
</div>
Expand Down
2 changes: 1 addition & 1 deletion README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[English](README.md) | 简体中文

[![Deploy state](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml/badge.svg)](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml) [![Server Status](https://img.shields.io/badge/dynamic/json?logo=linux&color=brightgreen&label=Server%20status&query=%24.status&cacheSeconds=600&url=https%3A%2F%2F2024.ch3nyang.top%2Fstatus)](https://2024.ch3nyang.top)
[![Pylint and Mypy](https://github.com/WCY-dt/my-github-2024/actions/workflows/pylint_and_mypy.yml/badge.svg)](https://github.com/WCY-dt/my-github-2024/actions/workflows/pylint_and_mypy.yml) [![Deploy state](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml/badge.svg)](https://github.com/WCY-dt/my-github-2024/actions/workflows/deploy.yml) [![Server Status](https://img.shields.io/badge/dynamic/json?logo=linux&color=brightgreen&label=Server%20status&query=%24.status&cacheSeconds=600&url=https%3A%2F%2F2024.ch3nyang.top%2Fstatus)](https://2024.ch3nyang.top)

<strong style="font-size: 24px;">👉 立即体验: <a href="https://2024.ch3nyang.top">https://2024.ch3nyang.top</a></strong>
</div>
Expand Down
3 changes: 2 additions & 1 deletion generator/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging

from log.logging_config import setup_logging
from util import Github

setup_logging()

Expand Down Expand Up @@ -43,7 +44,7 @@
}


def fetch_github(github: object, year: int, skip_fetch: bool = False) -> tuple:
def fetch_github(github: Github, year: int, skip_fetch: bool = False) -> tuple:
"""
Fetches and processes GitHub data for a given year.
Expand Down
22 changes: 13 additions & 9 deletions util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging
from datetime import timedelta
from datetime import timezone as dt_timezone
from typing import Any

import pytz

Expand All @@ -27,7 +28,7 @@ class Github:
Attributes:
access_token (str): The Github access token.
username (str): The Github username.
timezone (pytz.timezone): The timezone.
timezone (pytz.BaseTzInfo): The timezone.
data (dict | list | None): The fetched data.
Methods:
Expand All @@ -54,6 +55,8 @@ class Github:
result: Get the result data.
"""

data: dict[str, Any] | None

def __init__(
self, access_token: str, username: str, timezone: str = "Asia/Shanghai"
) -> None:
Expand All @@ -67,26 +70,26 @@ def __init__(
"""
self.access_token: str = access_token
self.username: str = username
self.timezone: pytz.timezone = self._parse_timezone(timezone)
self.timezone: pytz.BaseTzInfo = self._parse_timezone(timezone)
self.data = None

def _parse_timezone(self, tz_str: str) -> pytz.timezone:
def _parse_timezone(self, tz_str: str) -> pytz.BaseTzInfo:
"""
Parse the timezone string to a pytz timezone object.
Args:
tz_str (str): The timezone string.
Returns:
pytz.timezone: The pytz timezone object.
pytz.BaseTzInfo: The pytz timezone object.
"""
try:
return pytz.timezone(tz_str)
except pytz.UnknownTimeZoneError as e:
if tz_str.startswith("+") or tz_str.startswith("-"):
hours_offset = int(tz_str)
timezone = dt_timezone(timedelta(hours=hours_offset))
return pytz.timezone(timezone)
return pytz.timezone(str(timezone))

raise e

Expand Down Expand Up @@ -186,7 +189,8 @@ def filter_json(self, key: dict) -> "Github":
Returns:
Github: The Github object with the filtered data.
"""
self.data = gh_filter.json_key(self.data, key)
if self.data is not None:
self.data = gh_filter.json_key(self.data, key)
return self

def count_all(self) -> "Github":
Expand Down Expand Up @@ -262,15 +266,15 @@ def write_to_file(self, filename: str) -> "Github":
f.write(json.dumps(self.result, indent=4))
return self

def _get_result_structure(self, data: dict | list | None) -> dict:
def _get_result_structure(self, data: dict | list | None) -> dict | list | str:
"""
Get the result structure of the data.
Args:
data (dict | list): The data to get the result structure from.
Returns:
dict: The result structure of the data.
dict | list | str: The result structure of the data.
"""
if isinstance(data, list):
return [self._get_result_structure(data[0])]
Expand Down Expand Up @@ -299,7 +303,7 @@ def result(self) -> dict | list | None:
return self.data

@result.setter
def result(self, value: dict | list | None) -> None:
def result(self, value: dict[str, Any] | None) -> None:
"""
Set the result data.
Expand Down
24 changes: 12 additions & 12 deletions util/gh_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def commits_types_number(data: dict) -> dict:
Returns:
dict: The data containing the number of commits of each type.
"""
commits_types = {}
commits_types: dict[str, int] = {}
for repo in data["repos_details"]:
repo_commits_types = {}
repo_commits_types: dict[str, int] = {}
for commit in repo["commits_details"]:
commit_type = _get_commit_type(commit["message"])

Expand All @@ -127,7 +127,7 @@ def commits_types_number(data: dict) -> dict:


def _count_monthly(items: list, key: str) -> dict:
monthly_count = {}
monthly_count: dict[int, int] = {}
for item in items:
month = datetime.fromisoformat(item[key]).month
if month in monthly_count:
Expand Down Expand Up @@ -172,7 +172,7 @@ def commits_monthly_number(data: dict) -> dict:


def _count_weekly(items: list, key: str) -> dict:
weekly_count = {}
weekly_count: dict[int, int] = {}
for item in items:
weekday = datetime.fromisoformat(item[key]).weekday()
if weekday in weekly_count:
Expand All @@ -182,7 +182,7 @@ def _count_weekly(items: list, key: str) -> dict:
return weekly_count


def _fill_weekly_list(weekly_count: dict) -> dict:
def _fill_weekly_list(weekly_count: dict) -> list:
weekly_list = []
for i in range(7):
if i in weekly_count:
Expand All @@ -202,7 +202,7 @@ def commits_weekdaily_number(data: dict) -> dict:
Returns:
dict: The data containing the number of commits in each weekday.
"""
commits_weekly = {}
commits_weekly: dict[int, int] = {}

for repo in data["repos_details"]:
repo_commits_weekly = _count_weekly(repo["commits_details"], "created_time")
Expand All @@ -222,7 +222,7 @@ def commits_weekdaily_number(data: dict) -> dict:


def _count_daily(items: list, key: str) -> dict:
daily_count = {}
daily_count: dict[str, int] = {}
for item in items:
date = datetime.fromisoformat(item[key]).date().isoformat()
if date in daily_count:
Expand All @@ -233,7 +233,7 @@ def _count_daily(items: list, key: str) -> dict:


def _fill_daily_list(daily_count: dict) -> dict:
daily_list = {}
daily_list: dict[int, list[int]] = {}
for year in range(2000, datetime.now().year + 2):
days_in_year = (
366 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else 365
Expand Down Expand Up @@ -280,7 +280,7 @@ def commits_daily_number(data: dict) -> dict:


def _count_hourly(items: list, key: str) -> dict:
hourly_count = {}
hourly_count: dict[int, int] = {}
for item in items:
hour = datetime.fromisoformat(item[key]).hour
if hour in hourly_count:
Expand Down Expand Up @@ -310,7 +310,7 @@ def commits_hourly_number(data: dict) -> dict:
Returns:
dict: The data containing the number of commits in each hour.
"""
commits_hourly = {}
commits_hourly: dict[int, int] = {}

for repo in data["repos_details"]:
repo_commits_hourly = _count_hourly(repo["commits_details"], "created_time")
Expand Down Expand Up @@ -355,8 +355,8 @@ def repos_languages_number(data: dict) -> dict:
Returns:
dict: The data containing the number of repositories using each language.
"""
languages_num = {}
repos_languages_count = {}
languages_num: dict[str, int] = {}
repos_languages_count: dict[str, int] = {}
for repo in data["repos_details"]:
for key, value in repo["languages_num"].items():
if key in languages_num:
Expand Down
Loading

0 comments on commit f58a748

Please sign in to comment.