Skip to content

Commit

Permalink
Remove use of typing's Optional and use new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
geordi committed Feb 15, 2024
1 parent aca0d53 commit e45bfd6
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
6 changes: 3 additions & 3 deletions common/inbus/inbus.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import urllib.parse

from django.core.cache import cache
from typing import Dict, List, Optional
from typing import List

import serde

Expand All @@ -10,7 +10,7 @@

# Actual INBUS API calls

def person_by_login(login: str) -> Optional[dto.PersonSimple]:
def person_by_login(login: str) -> dto.PersonSimple | None:
url = urllib.parse.urljoin(config.INBUS_SERVICE_IDM_URL, f'person/login/{login}')

person_resp = utils.inbus_request(url, {})
Expand Down Expand Up @@ -99,7 +99,7 @@ def students_in_concrete_activity(concrete_activity_id: dto.ConcreteActivityId)

# Kelvin's interface

def search_user(login: str) -> Optional[dto.PersonSimple]:
def search_user(login: str) -> dto.PersonSimple | None:
person_inbus = person_by_login(login)

return person_inbus
4 changes: 2 additions & 2 deletions common/inbus/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests

from typing import Dict, Optional
from typing import Dict

from django.core.cache import caches

Expand Down Expand Up @@ -47,7 +47,7 @@ def is_response_ok_or_new_token_(response: requests.Response) -> bool:
return False


def inbus_request(url, params: Dict = None) -> Optional[requests.Response]:
def inbus_request(url, params: Dict = None) -> requests.Response | None:
if params is None:
params={}
token = inbus_token()
Expand Down
12 changes: 6 additions & 6 deletions common/moss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import tempfile
from io import StringIO
from pathlib import Path
from typing import List, Optional
from typing import List

import django_rq
import mosspy
Expand Down Expand Up @@ -208,7 +208,7 @@ def get_relevant_submits(task_id: int) -> List[Submit]:


@django_rq.job("default", timeout=60 * 15)
def moss_check_task(task_id: int, notify_teacher: bool, submit_limit: Optional[int]):
def moss_check_task(task_id: int, notify_teacher: bool, submit_limit: int | None):
start_timestamp = datetime.datetime.now()

log_stream = StringIO()
Expand Down Expand Up @@ -326,7 +326,7 @@ def moss_check_task(task_id: int, notify_teacher: bool, submit_limit: Optional[i
moss_notify_teacher(task_id)


def enqueue_moss_check(task_id: int, notify: bool = False, submit_limit: Optional[int] = None):
def enqueue_moss_check(task_id: int, notify: bool = False, submit_limit: int | None = None):
cache = caches["default"]
moss_delete_result_from_cache(task_id)
job = django_rq.enqueue(moss_check_task, task_id=task_id, notify_teacher=notify,
Expand Down Expand Up @@ -454,10 +454,10 @@ def moss_task_get_opts(task_id: int) -> MossTaskOptions:

def moss_result(
task_id: int,
percent: Optional[int] = None,
lines: Optional[int] = None,
percent: int | None = None,
lines: int | None = None,
filtered: bool = True
) -> Optional[MossResult]:
) -> MossResult | None:
"""
If `filtered` is True, matches will be filtered according to saved or passed filter options.
"""
Expand Down
5 changes: 2 additions & 3 deletions common/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import timedelta
from typing import Optional
from .inbus import inbus
import django.contrib.auth.models
import re
Expand Down Expand Up @@ -32,7 +31,7 @@ def parse_time_interval(text):
return timedelta(**parsed)


def inbus_search_user(login: str) -> Optional[inbus.dto.PersonSimple]:
def inbus_search_user(login: str) -> inbus.dto.PersonSimple | None:
return inbus.search_user(login)


Expand All @@ -47,7 +46,7 @@ def user_from_inbus_person(person: inbus.dto.PersonSimple) -> django.contrib.aut
return user


def user_from_login(login: str) -> Optional[django.contrib.auth.models.User]:
def user_from_login(login: str) -> django.contrib.auth.models.User | None:
"""
A shotcut to calling `inbus_search_user` and `user_from_inbus_person`.
No need to further set anything.
Expand Down
6 changes: 3 additions & 3 deletions evaluator/images/cargo/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import subprocess
from collections import defaultdict
from typing import Any, Dict, List, Optional, Tuple, TypedDict
from typing import Any, Dict, List, Tuple, TypedDict

import bleach

Expand Down Expand Up @@ -42,7 +42,7 @@ def fail(stdout: str, stderr="") -> "BuildResult":
)


def get_param(name: str, default: Any, parse_json: bool = False) -> Optional[Any]:
def get_param(name: str, default: Any, parse_json: bool = False) -> Any | None:
value = os.getenv(f"PIPE_{name.upper()}")
if value is None:
return default
Expand All @@ -66,7 +66,7 @@ class CargoOutput:


# Returns (file, line)
def get_location_from_cargo_msg(message) -> Optional[Tuple[str, int]]:
def get_location_from_cargo_msg(message) -> Tuple[str, int] | None:
spans = message.get("spans")
if spans is None or len(spans) < 1:
return None
Expand Down
6 changes: 3 additions & 3 deletions evaluator/images/dotnet/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import glob
from collections import defaultdict
from pathlib import Path
from typing import Dict, List, Optional
from typing import Dict, List
import xml.etree.ElementTree as ET

import bleach
Expand All @@ -17,8 +17,8 @@
class BuildResult:
success: bool
output: str
comments: Optional[Dict[str, List[str]]] = dataclasses.field(default_factory=dict)
tests: Optional[List[dict]] = dataclasses.field(default_factory=list)
comments: Dict[str, List[str]] | None = dataclasses.field(default_factory=dict)
tests: List[dict] | None = dataclasses.field(default_factory=list)

@staticmethod
def fail(error: str) -> "BuildResult":
Expand Down
6 changes: 3 additions & 3 deletions evaluator/images/java/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import glob
from collections import defaultdict
from pathlib import Path
from typing import Dict, List, Optional
from typing import Dict, List
import xml.etree.ElementTree as ET

import bleach
Expand All @@ -17,8 +17,8 @@
class BuildResult:
success: bool
output: str
comments: Optional[Dict[str, List[str]]] = dataclasses.field(default_factory=dict)
tests: Optional[List[dict]] = dataclasses.field(default_factory=list)
comments: Dict[str, List[str]] | None = dataclasses.field(default_factory=dict)
tests: List[dict] | None = dataclasses.field(default_factory=list)

@staticmethod
def fail(error: str) -> "BuildResult":
Expand Down
16 changes: 8 additions & 8 deletions web/views/assets/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import subprocess
import sys
from pathlib import Path
from typing import List, Optional
from typing import List

# Usage: python3 run-tests.py <path-to-binary>
# Example: python3 run-tests.py ./main
Expand Down Expand Up @@ -84,14 +84,14 @@ def calculate_diff(workdir: Path, name: str, expected: Path, actual: Path, inden
def run_test(
binary: str,
name: str,
title: Optional[str] = None,
args: Optional[List[str]] = None,
stdin: Optional[str] = None,
stdout: Optional[str] = None,
stderr: Optional[str] = None,
title: str | None = None,
args: List[str] | None = None,
stdin: str | None = None,
stdout: str | None = None,
stderr: str | None = None,
exit_code: int = 0,
files_in: Optional[List[str]] = None,
files_out: Optional[List[str]] = None
files_in: List[str] | None = None,
files_out: List[str] | None = None
):
workdir = get_test_workdir(name)
shutil.rmtree(workdir, ignore_errors=True)
Expand Down

0 comments on commit e45bfd6

Please sign in to comment.