Skip to content

Commit

Permalink
Merge pull request #345 from BDadmehr0/mypy
Browse files Browse the repository at this point in the history
Mypy
  • Loading branch information
BaseMax authored Nov 26, 2024
2 parents f1cfecc + 01a021b commit 73a18f9
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 26 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Mypy Type Check

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
type-check:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the code
- name: Checkout code
uses: actions/checkout@v3

# Step 2: Set up Python
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9

# Step 3: Install dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Step 4: Run Mypy
- name: Run mypy
run: mypy . --explicit-package-bases

2 changes: 1 addition & 1 deletion config/language.validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
docs = yaml.safe_load_all(file)


def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
2 changes: 1 addition & 1 deletion config/layout/attribute/style/global_value.validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
docs = yaml.safe_load_all(file)


def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
2 changes: 1 addition & 1 deletion config/layout/attribute/style/state.validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
docs = yaml.safe_load_all(file)


def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
2 changes: 1 addition & 1 deletion config/layout/attribute/style/type.validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
docs = yaml.safe_load_all(file)


def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
2 changes: 1 addition & 1 deletion config/layout/attribute/style/value.validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
docs = yaml.safe_load_all(file)


def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
2 changes: 1 addition & 1 deletion config/layout/attribute/type.validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
docs = yaml.safe_load_all(file)


def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
2 changes: 1 addition & 1 deletion config/layout/type.validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
docs = yaml.safe_load_all(file)


def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
24 changes: 13 additions & 11 deletions config/sync.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, List

import yaml

SELECTED_LANGUAGE = "fa"
Expand All @@ -6,7 +8,7 @@
COMMENT_END = "// ----------- END AUTO GENERATED ----------- //"


def prettify_type(item, group):
def prettify_type(item: Dict[str, Any], group: Dict[str, Any]) -> str:
global SELECTED_LANGUAGE

itemid = item["id"]
Expand All @@ -15,7 +17,7 @@ def prettify_type(item, group):
return f"ADD_TYPE(" f"{itemid}, " f'"{idtext}", ' f'"{idtext.lower()}"' f")\n"


def prettify_block_type(item, group):
def prettify_block_type(item: Dict[str, Any], group: Dict[str, Any]) -> str:
global SELECTED_LANGUAGE

itemid = item["id"]
Expand All @@ -32,7 +34,7 @@ def prettify_block_type(item, group):
)


def prettify_layout_attribute_type(item, group):
def prettify_layout_attribute_type(item: Dict[str, Any], group: Dict[str, Any]) -> str:
global SELECTED_LANGUAGE

itemid = item["id"]
Expand All @@ -58,7 +60,7 @@ def prettify_layout_attribute_type(item, group):
)


def prettify_layout_attribute_style_global_value(item, group):
def prettify_layout_attribute_style_type(item: Dict[str, Any], group: Dict[str, Any]) -> str:
global SELECTED_LANGUAGE

itemid = item["id"]
Expand All @@ -76,10 +78,10 @@ def prettify_layout_attribute_style_global_value(item, group):
)


def prettify_layout_attribute_style_type(item, group):
def prettify_layout_attribute_style_global_value(item: Dict[str, Any], group: Dict[str, Any]) -> str:
global SELECTED_LANGUAGE

def command(value):
def command(value: str) -> str:
itemid = item["id"]
idtext = itemid.replace("AST_LAYOUT_ATTRIBUTE_STYLE_TYPE_", "")
idtextlower = idtext.lower()
Expand Down Expand Up @@ -132,7 +134,7 @@ def command(value):
return result


def prettify_layout_attribute_style_value(key, items):
def prettify_layout_attribute_style_value(key: str, items: List[Dict[str, Any]]) -> str:
global SELECTED_LANGUAGE

result = "const ast_layout_attribute_style_pair_t " + key + "[] = {\n"
Expand All @@ -159,7 +161,7 @@ def prettify_layout_attribute_style_value(key, items):
return result


def prettify_layout_attribute_style_state_type(item, group):
def prettify_layout_attribute_style_state_type(item: Dict[str, Any], group: Dict[str, Any]) -> str:
global SELECTED_LANGUAGE

itemid = item["id"]
Expand All @@ -183,10 +185,10 @@ def prettify_layout_attribute_style_state_type(item, group):
)


def prettify_layout_type(item, group):
def prettify_layout_type(item: Dict[str, Any], group: Dict[str, Any]) -> str:
global SELECTED_LANGUAGE

def command(value):
def command(value: str) -> str:
itemid = item["id"]
idtext = itemid.replace("AST_LAYOUT_TYPE_", "")
idtextlower = idtext.lower()
Expand Down Expand Up @@ -278,7 +280,7 @@ def command(value):
]


def sync_file(file):
def sync_file(file: Dict[str, Any]) -> None:
print("Syncing file: " + file["input"] + " -> " + file["output"])

output_filename = "../src/config/" + file["output"]
Expand Down
7 changes: 4 additions & 3 deletions config/type.validate.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import Dict, List

import yaml

FILE = "type.yaml"
LANGUAGES = ["en", "fa"]

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)

docs: List[Dict] = list(yaml.safe_load_all(file)) # Annotate docs as a list of dictionaries

def error(msg):
def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

Expand Down
2 changes: 1 addition & 1 deletion example/bintext.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys


def print_hexdump(filename):
def print_hexdump(filename: str) -> None:
try:
with open(filename, "rb") as file:
offset = 0
Expand Down
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mypy]
python_version = 3.9
disallow_untyped_defs = true
ignore_missing_imports = true
strict_optional = true

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pre-commit
mypy
types-PyYAML
9 changes: 5 additions & 4 deletions test/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import filecmp
import os
from pathlib import Path
from typing import Union # Import Union for type annotations

SALAM_BIN = "salam"

Expand All @@ -15,7 +16,7 @@
warnings = 0


def run_tests_in_directory(directory):
def run_tests_in_directory(directory: Path) -> None:
output_dir = directory / "output"

if "output" in directory.parts:
Expand All @@ -34,7 +35,7 @@ def run_tests_in_directory(directory):
)


def compare_output_to_expected(directory):
def compare_output_to_expected(directory: Path) -> None:
global total_tests, passed_tests, failed_tests, warnings

output_dir = directory / "output"
Expand Down Expand Up @@ -66,7 +67,7 @@ def compare_output_to_expected(directory):
total_tests += 1


def process_directory(directory):
def process_directory(directory: Path) -> None:
salam_files = list(directory.glob("*.salam"))
if not salam_files:
return
Expand All @@ -75,7 +76,7 @@ def process_directory(directory):
compare_output_to_expected(directory)


def iterate_directories(base_dir):
def iterate_directories(base_dir: Union[str, Path]) -> None:
for root, dirs, _ in os.walk(base_dir):
dirs[:] = [d for d in dirs if d != "output"]

Expand Down

0 comments on commit 73a18f9

Please sign in to comment.