Skip to content

Commit

Permalink
feat(py): introduce mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Nov 20, 2024
1 parent 0b7add8 commit ec48c71
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
6 changes: 5 additions & 1 deletion template/py/{{cookiecutter.project_slug}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sync:

# Build wheel
build:
@uvx hatch build
@uv build

# Test
test:
Expand All @@ -28,6 +28,10 @@ ruff:
@uvx ruff format .
@uvx ruff check . --fix

# Type check
type:
@uvx mypy .

# Build image
image:
@docker image build -t $(APP_NAME) .
Expand Down
25 changes: 23 additions & 2 deletions template/py/{{cookiecutter.project_slug}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,26 @@ exclude_lines = [
show_missing = true # Show missing lines in the report
precision = 2 # Number of decimal places to use when rounding

# [tool.uv.pip]
# index-url = "https://mirrors.cernet.edu.cn/pypi/web/simple"
[tool.mypy]
# Enable strict mode for comprehensive type checking
strict = true
python_version = "{{cookiecutter.python_version}}"

# Error reporting settings
pretty = true # Beautify output for better readability
show_error_codes = true # Display error codes for easier troubleshooting
show_error_context = true # Show context for errors to aid debugging

# Ignore missing type hints for third-party libraries
ignore_missing_imports = true

# Exclude specific paths or files from type checking
exclude = [
"tests/",
"setup.py",
]

# Relax type-checking rules for test files (e.g., allow functions without type annotations)
[[tool.mypy.overrides]]
module = "tests.*"
disallow_untyped_defs = false
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from http.server import HTTPServer, SimpleHTTPRequestHandler


def run_server(port=8000):
def run_server(port: int = 8000) -> None:
handler = SimpleHTTPRequestHandler
with HTTPServer(("", port), handler) as httpd:
print(f"Serving at port {port}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
def add(a, b):
def add(a: int | float, b: int | float) -> int | float:
return a + b


def subtract(a, b):
def subtract(a: int | float, b: int | float) -> int | float:
return a - b


def multiply(a, b):
def multiply(a: int | float, b: int | float) -> int | float:
return a * b


def divide(a, b):
def divide(a: int | float, b: int | float) -> int | float:
if b == 0:
raise ValueError("Division by zero is not allowed.")
return a / b

0 comments on commit ec48c71

Please sign in to comment.