Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add mypy type checking #15

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -5,5 +5,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
- uses: psf/black@stable
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Install tox
run: pip install tox
- name: Lint with ruff, black and mypy
run: tox -e lint
5 changes: 4 additions & 1 deletion git_hg_sync/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import sys
import logging
from pathlib import Path

import sentry_sdk
@@ -45,7 +46,9 @@ def get_queue(config):
)


def start_app(config, logger, *, one_shot=False):
def start_app(
config: Config, logger: logging.Logger, *, one_shot: bool = False
) -> None:
pulse_config = config.pulse
connection = get_connection(pulse_config)

8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -11,3 +11,11 @@ dependencies = ['kombu', 'mozillapulse', 'GitPython', 'mozlog', "pydantic", "sen

[tool.ruff]
line-length = 100

[[tool.mypy.overrides]]
module = [
'kombu.*',
'mozlog'
]

ignore_missing_imports = true
22 changes: 10 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -5,22 +5,20 @@


@pytest.fixture(autouse=True)
def mozlog_logging():
def mozlog_logging() -> None:
logger = mozlog.structuredlog.StructuredLogger("tests")
mozlog.structuredlog.set_default_logger(logger)


@pytest.fixture
def pulse_config():
def pulse_config() -> PulseConfig:
return PulseConfig(
**{
"userid": "guest",
"host": "pulse",
"port": 5672,
"exchange": "exchange/guest/test",
"routing_key": "#",
"queue": "queue/guest/test",
"password": "guest",
"ssl": False,
}
userid="guest",
host="pulse",
port=5672,
exchange="exchange/guest/test",
routing_key="#",
queue="queue/guest/test",
password="guest",
ssl=False,
)
11 changes: 7 additions & 4 deletions tests/pulse_utils.py
Original file line number Diff line number Diff line change
@@ -2,15 +2,18 @@
import sys
from datetime import datetime
from pathlib import Path
from typing import Any

import kombu

from git_hg_sync.config import Config
from git_hg_sync.config import Config, PulseConfig

HERE = Path(__file__).parent


def send_pulse_message(pulse_config, payload, purge=False):
def send_pulse_message(
pulse_config: PulseConfig, payload: Any, purge: bool = False
) -> None:
"""Send a pulse message
The routing key will be constructed from the repository URL.
The Pulse message will be constructed from the specified payload
@@ -22,7 +25,7 @@ def send_pulse_message(pulse_config, payload, purge=False):
host = pulse_config.host
port = pulse_config.port
exchange = pulse_config.exchange
queue = pulse_config.queue
queue_name = pulse_config.queue
print(f"connecting to pulse at {host}:{port} as {userid}")

connection = kombu.Connection(
@@ -38,7 +41,7 @@ def send_pulse_message(pulse_config, payload, purge=False):
with connection:
ex = kombu.Exchange(exchange, type="direct")
queue = kombu.Queue(
name=queue,
name=queue_name,
exchange=exchange,
routing_key=routing_key,
durable=True,
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
HERE = Path(__file__).parent


def test_load_config():
def test_load_config() -> None:
config = Config.from_file(HERE / "data" / "config.toml")
assert not config.pulse.ssl
assert config.mappings[0].destination.branch == "default"
4 changes: 2 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -9,14 +9,14 @@
from utils import hg_export_tip

from git_hg_sync.__main__ import get_connection, get_queue, start_app
from git_hg_sync.config import Config
from git_hg_sync.config import Config, PulseConfig

NO_RABBITMQ = not os.getenv("RABBITMQ") == "true"
HERE = Path(__file__).parent


@pytest.mark.skipif(NO_RABBITMQ, reason="This test doesn't work without rabbitMq")
def test_send_and_receive(pulse_config):
def test_send_and_receive(pulse_config: PulseConfig) -> None:

payload = {
"type": "tag",
4 changes: 2 additions & 2 deletions tests/test_pulse_worker.py
Original file line number Diff line number Diff line change
@@ -29,13 +29,13 @@ def raw_tag_entity():
}


def test_parse_entity_valid():
def test_parse_entity_valid() -> None:
push_entity = PulseWorker.parse_entity(raw_push_entity())
assert isinstance(push_entity, Push)
tag_entity = PulseWorker.parse_entity(raw_tag_entity())
assert isinstance(tag_entity, Tag)


def test_parse_invalid_type():
def test_parse_invalid_type() -> None:
with pytest.raises(EntityTypeError):
PulseWorker.parse_entity({"type": "unknown"})
4 changes: 2 additions & 2 deletions tests/test_repo_synchronizer.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
from utils import hg_export_tip

from git_hg_sync.__main__ import get_connection, get_queue
from git_hg_sync.config import TrackedRepository
from git_hg_sync.config import TrackedRepository, PulseConfig
from git_hg_sync.repo_synchronizer import RepoSynchronizer


@@ -46,7 +46,7 @@ def test_sync_process_(
assert "FOO CONTENT" in hg_export_tip(hg_remote_repo_path)


def test_get_connection_and_queue(pulse_config):
def test_get_connection_and_queue(pulse_config: PulseConfig) -> None:
connection = get_connection(pulse_config)
queue = get_queue(pulse_config)
assert connection.userid == pulse_config.userid
3 changes: 2 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path
import subprocess


def hg_export_tip(repo_path: str):
def hg_export_tip(repo_path: Path):
process = subprocess.run(
["hg", "export", "tip"],
cwd=repo_path,
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -19,6 +19,10 @@ description = lint source code
deps =
ruff
black
mypy
pytest # dev dependency
ignore_errors = true # Run commands even if an earlier command failed
commands =
ruff check git_hg_sync/ tests/
black --check git_hg_sync tests
mypy git_hg_sync/ tests/