Skip to content
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.12.0
rev: 26.1.0
hooks:
- id: black
args: [--safe, --quiet]
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""The setup script."""

import io
from setuptools import find_packages
from setuptools import setup
Expand Down
18 changes: 9 additions & 9 deletions src/oop_ext/foundation/_tests/test_cached_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,27 @@ def Foo(self, par):
return "%s %d" % (par, self.id)

alpha = TestObject()
alpha.Foo = AttributeBasedCachedMethod( # type:ignore[assignment]
alpha.Foo = AttributeBasedCachedMethod( # type: ignore[assignment]
alpha.Foo, "id", cache_size=3
)
alpha.Foo("test1") # type:ignore[misc]
alpha.Foo("test1") # type:ignore[misc]
alpha.Foo("test1") # type: ignore[misc]
alpha.Foo("test1") # type: ignore[misc]

assert alpha.n_calls == 1

alpha.Foo("test2") # type:ignore[misc]
alpha.Foo("test2") # type: ignore[misc]
assert alpha.n_calls == 2
assert len(alpha.Foo._results) == 2 # type:ignore[attr-defined]
assert len(alpha.Foo._results) == 2 # type: ignore[attr-defined]

alpha.id = 3
alpha.Foo("test2") # type:ignore[misc]
alpha.Foo("test2") # type: ignore[misc]
assert alpha.n_calls == 3

assert len(alpha.Foo._results) == 3 # type:ignore[attr-defined]
assert len(alpha.Foo._results) == 3 # type: ignore[attr-defined]

alpha.Foo("test3") # type:ignore[misc]
alpha.Foo("test3") # type: ignore[misc]
assert alpha.n_calls == 4
assert len(alpha.Foo._results) == 3 # type:ignore[attr-defined]
assert len(alpha.Foo._results) == 3 # type: ignore[attr-defined]


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions src/oop_ext/foundation/_tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def testNull() -> None:
n.method1().attr1

n.attr1 = "value"
n.attr1.attr2 = "value" # type:ignore[attr-defined]
n.attr1.attr2 = "value" # type: ignore[attr-defined]

del n.attr1
del n.attr1.attr2.attr3 # type:ignore[attr-defined]
del n.attr1.attr2.attr3 # type: ignore[attr-defined]

# Iteration
for _ in n:
Expand Down
4 changes: 2 additions & 2 deletions src/oop_ext/foundation/_tests/test_weak_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,13 @@ def testWeakList() -> None:
def m1() -> None:
"Never called"

weak_list.append(m1) # type:ignore[arg-type]
weak_list.append(m1) # type: ignore[arg-type]
assert 1 == len(weak_list[:])
del m1
assert 0 == len(weak_list[:])

s = _Stub()
weak_list.append(s.Method) # type:ignore[arg-type]
weak_list.append(s.Method) # type: ignore[arg-type]
assert 1 == len(weak_list[:])
ref_s = weakref.ref(s)
del s
Expand Down
2 changes: 1 addition & 1 deletion src/oop_ext/foundation/cached_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def DoClear(self) -> None:
def _GetCacheResult(self, key: Hashable, result: ResultType) -> ResultType:
# This could return None (_result is Optional), but not doing an assert
# here to avoid breaking code.
return self._result # type:ignore[return-value]
return self._result # type: ignore[return-value]


class AttributeBasedCachedMethod(CachedMethod, Generic[ResultType]):
Expand Down
25 changes: 13 additions & 12 deletions src/oop_ext/foundation/callback/_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def on_x(x: int) -> None:
is registered with :meth:`Contains <Callback.Contains>`, and unregister all connected functions
with :meth:`UnregisterAll <Callback.UnregisterAll>`.
"""

import types
from typing import Any
from typing import Optional
Expand Down Expand Up @@ -192,16 +193,16 @@ def _GetKey(
func = func.OriginalMethod()

try:
if func.__self__ is not None: # type:ignore[union-attr]
if func.__self__ is not None: # type: ignore[union-attr]
# bound method
return (
id(func.__self__), # type:ignore[union-attr]
id(func.__func__), # type:ignore[union-attr]
id(func.__self__.__class__), # type:ignore[union-attr]
id(func.__self__), # type: ignore[union-attr]
id(func.__func__), # type: ignore[union-attr]
id(func.__self__.__class__), # type: ignore[union-attr]
)
else:
return (
id(func.__func__), # type:ignore[union-attr]
id(func.__func__), # type: ignore[union-attr]
id(GetClassForUnboundMethod(func)),
)

Expand Down Expand Up @@ -240,20 +241,20 @@ def on_die(r: Any) -> None:

try:
if (
func.__self__ is not None # type:ignore[union-attr]
and func.__func__ is not None # type:ignore[union-attr]
func.__self__ is not None # type: ignore[union-attr]
and func.__func__ is not None # type: ignore[union-attr]
):
# bound method
return (
weakref.ref(func.__self__), # type:ignore[union-attr]
func.__func__, # type:ignore[union-attr]
func.__self__.__class__, # type:ignore[union-attr]
weakref.ref(func.__self__), # type: ignore[union-attr]
func.__func__, # type: ignore[union-attr]
func.__self__.__class__, # type: ignore[union-attr]
)
else:
# unbound method
return (
None,
func.__func__, # type:ignore[union-attr]
func.__func__, # type: ignore[union-attr]
GetClassForUnboundMethod(func),
)
except AttributeError:
Expand Down Expand Up @@ -424,7 +425,7 @@ def Contains(
if func_func == real_func:
return True
try:
f = real_func.__func__ # type:ignore[union-attr]
f = real_func.__func__ # type: ignore[union-attr]
except AttributeError:
return False
else:
Expand Down
4 changes: 2 additions & 2 deletions src/oop_ext/foundation/callback/_priority_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PriorityCallback(Callback):
INFO_POS_PRIORITY = 3

@Override(Callback._GetInfo)
def _GetInfo( # type:ignore[misc, override]
def _GetInfo( # type: ignore[misc, override]
self, func: Callable, priority: int
) -> Any:
"""
Expand All @@ -33,7 +33,7 @@ def _GetInfo( # type:ignore[misc, override]
return info + (priority,)

@Override(Callback.Register)
def Register( # type:ignore[misc, override]
def Register( # type: ignore[misc, override]
self,
func: Callable,
extra_args: tuple[object, ...] = Callback._EXTRA_ARGS_CONSTANT,
Expand Down
8 changes: 4 additions & 4 deletions src/oop_ext/foundation/callback/_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _GetWrapped(method: Method | _MethodWrapper | Callable) -> _MethodWrapper |
if isinstance(method, _MethodWrapper):
return method
try:
return method._wrapped_instance # type:ignore[attr-defined, union-attr]
return method._wrapped_instance # type: ignore[attr-defined, union-attr]
except AttributeError:
return None

Expand All @@ -194,7 +194,7 @@ def WrapForCallback(method: Method | _MethodWrapper | Callable) -> _MethodWrappe
# Note that the other way around does not work at all (i.e.: if a callback is first added
# to the instance, there's no way we'll find about that when adding it to the class
# anyways).
if method.__self__ is None: # type:ignore[union-attr]
if method.__self__ is None: # type: ignore[union-attr]
if wrapped._method._obj is None:
return wrapped

Expand All @@ -208,10 +208,10 @@ def call(*args: object, **kwargs: object) -> Any:
return wrapper(*args, **kwargs)

call.__name__ = method.__name__
call._wrapped_instance = wrapper # type:ignore[attr-defined]
call._wrapped_instance = wrapper # type: ignore[attr-defined]

setattr(GetClassForUnboundMethod(method), method.__name__, call)
else:
# override the instance method
setattr(method.__self__, method.__name__, wrapper) # type:ignore[union-attr]
setattr(method.__self__, method.__name__, wrapper) # type: ignore[union-attr]
return wrapper
4 changes: 2 additions & 2 deletions src/oop_ext/foundation/callback/_tests/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def restore_test_classes() -> Generator[None, None, None]:
original_c_foo = C.foo

yield
Stub.call = original_stub_call # type:ignore[assignment]
C.foo = original_c_foo # type:ignore[assignment]
Stub.call = original_stub_call # type: ignore[assignment]
C.foo = original_c_foo # type: ignore[assignment]


class Test:
Expand Down
54 changes: 18 additions & 36 deletions src/oop_ext/foundation/callback/_tests/test_typed_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@


def testCallback0(type_checker: TypeCheckerFixture) -> None:
type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import Callback0

c = Callback0()
c(10)

def fail(x): pass
c.Register(fail)
"""
)
""")
result = type_checker.run()
result.assert_errors(
[
Expand All @@ -26,24 +24,21 @@ def fail(x): pass
]
)

type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import Callback0

c = Callback0()
c()

def ok(): pass
c.Register(ok)
"""
)
""")
result = type_checker.run()
result.assert_ok()


def testCallback1(type_checker: TypeCheckerFixture) -> None:
type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import Callback1

c = Callback1[int]()
Expand All @@ -55,8 +50,7 @@ def fail(): pass

def fail2(x: str): pass
c.Register(fail2)
"""
)
""")
result = type_checker.run()
result.assert_errors(
[
Expand All @@ -71,33 +65,29 @@ def fail2(x: str): pass
]
)

type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import Callback1

c = Callback1[int]()
c(10)

def ok(x: int): pass
c.Register(ok)
"""
)
""")
result = type_checker.run()
result.assert_ok()


def testPriorityCallback0(type_checker: TypeCheckerFixture) -> None:
type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import PriorityCallback0

c = PriorityCallback0()
c(10)

def fail(x): pass
c.Register(fail, priority=2)
"""
)
""")
result = type_checker.run()
result.assert_errors(
[
Expand All @@ -106,24 +96,21 @@ def fail(x): pass
]
)

type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import PriorityCallback0

c = PriorityCallback0()
c()

def ok(): pass
c.Register(ok, priority=2)
"""
)
""")
result = type_checker.run()
result.assert_ok()


def testPriorityCallback1(type_checker: TypeCheckerFixture) -> None:
type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import PriorityCallback1

c = PriorityCallback1[int]()
Expand All @@ -135,8 +122,7 @@ def fail(): pass

def fail2(x: str): pass
c.Register(fail2, priority=2)
"""
)
""")
result = type_checker.run()
result.assert_errors(
[
Expand All @@ -151,17 +137,15 @@ def fail2(x: str): pass
]
)

type_checker.make_file(
"""
type_checker.make_file("""
from oop_ext.foundation.callback import PriorityCallback1

c = PriorityCallback1[int]()
c(10)

def ok(x: int): pass
c.Register(ok, priority=2)
"""
)
""")
result = type_checker.run()
result.assert_ok()

Expand Down Expand Up @@ -193,8 +177,7 @@ def gen_signature_and_args(count: int) -> tuple[str, str, str]:
sig_too_many, args_too_many, types_too_many = gen_signature_and_args(args_count + 1)
sig_ok, args_ok, types_ok = gen_signature_and_args(args_count)

type_checker.make_file(
f"""
type_checker.make_file(f"""
from oop_ext.foundation.callback import Callback{args_count}

c = Callback{args_count}[{types_ok}]()
Expand All @@ -210,8 +193,7 @@ def too_many_func({sig_too_many}) -> None: ...
def ok_func({sig_ok}) -> None: ...
c.Register(ok_func)
c({args_ok})
"""
)
""")
result = type_checker.run()
result.assert_errors(
[
Expand Down
Loading
Loading