pyapp-kit / psygnal

Python observer pattern (callback/event system). Modeled after Qt Signals & Slots (but independent of Qt)
https://psygnal.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
84 stars 13 forks source link

feat: make Signal and SignalInstance Generic, support static type validation of signal connections #304

Open tlambert03 opened 6 months ago

tlambert03 commented 6 months ago

supports the ability to perform static type checking on signal connection signatures

class Thing:
    sig = Signal(int, str)

t = Thing()

@t.sig.connect
def cb1() -> None: ...  # OK
@t.sig.connect
def cb2(a0: int) -> None: ...  # OK
@t.sig.connect
def cb3(a0: int, a1: str) -> None: ...  # OK

@t.sig.connect
def cb5(a0: int, a1: str, a2: float) -> None: ...  # ERR: too many params
@t.sig.connect
def cb6(a0: str) -> None: ...  # ERR: wrong type for a0
codecov[bot] commented 6 months ago

Codecov Report

Attention: Patch coverage is 96.77419% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 99.95%. Comparing base (c4c83b2) to head (66a86ce).

Files Patch % Lines
src/psygnal/_signal.py 96.15% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #304 +/- ## =========================================== - Coverage 100.00% 99.95% -0.05% =========================================== Files 22 22 Lines 2071 2084 +13 =========================================== + Hits 2071 2083 +12 - Misses 0 1 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codspeed-hq[bot] commented 6 months ago

CodSpeed Performance Report

Merging #304 will degrade performances by 66.04%

Comparing tlambert03:generic-signal (66a86ce) with main (c4c83b2)

Summary

⚡ 1 improvements ❌ 5 regressions ✅ 60 untouched benchmarks

:warning: Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main tlambert03:generic-signal Change
test_create_signal 144.7 µs 51.3 µs ×2.8
test_create_signal_instance 51.9 µs 152.8 µs -66.04%
test_dataclass_group_create[attrs] 2.4 ms 3.1 ms -22.91%
test_dataclass_group_create[dataclass] 2 ms 2.7 ms -24.91%
test_dataclass_group_create[msgspec] 2.4 ms 3.1 ms -22.15%
test_dataclass_group_create[pydantic] 2.4 ms 3.1 ms -22.39%