From 00adb83fd0ab6a0cf67442d60c73b5db4ede31be Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Tue, 1 Oct 2024 16:41:40 +0200 Subject: [PATCH] Pull in changes from 3.10 bump and ruff pyupgrade --- examples/huggingface/benchmark.py | 2 +- examples/mnist/mnist.py | 2 +- examples/prefect/src/runner.py | 2 -- examples/prefect/src/training.py | 2 -- src/nnbench/context.py | 8 +++----- src/nnbench/core.py | 5 ++--- src/nnbench/reporter/__init__.py | 2 -- src/nnbench/reporter/base.py | 5 ++--- src/nnbench/reporter/duckdb_sql.py | 4 +--- src/nnbench/reporter/file.py | 5 ++--- src/nnbench/runner.py | 7 +++---- src/nnbench/transforms/base.py | 2 +- src/nnbench/transforms/params.py | 3 ++- src/nnbench/types/benchmark.py | 17 +++++++++-------- src/nnbench/types/interface.py | 9 +++++---- src/nnbench/types/memo.py | 5 ++--- src/nnbench/util.py | 2 -- tests/test_memos.py | 2 +- 18 files changed, 35 insertions(+), 49 deletions(-) diff --git a/examples/huggingface/benchmark.py b/examples/huggingface/benchmark.py index 050396b..5765124 100644 --- a/examples/huggingface/benchmark.py +++ b/examples/huggingface/benchmark.py @@ -1,8 +1,8 @@ import os import tempfile import time +from collections.abc import Sequence from functools import cache, lru_cache, partial -from typing import Sequence import torch from datasets import Dataset, load_dataset diff --git a/examples/mnist/mnist.py b/examples/mnist/mnist.py index 0c77789..1643e1e 100644 --- a/examples/mnist/mnist.py +++ b/examples/mnist/mnist.py @@ -7,9 +7,9 @@ """ import random +from collections.abc import Mapping from dataclasses import dataclass from pathlib import Path -from typing import Mapping import flax.linen as nn import fsspec diff --git a/examples/prefect/src/runner.py b/examples/prefect/src/runner.py index 8e11294..909d3cb 100644 --- a/examples/prefect/src/runner.py +++ b/examples/prefect/src/runner.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import asyncio import os diff --git a/examples/prefect/src/training.py b/examples/prefect/src/training.py index beb92d0..7a34f83 100644 --- a/examples/prefect/src/training.py +++ b/examples/prefect/src/training.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import numpy as np from prefect import flow, task from sklearn import base diff --git a/src/nnbench/context.py b/src/nnbench/context.py index 8a563b8..c02fa91 100644 --- a/src/nnbench/context.py +++ b/src/nnbench/context.py @@ -1,11 +1,10 @@ """Utilities for collecting context key-value pairs as metadata in benchmark runs.""" -from __future__ import annotations - import itertools import platform import sys -from typing import Any, Callable, Iterator, Literal +from collections.abc import Callable, Iterator +from typing import Any, Literal ContextProvider = Callable[[], dict[str, Any]] """A function providing a dictionary of context values.""" @@ -89,8 +88,7 @@ def git_subprocess(args: list[str]) -> subprocess.CompletedProcess: return subprocess.run( # nosec: B603 [git, *args], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, encoding="utf-8", ) diff --git a/src/nnbench/core.py b/src/nnbench/core.py index 56b15da..ce09cc9 100644 --- a/src/nnbench/core.py +++ b/src/nnbench/core.py @@ -1,13 +1,12 @@ """Data model, registration, and parametrization facilities for defining benchmarks.""" -from __future__ import annotations - import inspect import itertools import sys import types import warnings -from typing import Any, Callable, Iterable, Union, get_args, get_origin, overload +from collections.abc import Callable, Iterable +from typing import Any, Union, get_args, get_origin, overload from nnbench.types import Benchmark from nnbench.types.benchmark import NoOp diff --git a/src/nnbench/reporter/__init__.py b/src/nnbench/reporter/__init__.py index e22f9f7..20eac29 100644 --- a/src/nnbench/reporter/__init__.py +++ b/src/nnbench/reporter/__init__.py @@ -2,8 +2,6 @@ A lightweight interface for refining, displaying, and streaming benchmark results to various sinks. """ -from __future__ import annotations - from .base import BenchmarkReporter from .duckdb_sql import DuckDBReporter from .file import FileReporter diff --git a/src/nnbench/reporter/base.py b/src/nnbench/reporter/base.py index d2204e7..fb68d92 100644 --- a/src/nnbench/reporter/base.py +++ b/src/nnbench/reporter/base.py @@ -1,7 +1,6 @@ -from __future__ import annotations - import re -from typing import Any, Callable +from collections.abc import Callable +from typing import Any from tabulate import tabulate diff --git a/src/nnbench/reporter/duckdb_sql.py b/src/nnbench/reporter/duckdb_sql.py index 70180f0..12a169a 100644 --- a/src/nnbench/reporter/duckdb_sql.py +++ b/src/nnbench/reporter/duckdb_sql.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import os import shutil import sys @@ -130,6 +128,6 @@ def read_sql( return BenchmarkRecord(context=context, benchmarks=benchmarks) - def raw_sql(self, query: str) -> duckdb.DuckDBPyRelation: + def raw_sql(self, query: str) -> "duckdb.DuckDBPyRelation": rel = self.conn.sql(query=query) return rel diff --git a/src/nnbench/reporter/file.py b/src/nnbench/reporter/file.py index 55b5078..835289c 100644 --- a/src/nnbench/reporter/file.py +++ b/src/nnbench/reporter/file.py @@ -1,10 +1,9 @@ -from __future__ import annotations - import os import threading +from collections.abc import Callable, Sequence from dataclasses import dataclass, field from pathlib import Path -from typing import IO, Any, Callable, Literal, Sequence, cast +from typing import IO, Any, Literal, cast from nnbench.reporter.base import BenchmarkReporter from nnbench.types import BenchmarkRecord diff --git a/src/nnbench/runner.py b/src/nnbench/runner.py index 517e8cc..f23993e 100644 --- a/src/nnbench/runner.py +++ b/src/nnbench/runner.py @@ -1,7 +1,5 @@ """The abstract benchmark runner interface, which can be overridden for custom benchmark workloads.""" -from __future__ import annotations - import collections import contextlib import inspect @@ -10,10 +8,11 @@ import sys import time import warnings +from collections.abc import Callable, Generator, Sequence from dataclasses import asdict from datetime import datetime from pathlib import Path -from typing import Any, Callable, Generator, Sequence, get_origin +from typing import Any, get_origin from nnbench.context import Context, ContextProvider from nnbench.types import Benchmark, BenchmarkRecord, Parameters, State @@ -24,7 +23,7 @@ def iscontainer(s: Any) -> bool: - return isinstance(s, (tuple, list)) + return isinstance(s, tuple | list) def isdunder(s: str) -> bool: diff --git a/src/nnbench/transforms/base.py b/src/nnbench/transforms/base.py index a4a3cd8..b21e8e6 100644 --- a/src/nnbench/transforms/base.py +++ b/src/nnbench/transforms/base.py @@ -1,7 +1,7 @@ """Metaclasses for defining transforms acting on benchmark records.""" from abc import ABC, abstractmethod -from typing import Sequence +from collections.abc import Sequence from nnbench.types import BenchmarkRecord diff --git a/src/nnbench/transforms/params.py b/src/nnbench/transforms/params.py index 6c8c70c..82d7b10 100644 --- a/src/nnbench/transforms/params.py +++ b/src/nnbench/transforms/params.py @@ -1,4 +1,5 @@ -from typing import Any, Sequence +from collections.abc import Sequence +from typing import Any from nnbench.transforms import ManyToManyTransform, OneToOneTransform from nnbench.types import BenchmarkRecord diff --git a/src/nnbench/types/benchmark.py b/src/nnbench/types/benchmark.py index a9bf97c..a18dc63 100644 --- a/src/nnbench/types/benchmark.py +++ b/src/nnbench/types/benchmark.py @@ -1,20 +1,17 @@ """Type interfaces for benchmarks and benchmark collections.""" -from __future__ import annotations - import copy +from collections.abc import Callable, Mapping from dataclasses import dataclass, field from types import MappingProxyType -from typing import Any, Callable, Literal, Mapping +from typing import Any, Literal + +from typing_extensions import Self from nnbench.context import Context from nnbench.types.interface import Interface -def NoOp(state: State, params: Mapping[str, Any] = MappingProxyType({})) -> None: - pass - - @dataclass(frozen=True) class State: name: str @@ -23,6 +20,10 @@ class State: family_index: int +def NoOp(state: State, params: Mapping[str, Any] = MappingProxyType({})) -> None: + pass + + @dataclass(frozen=True) class BenchmarkRecord: context: Context @@ -68,7 +69,7 @@ def compact( return result @classmethod - def expand(cls, bms: list[dict[str, Any]]) -> BenchmarkRecord: + def expand(cls, bms: list[dict[str, Any]]) -> Self: """ Expand a list of deserialized JSON-like objects into a benchmark record. This is equivalent to extracting the context given by the method it was diff --git a/src/nnbench/types/interface.py b/src/nnbench/types/interface.py index 8624546..da0a381 100644 --- a/src/nnbench/types/interface.py +++ b/src/nnbench/types/interface.py @@ -1,10 +1,11 @@ """Type interface for the function interface""" -from __future__ import annotations - import inspect +from collections.abc import Callable from dataclasses import dataclass -from typing import Any, Callable, TypeVar +from typing import Any, TypeVar + +from typing_extensions import Self T = TypeVar("T") Variable = tuple[str, type, Any] @@ -37,7 +38,7 @@ class Interface: returntype: type @classmethod - def from_callable(cls, fn: Callable, defaults: dict[str, Any]) -> Interface: + def from_callable(cls, fn: Callable, defaults: dict[str, Any]) -> Self: """ Creates an interface instance from the given callable. """ diff --git a/src/nnbench/types/memo.py b/src/nnbench/types/memo.py index fcfad49..716abda 100644 --- a/src/nnbench/types/memo.py +++ b/src/nnbench/types/memo.py @@ -1,11 +1,10 @@ -from __future__ import annotations - import collections import functools import inspect import logging import threading -from typing import Any, Callable, Generic, TypeVar, get_args, get_origin +from collections.abc import Callable +from typing import Any, Generic, TypeVar, get_args, get_origin T = TypeVar("T") Variable = tuple[str, type, Any] diff --git a/src/nnbench/util.py b/src/nnbench/util.py index 2a4d88c..d4a2a8d 100644 --- a/src/nnbench/util.py +++ b/src/nnbench/util.py @@ -1,7 +1,5 @@ """Various utilities related to benchmark collection, filtering, and more.""" -from __future__ import annotations - import importlib import importlib.util import os diff --git a/tests/test_memos.py b/tests/test_memos.py index 01bf398..2f73a44 100644 --- a/tests/test_memos.py +++ b/tests/test_memos.py @@ -1,4 +1,4 @@ -from typing import Generator +from collections.abc import Generator import pytest