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

Add more typing to the codebase. #287

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions green/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,13 +580,13 @@ class ConfigFile: # pragma: no cover
"""

def __init__(self, filepath: pathlib.Path) -> None:
self._first = True
self._lines = filepath.read_text().splitlines(keepends=True)
self._first: bool = True
self._lines: list[str] = filepath.read_text().splitlines(keepends=True)

def __iter__(self):
def __iter__(self) -> ConfigFile:
return self

def __next__(self):
def __next__(self) -> str:
if self._first:
self._first = False
return "[green]\n"
Expand Down
17 changes: 9 additions & 8 deletions green/djangorunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import annotations

from argparse import ArgumentParser, Namespace
import pathlib
import os
import sys
from typing import Any, Final, Sequence
Expand All @@ -23,10 +24,10 @@

# If we're not being run from an actual django project, set up django config
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "green.djangorunner")
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = pathlib.Path(__file__).absolute().parent.parent
SECRET_KEY: Final[str] = ")9^_e(=cisybdt4m4+fs+_wb%d$!9mpcoy0um^alvx%gexj#jv"
DEBUG = True
TEMPLATE_DEBUG = True
DEBUG: bool = True
TEMPLATE_DEBUG: bool = True
ALLOWED_HOSTS: Sequence[str] = []
INSTALLED_APPS: Final[Sequence[str]] = (
"django.contrib.admin",
Expand All @@ -51,14 +52,14 @@
DATABASES: Final[dict[str, dict[str, str]]] = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
"NAME": str(BASE_DIR / "db.sqlite3"),
}
}
LANGUAGE_CODE: Final[str] = "en-us"
TIME_ZONE: Final[str] = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True
USE_I18N: bool = True
USE_L10N: bool = True
USE_TZ: bool = True
STATIC_URL: Final[str] = "/static/"
# End of django fake config stuff

Expand All @@ -75,7 +76,7 @@ def django_missing() -> None:
from django.test.runner import DiscoverRunner

class DjangoRunner(DiscoverRunner):
def __init__(self, verbose: int = -1, **kwargs):
def __init__(self, verbose: int = -1, **kwargs: Any):
super().__init__(**kwargs)
self.verbose = verbose
self.loader = GreenTestLoader()
Expand Down
36 changes: 26 additions & 10 deletions green/junit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from green.result import GreenTestResult, ProtoTest, ProtoError
from lxml.etree import _Element

# TODO: use NamedTuple for TestVerdict.
TestVerdict: TypeAlias = Union[
Tuple[int, ProtoTest], Tuple[int, ProtoTest, Union[str, ProtoError]]
]
Expand Down Expand Up @@ -111,7 +112,9 @@ def _suite_name(test) -> str:
return f"{test.module}.{test.class_name}"

@staticmethod
def _add_failures(collection: TestsCollection, test_results: GreenTestResult):
def _add_failures(
collection: TestsCollection, test_results: GreenTestResult
) -> None:
for each_test, failure in test_results.failures:
key = JUnitXML._suite_name(each_test)
if key not in collection:
Expand All @@ -127,7 +130,9 @@ def _add_errors(collection: TestsCollection, test_results: GreenTestResult):
collection[key].append((Verdict.ERROR, each_test, error))

@staticmethod
def _add_skipped_tests(collection: TestsCollection, test_results: GreenTestResult):
def _add_skipped_tests(
collection: TestsCollection, test_results: GreenTestResult
) -> None:
for each_test, reason in test_results.skipped:
key = JUnitXML._suite_name(each_test)
if key not in collection:
Expand Down Expand Up @@ -160,16 +165,23 @@ def _convert_suite(
return xml_suite

@staticmethod
def _count_test_with_verdict(verdict: int, suite):
def _count_test_with_verdict(verdict: int, suite: list[TestVerdict]) -> int:
return sum(1 for entry in suite if entry[0] == verdict)

def _convert_test(self, results, verdict, test, *details) -> _Element:
def _convert_test(
self,
results: GreenTestResult,
verdict: int,
test: ProtoTest,
*details: str | ProtoError,
) -> _Element:
xml_test = Element(JUnitDialect.TEST_CASE)
xml_test.set(JUnitDialect.NAME, test.method_name)
xml_test.set(JUnitDialect.CLASS_NAME, test.class_name)
xml_test.set(JUnitDialect.TEST_TIME, test.test_time)

xml_verdict = self._convert_verdict(verdict, test, details)
error: str | ProtoError | None = details[0] if details else None
xml_verdict = self._convert_verdict(verdict, test, error)
if xml_verdict is not None:
xml_test.append(xml_verdict)

Expand All @@ -185,21 +197,25 @@ def _convert_test(self, results, verdict, test, *details) -> _Element:

return xml_test

def _convert_verdict(self, verdict: int, test, details) -> _Element | None:
# FIXME: test is not used.
def _convert_verdict(
self, verdict: int, test: ProtoTest, error_details: str | ProtoError | None
) -> _Element | None:
message = str(error_details) if error_details else ""
if verdict == Verdict.FAILED:
failure = Element(JUnitDialect.FAILURE)
failure.text = str(details[0])
failure.text = message
return failure
if verdict == Verdict.ERROR:
error = Element(JUnitDialect.ERROR)
error.text = str(details[0])
error.text = message
return error
if verdict == Verdict.SKIPPED:
skipped = Element(JUnitDialect.SKIPPED)
skipped.text = str(details[0])
skipped.text = message
return skipped
return None

@staticmethod
def _suite_time(suite) -> float:
def _suite_time(suite: list[TestVerdict]) -> float:
return sum(float(each_test.test_time) for verdict, each_test, *details in suite)
Loading