Skip to content

Either

Either values.

The Either[L, R] type is symmetric and treats its variants the same way, without preference. For representing results (values and errors), use the Result[T, E] instead.

Either = Union[Left[L], Right[R]] module-attribute

Either value, expressed as the union of Left[L] and Right[R].

Left

Bases: EitherProtocol[L, Never]

Left[L] variant of Either[L, R].

Source code in src/wraps/either.py
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
@final
@frozen()
class Left(EitherProtocol[L, Never]):
    """[`Left[L]`][wraps.either.Left] variant of [`Either[L, R]`][wraps.either.Either]."""

    value: L

    def __repr__(self) -> str:
        return wrap_repr(self, self.value)

    @classmethod
    def create(cls, value: M) -> Left[M]:
        return cls(value)  # type: ignore[arg-type, return-value]

    def is_left(self) -> Literal[True]:
        return True

    def is_left_and(self, predicate: Predicate[L]) -> bool:
        return predicate(self.value)

    async def is_left_and_await(self, predicate: AsyncPredicate[L]) -> bool:
        return await predicate(self.value)

    def is_right(self) -> Literal[False]:
        return False

    def is_right_and(self, predicate: Predicate[R]) -> Literal[False]:
        return False

    async def is_right_and_await(self, predicate: AsyncPredicate[R]) -> Literal[False]:
        return False

    def left(self) -> Some[L]:
        return Some(self.value)

    def left_or(self, default: L) -> L:  # type: ignore[misc]
        return self.value

    def left_or_else(self, default: Nullary[L]) -> L:
        return self.value

    async def left_or_else_await(self, default: AsyncNullary[L]) -> L:
        return self.value

    def right(self) -> Null:
        return NULL

    def right_or(self, default: R) -> R:  # type: ignore[misc]
        return default

    def right_or_else(self, default: Nullary[R]) -> R:
        return default()

    async def right_or_else_await(self, default: AsyncNullary[R]) -> R:
        return await default()

    def expect_left(self, message: str) -> L:
        return self.value

    def expect_right(self, message: str) -> Never:
        panic(message)

    def unwrap_left(self) -> L:
        return self.value

    def unwrap_right(self) -> Never:
        panic(UNWRAP_RIGHT_ON_LEFT)

    def into_either(self: Left[T]) -> T:
        return self.value

    def inspect_left(self, function: Inspect[L]) -> Left[L]:
        function(self.value)

        return self

    def inspect_right(self, function: Inspect[R]) -> Left[L]:
        return self

    async def inspect_left_await(self, function: AsyncInspect[L]) -> Left[L]:
        await function(self.value)

        return self

    async def inspect_right_await(self, function: AsyncInspect[R]) -> Left[L]:
        return self

    def flip(self) -> Right[L]:
        return Right(self.value)

    def map_left(self, function: Unary[L, M]) -> Left[M]:
        return self.create(function(self.value))

    async def map_left_await(self, function: AsyncUnary[L, M]) -> Left[M]:
        return self.create(await function(self.value))

    def map_right(self, function: Unary[R, S]) -> Left[L]:
        return self

    async def map_right_await(self, function: AsyncUnary[R, S]) -> Left[L]:
        return self

    def map(self: Left[T], function: Unary[T, U]) -> Left[U]:
        return self.create(function(self.value))

    async def map_await(self: Left[T], function: AsyncUnary[T, U]) -> Left[U]:
        return self.create(await function(self.value))

    def map_either(self, left: Unary[L, M], right: Unary[R, S]) -> Left[M]:
        return self.create(left(self.value))

    async def map_either_await(self, left: AsyncUnary[L, M], right: Unary[R, S]) -> Left[M]:
        return self.create(await left(self.value))

    def either(self, left: Unary[L, T], right: Unary[R, T]) -> T:
        return left(self.value)

    async def either_await(self, left: AsyncUnary[L, T], right: AsyncUnary[R, T]) -> T:
        return await left(self.value)

    def left_and_then(self, function: Unary[L, Either[M, R]]) -> Either[M, R]:
        return function(self.value)

    async def left_and_then_await(self, function: AsyncUnary[L, Either[M, R]]) -> Either[M, R]:
        return await function(self.value)

    def right_and_then(self, function: Unary[R, Either[L, S]]) -> Left[L]:
        return self

    async def right_and_then_await(self, function: AsyncUnary[R, Either[L, S]]) -> Left[L]:
        return self

    def iter_left(self) -> Iterator[L]:
        return once(self.value)

    def iter_right(self) -> Iterator[Never]:
        return empty()

    def iter_either(self: Left[T]) -> Iterator[T]:
        return once(self.value)

    def async_iter_left(self) -> AsyncIterator[L]:
        return async_once(self.value)

    def async_iter_right(self) -> AsyncIterator[Never]:
        return async_empty()

    def async_iter_either(self: Left[T]) -> AsyncIterator[T]:
        return async_once(self.value)

    def contains_left(self, value: M) -> bool:
        return self.value == value

    def contains_right(self, value: S) -> Literal[False]:
        return False

    def contains(self: Left[T], value: U) -> bool:
        return self.value == value

    def into_result(self) -> Ok[L]:
        return Ok(self.value)

Right

Bases: EitherProtocol[Never, R]

Right[R] variant of Either[L, R].

Source code in src/wraps/either.py
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
@final
@frozen()
class Right(EitherProtocol[Never, R]):
    """[`Right[R]`][wraps.either.Right] variant of [`Either[L, R]`][wraps.either.Either]."""

    value: R

    def __repr__(self) -> str:
        return wrap_repr(self, self.value)

    @classmethod
    def create(cls, value: S) -> Right[S]:
        return cls(value)  # type: ignore[arg-type, return-value]

    def is_left(self) -> Literal[False]:
        return False

    def is_left_and(self, predicate: Predicate[L]) -> Literal[False]:
        return False

    async def is_left_and_await(self, predicate: AsyncPredicate[L]) -> Literal[False]:
        return False

    def is_right(self) -> Literal[True]:
        return True

    def is_right_and(self, predicate: Predicate[R]) -> bool:
        return predicate(self.value)

    async def is_right_and_await(self, predicate: AsyncPredicate[R]) -> bool:
        return await predicate(self.value)

    def left(self) -> Null:
        return NULL

    def left_or(self, default: L) -> L:  # type: ignore[misc]
        return default

    def left_or_else(self, default: Nullary[L]) -> L:
        return default()

    async def left_or_else_await(self, default: AsyncNullary[L]) -> L:
        return await default()

    def right(self) -> Some[R]:
        return Some(self.value)

    def right_or(self, default: R) -> R:  # type: ignore[misc]
        return self.value

    def right_or_else(self, default: Nullary[R]) -> R:
        return self.value

    async def right_or_else_await(self, default: AsyncNullary[R]) -> R:
        return self.value

    def expect_left(self, message: str) -> Never:
        panic(message)

    def expect_right(self, message: str) -> R:
        return self.value

    def unwrap_left(self) -> Never:
        panic(UNWRAP_LEFT_ON_RIGHT)

    def unwrap_right(self) -> R:
        return self.value

    def into_either(self: Right[T]) -> T:
        return self.value

    def inspect_left(self, function: Inspect[L]) -> Right[R]:
        return self

    def inspect_right(self, function: Inspect[R]) -> Right[R]:
        function(self.value)

        return self

    async def inspect_left_await(self, function: AsyncInspect[L]) -> Right[R]:
        return self

    async def inspect_right_await(self, function: AsyncInspect[R]) -> Right[R]:
        await function(self.value)

        return self

    def flip(self) -> Left[R]:
        return Left(self.value)

    def map_left(self, function: Unary[L, M]) -> Right[R]:
        return self

    async def map_left_await(self, function: AsyncUnary[L, M]) -> Right[R]:
        return self

    def map_right(self, function: Unary[R, S]) -> Right[S]:
        return self.create(function(self.value))

    async def map_right_await(self, function: AsyncUnary[R, S]) -> Right[S]:
        return self.create(await function(self.value))

    def map(self: Right[T], function: Unary[T, U]) -> Right[U]:
        return self.create(function(self.value))

    async def map_await(self: Right[T], function: AsyncUnary[T, U]) -> Right[U]:
        return self.create(await function(self.value))

    def map_either(self, left: Unary[L, M], right: Unary[R, S]) -> Right[S]:
        return self.create(right(self.value))

    async def map_either_await(self, left: AsyncUnary[L, M], right: AsyncUnary[R, S]) -> Right[S]:
        return self.create(await right(self.value))

    def either(self, left: Unary[L, T], right: Unary[R, T]) -> T:
        return right(self.value)

    async def either_await(self, left: AsyncUnary[L, T], right: AsyncUnary[R, T]) -> T:
        return await right(self.value)

    def left_and_then(self, function: Unary[L, Either[M, R]]) -> Right[R]:
        return self

    async def left_and_then_await(self, function: AsyncUnary[L, Either[M, R]]) -> Right[R]:
        return self

    def right_and_then(self, function: Unary[R, Either[L, S]]) -> Either[L, S]:
        return function(self.value)

    async def right_and_then_await(self, function: AsyncUnary[R, Either[L, S]]) -> Either[L, S]:
        return await function(self.value)

    def iter_left(self) -> Iterator[Never]:
        return empty()

    def iter_right(self) -> Iterator[R]:
        return once(self.value)

    def iter_either(self: Right[T]) -> Iterator[T]:
        return once(self.value)

    def async_iter_left(self) -> AsyncIterator[Never]:
        return async_empty()

    def async_iter_right(self) -> AsyncIterator[R]:
        return async_once(self.value)

    def async_iter_either(self: Right[T]) -> AsyncIterator[T]:
        return async_once(self.value)

    def contains_left(self, value: M) -> Literal[False]:
        return False

    def contains_right(self, value: S) -> bool:
        return self.value == value

    def contains(self: Right[T], value: U) -> bool:
        return self.value == value

    def into_result(self) -> Err[R]:
        return Err(self.value)