Skip to content

API Reference

A reactive programming library for creating and managing reactive values and computations.

This module provides tools for building reactive systems, where changes in one value automatically propagate to dependent values.

Classes:

Name Description
Variable

Abstract base class for reactive values.

Signal

A container for mutable reactive values.

Computed

A container for computed reactive values (from functions).

Functions:

Name Description
unref

Dereference a potentially reactive value.

computed

Decorator to create a reactive value from a function.

reactive_method

Decorator to create a reactive method.

as_signal

Convert a value to a Signal if it's not already a reactive value.

has_value

Type guard to check if an object has a value of a specific type.

Attributes:

Name Type Description
ReactiveValue TypeAlias

Union of Computed and Signal types.

HasValue TypeAlias

Union of basic types and reactive types.

NestedValue TypeAlias

Recursive type for arbitrarily nested reactive values.

HasValue: TypeAlias = Union[T, Computed[T], Signal[T]] module-attribute

This object would return a value of type T when calling unref(obj).

This type alias represents any value that can be dereferenced, including plain values and reactive values.

See Also
  • Computed: The class for computed reactive values.
  • Signal: The class for mutable reactive values.
  • unref: Function to dereference values.

NestedValue: TypeAlias = Union[T, '_HasValue[NestedValue[T]]'] module-attribute

Insane recursive type hint to try to encode an arbitrarily nested reactive values.

E.g., float | Signal[float] | Signal[Signal[float]] | Signal[Signal[Signal[float]]].

ReactiveValue: TypeAlias = Union[Computed[T], Signal[T]] module-attribute

A reactive object that would return a value of type T when calling unref(obj).

This type alias represents any reactive value, either a Computed or a Signal.

See Also
  • Computed: The class for computed reactive values.
  • Signal: The class for mutable reactive values.
  • unref: Function to dereference values.

Computed

Bases: Variable[T, T]

A reactive value defined by a function.

Parameters:

Name Type Description Default
f Callable[[], T]

The function that computes the value.

required
dependencies Any

Dependencies to observe.

None

Attributes:

Name Type Description
f Callable[[], T]

The function that computes the value.

_value T

The current computed value.

Source code in src/signified/__init__.py
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
class Computed(Variable[T, T]):
    """A reactive value defined by a function.

    Args:
        f: The function that computes the value.
        dependencies: Dependencies to observe.

    Attributes:
        f (Callable[[], T]): The function that computes the value.
        _value (T): The current computed value.
    """

    def __init__(self, f: Callable[[], T], dependencies: Any = None) -> None:
        super().__init__()
        self.f = f
        self.observe(dependencies)
        self._value = unref(self.f())
        self.notify()

    def update(self) -> None:
        """Update the value by re-evaluating the function."""
        new_value = self.f()
        change = new_value != self._value
        if isinstance(change, np.ndarray):
            change = change.any()
        elif callable(self._value):
            change = True

        if change:
            self._value: T = new_value
            self.notify()

    @property
    def value(self) -> T:
        """Get the current value.

        Returns:
            The current value.
        """
        return unref(self._value)

value: T property

Get the current value.

Returns:

Type Description
T

The current value.

update()

Update the value by re-evaluating the function.

Source code in src/signified/__init__.py
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
def update(self) -> None:
    """Update the value by re-evaluating the function."""
    new_value = self.f()
    change = new_value != self._value
    if isinstance(change, np.ndarray):
        change = change.any()
    elif callable(self._value):
        change = True

    if change:
        self._value: T = new_value
        self.notify()

ReactiveMixIn

Bases: Generic[T]

Methods for easily creating reactive values.

Source code in src/signified/__init__.py
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
class ReactiveMixIn(Generic[T]):
    """Methods for easily creating reactive values."""

    @property
    def value(self) -> T:
        """The current value of the reactive object."""
        ...

    def notify(self) -> None:
        """Notify all observers by calling their ``update`` method."""
        ...

    @overload
    def __getattr__(self, name: Literal["value", "_value"]) -> T: ...  # type: ignore

    @overload
    def __getattr__(self, name: str) -> Computed[Any]: ...

    def __getattr__(self, name: str) -> Union[T, Computed[Any]]:
        """Create a reactive value for retrieving an attribute from ``self.value``.

        Args:
            name: The name of the attribute to access.

        Returns:
            A reactive value for the attribute access.

        Raises:
            AttributeError: If the attribute doesn't exist.

        Note:
            Type inference is poor whenever `__getattr__` is used.

        Example:
            ```py
            >>> class Person:
            ...     def __init__(self, name):
            ...         self.name = name
            >>> s = Signal(Person("Alice"))
            >>> result = s.name
            >>> result.value
            'Alice'
            >>> s.value = Person("Bob")
            >>> result.value
            'Bob'

            ```
        """
        if name in {"value", "_value"}:
            return super().__getattribute__(name)

        if hasattr(self.value, name):
            return computed(getattr)(self, name)
        else:
            return super().__getattribute__(name)

    @overload
    def __call__(self: "ReactiveMixIn[Callable[..., R]]", *args: Any, **kwargs: Any) -> Computed[R]: ...

    @overload
    def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
    def __call__(self, *args: Any, **kwargs: Any) -> Any:
        """Create a reactive value for calling `self.value(*args, **kwargs)`.

        Args:
            *args: Positional arguments to pass to the callable value.
            **kwargs: Keyword arguments to pass to the callable value.

        Returns:
            A reactive value for the function call.

        Raises:
            ValueError: If the value is not callable.

        Example:
            ```py
            >>> class Person:
            ...     def __init__(self, name):
            ...         self.name = name
            ...     def greet(self):
            ...         return f"Hi, I'm {self.name}!"
            >>> s = Signal(Person("Alice"))
            >>> result = s.greet()
            >>> result.value
            "Hi, I'm Alice!"
            >>> s.name = "Bob"
            >>> result.value
            "Hi, I'm Bob!"

            ```
        """
        if not callable(self.value):
            raise ValueError("Value is not callable.")

        def f(*args: Any, **kwargs: Any):
            _f = getattr(self, "value")
            return _f(*args, **kwargs)

        return computed(f)(*args, **kwargs).observe([self, self.value])

    def __abs__(self) -> Computed[T]:
        """Return a reactive value for the absolute value of `self`.

        Returns:
            A reactive value for `abs(self.value)`.

        Example:
            ```py
            >>> s = Signal(-5)
            >>> result = abs(s)
            >>> result.value
            5
            >>> s.value = -10
            >>> result.value
            10

            ```
        """
        return computed(abs)(self)

    def bool(self) -> Computed[bool]:
        """Return a reactive value for the boolean value of `self`.

        Note:
            `__bool__` cannot be implemented to return a non-`bool`, so it is provided as a method.

        Returns:
            A reactive value for `bool(self.value)`.

        Example:
            ```py
            >>> s = Signal(1)
            >>> result = s.bool()
            >>> result.value
            True
            >>> s.value = 0
            >>> result.value
            False

            ```
        """
        return computed(bool)(self)

    def __str__(self) -> str:
        """Return a string of the current value.

        Note:
            This is not reactive.

        Returns:
            A string representation of `self.value`.
        """
        return str(self.value)

    @overload
    def __round__(self) -> Computed[int]: ...
    @overload
    def __round__(self, ndigits: None) -> Computed[int]: ...
    @overload
    def __round__(self, ndigits: int) -> Computed[float]: ...

    def __round__(self, ndigits: int | None = None) -> Computed[int] | Computed[float]:
        """Return a reactive value for the rounded value of self.

        Args:
            ndigits: Number of decimal places to round to.

        Returns:
            A reactive value for `round(self.value, ndigits)`.

        Example:
            ```py
            >>> s = Signal(3.14159)
            >>> result = round(s, 2)
            >>> result.value
            3.14
            >>> s.value = 2.71828
            >>> result.value
            2.72

            ```
        """
        if ndigits is None or ndigits == 0:
            # When ndigits is None or 0, round returns an integer
            return cast(Computed[int], computed(round)(self, ndigits=ndigits))
        else:
            # Otherwise, float
            return cast(Computed[float], computed(round)(self, ndigits=ndigits))

    def __ceil__(self) -> Computed[int]:
        """Return a reactive value for the ceiling of `self`.

        Returns:
            A reactive value for `math.ceil(self.value)`.

        Example:
            ```py
            >>> from math import ceil
            >>> s = Signal(3.14)
            >>> result = ceil(s)
            >>> result.value
            4
            >>> s.value = 2.01
            >>> result.value
            3

            ```
        """
        return cast(Computed[int], computed(math.ceil)(self))

    def __floor__(self) -> Computed[int]:
        """Return a reactive value for the floor of `self`.

        Returns:
            A reactive value for `math.floor(self.value)`.

        Example:
            ```py
            >>> from math import floor
            >>> s = Signal(3.99)
            >>> result = floor(s)
            >>> result.value
            3
            >>> s.value = 4.01
            >>> result.value
            4

            ```
        """
        return cast(Computed[int], computed(math.floor)(self))

    def __invert__(self) -> Computed[T]:
        """Return a reactive value for the bitwise inversion of `self`.

        Returns:
            A reactive value for `~self.value`.

        Example:
            ```py
            >>> s = Signal(5)
            >>> result = ~s
            >>> result.value
            -6
            >>> s.value = -3
            >>> result.value
            2

            ```
        """
        return computed(operator.inv)(self)

    def __neg__(self) -> Computed[T]:
        """Return a reactive value for the negation of `self`.

        Returns:
            A reactive value for `-self.value`.

        Example:
            ```py
            >>> s = Signal(5)
            >>> result = -s
            >>> result.value
            -5
            >>> s.value = -3
            >>> result.value
            3

            ```
        """
        return computed(operator.neg)(self)

    def __pos__(self) -> Computed[T]:
        """Return a reactive value for the positive of self.

        Returns:
            A reactive value for `+self.value`.

        Example:
            ```py
            >>> s = Signal(-5)
            >>> result = +s
            >>> result.value
            -5
            >>> s.value = 3
            >>> result.value
            3

            ```
        """
        return computed(operator.pos)(self)

    def __trunc__(self) -> Computed[T]:
        """Return a reactive value for the truncated value of `self`.

        Returns:
            A reactive value for `math.trunc(self.value)`.

        Example:
            ```py
            >>> from math import trunc
            >>> s = Signal(3.99)
            >>> result = trunc(s)
            >>> result.value
            3
            >>> s.value = -4.01
            >>> result.value
            -4

            ```
        """
        return computed(math.trunc)(self)

    def __add__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the sum of `self` and `other`.

        Args:
            other: The value to add.

        Returns:
            A reactive value for `self.value + other.value`.

        Example:
            ```py
            >>> s = Signal(5)
            >>> result = s + 3
            >>> result.value
            8
            >>> s.value = 10
            >>> result.value
            13

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.add
        return computed(f)(self, other)

    def __and__(self, other: HasValue[Y]) -> Computed[bool]:
        """Return a reactive value for the bitwise AND of self and other.

        Args:
            other: The value to AND with.

        Returns:
            A reactive value for `self.value & other.value`.

        Example:
            ```py
            >>> s = Signal(True)
            >>> result = s & False
            >>> result.value
            False
            >>> s.value = True
            >>> result.value
            False

            ```
        """
        return computed(operator.and_)(self, other)

    def contains(self, other: Any) -> Computed[bool]:
        """Return a reactive value for whether `other` is in `self`.

        Args:
            other: The value to check for containment.

        Returns:
            A reactive value for `other in self.value`.

        Example:
            ```py
            >>> s = Signal([1, 2, 3, 4])
            >>> result = s.contains(3)
            >>> result.value
            True
            >>> s.value = [5, 6, 7, 8]
            >>> result.value
            False

            ```
        """
        return computed(operator.contains)(self, other)

    def __divmod__(self, other: Any) -> Computed[tuple[float, float]]:
        """Return a reactive value for the divmod of `self` and other.

        Args:
            other: The value to use as the divisor.

        Returns:
            A reactive value for `divmod(self.value, other)`.

        Example:
            ```py
            >>> s = Signal(10)
            >>> result = divmod(s, 3)
            >>> result.value
            (3, 1)
            >>> s.value = 20
            >>> result.value
            (6, 2)

            ```
        """
        return cast(Computed[tuple[float, float]], computed(divmod)(self, other))

    def is_not(self, other: Any) -> Computed[bool]:
        """Return a reactive value for whether `self` is not other.

        Args:
            other: The value to compare against.

        Returns:
            A reactive value for self.value is not other.

        Example:
            ```py
            >>> s = Signal(10)
            >>> other = None
            >>> result = s.is_not(other)
            >>> result.value
            True
            >>> s.value = None
            >>> result.value
            False

            ```
        """
        return computed(operator.is_not)(self, other)

    def eq(self, other: Any) -> Computed[bool]:
        """Return a reactive value for whether `self` equals other.

        Args:
            other: The value to compare against.

        Returns:
            A reactive value for self.value == other.

        Note:
            We can't overload `__eq__` because it interferes with basic Python operations.

        Example:
            ```py
            >>> s = Signal(10)
            >>> result = s.eq(10)
            >>> result.value
            True
            >>> s.value = 25
            >>> result.value
            False

            ```
        """
        return computed(operator.eq)(self, other)

    def __floordiv__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the floor division of `self` by other.

        Args:
            other: The value to use as the divisor.

        Returns:
            A reactive value for self.value // other.value.

        Example:
            ```py
            >>> s = Signal(20)
            >>> result = s // 3
            >>> result.value
            6
            >>> s.value = 25
            >>> result.value
            8

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.floordiv
        return computed(f)(self, other)

    def __ge__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for whether `self` is greater than or equal to other.

        Args:
            other: The value to compare against.

        Returns:
            A reactive value for self.value >= other.

        Example:
            ```py
            >>> s = Signal(10)
            >>> result = s >= 5
            >>> result.value
            True
            >>> s.value = 3
            >>> result.value
            False

            ```
        """
        return computed(operator.ge)(self, other)

    def __gt__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for whether `self` is greater than other.

        Args:
            other: The value to compare against.

        Returns:
            A reactive value for self.value > other.

        Example:
            ```py
            >>> s = Signal(10)
            >>> result = s > 5
            >>> result.value
            True
            >>> s.value = 3
            >>> result.value
            False

            ```
        """
        return computed(operator.gt)(self, other)

    def __le__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for whether `self` is less than or equal to `other`.

        Args:
            other: The value to compare against.

        Returns:
            A reactive value for `self.value <= other`.

        Example:
            ```py
            >>> s = Signal(5)
            >>> result = s <= 5
            >>> result.value
            True
            >>> s.value = 6
            >>> result.value
            False

            ```
        """
        return computed(operator.le)(self, other)

    def __lt__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for whether `self` is less than `other`.

        Args:
            other: The value to compare against.

        Returns:
            A reactive value for `self.value < other`.

        Example:
            ```py
            >>> s = Signal(5)
            >>> result = s < 10
            >>> result.value
            True
            >>> s.value = 15
            >>> result.value
            False

            ```
        """
        return computed(operator.lt)(self, other)

    def __lshift__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `self` left-shifted by `other`.

        Args:
            other: The number of positions to shift.

        Returns:
            A reactive value for `self.value << other.value`.

        Example:
            ```py
            >>> s = Signal(8)
            >>> result = s << 2
            >>> result.value
            32
            >>> s.value = 3
            >>> result.value
            12

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.lshift
        return computed(f)(self, other)

    def __matmul__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the matrix multiplication of `self` and `other`.

        Args:
            other: The value to multiply with.

        Returns:
            A reactive value for `self.value @ other.value`.

        Example:
            ```py
            >>> import numpy as np
            >>> s = Signal(np.array([1, 2]))
            >>> result = s @ np.array([[1, 2], [3, 4]])
            >>> result.value
            array([ 7, 10])
            >>> s.value = np.array([2, 3])
            >>> result.value
            array([11, 16])

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.matmul
        return computed(f)(self, other)

    def __mod__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `self` modulo `other`.

        Args:
            other: The divisor.

        Returns:
            A reactive value for `self.value % other.value`.

        Example:
            ```py
            >>> s = Signal(17)
            >>> result = s % 5
            >>> result.value
            2
            >>> s.value = 23
            >>> result.value
            3

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.mod
        return computed(f)(self, other)

    def __mul__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the product of `self` and `other`.

        Args:
            other: The value to multiply with.

        Returns:
            A reactive value for `self.value * other.value`.

        Example:
            ```py
            >>> s = Signal(4)
            >>> result = s * 3
            >>> result.value
            12
            >>> s.value = 5
            >>> result.value
            15

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.mul
        return computed(f)(self, other)

    def __ne__(self, other: Any) -> Computed[bool]:  # type: ignore[override]
        """Return a reactive value for whether `self` is not equal to `other`.

        Args:
            other: The value to compare against.

        Returns:
            A reactive value for `self.value != other`.

        Example:
            ```py
            >>> s = Signal(5)
            >>> result = s != 5
            >>> result.value
            False
            >>> s.value = 6
            >>> result.value
            True

            ```
        """
        return computed(operator.ne)(self, other)

    def __or__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for the bitwise OR of `self` and `other`.

        Args:
            other: The value to OR with.

        Returns:
            A reactive value for `self.value or other.value`.

        Example:
            ```py
            >>> s = Signal(False)
            >>> result = s | True
            >>> result.value
            True
            >>> s.value = True
            >>> result.value
            True

            ```
        """
        return computed(operator.or_)(self, other)

    def __rshift__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `self` right-shifted by `other`.

        Args:
            other: The number of positions to shift.

        Returns:
            A reactive value for `self.value >> other.value`.

        Example:
            ```py
            >>> s = Signal(32)
            >>> result = s >> 2
            >>> result.value
            8
            >>> s.value = 24
            >>> result.value
            6

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.rshift
        return computed(f)(self, other)

    def __pow__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `self` raised to the power of `other`.

        Args:
            other: The exponent.

        Returns:
            A reactive value for `self.value ** other.value`.

        Example:
            ```py
            >>> s = Signal(2)
            >>> result = s ** 3
            >>> result.value
            8
            >>> s.value = 3
            >>> result.value
            27

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.pow
        return computed(f)(self, other)

    def __sub__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the difference of `self` and `other`.

        Args:
            other: The value to subtract.

        Returns:
            A reactive value for `self.value - other.value`.

        Example:
            ```py
            >>> s = Signal(10)
            >>> result = s - 3
            >>> result.value
            7
            >>> s.value = 15
            >>> result.value
            12

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.sub
        return computed(f)(self, other)

    def __truediv__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `self` divided by `other`.

        Args:
            other: The value to use as the divisor.

        Returns:
            A reactive value for `self.value / other.value`.

        Example:
            ```py
            >>> s = Signal(20)
            >>> result = s / 4
            >>> result.value
            5.0
            >>> s.value = 30
            >>> result.value
            7.5

            ```
        """
        f: Callable[[T, Y], T | Y] = operator.truediv
        return computed(f)(self, other)

    def __xor__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for the bitwise XOR of `self` and `other`.

        Args:
            other: The value to XOR with.

        Returns:
            A reactive value for `self.value ^ other.value`.

        Example:
            ```py
            >>> s = Signal(True)
            >>> result = s ^ False
            >>> result.value
            True
            >>> s.value = False
            >>> result.value
            False

            ```
        """
        return computed(operator.xor)(self, other)

    def __radd__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the sum of `self` and `other`.

        Args:
            other: The value to add.

        Returns:
            A reactive value for `self.value + other.value`.

        Example:
            ```py
            >>> s = Signal(5)
            >>> result = 3 + s
            >>> result.value
            8
            >>> s.value = 10
            >>> result.value
            13

            ```
        """
        f: Callable[[Y, T], T | Y] = operator.add
        return computed(f)(other, self)

    def __rand__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for the bitwise AND of `self` and `other`.

        Args:
            other: The value to AND with.

        Returns:
            A reactive value for `self.value and other.value`.

        Example:
            ```py
            >>> s = Signal(True)
            >>> result = False & s
            >>> result.value
            False
            >>> s.value = True
            >>> result.value
            False

            ```
        """
        return computed(operator.and_)(other, self)

    def __rdivmod__(self, other: Any) -> Computed[tuple[float, float]]:
        """Return a reactive value for the divmod of `self` and `other`.

        Args:
            other: The value to use as the numerator.

        Returns:
            A reactive value for `divmod(other, self.value)`.

        Example:
            ```py
            >>> s = Signal(3)
            >>> result = divmod(10, s)
            >>> result.value
            (3, 1)
            >>> s.value = 4
            >>> result.value
            (2, 2)

            ```
        """
        return cast(Computed[tuple[float, float]], computed(divmod)(other, self))

    def __rfloordiv__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the floor division of `other` by `self`.

        Args:
            other: The value to use as the numerator.

        Returns:
            A reactive value for `other.value // self.value`.

        Example:
            ```py
            >>> s = Signal(3)
            >>> result = 10 // s
            >>> result.value
            3
            >>> s.value = 4
            >>> result.value
            2

            ```
        """
        f: Callable[[Y, T], T | Y] = operator.floordiv
        return computed(f)(other, self)

    def __rmod__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `other` modulo `self`.

        Args:
            other: The dividend.

        Returns:
            A reactive value for `other.value % self.value`.

        Example:
            ```py
            >>> s = Signal(3)
            >>> result = 10 % s
            >>> result.value
            1
            >>> s.value = 4
            >>> result.value
            2

            ```
        """
        f: Callable[[Y, T], T | Y] = operator.mod
        return computed(f)(other, self)

    def __rmul__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the product of `self` and `other`.

        Args:
            other: The value to multiply with.

        Returns:
            A reactive value for `self.value * other.value`.

        Example:
            ```py
            >>> s = Signal(4)
            >>> result = 3 * s
            >>> result.value
            12
            >>> s.value = 5
            >>> result.value
            15

            ```
        """
        f: Callable[[Y, T], T | Y] = operator.mul
        return computed(f)(other, self)

    def __ror__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for the bitwise OR of `self` and `other`.

        Args:
            other: The value to OR with.

        Returns:
            A reactive value for `self.value or other.value`.

        Example:
            ```py
            >>> s = Signal(False)
            >>> result = True | s
            >>> result.value
            True
            >>> s.value = True
            >>> result.value
            True

            ```
        """
        return computed(operator.or_)(other, self)

    def __rpow__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `self` raised to the power of `other`.

        Args:
            other: The base.

        Returns:
            A reactive value for `self.value ** other.value`.

        Example:
            ```py
            >>> s = Signal(2)
            >>> result = 3 ** s
            >>> result.value
            9
            >>> s.value = 3
            >>> result.value
            27

            ```
        """
        f: Callable[[Y, T], T | Y] = operator.pow
        return computed(f)(other, self)

    def __rsub__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for the difference of `self` and `other`.

        Args:
            other: The value to subtract from.

        Returns:
            A reactive value for `other.value - self.value`.

        Example:
            ```py
            >>> s = Signal(10)
            >>> result = 15 - s
            >>> result.value
            5
            >>> s.value = 15
            >>> result.value
            0

            ```
        """
        f: Callable[[Y, T], T | Y] = operator.sub
        return computed(f)(other, self)

    def __rtruediv__(self, other: HasValue[Y]) -> Computed[T | Y]:
        """Return a reactive value for `self` divided by `other`.

        Args:
            other: The value to use as the divisor.

        Returns:
            A reactive value for `self.value / other.value`.

        Example:
            ```py
            >>> s = Signal(2)
            >>> result = 30 / s
            >>> result.value
            15.0
            >>> s.value = 3
            >>> result.value
            10.0

            ```
        """
        f: Callable[[Y, T], T | Y] = operator.truediv
        return computed(f)(other, self)

    def __rxor__(self, other: Any) -> Computed[bool]:
        """Return a reactive value for the bitwise XOR of `self` and `other`.

        Args:
            other: The value to XOR with.

        Returns:
            A reactive value for `self.value ^ other.value`.

        Example:
            ```py
            >>> s = Signal(True)
            >>> result = False ^ s
            >>> result.value
            True
            >>> s.value = False
            >>> result.value
            False

            ```
        """
        return computed(operator.xor)(other, self)

    def __getitem__(self, key: Any) -> Computed[Any]:
        """Return a reactive value for the item or slice of `self`.

        Args:
            key: The index or slice to retrieve.

        Returns:
            A reactive value for `self.value[key]`.

        Example:
            ```py
            >>> s = Signal([1, 2, 3, 4, 5])
            >>> result = s[2]
            >>> result.value
            3
            >>> s.value = [10, 20, 30, 40, 50]
            >>> result.value
            30

            ```
        """
        return computed(operator.getitem)(self, key)

    def __setattr__(self, name: str, value: Any) -> None:
        """Set an attribute on the underlying `self.value`.

        Note:
            It is necessary to set the attribute via the Signal, rather than the
            underlying `signal.value`, to properly notify downstream observers
            of changes. Reason being, mutable objects that, for example, fallback
            to id comparison for equality checks will appear as if nothing changed
            even if one of its attributes changed.

        Args:
            name: The name of the attribute to access.
            value: The value to set it to.

        Example:
            ```py
                >>> class Person:
                ...    def __init__(self, name: str):
                ...        self.name = name
                ...    def greet(self) -> str:
                ...        return f"Hi, I'm {self.name}!"
                >>> s = Signal(Person("Alice"))
                >>> result = s.greet()
                >>> result.value
                "Hi, I'm Alice!"
                >>> s.name = "Bob"  # Modify attribute on Person instance through the reactive value s
                >>> result.value
                "Hi, I'm Bob!"

            ```
        """
        if name == "_value" or not hasattr(self, "_value"):
            super().__setattr__(name, value)
        elif hasattr(self.value, name):
            setattr(self.value, name, value)
            self.notify()
        else:
            super().__setattr__(name, value)

    def __setitem__(self, key: Any, value: Any) -> None:
        """Set an item on the underlying `self.value`.

        Note:
            It is necessary to set the item via the Signal, rather than the
            underlying `signal.value`, to properly notify downstream observers
            of changes. Reason being, mutable objects that, for example, fallback
            to id comparison for equality checks will appear as if nothing changed
            even an element of the object is changed.

        Args:
            key: The key to change.
            value: The value to set it to.

        Example:
            ```py
            >>> s = Signal([1, 2, 3])
            >>> result = computed(sum)(s)
            >>> result.value
            6
            >>> s[1] = 4
            >>> result.value
            8
        """
        if isinstance(self.value, (list, dict)):
            self.value[key] = value
            self.notify()
        else:
            raise TypeError(f"'{type(self.value).__name__}' object does not support item assignment")

    def where(self, a: HasValue[A], b: HasValue[B]) -> Computed[A | B]:
        """Return a reactive value for `a` if `self` is `True`, else `b`.

        Args:
            a: The value to return if `self` is `True`.
            b: The value to return if `self` is `False`.

        Returns:
            A reactive value for `a if self.value else b`.

        Example:
            ```py
            >>> condition = Signal(True)
            >>> result = condition.where("Yes", "No")
            >>> result.value
            'Yes'
            >>> condition.value = False
            >>> result.value
            'No'

            ```
        """

        @computed
        def ternary(a: A, b: B, self: Any) -> A | B:
            return a if self else b

        return ternary(a, b, self)

value: T property

The current value of the reactive object.

__abs__()

Return a reactive value for the absolute value of self.

Returns:

Type Description
Computed[T]

A reactive value for abs(self.value).

Example
>>> s = Signal(-5)
>>> result = abs(s)
>>> result.value
5
>>> s.value = -10
>>> result.value
10
Source code in src/signified/__init__.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def __abs__(self) -> Computed[T]:
    """Return a reactive value for the absolute value of `self`.

    Returns:
        A reactive value for `abs(self.value)`.

    Example:
        ```py
        >>> s = Signal(-5)
        >>> result = abs(s)
        >>> result.value
        5
        >>> s.value = -10
        >>> result.value
        10

        ```
    """
    return computed(abs)(self)

__add__(other)

Return a reactive value for the sum of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to add.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value + other.value.

Example
>>> s = Signal(5)
>>> result = s + 3
>>> result.value
8
>>> s.value = 10
>>> result.value
13
Source code in src/signified/__init__.py
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
def __add__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the sum of `self` and `other`.

    Args:
        other: The value to add.

    Returns:
        A reactive value for `self.value + other.value`.

    Example:
        ```py
        >>> s = Signal(5)
        >>> result = s + 3
        >>> result.value
        8
        >>> s.value = 10
        >>> result.value
        13

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.add
    return computed(f)(self, other)

__and__(other)

Return a reactive value for the bitwise AND of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to AND with.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value & other.value.

Example
>>> s = Signal(True)
>>> result = s & False
>>> result.value
False
>>> s.value = True
>>> result.value
False
Source code in src/signified/__init__.py
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
def __and__(self, other: HasValue[Y]) -> Computed[bool]:
    """Return a reactive value for the bitwise AND of self and other.

    Args:
        other: The value to AND with.

    Returns:
        A reactive value for `self.value & other.value`.

    Example:
        ```py
        >>> s = Signal(True)
        >>> result = s & False
        >>> result.value
        False
        >>> s.value = True
        >>> result.value
        False

        ```
    """
    return computed(operator.and_)(self, other)

__call__(*args, **kwargs)

__call__(*args: Any, **kwargs: Any) -> Computed[R]
__call__(*args: Any, **kwargs: Any) -> Any

Create a reactive value for calling self.value(*args, **kwargs).

Parameters:

Name Type Description Default
*args Any

Positional arguments to pass to the callable value.

()
**kwargs Any

Keyword arguments to pass to the callable value.

{}

Returns:

Type Description
Any

A reactive value for the function call.

Raises:

Type Description
ValueError

If the value is not callable.

Example
>>> class Person:
...     def __init__(self, name):
...         self.name = name
...     def greet(self):
...         return f"Hi, I'm {self.name}!"
>>> s = Signal(Person("Alice"))
>>> result = s.greet()
>>> result.value
"Hi, I'm Alice!"
>>> s.name = "Bob"
>>> result.value
"Hi, I'm Bob!"
Source code in src/signified/__init__.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def __call__(self, *args: Any, **kwargs: Any) -> Any:
    """Create a reactive value for calling `self.value(*args, **kwargs)`.

    Args:
        *args: Positional arguments to pass to the callable value.
        **kwargs: Keyword arguments to pass to the callable value.

    Returns:
        A reactive value for the function call.

    Raises:
        ValueError: If the value is not callable.

    Example:
        ```py
        >>> class Person:
        ...     def __init__(self, name):
        ...         self.name = name
        ...     def greet(self):
        ...         return f"Hi, I'm {self.name}!"
        >>> s = Signal(Person("Alice"))
        >>> result = s.greet()
        >>> result.value
        "Hi, I'm Alice!"
        >>> s.name = "Bob"
        >>> result.value
        "Hi, I'm Bob!"

        ```
    """
    if not callable(self.value):
        raise ValueError("Value is not callable.")

    def f(*args: Any, **kwargs: Any):
        _f = getattr(self, "value")
        return _f(*args, **kwargs)

    return computed(f)(*args, **kwargs).observe([self, self.value])

__ceil__()

Return a reactive value for the ceiling of self.

Returns:

Type Description
Computed[int]

A reactive value for math.ceil(self.value).

Example
>>> from math import ceil
>>> s = Signal(3.14)
>>> result = ceil(s)
>>> result.value
4
>>> s.value = 2.01
>>> result.value
3
Source code in src/signified/__init__.py
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
def __ceil__(self) -> Computed[int]:
    """Return a reactive value for the ceiling of `self`.

    Returns:
        A reactive value for `math.ceil(self.value)`.

    Example:
        ```py
        >>> from math import ceil
        >>> s = Signal(3.14)
        >>> result = ceil(s)
        >>> result.value
        4
        >>> s.value = 2.01
        >>> result.value
        3

        ```
    """
    return cast(Computed[int], computed(math.ceil)(self))

__divmod__(other)

Return a reactive value for the divmod of self and other.

Parameters:

Name Type Description Default
other Any

The value to use as the divisor.

required

Returns:

Type Description
Computed[tuple[float, float]]

A reactive value for divmod(self.value, other).

Example
>>> s = Signal(10)
>>> result = divmod(s, 3)
>>> result.value
(3, 1)
>>> s.value = 20
>>> result.value
(6, 2)
Source code in src/signified/__init__.py
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
def __divmod__(self, other: Any) -> Computed[tuple[float, float]]:
    """Return a reactive value for the divmod of `self` and other.

    Args:
        other: The value to use as the divisor.

    Returns:
        A reactive value for `divmod(self.value, other)`.

    Example:
        ```py
        >>> s = Signal(10)
        >>> result = divmod(s, 3)
        >>> result.value
        (3, 1)
        >>> s.value = 20
        >>> result.value
        (6, 2)

        ```
    """
    return cast(Computed[tuple[float, float]], computed(divmod)(self, other))

__floor__()

Return a reactive value for the floor of self.

Returns:

Type Description
Computed[int]

A reactive value for math.floor(self.value).

Example
>>> from math import floor
>>> s = Signal(3.99)
>>> result = floor(s)
>>> result.value
3
>>> s.value = 4.01
>>> result.value
4
Source code in src/signified/__init__.py
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
def __floor__(self) -> Computed[int]:
    """Return a reactive value for the floor of `self`.

    Returns:
        A reactive value for `math.floor(self.value)`.

    Example:
        ```py
        >>> from math import floor
        >>> s = Signal(3.99)
        >>> result = floor(s)
        >>> result.value
        3
        >>> s.value = 4.01
        >>> result.value
        4

        ```
    """
    return cast(Computed[int], computed(math.floor)(self))

__floordiv__(other)

Return a reactive value for the floor division of self by other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to use as the divisor.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value // other.value.

Example
>>> s = Signal(20)
>>> result = s // 3
>>> result.value
6
>>> s.value = 25
>>> result.value
8
Source code in src/signified/__init__.py
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
def __floordiv__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the floor division of `self` by other.

    Args:
        other: The value to use as the divisor.

    Returns:
        A reactive value for self.value // other.value.

    Example:
        ```py
        >>> s = Signal(20)
        >>> result = s // 3
        >>> result.value
        6
        >>> s.value = 25
        >>> result.value
        8

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.floordiv
    return computed(f)(self, other)

__ge__(other)

Return a reactive value for whether self is greater than or equal to other.

Parameters:

Name Type Description Default
other Any

The value to compare against.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value >= other.

Example
>>> s = Signal(10)
>>> result = s >= 5
>>> result.value
True
>>> s.value = 3
>>> result.value
False
Source code in src/signified/__init__.py
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
def __ge__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for whether `self` is greater than or equal to other.

    Args:
        other: The value to compare against.

    Returns:
        A reactive value for self.value >= other.

    Example:
        ```py
        >>> s = Signal(10)
        >>> result = s >= 5
        >>> result.value
        True
        >>> s.value = 3
        >>> result.value
        False

        ```
    """
    return computed(operator.ge)(self, other)

__getattr__(name)

__getattr__(name: Literal['value', '_value']) -> T
__getattr__(name: str) -> Computed[Any]

Create a reactive value for retrieving an attribute from self.value.

Parameters:

Name Type Description Default
name str

The name of the attribute to access.

required

Returns:

Type Description
Union[T, Computed[Any]]

A reactive value for the attribute access.

Raises:

Type Description
AttributeError

If the attribute doesn't exist.

Note

Type inference is poor whenever __getattr__ is used.

Example
>>> class Person:
...     def __init__(self, name):
...         self.name = name
>>> s = Signal(Person("Alice"))
>>> result = s.name
>>> result.value
'Alice'
>>> s.value = Person("Bob")
>>> result.value
'Bob'
Source code in src/signified/__init__.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def __getattr__(self, name: str) -> Union[T, Computed[Any]]:
    """Create a reactive value for retrieving an attribute from ``self.value``.

    Args:
        name: The name of the attribute to access.

    Returns:
        A reactive value for the attribute access.

    Raises:
        AttributeError: If the attribute doesn't exist.

    Note:
        Type inference is poor whenever `__getattr__` is used.

    Example:
        ```py
        >>> class Person:
        ...     def __init__(self, name):
        ...         self.name = name
        >>> s = Signal(Person("Alice"))
        >>> result = s.name
        >>> result.value
        'Alice'
        >>> s.value = Person("Bob")
        >>> result.value
        'Bob'

        ```
    """
    if name in {"value", "_value"}:
        return super().__getattribute__(name)

    if hasattr(self.value, name):
        return computed(getattr)(self, name)
    else:
        return super().__getattribute__(name)

__getitem__(key)

Return a reactive value for the item or slice of self.

Parameters:

Name Type Description Default
key Any

The index or slice to retrieve.

required

Returns:

Type Description
Computed[Any]

A reactive value for self.value[key].

Example
>>> s = Signal([1, 2, 3, 4, 5])
>>> result = s[2]
>>> result.value
3
>>> s.value = [10, 20, 30, 40, 50]
>>> result.value
30
Source code in src/signified/__init__.py
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
def __getitem__(self, key: Any) -> Computed[Any]:
    """Return a reactive value for the item or slice of `self`.

    Args:
        key: The index or slice to retrieve.

    Returns:
        A reactive value for `self.value[key]`.

    Example:
        ```py
        >>> s = Signal([1, 2, 3, 4, 5])
        >>> result = s[2]
        >>> result.value
        3
        >>> s.value = [10, 20, 30, 40, 50]
        >>> result.value
        30

        ```
    """
    return computed(operator.getitem)(self, key)

__gt__(other)

Return a reactive value for whether self is greater than other.

Parameters:

Name Type Description Default
other Any

The value to compare against.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value > other.

Example
>>> s = Signal(10)
>>> result = s > 5
>>> result.value
True
>>> s.value = 3
>>> result.value
False
Source code in src/signified/__init__.py
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
def __gt__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for whether `self` is greater than other.

    Args:
        other: The value to compare against.

    Returns:
        A reactive value for self.value > other.

    Example:
        ```py
        >>> s = Signal(10)
        >>> result = s > 5
        >>> result.value
        True
        >>> s.value = 3
        >>> result.value
        False

        ```
    """
    return computed(operator.gt)(self, other)

__invert__()

Return a reactive value for the bitwise inversion of self.

Returns:

Type Description
Computed[T]

A reactive value for ~self.value.

Example
>>> s = Signal(5)
>>> result = ~s
>>> result.value
-6
>>> s.value = -3
>>> result.value
2
Source code in src/signified/__init__.py
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
def __invert__(self) -> Computed[T]:
    """Return a reactive value for the bitwise inversion of `self`.

    Returns:
        A reactive value for `~self.value`.

    Example:
        ```py
        >>> s = Signal(5)
        >>> result = ~s
        >>> result.value
        -6
        >>> s.value = -3
        >>> result.value
        2

        ```
    """
    return computed(operator.inv)(self)

__le__(other)

Return a reactive value for whether self is less than or equal to other.

Parameters:

Name Type Description Default
other Any

The value to compare against.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value <= other.

Example
>>> s = Signal(5)
>>> result = s <= 5
>>> result.value
True
>>> s.value = 6
>>> result.value
False
Source code in src/signified/__init__.py
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
def __le__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for whether `self` is less than or equal to `other`.

    Args:
        other: The value to compare against.

    Returns:
        A reactive value for `self.value <= other`.

    Example:
        ```py
        >>> s = Signal(5)
        >>> result = s <= 5
        >>> result.value
        True
        >>> s.value = 6
        >>> result.value
        False

        ```
    """
    return computed(operator.le)(self, other)

__lshift__(other)

Return a reactive value for self left-shifted by other.

Parameters:

Name Type Description Default
other HasValue[Y]

The number of positions to shift.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value << other.value.

Example
>>> s = Signal(8)
>>> result = s << 2
>>> result.value
32
>>> s.value = 3
>>> result.value
12
Source code in src/signified/__init__.py
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
def __lshift__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `self` left-shifted by `other`.

    Args:
        other: The number of positions to shift.

    Returns:
        A reactive value for `self.value << other.value`.

    Example:
        ```py
        >>> s = Signal(8)
        >>> result = s << 2
        >>> result.value
        32
        >>> s.value = 3
        >>> result.value
        12

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.lshift
    return computed(f)(self, other)

__lt__(other)

Return a reactive value for whether self is less than other.

Parameters:

Name Type Description Default
other Any

The value to compare against.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value < other.

Example
>>> s = Signal(5)
>>> result = s < 10
>>> result.value
True
>>> s.value = 15
>>> result.value
False
Source code in src/signified/__init__.py
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
def __lt__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for whether `self` is less than `other`.

    Args:
        other: The value to compare against.

    Returns:
        A reactive value for `self.value < other`.

    Example:
        ```py
        >>> s = Signal(5)
        >>> result = s < 10
        >>> result.value
        True
        >>> s.value = 15
        >>> result.value
        False

        ```
    """
    return computed(operator.lt)(self, other)

__matmul__(other)

Return a reactive value for the matrix multiplication of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to multiply with.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value @ other.value.

Example
>>> import numpy as np
>>> s = Signal(np.array([1, 2]))
>>> result = s @ np.array([[1, 2], [3, 4]])
>>> result.value
array([ 7, 10])
>>> s.value = np.array([2, 3])
>>> result.value
array([11, 16])
Source code in src/signified/__init__.py
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
def __matmul__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the matrix multiplication of `self` and `other`.

    Args:
        other: The value to multiply with.

    Returns:
        A reactive value for `self.value @ other.value`.

    Example:
        ```py
        >>> import numpy as np
        >>> s = Signal(np.array([1, 2]))
        >>> result = s @ np.array([[1, 2], [3, 4]])
        >>> result.value
        array([ 7, 10])
        >>> s.value = np.array([2, 3])
        >>> result.value
        array([11, 16])

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.matmul
    return computed(f)(self, other)

__mod__(other)

Return a reactive value for self modulo other.

Parameters:

Name Type Description Default
other HasValue[Y]

The divisor.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value % other.value.

Example
>>> s = Signal(17)
>>> result = s % 5
>>> result.value
2
>>> s.value = 23
>>> result.value
3
Source code in src/signified/__init__.py
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
def __mod__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `self` modulo `other`.

    Args:
        other: The divisor.

    Returns:
        A reactive value for `self.value % other.value`.

    Example:
        ```py
        >>> s = Signal(17)
        >>> result = s % 5
        >>> result.value
        2
        >>> s.value = 23
        >>> result.value
        3

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.mod
    return computed(f)(self, other)

__mul__(other)

Return a reactive value for the product of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to multiply with.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value * other.value.

Example
>>> s = Signal(4)
>>> result = s * 3
>>> result.value
12
>>> s.value = 5
>>> result.value
15
Source code in src/signified/__init__.py
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
def __mul__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the product of `self` and `other`.

    Args:
        other: The value to multiply with.

    Returns:
        A reactive value for `self.value * other.value`.

    Example:
        ```py
        >>> s = Signal(4)
        >>> result = s * 3
        >>> result.value
        12
        >>> s.value = 5
        >>> result.value
        15

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.mul
    return computed(f)(self, other)

__ne__(other)

Return a reactive value for whether self is not equal to other.

Parameters:

Name Type Description Default
other Any

The value to compare against.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value != other.

Example
>>> s = Signal(5)
>>> result = s != 5
>>> result.value
False
>>> s.value = 6
>>> result.value
True
Source code in src/signified/__init__.py
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
def __ne__(self, other: Any) -> Computed[bool]:  # type: ignore[override]
    """Return a reactive value for whether `self` is not equal to `other`.

    Args:
        other: The value to compare against.

    Returns:
        A reactive value for `self.value != other`.

    Example:
        ```py
        >>> s = Signal(5)
        >>> result = s != 5
        >>> result.value
        False
        >>> s.value = 6
        >>> result.value
        True

        ```
    """
    return computed(operator.ne)(self, other)

__neg__()

Return a reactive value for the negation of self.

Returns:

Type Description
Computed[T]

A reactive value for -self.value.

Example
>>> s = Signal(5)
>>> result = -s
>>> result.value
-5
>>> s.value = -3
>>> result.value
3
Source code in src/signified/__init__.py
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
def __neg__(self) -> Computed[T]:
    """Return a reactive value for the negation of `self`.

    Returns:
        A reactive value for `-self.value`.

    Example:
        ```py
        >>> s = Signal(5)
        >>> result = -s
        >>> result.value
        -5
        >>> s.value = -3
        >>> result.value
        3

        ```
    """
    return computed(operator.neg)(self)

__or__(other)

Return a reactive value for the bitwise OR of self and other.

Parameters:

Name Type Description Default
other Any

The value to OR with.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value or other.value.

Example
>>> s = Signal(False)
>>> result = s | True
>>> result.value
True
>>> s.value = True
>>> result.value
True
Source code in src/signified/__init__.py
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
def __or__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for the bitwise OR of `self` and `other`.

    Args:
        other: The value to OR with.

    Returns:
        A reactive value for `self.value or other.value`.

    Example:
        ```py
        >>> s = Signal(False)
        >>> result = s | True
        >>> result.value
        True
        >>> s.value = True
        >>> result.value
        True

        ```
    """
    return computed(operator.or_)(self, other)

__pos__()

Return a reactive value for the positive of self.

Returns:

Type Description
Computed[T]

A reactive value for +self.value.

Example
>>> s = Signal(-5)
>>> result = +s
>>> result.value
-5
>>> s.value = 3
>>> result.value
3
Source code in src/signified/__init__.py
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
def __pos__(self) -> Computed[T]:
    """Return a reactive value for the positive of self.

    Returns:
        A reactive value for `+self.value`.

    Example:
        ```py
        >>> s = Signal(-5)
        >>> result = +s
        >>> result.value
        -5
        >>> s.value = 3
        >>> result.value
        3

        ```
    """
    return computed(operator.pos)(self)

__pow__(other)

Return a reactive value for self raised to the power of other.

Parameters:

Name Type Description Default
other HasValue[Y]

The exponent.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value ** other.value.

Example
>>> s = Signal(2)
>>> result = s ** 3
>>> result.value
8
>>> s.value = 3
>>> result.value
27
Source code in src/signified/__init__.py
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
def __pow__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `self` raised to the power of `other`.

    Args:
        other: The exponent.

    Returns:
        A reactive value for `self.value ** other.value`.

    Example:
        ```py
        >>> s = Signal(2)
        >>> result = s ** 3
        >>> result.value
        8
        >>> s.value = 3
        >>> result.value
        27

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.pow
    return computed(f)(self, other)

__radd__(other)

Return a reactive value for the sum of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to add.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value + other.value.

Example
>>> s = Signal(5)
>>> result = 3 + s
>>> result.value
8
>>> s.value = 10
>>> result.value
13
Source code in src/signified/__init__.py
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
def __radd__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the sum of `self` and `other`.

    Args:
        other: The value to add.

    Returns:
        A reactive value for `self.value + other.value`.

    Example:
        ```py
        >>> s = Signal(5)
        >>> result = 3 + s
        >>> result.value
        8
        >>> s.value = 10
        >>> result.value
        13

        ```
    """
    f: Callable[[Y, T], T | Y] = operator.add
    return computed(f)(other, self)

__rand__(other)

Return a reactive value for the bitwise AND of self and other.

Parameters:

Name Type Description Default
other Any

The value to AND with.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value and other.value.

Example
>>> s = Signal(True)
>>> result = False & s
>>> result.value
False
>>> s.value = True
>>> result.value
False
Source code in src/signified/__init__.py
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
def __rand__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for the bitwise AND of `self` and `other`.

    Args:
        other: The value to AND with.

    Returns:
        A reactive value for `self.value and other.value`.

    Example:
        ```py
        >>> s = Signal(True)
        >>> result = False & s
        >>> result.value
        False
        >>> s.value = True
        >>> result.value
        False

        ```
    """
    return computed(operator.and_)(other, self)

__rdivmod__(other)

Return a reactive value for the divmod of self and other.

Parameters:

Name Type Description Default
other Any

The value to use as the numerator.

required

Returns:

Type Description
Computed[tuple[float, float]]

A reactive value for divmod(other, self.value).

Example
>>> s = Signal(3)
>>> result = divmod(10, s)
>>> result.value
(3, 1)
>>> s.value = 4
>>> result.value
(2, 2)
Source code in src/signified/__init__.py
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
def __rdivmod__(self, other: Any) -> Computed[tuple[float, float]]:
    """Return a reactive value for the divmod of `self` and `other`.

    Args:
        other: The value to use as the numerator.

    Returns:
        A reactive value for `divmod(other, self.value)`.

    Example:
        ```py
        >>> s = Signal(3)
        >>> result = divmod(10, s)
        >>> result.value
        (3, 1)
        >>> s.value = 4
        >>> result.value
        (2, 2)

        ```
    """
    return cast(Computed[tuple[float, float]], computed(divmod)(other, self))

__rfloordiv__(other)

Return a reactive value for the floor division of other by self.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to use as the numerator.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for other.value // self.value.

Example
>>> s = Signal(3)
>>> result = 10 // s
>>> result.value
3
>>> s.value = 4
>>> result.value
2
Source code in src/signified/__init__.py
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
def __rfloordiv__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the floor division of `other` by `self`.

    Args:
        other: The value to use as the numerator.

    Returns:
        A reactive value for `other.value // self.value`.

    Example:
        ```py
        >>> s = Signal(3)
        >>> result = 10 // s
        >>> result.value
        3
        >>> s.value = 4
        >>> result.value
        2

        ```
    """
    f: Callable[[Y, T], T | Y] = operator.floordiv
    return computed(f)(other, self)

__rmod__(other)

Return a reactive value for other modulo self.

Parameters:

Name Type Description Default
other HasValue[Y]

The dividend.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for other.value % self.value.

Example
>>> s = Signal(3)
>>> result = 10 % s
>>> result.value
1
>>> s.value = 4
>>> result.value
2
Source code in src/signified/__init__.py
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
def __rmod__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `other` modulo `self`.

    Args:
        other: The dividend.

    Returns:
        A reactive value for `other.value % self.value`.

    Example:
        ```py
        >>> s = Signal(3)
        >>> result = 10 % s
        >>> result.value
        1
        >>> s.value = 4
        >>> result.value
        2

        ```
    """
    f: Callable[[Y, T], T | Y] = operator.mod
    return computed(f)(other, self)

__rmul__(other)

Return a reactive value for the product of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to multiply with.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value * other.value.

Example
>>> s = Signal(4)
>>> result = 3 * s
>>> result.value
12
>>> s.value = 5
>>> result.value
15
Source code in src/signified/__init__.py
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
def __rmul__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the product of `self` and `other`.

    Args:
        other: The value to multiply with.

    Returns:
        A reactive value for `self.value * other.value`.

    Example:
        ```py
        >>> s = Signal(4)
        >>> result = 3 * s
        >>> result.value
        12
        >>> s.value = 5
        >>> result.value
        15

        ```
    """
    f: Callable[[Y, T], T | Y] = operator.mul
    return computed(f)(other, self)

__ror__(other)

Return a reactive value for the bitwise OR of self and other.

Parameters:

Name Type Description Default
other Any

The value to OR with.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value or other.value.

Example
>>> s = Signal(False)
>>> result = True | s
>>> result.value
True
>>> s.value = True
>>> result.value
True
Source code in src/signified/__init__.py
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
def __ror__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for the bitwise OR of `self` and `other`.

    Args:
        other: The value to OR with.

    Returns:
        A reactive value for `self.value or other.value`.

    Example:
        ```py
        >>> s = Signal(False)
        >>> result = True | s
        >>> result.value
        True
        >>> s.value = True
        >>> result.value
        True

        ```
    """
    return computed(operator.or_)(other, self)

__round__(ndigits=None)

__round__() -> Computed[int]
__round__(ndigits: None) -> Computed[int]
__round__(ndigits: int) -> Computed[float]

Return a reactive value for the rounded value of self.

Parameters:

Name Type Description Default
ndigits int | None

Number of decimal places to round to.

None

Returns:

Type Description
Computed[int] | Computed[float]

A reactive value for round(self.value, ndigits).

Example
>>> s = Signal(3.14159)
>>> result = round(s, 2)
>>> result.value
3.14
>>> s.value = 2.71828
>>> result.value
2.72
Source code in src/signified/__init__.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def __round__(self, ndigits: int | None = None) -> Computed[int] | Computed[float]:
    """Return a reactive value for the rounded value of self.

    Args:
        ndigits: Number of decimal places to round to.

    Returns:
        A reactive value for `round(self.value, ndigits)`.

    Example:
        ```py
        >>> s = Signal(3.14159)
        >>> result = round(s, 2)
        >>> result.value
        3.14
        >>> s.value = 2.71828
        >>> result.value
        2.72

        ```
    """
    if ndigits is None or ndigits == 0:
        # When ndigits is None or 0, round returns an integer
        return cast(Computed[int], computed(round)(self, ndigits=ndigits))
    else:
        # Otherwise, float
        return cast(Computed[float], computed(round)(self, ndigits=ndigits))

__rpow__(other)

Return a reactive value for self raised to the power of other.

Parameters:

Name Type Description Default
other HasValue[Y]

The base.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value ** other.value.

Example
>>> s = Signal(2)
>>> result = 3 ** s
>>> result.value
9
>>> s.value = 3
>>> result.value
27
Source code in src/signified/__init__.py
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
def __rpow__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `self` raised to the power of `other`.

    Args:
        other: The base.

    Returns:
        A reactive value for `self.value ** other.value`.

    Example:
        ```py
        >>> s = Signal(2)
        >>> result = 3 ** s
        >>> result.value
        9
        >>> s.value = 3
        >>> result.value
        27

        ```
    """
    f: Callable[[Y, T], T | Y] = operator.pow
    return computed(f)(other, self)

__rshift__(other)

Return a reactive value for self right-shifted by other.

Parameters:

Name Type Description Default
other HasValue[Y]

The number of positions to shift.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value >> other.value.

Example
>>> s = Signal(32)
>>> result = s >> 2
>>> result.value
8
>>> s.value = 24
>>> result.value
6
Source code in src/signified/__init__.py
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
def __rshift__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `self` right-shifted by `other`.

    Args:
        other: The number of positions to shift.

    Returns:
        A reactive value for `self.value >> other.value`.

    Example:
        ```py
        >>> s = Signal(32)
        >>> result = s >> 2
        >>> result.value
        8
        >>> s.value = 24
        >>> result.value
        6

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.rshift
    return computed(f)(self, other)

__rsub__(other)

Return a reactive value for the difference of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to subtract from.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for other.value - self.value.

Example
>>> s = Signal(10)
>>> result = 15 - s
>>> result.value
5
>>> s.value = 15
>>> result.value
0
Source code in src/signified/__init__.py
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
def __rsub__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the difference of `self` and `other`.

    Args:
        other: The value to subtract from.

    Returns:
        A reactive value for `other.value - self.value`.

    Example:
        ```py
        >>> s = Signal(10)
        >>> result = 15 - s
        >>> result.value
        5
        >>> s.value = 15
        >>> result.value
        0

        ```
    """
    f: Callable[[Y, T], T | Y] = operator.sub
    return computed(f)(other, self)

__rtruediv__(other)

Return a reactive value for self divided by other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to use as the divisor.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value / other.value.

Example
>>> s = Signal(2)
>>> result = 30 / s
>>> result.value
15.0
>>> s.value = 3
>>> result.value
10.0
Source code in src/signified/__init__.py
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
def __rtruediv__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `self` divided by `other`.

    Args:
        other: The value to use as the divisor.

    Returns:
        A reactive value for `self.value / other.value`.

    Example:
        ```py
        >>> s = Signal(2)
        >>> result = 30 / s
        >>> result.value
        15.0
        >>> s.value = 3
        >>> result.value
        10.0

        ```
    """
    f: Callable[[Y, T], T | Y] = operator.truediv
    return computed(f)(other, self)

__rxor__(other)

Return a reactive value for the bitwise XOR of self and other.

Parameters:

Name Type Description Default
other Any

The value to XOR with.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value ^ other.value.

Example
>>> s = Signal(True)
>>> result = False ^ s
>>> result.value
True
>>> s.value = False
>>> result.value
False
Source code in src/signified/__init__.py
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
def __rxor__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for the bitwise XOR of `self` and `other`.

    Args:
        other: The value to XOR with.

    Returns:
        A reactive value for `self.value ^ other.value`.

    Example:
        ```py
        >>> s = Signal(True)
        >>> result = False ^ s
        >>> result.value
        True
        >>> s.value = False
        >>> result.value
        False

        ```
    """
    return computed(operator.xor)(other, self)

__setattr__(name, value)

Set an attribute on the underlying self.value.

Note

It is necessary to set the attribute via the Signal, rather than the underlying signal.value, to properly notify downstream observers of changes. Reason being, mutable objects that, for example, fallback to id comparison for equality checks will appear as if nothing changed even if one of its attributes changed.

Parameters:

Name Type Description Default
name str

The name of the attribute to access.

required
value Any

The value to set it to.

required
Example
    >>> class Person:
    ...    def __init__(self, name: str):
    ...        self.name = name
    ...    def greet(self) -> str:
    ...        return f"Hi, I'm {self.name}!"
    >>> s = Signal(Person("Alice"))
    >>> result = s.greet()
    >>> result.value
    "Hi, I'm Alice!"
    >>> s.name = "Bob"  # Modify attribute on Person instance through the reactive value s
    >>> result.value
    "Hi, I'm Bob!"
Source code in src/signified/__init__.py
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
def __setattr__(self, name: str, value: Any) -> None:
    """Set an attribute on the underlying `self.value`.

    Note:
        It is necessary to set the attribute via the Signal, rather than the
        underlying `signal.value`, to properly notify downstream observers
        of changes. Reason being, mutable objects that, for example, fallback
        to id comparison for equality checks will appear as if nothing changed
        even if one of its attributes changed.

    Args:
        name: The name of the attribute to access.
        value: The value to set it to.

    Example:
        ```py
            >>> class Person:
            ...    def __init__(self, name: str):
            ...        self.name = name
            ...    def greet(self) -> str:
            ...        return f"Hi, I'm {self.name}!"
            >>> s = Signal(Person("Alice"))
            >>> result = s.greet()
            >>> result.value
            "Hi, I'm Alice!"
            >>> s.name = "Bob"  # Modify attribute on Person instance through the reactive value s
            >>> result.value
            "Hi, I'm Bob!"

        ```
    """
    if name == "_value" or not hasattr(self, "_value"):
        super().__setattr__(name, value)
    elif hasattr(self.value, name):
        setattr(self.value, name, value)
        self.notify()
    else:
        super().__setattr__(name, value)

__setitem__(key, value)

Set an item on the underlying self.value.

Note

It is necessary to set the item via the Signal, rather than the underlying signal.value, to properly notify downstream observers of changes. Reason being, mutable objects that, for example, fallback to id comparison for equality checks will appear as if nothing changed even an element of the object is changed.

Parameters:

Name Type Description Default
key Any

The key to change.

required
value Any

The value to set it to.

required
Example

```py

s = Signal([1, 2, 3]) result = computed(sum)(s) result.value 6 s[1] = 4 result.value 8

Source code in src/signified/__init__.py
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
def __setitem__(self, key: Any, value: Any) -> None:
    """Set an item on the underlying `self.value`.

    Note:
        It is necessary to set the item via the Signal, rather than the
        underlying `signal.value`, to properly notify downstream observers
        of changes. Reason being, mutable objects that, for example, fallback
        to id comparison for equality checks will appear as if nothing changed
        even an element of the object is changed.

    Args:
        key: The key to change.
        value: The value to set it to.

    Example:
        ```py
        >>> s = Signal([1, 2, 3])
        >>> result = computed(sum)(s)
        >>> result.value
        6
        >>> s[1] = 4
        >>> result.value
        8
    """
    if isinstance(self.value, (list, dict)):
        self.value[key] = value
        self.notify()
    else:
        raise TypeError(f"'{type(self.value).__name__}' object does not support item assignment")

__str__()

Return a string of the current value.

Note

This is not reactive.

Returns:

Type Description
str

A string representation of self.value.

Source code in src/signified/__init__.py
248
249
250
251
252
253
254
255
256
257
def __str__(self) -> str:
    """Return a string of the current value.

    Note:
        This is not reactive.

    Returns:
        A string representation of `self.value`.
    """
    return str(self.value)

__sub__(other)

Return a reactive value for the difference of self and other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to subtract.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value - other.value.

Example
>>> s = Signal(10)
>>> result = s - 3
>>> result.value
7
>>> s.value = 15
>>> result.value
12
Source code in src/signified/__init__.py
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
def __sub__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for the difference of `self` and `other`.

    Args:
        other: The value to subtract.

    Returns:
        A reactive value for `self.value - other.value`.

    Example:
        ```py
        >>> s = Signal(10)
        >>> result = s - 3
        >>> result.value
        7
        >>> s.value = 15
        >>> result.value
        12

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.sub
    return computed(f)(self, other)

__truediv__(other)

Return a reactive value for self divided by other.

Parameters:

Name Type Description Default
other HasValue[Y]

The value to use as the divisor.

required

Returns:

Type Description
Computed[T | Y]

A reactive value for self.value / other.value.

Example
>>> s = Signal(20)
>>> result = s / 4
>>> result.value
5.0
>>> s.value = 30
>>> result.value
7.5
Source code in src/signified/__init__.py
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
def __truediv__(self, other: HasValue[Y]) -> Computed[T | Y]:
    """Return a reactive value for `self` divided by `other`.

    Args:
        other: The value to use as the divisor.

    Returns:
        A reactive value for `self.value / other.value`.

    Example:
        ```py
        >>> s = Signal(20)
        >>> result = s / 4
        >>> result.value
        5.0
        >>> s.value = 30
        >>> result.value
        7.5

        ```
    """
    f: Callable[[T, Y], T | Y] = operator.truediv
    return computed(f)(self, other)

__trunc__()

Return a reactive value for the truncated value of self.

Returns:

Type Description
Computed[T]

A reactive value for math.trunc(self.value).

Example
>>> from math import trunc
>>> s = Signal(3.99)
>>> result = trunc(s)
>>> result.value
3
>>> s.value = -4.01
>>> result.value
-4
Source code in src/signified/__init__.py
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
def __trunc__(self) -> Computed[T]:
    """Return a reactive value for the truncated value of `self`.

    Returns:
        A reactive value for `math.trunc(self.value)`.

    Example:
        ```py
        >>> from math import trunc
        >>> s = Signal(3.99)
        >>> result = trunc(s)
        >>> result.value
        3
        >>> s.value = -4.01
        >>> result.value
        -4

        ```
    """
    return computed(math.trunc)(self)

__xor__(other)

Return a reactive value for the bitwise XOR of self and other.

Parameters:

Name Type Description Default
other Any

The value to XOR with.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value ^ other.value.

Example
>>> s = Signal(True)
>>> result = s ^ False
>>> result.value
True
>>> s.value = False
>>> result.value
False
Source code in src/signified/__init__.py
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
def __xor__(self, other: Any) -> Computed[bool]:
    """Return a reactive value for the bitwise XOR of `self` and `other`.

    Args:
        other: The value to XOR with.

    Returns:
        A reactive value for `self.value ^ other.value`.

    Example:
        ```py
        >>> s = Signal(True)
        >>> result = s ^ False
        >>> result.value
        True
        >>> s.value = False
        >>> result.value
        False

        ```
    """
    return computed(operator.xor)(self, other)

bool()

Return a reactive value for the boolean value of self.

Note

__bool__ cannot be implemented to return a non-bool, so it is provided as a method.

Returns:

Type Description
Computed[bool]

A reactive value for bool(self.value).

Example
>>> s = Signal(1)
>>> result = s.bool()
>>> result.value
True
>>> s.value = 0
>>> result.value
False
Source code in src/signified/__init__.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def bool(self) -> Computed[bool]:
    """Return a reactive value for the boolean value of `self`.

    Note:
        `__bool__` cannot be implemented to return a non-`bool`, so it is provided as a method.

    Returns:
        A reactive value for `bool(self.value)`.

    Example:
        ```py
        >>> s = Signal(1)
        >>> result = s.bool()
        >>> result.value
        True
        >>> s.value = 0
        >>> result.value
        False

        ```
    """
    return computed(bool)(self)

contains(other)

Return a reactive value for whether other is in self.

Parameters:

Name Type Description Default
other Any

The value to check for containment.

required

Returns:

Type Description
Computed[bool]

A reactive value for other in self.value.

Example
>>> s = Signal([1, 2, 3, 4])
>>> result = s.contains(3)
>>> result.value
True
>>> s.value = [5, 6, 7, 8]
>>> result.value
False
Source code in src/signified/__init__.py
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
def contains(self, other: Any) -> Computed[bool]:
    """Return a reactive value for whether `other` is in `self`.

    Args:
        other: The value to check for containment.

    Returns:
        A reactive value for `other in self.value`.

    Example:
        ```py
        >>> s = Signal([1, 2, 3, 4])
        >>> result = s.contains(3)
        >>> result.value
        True
        >>> s.value = [5, 6, 7, 8]
        >>> result.value
        False

        ```
    """
    return computed(operator.contains)(self, other)

eq(other)

Return a reactive value for whether self equals other.

Parameters:

Name Type Description Default
other Any

The value to compare against.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value == other.

Note

We can't overload __eq__ because it interferes with basic Python operations.

Example
>>> s = Signal(10)
>>> result = s.eq(10)
>>> result.value
True
>>> s.value = 25
>>> result.value
False
Source code in src/signified/__init__.py
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
def eq(self, other: Any) -> Computed[bool]:
    """Return a reactive value for whether `self` equals other.

    Args:
        other: The value to compare against.

    Returns:
        A reactive value for self.value == other.

    Note:
        We can't overload `__eq__` because it interferes with basic Python operations.

    Example:
        ```py
        >>> s = Signal(10)
        >>> result = s.eq(10)
        >>> result.value
        True
        >>> s.value = 25
        >>> result.value
        False

        ```
    """
    return computed(operator.eq)(self, other)

is_not(other)

Return a reactive value for whether self is not other.

Parameters:

Name Type Description Default
other Any

The value to compare against.

required

Returns:

Type Description
Computed[bool]

A reactive value for self.value is not other.

Example
>>> s = Signal(10)
>>> other = None
>>> result = s.is_not(other)
>>> result.value
True
>>> s.value = None
>>> result.value
False
Source code in src/signified/__init__.py
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
def is_not(self, other: Any) -> Computed[bool]:
    """Return a reactive value for whether `self` is not other.

    Args:
        other: The value to compare against.

    Returns:
        A reactive value for self.value is not other.

    Example:
        ```py
        >>> s = Signal(10)
        >>> other = None
        >>> result = s.is_not(other)
        >>> result.value
        True
        >>> s.value = None
        >>> result.value
        False

        ```
    """
    return computed(operator.is_not)(self, other)

notify()

Notify all observers by calling their update method.

Source code in src/signified/__init__.py
113
114
115
def notify(self) -> None:
    """Notify all observers by calling their ``update`` method."""
    ...

where(a, b)

Return a reactive value for a if self is True, else b.

Parameters:

Name Type Description Default
a HasValue[A]

The value to return if self is True.

required
b HasValue[B]

The value to return if self is False.

required

Returns:

Type Description
Computed[A | B]

A reactive value for a if self.value else b.

Example
>>> condition = Signal(True)
>>> result = condition.where("Yes", "No")
>>> result.value
'Yes'
>>> condition.value = False
>>> result.value
'No'
Source code in src/signified/__init__.py
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
def where(self, a: HasValue[A], b: HasValue[B]) -> Computed[A | B]:
    """Return a reactive value for `a` if `self` is `True`, else `b`.

    Args:
        a: The value to return if `self` is `True`.
        b: The value to return if `self` is `False`.

    Returns:
        A reactive value for `a if self.value else b`.

    Example:
        ```py
        >>> condition = Signal(True)
        >>> result = condition.where("Yes", "No")
        >>> result.value
        'Yes'
        >>> condition.value = False
        >>> result.value
        'No'

        ```
    """

    @computed
    def ternary(a: A, b: B, self: Any) -> A | B:
        return a if self else b

    return ternary(a, b, self)

Signal

Bases: Variable[NestedValue[T], T]

A container that holds a reactive value.

Note

A Signal is a Generic container with type T. T is defined as the type that would be returned by signal.value which automatically handles unnesting reactive values. For example the below expression would be inferred by pyright to be of type Signal[str].

Signal(Signal(Signal("abc")))  # Signal[str]

Parameters:

Name Type Description Default
value NestedValue[T]

The initial value of the signal, which can be a nested structure.

required

Attributes:

Name Type Description
_value NestedValue[T]

The current value of the signal.

Source code in src/signified/__init__.py
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
class Signal(Variable[NestedValue[T], T]):
    """A container that holds a reactive value.

    Note:
        A Signal is a Generic container with type ``T``. ``T`` is defined as the type
        that would be returned by ``signal.value`` which automatically handles
        unnesting reactive values. For example the below expression would be
        inferred by ``pyright`` to be of type ``Signal[str]``.
        ```py
        Signal(Signal(Signal("abc")))  # Signal[str]
        ```

    Args:
        value: The initial value of the signal, which can be a nested structure.

    Attributes:
        _value (NestedValue[T]): The current value of the signal.
    """

    def __init__(self, value: NestedValue[T]) -> None:
        super().__init__()
        self._value: T = cast(T, value)
        self.observe(value)

    @property
    def value(self) -> T:
        """Get or set the current value.

        When setting a value, observers will be notified if the value has changed.

        Returns:
            The current value (when getting).
        """
        return unref(self._value)

    @value.setter
    def value(self, new_value: HasValue[T]) -> None:
        old_value = self._value
        change = new_value != old_value
        if isinstance(change, np.ndarray):
            change = change.any()
        elif callable(old_value):
            change = True
        if change:
            self._value = cast(T, new_value)
            self.unobserve(old_value)
            self.observe(new_value)
            self.notify()

    @contextmanager
    def at(self, value: T) -> Generator[None, None, None]:
        """Temporarily set the signal to a given value within a context.

        Args:
            value: The temporary value to set.

        Yields:
            None

        Example:
            ```py
            >>> x = Signal(2)
            >>> x_plus_2 = x + 2
            >>> x_plus_2.value
            4
            >>> with x.at(8):
            ...     x_plus_2.value
            10

            ```
        """
        before = self.value
        try:
            before = self.value
            self.value = value
            yield
        finally:
            self.value = before

    def update(self) -> None:
        """Update the signal and notify subscribers."""
        self.notify()

value: T property writable

Get or set the current value.

When setting a value, observers will be notified if the value has changed.

Returns:

Type Description
T

The current value (when getting).

at(value)

Temporarily set the signal to a given value within a context.

Parameters:

Name Type Description Default
value T

The temporary value to set.

required

Yields:

Type Description
None

None

Example
>>> x = Signal(2)
>>> x_plus_2 = x + 2
>>> x_plus_2.value
4
>>> with x.at(8):
...     x_plus_2.value
10
Source code in src/signified/__init__.py
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
@contextmanager
def at(self, value: T) -> Generator[None, None, None]:
    """Temporarily set the signal to a given value within a context.

    Args:
        value: The temporary value to set.

    Yields:
        None

    Example:
        ```py
        >>> x = Signal(2)
        >>> x_plus_2 = x + 2
        >>> x_plus_2.value
        4
        >>> with x.at(8):
        ...     x_plus_2.value
        10

        ```
    """
    before = self.value
    try:
        before = self.value
        self.value = value
        yield
    finally:
        self.value = before

update()

Update the signal and notify subscribers.

Source code in src/signified/__init__.py
1502
1503
1504
def update(self) -> None:
    """Update the signal and notify subscribers."""
    self.notify()

Variable

Bases: ABC, _HasValue[Y], ReactiveMixIn[T]

An abstract base class for reactive values.

A reactive value is an object that can be observed by observer for changes and can notify observers when its value changes. This class implements both the observer and observable patterns.

This class implements both the observer and observable pattern.

Subclasses should implement the update method.

Attributes:

Name Type Description
_observers list[Observer]

List of observers subscribed to this variable.

Source code in src/signified/__init__.py
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
class Variable(ABC, _HasValue[Y], ReactiveMixIn[T]):  # type: ignore[misc]
    """An abstract base class for reactive values.

    A reactive value is an object that can be observed by observer for changes and
    can notify observers when its value changes. This class implements both the observer
    and observable patterns.

    This class implements both the observer and observable pattern.

    Subclasses should implement the `update` method.

    Attributes:
        _observers (list[Observer]): List of observers subscribed to this variable.
    """

    def __init__(self):
        """Initialize the variable."""
        self._observers: list[Observer] = []

    def subscribe(self, observer: Observer) -> None:
        """Subscribe an observer to this variable.

        Args:
            observer: The observer to subscribe.
        """
        if observer not in self._observers:
            self._observers.append(observer)

    def unsubscribe(self, observer: Observer) -> None:
        """Unsubscribe an observer from this variable.

        Args:
            observer: The observer to unsubscribe.
        """
        if observer in self._observers:
            self._observers.remove(observer)

    def observe(self, items: Any) -> Self:
        """Subscribe the observer (`self`) to all items that are Observable.

        This method handles arbitrarily nested iterables.

        Args:
            items: A single item, an iterable, or a nested structure of items to potentially subscribe to.

        Returns:
            self
        """

        def _observe(item: Any) -> None:
            if isinstance(item, Variable) and item is not self:
                item.subscribe(self)
            elif isinstance(item, Iterable) and not isinstance(item, str):
                for sub_item in item:
                    _observe(sub_item)

        _observe(items)
        return self

    def unobserve(self, items: Any) -> Self:
        """Unsubscribe the observer (`self`) from all items that are Observable.

        Args:
            items: A single item or an iterable of items to potentially unsubscribe from.

        Returns:
            self
        """

        def _unobserve(item: Any) -> None:
            if isinstance(item, Variable) and item is not self:
                item.subscribe(self)
            elif isinstance(item, Iterable) and not isinstance(item, str):
                for sub_item in item:
                    _unobserve(sub_item)

        _unobserve(items)
        return self

    def notify(self) -> None:
        """Notify all observers by calling their update method."""
        for observer in self._observers:
            observer.update()

    def __repr__(self) -> str:
        """Represent the object in a way that shows the inner value."""
        return f"<{self.value}>"

    @abstractmethod
    def update(self) -> None:
        """Update method to be overridden by subclasses.

        Raises:
            NotImplementedError: If not overridden by a subclass.
        """
        raise NotImplementedError("Update method should be overridden by subclasses")

    def _ipython_display_(self) -> None:
        handle = display(self.value, display_id=True)
        assert handle is not None
        IPythonObserver(self, handle)

__init__()

Initialize the variable.

Source code in src/signified/__init__.py
1335
1336
1337
def __init__(self):
    """Initialize the variable."""
    self._observers: list[Observer] = []

__repr__()

Represent the object in a way that shows the inner value.

Source code in src/signified/__init__.py
1404
1405
1406
def __repr__(self) -> str:
    """Represent the object in a way that shows the inner value."""
    return f"<{self.value}>"

notify()

Notify all observers by calling their update method.

Source code in src/signified/__init__.py
1399
1400
1401
1402
def notify(self) -> None:
    """Notify all observers by calling their update method."""
    for observer in self._observers:
        observer.update()

observe(items)

Subscribe the observer (self) to all items that are Observable.

This method handles arbitrarily nested iterables.

Parameters:

Name Type Description Default
items Any

A single item, an iterable, or a nested structure of items to potentially subscribe to.

required

Returns:

Type Description
Self

self

Source code in src/signified/__init__.py
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
def observe(self, items: Any) -> Self:
    """Subscribe the observer (`self`) to all items that are Observable.

    This method handles arbitrarily nested iterables.

    Args:
        items: A single item, an iterable, or a nested structure of items to potentially subscribe to.

    Returns:
        self
    """

    def _observe(item: Any) -> None:
        if isinstance(item, Variable) and item is not self:
            item.subscribe(self)
        elif isinstance(item, Iterable) and not isinstance(item, str):
            for sub_item in item:
                _observe(sub_item)

    _observe(items)
    return self

subscribe(observer)

Subscribe an observer to this variable.

Parameters:

Name Type Description Default
observer Observer

The observer to subscribe.

required
Source code in src/signified/__init__.py
1339
1340
1341
1342
1343
1344
1345
1346
def subscribe(self, observer: Observer) -> None:
    """Subscribe an observer to this variable.

    Args:
        observer: The observer to subscribe.
    """
    if observer not in self._observers:
        self._observers.append(observer)

unobserve(items)

Unsubscribe the observer (self) from all items that are Observable.

Parameters:

Name Type Description Default
items Any

A single item or an iterable of items to potentially unsubscribe from.

required

Returns:

Type Description
Self

self

Source code in src/signified/__init__.py
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
def unobserve(self, items: Any) -> Self:
    """Unsubscribe the observer (`self`) from all items that are Observable.

    Args:
        items: A single item or an iterable of items to potentially unsubscribe from.

    Returns:
        self
    """

    def _unobserve(item: Any) -> None:
        if isinstance(item, Variable) and item is not self:
            item.subscribe(self)
        elif isinstance(item, Iterable) and not isinstance(item, str):
            for sub_item in item:
                _unobserve(sub_item)

    _unobserve(items)
    return self

unsubscribe(observer)

Unsubscribe an observer from this variable.

Parameters:

Name Type Description Default
observer Observer

The observer to unsubscribe.

required
Source code in src/signified/__init__.py
1348
1349
1350
1351
1352
1353
1354
1355
def unsubscribe(self, observer: Observer) -> None:
    """Unsubscribe an observer from this variable.

    Args:
        observer: The observer to unsubscribe.
    """
    if observer in self._observers:
        self._observers.remove(observer)

update() abstractmethod

Update method to be overridden by subclasses.

Raises:

Type Description
NotImplementedError

If not overridden by a subclass.

Source code in src/signified/__init__.py
1408
1409
1410
1411
1412
1413
1414
1415
@abstractmethod
def update(self) -> None:
    """Update method to be overridden by subclasses.

    Raises:
        NotImplementedError: If not overridden by a subclass.
    """
    raise NotImplementedError("Update method should be overridden by subclasses")

as_signal(val)

Convert a value to a Signal if it's not already a reactive value.

Parameters:

Name Type Description Default
val HasValue[T]

The value to convert.

required

Returns:

Type Description
Signal[T]

The value as a Signal or the original reactive value.

Example

```py

as_signal(2) <2> as_signal(Signal(2)) <2>

Source code in src/signified/__init__.py
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
def as_signal(val: HasValue[T]) -> Signal[T]:
    """Convert a value to a [`Signal`][signified.Signal] if it's not already a reactive value.

    Args:
        val: The value to convert.

    Returns:
        The value as a [`Signal`][signified.Signal] or the original reactive value.

    Example:
        ```py
        >>> as_signal(2)
        <2>
        >>> as_signal(Signal(2))
        <2>
    """
    return cast(Signal[T], val) if isinstance(val, Variable) else Signal(val)

computed(func)

Decorate the function to return a reactive value.

Parameters:

Name Type Description Default
func Callable[..., R]

The function to compute the value.

required

Returns:

Type Description
Callable[..., Computed[R]]

A function that returns a reactive value.

Examples:

>>> x = Signal([1,2,3])
>>> sum_x = computed(sum)(x)
>>> sum_x
<6>
>>> x.value = range(10)
>>> sum_x
<45>
>>> @computed
... def my_add(x, y):
...     return x + y
>>> x = Signal(2)
>>> my_add(x, 10)
<12>
Source code in src/signified/__init__.py
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
def computed(func: Callable[..., R]) -> Callable[..., Computed[R]]:
    """Decorate the function to return a reactive value.

    Args:
        func: The function to compute the value.

    Returns:
        A function that returns a reactive value.

    Examples:
        ```py
        >>> x = Signal([1,2,3])
        >>> sum_x = computed(sum)(x)
        >>> sum_x
        <6>
        >>> x.value = range(10)
        >>> sum_x
        <45>

        ```

        ```py
        >>> @computed
        ... def my_add(x, y):
        ...     return x + y
        >>> x = Signal(2)
        >>> my_add(x, 10)
        <12>

        ```
    """

    @wraps(func)
    def wrapper(*args: Any, **kwargs: Any) -> Computed[R]:
        def compute_func() -> R:
            resolved_args = tuple(unref(arg) for arg in args)
            resolved_kwargs = {key: unref(value) for key, value in kwargs.items()}
            return func(*resolved_args, **resolved_kwargs)

        return Computed(compute_func, (*args, *kwargs.values()))

    return wrapper

has_value(obj, type_)

Check if an object has a value of a specific type.

Note

This serves as a TypeGuard to help support type narrowing.

Parameters:

Name Type Description Default
obj Any

The object to check.

required
type_ type[T]

The type to check against.

required

Returns:

Type Description
TypeGuard[HasValue[T]]

True if the object has a value of the specified type.

Example
>>> has_value(Signal("abc"), str)
True
Source code in src/signified/__init__.py
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
def has_value(obj: Any, type_: type[T]) -> TypeGuard[HasValue[T]]:
    """Check if an object has a value of a specific type.

    Note:
        This serves as a TypeGuard to help support type narrowing.

    Args:
        obj: The object to check.
        type_: The type to check against.

    Returns:
        True if the object has a value of the specified type.

    Example:
        ```py
        >>> has_value(Signal("abc"), str)
        True

        ```
    """
    return isinstance(unref(obj), type_)

reactive_method(*dep_names)

Decorate the method to return a reactive value.

Parameters:

Name Type Description Default
*dep_names str

Names of object attributes to track as dependencies.

()

Returns:

Type Description
Callable[[Callable[..., T]], Callable[..., Computed[T]]]

A decorator function.

Source code in src/signified/__init__.py
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
def reactive_method(*dep_names: str) -> Callable[[Callable[..., T]], Callable[..., Computed[T]]]:
    """Decorate the method to return a reactive value.

    Args:
        *dep_names: Names of object attributes to track as dependencies.

    Returns:
        A decorator function.
    """

    def decorator(func: Callable[..., T]) -> Callable[..., Computed[T]]:
        @wraps(func)
        def wrapper(self: Any, *args: Any, **kwargs: Any) -> Computed[T]:
            object_deps = [getattr(self, name) for name in dep_names if hasattr(self, name)]
            all_deps = (*object_deps, *args, *kwargs.values())
            return Computed(lambda: func(self, *args, **kwargs), all_deps)

        return wrapper

    return decorator

unref(value)

Dereference a value, resolving any nested reactive variables.

Parameters:

Name Type Description Default
value HasValue[T]

The value to dereference.

required

Returns:

Type Description
T

The dereferenced value.

Example

```py

x = Signal(Signal(Signal(2))) unref(x) 2

Source code in src/signified/__init__.py
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
def unref(value: HasValue[T]) -> T:
    """Dereference a value, resolving any nested reactive variables.

    Args:
        value: The value to dereference.

    Returns:
        The dereferenced value.

    Example:
        ```py
        >>> x = Signal(Signal(Signal(2)))
        >>> unref(x)
        2
    """
    while isinstance(value, Variable):
        value = value._value
    return cast(T, value)