Skip to content

Commit

Permalink
Run pyupgrade --py310-plus in all files
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Oct 23, 2024
1 parent b097ed7 commit 5942d8e
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 119 deletions.
6 changes: 3 additions & 3 deletions src/oop_ext/_type_checker_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class _Result:
that output.
"""

output: Tuple[str, str, int]
output: tuple[str, str, int]

def assert_errors(self, messages: List[str]) -> None:
def assert_errors(self, messages: list[str]) -> None:
assert self.error_report == ""
lines = self.report_lines
assert len(lines) == len(
Expand Down Expand Up @@ -56,7 +56,7 @@ def exit_status(self) -> int:
return self.output[2]

@property
def report_lines(self) -> List[str]:
def report_lines(self) -> list[str]:
lines = [x.strip() for x in self.normal_report.split("\n") if x.strip()]
# Drop last line (summary).
return lines[:-1]
Expand Down
2 changes: 1 addition & 1 deletion src/oop_ext/foundation/_tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def MyWarn(*args, **kwargs):
was_development = is_frozen.SetIsDevelopment(True)
try:
# Emit messages when in development
warn_params: List[Tuple[Any, Any]] = []
warn_params: list[tuple[Any, Any]] = []

# ... deprecation with alternative
@Deprecated("OtherMethod")
Expand Down
4 changes: 2 additions & 2 deletions src/oop_ext/foundation/_tests/test_immutable.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def AssertIsSame(value):
AssertIsSame(1)
AssertIsSame("a") # native string
AssertIsSame(b"b") # native bytes
AssertIsSame(str("a")) # future's str compatibility type
AssertIsSame(bytes(b"b")) # future's byte compatibility type
AssertIsSame("a") # future's str compatibility type
AssertIsSame(b"b") # future's byte compatibility type

# Dealing with derived values
a = MyClass()
Expand Down
2 changes: 1 addition & 1 deletion src/oop_ext/foundation/_tests/test_weak_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def CustomAssertEqual(a, b):
if a == b:
pass
else:
assert False, "{} != {}".format(a, b)
assert False, f"{a} != {b}"


def SetupTestAttributes() -> Any:
Expand Down
16 changes: 8 additions & 8 deletions src/oop_ext/foundation/cached_method.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# mypy: disallow-untyped-defs
from typing import Callable
from typing import Dict
from typing import Generic
from typing import Hashable
from typing import Optional
from typing import Sequence
from typing import TypeVar
from typing import Union
from typing import cast

from abc import abstractmethod
from collections.abc import Callable
from collections.abc import Hashable
from collections.abc import Sequence

from .immutable import AsImmutable
from .odict import odict
Expand Down Expand Up @@ -96,7 +96,7 @@ class CachedMethod(AbstractCachedMethod, Generic[ResultType]):

def __init__(self, cached_method: Callable[..., ResultType]) -> None:
super().__init__(cached_method)
self._results: Dict[Hashable, ResultType] = {}
self._results: dict[Hashable, ResultType] = {}

def _HasResult(self, key: Hashable) -> bool:
return key in self._results
Expand Down Expand Up @@ -131,8 +131,8 @@ class LastResultCachedMethod(AbstractCachedMethod, Generic[ResultType]):

def __init__(self, cached_method: Callable[..., ResultType]) -> None:
super().__init__(cached_method)
self._key: Optional[object] = None
self._result: Optional[ResultType] = None
self._key: object | None = None
self._result: ResultType | None = None

def _HasResult(self, key: Hashable) -> bool:
return self._key == key
Expand All @@ -159,9 +159,9 @@ class AttributeBasedCachedMethod(CachedMethod, Generic[ResultType]):
def __init__(
self,
cached_method: Callable[..., ResultType],
attr_name_list: Union[str, Sequence[str]],
attr_name_list: str | Sequence[str],
cache_size: int = 1,
results: Optional[odict] = None,
results: odict | None = None,
):
"""
:type cached_method: bound method to be cached
Expand Down
18 changes: 9 additions & 9 deletions src/oop_ext/foundation/callback/_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def on_x(x: int) -> None:
"""
import types
from typing import Any
from typing import Callable
from typing import Hashable
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Union
from typing import cast
Expand All @@ -64,6 +61,9 @@ def on_x(x: int) -> None:
import inspect
import logging
import weakref
from collections.abc import Callable
from collections.abc import Hashable
from collections.abc import Sequence

from oop_ext.foundation.compat import GetClassForUnboundMethod
from oop_ext.foundation.is_frozen import IsDevelopment
Expand Down Expand Up @@ -211,8 +211,8 @@ def _GetKey(
return id(func)

def _GetInfo(
self, func: Union[None, WeakMethodProxy, Method, Callable]
) -> Tuple[Any, Any, Any]:
self, func: None | WeakMethodProxy | Method | Callable
) -> tuple[Any, Any, Any]:
"""
:rtype: tuple(func_obj, func_func, func_class)
:returns:
Expand All @@ -228,12 +228,12 @@ def _GetInfo(

if _IsCallableObject(func):
if self.DEBUG_NEW_WEAKREFS:
obj_str = "{}".format(func.__class__)
obj_str = f"{func.__class__}"
print("Changed behavior for: %s" % obj_str)

def on_die(r: Any) -> None:
# I.e.: the hint here is that a reference may die before expected
print("Reference died: {}".format(obj_str))
print(f"Reference died: {obj_str}")

return (weakref.ref(func, on_die), None, None)
return (weakref.ref(func), None, None)
Expand Down Expand Up @@ -327,7 +327,7 @@ def _FilterToCall(self, to_call: Any, args: Any, kwargs: Any) -> Any:
"""
return to_call

_EXTRA_ARGS_CONSTANT: Tuple[object, ...] = tuple()
_EXTRA_ARGS_CONSTANT: tuple[object, ...] = tuple()

def Register(
self,
Expand Down Expand Up @@ -389,7 +389,7 @@ def Contains(
if info_and_extra_args is None:
return False

real_func: Optional[Callable] = func
real_func: Callable | None = func

if isinstance(real_func, WeakMethodProxy):
real_func = real_func.GetWrappedFunction()
Expand Down
7 changes: 4 additions & 3 deletions src/oop_ext/foundation/callback/_callbacks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# mypy: disallow-untyped-defs
# mypy: disallow-any-decorated
from typing import Any
from typing import Callable
from typing import List
from typing import Tuple
from typing import TypeVar
from typing import cast

from collections.abc import Callable

from ._callback import Callback
from ._callback import _UnregisterContext
from ._shortcuts import After
Expand Down Expand Up @@ -43,8 +44,8 @@ class Callbacks:
"""

def __init__(self) -> None:
self._function_callbacks: List[Tuple[Callable, Callable]] = []
self._contexts: List[_UnregisterContext] = []
self._function_callbacks: list[tuple[Callable, Callable]] = []
self._contexts: list[_UnregisterContext] = []

def Before(
self, sender: T, callback: Callable, *, sender_as_parameter: bool = False
Expand Down
5 changes: 3 additions & 2 deletions src/oop_ext/foundation/callback/_priority_callback.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# mypy: disallow-untyped-defs
# mypy: disallow-any-decorated
from typing import Any
from typing import Callable
from typing import Tuple

from collections.abc import Callable

from oop_ext.foundation.decorators import Override
from oop_ext.foundation.odict import odict

Expand Down Expand Up @@ -34,7 +35,7 @@ def _GetInfo( # type:ignore[misc, override]
def Register( # type:ignore[misc, override]
self,
func: Callable,
extra_args: Tuple[object, ...] = Callback._EXTRA_ARGS_CONSTANT,
extra_args: tuple[object, ...] = Callback._EXTRA_ARGS_CONSTANT,
priority: int = 5,
) -> None:
"""
Expand Down
20 changes: 9 additions & 11 deletions src/oop_ext/foundation/callback/_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# mypy: disallow-untyped-defs
# mypy: disallow-any-decorated
from typing import Any
from typing import Callable
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Union

import weakref
from collections.abc import Callable
from collections.abc import Sequence

from oop_ext.foundation.types_ import Method
from oop_ext.foundation.weak_ref import WeakMethodRef
Expand Down Expand Up @@ -98,16 +98,16 @@ class _MethodWrapper(
__slots__ = ["_before", "_after", "_method", "_name", "OriginalMethod"]

def __init__(self, method: Union[Method, "_MethodWrapper", Callable]):
self._before: Optional[Callback] = None
self._after: Optional[Callback] = None
self._before: Callback | None = None
self._after: Callback | None = None
self._method = WeakMethodRef(method)
self._name = method.__name__

# Maintaining the OriginalMethod() interface that clients expect.
self.OriginalMethod = self._method

def __repr__(self) -> str:
return "_MethodWrapper({}): {}".format(id(self), self._name)
return f"_MethodWrapper({id(self)}): {self._name}"

def __call__(self, *args: object, **kwargs: object) -> Any:
if self._before is not None:
Expand All @@ -128,7 +128,7 @@ def __call__(self, *args: object, **kwargs: object) -> Any:
return result

def AppendBefore(
self, callback: Callable, extra_args: Optional[Sequence[object]] = None
self, callback: Callable, extra_args: Sequence[object] | None = None
) -> None:
"""
Append the given callbacks in the list of callback to be executed BEFORE the method.
Expand All @@ -141,7 +141,7 @@ def AppendBefore(
self._before.Register(callback, extra_args)

def AppendAfter(
self, callback: Callable, extra_args: Optional[Sequence[object]] = None
self, callback: Callable, extra_args: Sequence[object] | None = None
) -> None:
"""
Append the given callbacks in the list of callback to be executed AFTER the method.
Expand All @@ -167,9 +167,7 @@ def Remove(self, callback: Callable) -> bool:
return False


def _GetWrapped(
method: Union[Method, _MethodWrapper, Callable]
) -> Optional[_MethodWrapper]:
def _GetWrapped(method: Method | _MethodWrapper | Callable) -> _MethodWrapper | None:
"""
Returns true if the given method is already wrapped.
"""
Expand All @@ -181,7 +179,7 @@ def _GetWrapped(
return None


def WrapForCallback(method: Union[Method, _MethodWrapper, Callable]) -> _MethodWrapper:
def WrapForCallback(method: Method | _MethodWrapper | Callable) -> _MethodWrapper:
"""Generates a wrapper for the given method, or returns the method itself
if it is already a wrapper.
"""
Expand Down
10 changes: 5 additions & 5 deletions src/oop_ext/foundation/callback/_tests/test_callback.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any
from typing import Generator
from typing import List

import pytest
import weakref
from collections.abc import Generator
from functools import partial

from oop_ext.foundation.callback import After
Expand Down Expand Up @@ -282,7 +282,7 @@ def test_extra_args(self) -> None:
"""
Tests the extra-args parameter in Register method.
"""
self.zulu_calls: List[Any] = []
self.zulu_calls: list[Any] = []

def zulu_one(*args):
self.zulu_calls.append(args)
Expand Down Expand Up @@ -910,7 +910,7 @@ def testListMethodAsCallback(self, mocker) -> None:
souring20.core.model.multiple_simulation_runs._tests.test_multiple_simulation_runs.testNormalExecution
which failed with "TypeError: cannot create weak reference to 'list' object"
"""
vals: List[str] = []
vals: list[str] = []

class Stub:
def call(self, *args, **kwargs):
Expand Down Expand Up @@ -1082,7 +1082,7 @@ def Method(a):
assert called == ["lambda", "partial"]

def testCallbackInsideCallback(self) -> None:
class A(object):
class A:
c = Callback()

def __init__(self, **ka):
Expand All @@ -1098,7 +1098,7 @@ def SetValue(self, value):
def _UpdateBValue(self, new_value):
self.other_value = new_value / 0 # division by zero

class B(object):
class B:
c = Callback()

def __init__(self, **ka):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def testAllCallbacksSmokeTest(
Callback overloads.
"""

def gen_signature_and_args(count: int) -> Tuple[str, str, str]:
def gen_signature_and_args(count: int) -> tuple[str, str, str]:
# Generates "v1: int, v2: int" etc
signature = ", ".join(f"v{i}: int" for i in range(count))
# Generates "10, 20" etc
Expand Down
5 changes: 3 additions & 2 deletions src/oop_ext/foundation/callback/_typed_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
implement the generic variants into ``Callback`` itself.
"""
from typing import TYPE_CHECKING
from typing import Callable
from typing import Generic
from typing import Sequence
from typing import TypeVar

from collections.abc import Callable
from collections.abc import Sequence

from ._callback import Callback
from ._callback import _UnregisterContext

Expand Down
7 changes: 4 additions & 3 deletions src/oop_ext/foundation/callback/single_call_callback.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# mypy: disallow-untyped-defs
# mypy: disallow-any-decorated
from typing import Callable
from typing import Dict
from typing import Tuple

from collections.abc import Callable

from ._callback import Callback


Expand All @@ -30,8 +31,8 @@ def __init__(self, callback_parameter: object) -> None:
self._done_callbacks = Callback()
self._done = False

self._args: Tuple[object, ...] = ()
self._kwargs: Dict[str, object] = {}
self._args: tuple[object, ...] = ()
self._kwargs: dict[str, object] = {}

def __call__(self, *args: object, **kwargs: object) -> None:
if self._done:
Expand Down
Loading

0 comments on commit 5942d8e

Please sign in to comment.