From 2547d52f4ec01a7822dda1ef8be39c9745f66945 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 10 Jan 2025 15:38:06 -0600 Subject: [PATCH] Update to ruff 0.9, fix warnings --- course/content.py | 5 ++-- course/grades.py | 8 +++---- course/page/base.py | 33 ++++++++++---------------- course/validation.py | 13 ++++++----- poetry.lock | 40 ++++++++++++++++---------------- pyproject.toml | 2 +- tests/base_test_mixins.py | 2 +- tests/test_content.py | 5 ++-- tests/test_grades/test_grades.py | 5 ++-- 9 files changed, 54 insertions(+), 59 deletions(-) diff --git a/course/content.py b/course/content.py index 700ff5be0..1e4a7c656 100644 --- a/course/content.py +++ b/course/content.py @@ -28,6 +28,7 @@ import os import re import sys +from dataclasses import dataclass from typing import cast from xml.etree.ElementTree import Element, tostring @@ -1195,9 +1196,9 @@ def unknown_decl(self, data): self.out_file.write(f"") +@dataclass class PreserveFragment: - def __init__(self, s: str) -> None: - self.s = s + s: str class LinkFixerTreeprocessor(Treeprocessor): diff --git a/course/grades.py b/course/grades.py index 08ecb593d..0bd9ba373 100644 --- a/course/grades.py +++ b/course/grades.py @@ -24,6 +24,7 @@ """ import re +from dataclasses import dataclass from decimal import Decimal from typing import ( TYPE_CHECKING, @@ -210,11 +211,10 @@ def view_grading_opportunity_list(pctx): # {{{ teacher grade book +@dataclass class GradeInfo: - def __init__(self, opportunity: GradingOpportunity, - grade_state_machine: GradeStateMachine) -> None: - self.opportunity = opportunity - self.grade_state_machine = grade_state_machine + opportunity: GradingOpportunity + grade_state_machine: GradeStateMachine def get_grade_table(course: Course) -> tuple[ diff --git a/course/page/base.py b/course/page/base.py index f4bdb1cb1..6f415680d 100644 --- a/course/page/base.py +++ b/course/page/base.py @@ -25,6 +25,7 @@ from abc import ABC, abstractmethod from collections.abc import Callable, Sequence +from dataclasses import dataclass from typing import TYPE_CHECKING, Any import django.forms as forms @@ -97,6 +98,7 @@ """ +@dataclass class PageContext: """ .. attribute:: course @@ -113,24 +115,13 @@ class PageContext: which is used internally by the flow views. """ - def __init__( - self, - course: Course, - repo: Repo_ish, - commit_sha: bytes, - flow_session: FlowSession, - in_sandbox: bool = False, - page_uri: str | None = None, - request: django.http.HttpRequest | None = None, - ) -> None: - - self.course = course - self.repo = repo - self.commit_sha = commit_sha - self.flow_session = flow_session - self.in_sandbox = in_sandbox - self.page_uri = page_uri - self.request = request + course: Course + repo: Repo_ish + commit_sha: bytes + flow_session: FlowSession + in_sandbox: bool = False + page_uri: str | None = None + request: django.http.HttpRequest | None = None class PageBehavior: @@ -194,11 +185,11 @@ def round_point_count_to_quarters( return int(value) import math - _atol = atol * 4 + actual_atol = atol * 4 v = value * 4 - if abs(v - math.floor(v)) < _atol: + if abs(v - math.floor(v)) < actual_atol: v = math.floor(v) - elif abs(v - math.ceil(v)) < _atol: + elif abs(v - math.ceil(v)) < actual_atol: v = math.ceil(v) else: return value diff --git a/course/validation.py b/course/validation.py index 87554bedf..e42ebb86e 100644 --- a/course/validation.py +++ b/course/validation.py @@ -27,6 +27,7 @@ import re import sys from collections.abc import Sequence +from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Literal, TypeAlias import dulwich.objects @@ -226,10 +227,10 @@ def validate_struct( # }}} +@dataclass class ValidationWarning: - def __init__(self, location: str | None, text: str) -> None: - self.location = location - self.text = text + location: str | None + text: str class ValidationContext: @@ -1588,10 +1589,10 @@ def tree(self): return FileSystemFakeRepoTree(self.root) +@dataclass class FileSystemFakeRepoTreeEntry: # pragma: no cover - def __init__(self, path: bytes, mode: int) -> None: - self.path = path - self.mode = mode + path: bytes + mode: int class FileSystemFakeRepoTree: # pragma: no cover diff --git a/poetry.lock b/poetry.lock index d59db7887..563ce6f52 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2432,29 +2432,29 @@ files = [ [[package]] name = "ruff" -version = "0.8.6" +version = "0.9.1" description = "An extremely fast Python linter and code formatter, written in Rust." optional = false python-versions = ">=3.7" files = [ - {file = "ruff-0.8.6-py3-none-linux_armv6l.whl", hash = "sha256:defed167955d42c68b407e8f2e6f56ba52520e790aba4ca707a9c88619e580e3"}, - {file = "ruff-0.8.6-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:54799ca3d67ae5e0b7a7ac234baa657a9c1784b48ec954a094da7c206e0365b1"}, - {file = "ruff-0.8.6-py3-none-macosx_11_0_arm64.whl", hash = "sha256:e88b8f6d901477c41559ba540beeb5a671e14cd29ebd5683903572f4b40a9807"}, - {file = "ruff-0.8.6-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0509e8da430228236a18a677fcdb0c1f102dd26d5520f71f79b094963322ed25"}, - {file = "ruff-0.8.6-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:91a7ddb221779871cf226100e677b5ea38c2d54e9e2c8ed847450ebbdf99b32d"}, - {file = "ruff-0.8.6-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:248b1fb3f739d01d528cc50b35ee9c4812aa58cc5935998e776bf8ed5b251e75"}, - {file = "ruff-0.8.6-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:bc3c083c50390cf69e7e1b5a5a7303898966be973664ec0c4a4acea82c1d4315"}, - {file = "ruff-0.8.6-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:52d587092ab8df308635762386f45f4638badb0866355b2b86760f6d3c076188"}, - {file = "ruff-0.8.6-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:61323159cf21bc3897674e5adb27cd9e7700bab6b84de40d7be28c3d46dc67cf"}, - {file = "ruff-0.8.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7ae4478b1471fc0c44ed52a6fb787e641a2ac58b1c1f91763bafbc2faddc5117"}, - {file = "ruff-0.8.6-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:0c000a471d519b3e6cfc9c6680025d923b4ca140ce3e4612d1a2ef58e11f11fe"}, - {file = "ruff-0.8.6-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:9257aa841e9e8d9b727423086f0fa9a86b6b420fbf4bf9e1465d1250ce8e4d8d"}, - {file = "ruff-0.8.6-py3-none-musllinux_1_2_i686.whl", hash = "sha256:45a56f61b24682f6f6709636949ae8cc82ae229d8d773b4c76c09ec83964a95a"}, - {file = "ruff-0.8.6-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:496dd38a53aa173481a7d8866bcd6451bd934d06976a2505028a50583e001b76"}, - {file = "ruff-0.8.6-py3-none-win32.whl", hash = "sha256:e169ea1b9eae61c99b257dc83b9ee6c76f89042752cb2d83486a7d6e48e8f764"}, - {file = "ruff-0.8.6-py3-none-win_amd64.whl", hash = "sha256:f1d70bef3d16fdc897ee290d7d20da3cbe4e26349f62e8a0274e7a3f4ce7a905"}, - {file = "ruff-0.8.6-py3-none-win_arm64.whl", hash = "sha256:7d7fc2377a04b6e04ffe588caad613d0c460eb2ecba4c0ccbbfe2bc973cbc162"}, - {file = "ruff-0.8.6.tar.gz", hash = "sha256:dcad24b81b62650b0eb8814f576fc65cfee8674772a6e24c9b747911801eeaa5"}, + {file = "ruff-0.9.1-py3-none-linux_armv6l.whl", hash = "sha256:84330dda7abcc270e6055551aca93fdde1b0685fc4fd358f26410f9349cf1743"}, + {file = "ruff-0.9.1-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:3cae39ba5d137054b0e5b472aee3b78a7c884e61591b100aeb544bcd1fc38d4f"}, + {file = "ruff-0.9.1-py3-none-macosx_11_0_arm64.whl", hash = "sha256:50c647ff96f4ba288db0ad87048257753733763b409b2faf2ea78b45c8bb7fcb"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f0c8b149e9c7353cace7d698e1656ffcf1e36e50f8ea3b5d5f7f87ff9986a7ca"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:beb3298604540c884d8b282fe7625651378e1986c25df51dec5b2f60cafc31ce"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:39d0174ccc45c439093971cc06ed3ac4dc545f5e8bdacf9f067adf879544d969"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:69572926c0f0c9912288915214ca9b2809525ea263603370b9e00bed2ba56dbd"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:937267afce0c9170d6d29f01fcd1f4378172dec6760a9f4dface48cdabf9610a"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:186c2313de946f2c22bdf5954b8dd083e124bcfb685732cfb0beae0c47233d9b"}, + {file = "ruff-0.9.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3f94942a3bb767675d9a051867c036655fe9f6c8a491539156a6f7e6b5f31831"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:728d791b769cc28c05f12c280f99e8896932e9833fef1dd8756a6af2261fd1ab"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:2f312c86fb40c5c02b44a29a750ee3b21002bd813b5233facdaf63a51d9a85e1"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_i686.whl", hash = "sha256:ae017c3a29bee341ba584f3823f805abbe5fe9cd97f87ed07ecbf533c4c88366"}, + {file = "ruff-0.9.1-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:5dc40a378a0e21b4cfe2b8a0f1812a6572fc7b230ef12cd9fac9161aa91d807f"}, + {file = "ruff-0.9.1-py3-none-win32.whl", hash = "sha256:46ebf5cc106cf7e7378ca3c28ce4293b61b449cd121b98699be727d40b79ba72"}, + {file = "ruff-0.9.1-py3-none-win_amd64.whl", hash = "sha256:342a824b46ddbcdddd3abfbb332fa7fcaac5488bf18073e841236aadf4ad5c19"}, + {file = "ruff-0.9.1-py3-none-win_arm64.whl", hash = "sha256:1cd76c7f9c679e6e8f2af8f778367dca82b95009bc7b1a85a47f1521ae524fa7"}, + {file = "ruff-0.9.1.tar.gz", hash = "sha256:fd2b25ecaf907d6458fa842675382c8597b3c746a2dde6717fe3415425df0c17"}, ] [[package]] @@ -3129,4 +3129,4 @@ postgres = ["psycopg2"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<4" -content-hash = "595bf032972f47067537ef9023d31850b70af0491566645d122dba4398f5042a" +content-hash = "ff7d8bf4417cb8b7998e791fc9cb82083ddbd4834ac477a9f3458029001c0d96" diff --git a/pyproject.toml b/pyproject.toml index 22ecdcef0..ba4d7c337 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -125,7 +125,7 @@ urllib3 = "^2.3.0" [tool.poetry.dev-dependencies] factory_boy = "^3.3.1" -ruff = "^0.8" +ruff = "^0" django-stubs = { version ="5.1.*", extras = ["compatible-mypy"] } diff --git a/tests/base_test_mixins.py b/tests/base_test_mixins.py index 8658756b2..b03f22d81 100644 --- a/tests/base_test_mixins.py +++ b/tests/base_test_mixins.py @@ -1639,7 +1639,7 @@ def get_page_visits(cls, course_identifier=None, flow_session_id=None, page_ordinal=None, page_id=None, **kwargs): query_kwargs = {} - if kwargs.get("answer_visit", False): + if kwargs.get("answer_visit"): query_kwargs.update({"answer__isnull": False}) flow_params = cls.get_flow_params(course_identifier, flow_session_id) query_kwargs.update({"flow_session_id": flow_params["flow_session_id"]}) diff --git a/tests/test_content.py b/tests/test_content.py index 15f81818b..c29ff17b4 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -28,6 +28,7 @@ import stat import unittest from copy import deepcopy +from dataclasses import dataclass from zoneinfo import ZoneInfo import pytest @@ -334,9 +335,9 @@ def setUp(self): def test_file_uses_tab_in_indentation(self): fake_yaml_bytestream = b"\tabcd\n" + @dataclass class _Blob: - def __init__(self): - self.data = fake_yaml_bytestream + data: bytes = fake_yaml_bytestream with mock.patch("course.content.get_repo_blob") as mock_get_repo_blob: mock_get_repo_blob.return_value = _Blob() diff --git a/tests/test_grades/test_grades.py b/tests/test_grades/test_grades.py index cf28b453e..99d023df3 100644 --- a/tests/test_grades/test_grades.py +++ b/tests/test_grades/test_grades.py @@ -26,6 +26,7 @@ import datetime import io import unittest +from dataclasses import dataclass import pytest from django.test import Client, TestCase @@ -659,9 +660,9 @@ def test(self): fake_task_id = "abcdef123" +@dataclass class MockAsyncRes: - def __init__(self): - self.id = fake_task_id + id: str = fake_task_id class ViewGradesByOpportunityTest(GradesTestMixin, TestCase):