@@ -103,37 +103,37 @@ def __init__(self, result: T) -> None:
103
103
"""Wrap a result."""
104
104
self ._value : T = result
105
105
106
- def and_ (self , res : "_Result [U, E]" ) -> "_Result [U, E]" :
106
+ def and_ (self , res : "Result [U, E]" ) -> "Result [U, E]" :
107
107
"""Return `res` if the result is `Ok`, otherwise return `self`."""
108
108
return res
109
109
110
- def or_ (self , res : "_Result [T, F]" ) -> "_Result [T, F]" :
110
+ def or_ (self , res : "Result [T, F]" ) -> "Result [T, F]" :
111
111
"""Return `res` if the result is `Err`, otherwise `self`."""
112
112
return t .cast (Result [T , F ], self )
113
113
114
- def and_then (self , fn : t .Callable [[T ], "_Result [U, E]" ]) -> "_Result [U, E]" :
114
+ def and_then (self , fn : t .Callable [[T ], "Result [U, E]" ]) -> "Result [U, E]" :
115
115
"""Call `fn` if Ok, or ignore an error.
116
116
117
117
This can be used to chain functions that return results.
118
118
"""
119
119
return fn (self ._value )
120
120
121
- def flatmap (self , fn : t .Callable [[T ], "_Result [U, E]" ]) -> "_Result [U, E]" :
121
+ def flatmap (self , fn : t .Callable [[T ], "Result [U, E]" ]) -> "Result [U, E]" :
122
122
"""Call `fn` if Ok, or ignore an error.
123
123
124
124
This can be used to chain functions that return results.
125
125
"""
126
126
return self .and_then (fn )
127
127
128
- def or_else (self , fn : t .Callable [[E ], "_Result [T, F]" ]) -> "_Result [T, F]" :
128
+ def or_else (self , fn : t .Callable [[E ], "Result [T, F]" ]) -> "Result [T, F]" :
129
129
"""Return `self` if `Ok`, or call `fn` with `self` if `Err`."""
130
130
return t .cast (Result [T , F ], self )
131
131
132
- def err (self ) -> _Option [E ]:
132
+ def err (self ) -> Option [E ]:
133
133
"""Return Err value if result is Err."""
134
134
return Nothing ()
135
135
136
- def ok (self ) -> _Option [T ]:
136
+ def ok (self ) -> Option [T ]:
137
137
"""Return OK value if result is Ok."""
138
138
return Some (self ._value )
139
139
@@ -170,11 +170,11 @@ def iter(self) -> t.Iterator[T]:
170
170
"""
171
171
return iter (self )
172
172
173
- def map (self , fn : t .Callable [[T ], U ]) -> "_Result [U, E]" :
173
+ def map (self , fn : t .Callable [[T ], U ]) -> "Result [U, E]" :
174
174
"""Map a function onto an okay result, or ignore an error."""
175
175
return Ok (fn (self ._value ))
176
176
177
- def map_err (self , fn : t .Callable [[E ], F ]) -> "_Result [T, F]" :
177
+ def map_err (self , fn : t .Callable [[E ], F ]) -> "Result [T, F]" :
178
178
"""Map a function onto an error, or ignore a success."""
179
179
return t .cast (Result [T , F ], self )
180
180
@@ -232,37 +232,37 @@ def __init__(self, result: E) -> None:
232
232
"""Wrap a result."""
233
233
self ._value = result
234
234
235
- def and_ (self , res : "_Result [U, E]" ) -> "_Result [U, E]" :
235
+ def and_ (self , res : "Result [U, E]" ) -> "Result [U, E]" :
236
236
"""Return `res` if the result is `Ok`, otherwise return `self`."""
237
237
return t .cast (Result [U , E ], self )
238
238
239
- def or_ (self , res : "_Result [T, F]" ) -> "_Result [T, F]" :
239
+ def or_ (self , res : "Result [T, F]" ) -> "Result [T, F]" :
240
240
"""Return `res` if the result is `Err`, otherwise `self`."""
241
241
return res
242
242
243
- def and_then (self , fn : t .Callable [[T ], "_Result [U, E]" ]) -> "_Result [U, E]" :
243
+ def and_then (self , fn : t .Callable [[T ], "Result [U, E]" ]) -> "Result [U, E]" :
244
244
"""Call `fn` if Ok, or ignore an error.
245
245
246
246
This can be used to chain functions that return results.
247
247
"""
248
248
return t .cast (Result [U , E ], self )
249
249
250
- def flatmap (self , fn : t .Callable [[T ], "_Result [U, E]" ]) -> "_Result [U, E]" :
250
+ def flatmap (self , fn : t .Callable [[T ], "Result [U, E]" ]) -> "Result [U, E]" :
251
251
"""Call `fn` if Ok, or ignore an error.
252
252
253
253
This can be used to chain functions that return results.
254
254
"""
255
- return self .and_then (fn )
255
+ return t . cast ( Result [ U , E ], self .and_then (fn ) )
256
256
257
- def or_else (self , fn : t .Callable [[E ], "_Result [T, F]" ]) -> "_Result [T, F]" :
257
+ def or_else (self , fn : t .Callable [[E ], "Result [T, F]" ]) -> "Result [T, F]" :
258
258
"""Return `self` if `Ok`, or call `fn` with `self` if `Err`."""
259
259
return fn (self ._value )
260
260
261
- def err (self ) -> _Option [E ]:
261
+ def err (self ) -> Option [E ]:
262
262
"""Return Err value if result is Err."""
263
263
return Some (self ._value )
264
264
265
- def ok (self ) -> _Option [T ]:
265
+ def ok (self ) -> Option [T ]:
266
266
"""Return OK value if result is Ok."""
267
267
return Nothing ()
268
268
@@ -299,11 +299,11 @@ def iter(self) -> t.Iterator[T]:
299
299
"""
300
300
return iter (self )
301
301
302
- def map (self , fn : t .Callable [[T ], U ]) -> "_Result [U, E]" :
302
+ def map (self , fn : t .Callable [[T ], U ]) -> "Result [U, E]" :
303
303
"""Map a function onto an okay result, or ignore an error."""
304
304
return t .cast (Result [U , E ], self )
305
305
306
- def map_err (self , fn : t .Callable [[E ], F ]) -> "_Result [T, F]" :
306
+ def map_err (self , fn : t .Callable [[E ], F ]) -> "Result [T, F]" :
307
307
"""Map a function onto an error, or ignore a success."""
308
308
return Err (fn (self ._value ))
309
309
@@ -363,29 +363,29 @@ def __init__(self, value: T) -> None:
363
363
# not sure why pylint things _value is not in __slots__
364
364
self ._value = value # pylint: disable=assigning-non-slot
365
365
366
- def and_ (self , alternative : _Option [U ]) -> _Option [U ]:
366
+ def and_ (self , alternative : Option [U ]) -> Option [U ]:
367
367
"""Return `Nothing` if `self` is `Nothing`, or the `alternative`."""
368
368
return alternative
369
369
370
- def or_ (self , alternative : _Option [T ]) -> _Option [T ]:
370
+ def or_ (self , alternative : Option [T ]) -> Option [T ]:
371
371
"""Return option if it is `Some`, or the `alternative`."""
372
372
return self
373
373
374
- def xor (self , alternative : _Option [T ]) -> _Option [T ]:
374
+ def xor (self , alternative : Option [T ]) -> Option [T ]:
375
375
"""Return Some IFF exactly one of `self`, `alternative` is `Some`."""
376
376
return (
377
- t .cast (_Option [T ], self ) if alternative .is_nothing () else Nothing ()
377
+ t .cast (Option [T ], self ) if alternative .is_nothing () else Nothing ()
378
378
)
379
379
380
- def and_then (self , fn : t .Callable [[T ], _Option [U ]]) -> _Option [U ]:
380
+ def and_then (self , fn : t .Callable [[T ], Option [U ]]) -> Option [U ]:
381
381
"""Return `Nothing`, or call `fn` with the `Some` value."""
382
382
return fn (self ._value )
383
383
384
- def flatmap (self , fn : t .Callable [[T ], "_Option [U]" ]) -> "_Option [U]" :
384
+ def flatmap (self , fn : t .Callable [[T ], Option [U ]]) -> Option [U ]:
385
385
"""Return `Nothing`, or call `fn` with the `Some` value."""
386
- return self .and_then (fn )
386
+ return t . cast ( Option [ U ], self .and_then (fn ) )
387
387
388
- def or_else (self , fn : t .Callable [[], _Option [T ]]) -> _Option [T ]:
388
+ def or_else (self , fn : t .Callable [[], Option [T ]]) -> Option [T ]:
389
389
"""Return option if it is `Some`, or calculate an alternative."""
390
390
return self
391
391
@@ -397,7 +397,7 @@ def expect(self, msg: str, exc_cls: t.Type[Exception] = RuntimeError) -> T:
397
397
"""
398
398
return self ._value
399
399
400
- def filter (self , predicate : t .Callable [[T ], bool ]) -> _Option [T ]:
400
+ def filter (self , predicate : t .Callable [[T ], bool ]) -> Option [T ]:
401
401
"""Return `Nothing`, or an option determined by the predicate.
402
402
403
403
If `self` is `Some`, call `predicate` with the wrapped value and
@@ -423,7 +423,7 @@ def iter(self) -> t.Iterator[T]:
423
423
"""Return an iterator over the possibly contained value."""
424
424
return iter (self )
425
425
426
- def map (self , fn : t .Callable [[T ], U ]) -> _Option [U ]:
426
+ def map (self , fn : t .Callable [[T ], U ]) -> Option [U ]:
427
427
"""Apply `fn` to the contained value if any."""
428
428
return Some (fn (self ._value ))
429
429
@@ -442,15 +442,15 @@ def ok_or(self, err: E) -> Result[T, E]:
442
442
443
443
Maps `Some(v)` to `Ok(v)` or `None` to `Err(err)`.
444
444
"""
445
- res : Result [T , E ] = Ok (t . cast ( T , self ._value ) )
445
+ res : Result [T , E ] = Ok (self ._value )
446
446
return res
447
447
448
448
def ok_or_else (self , err_fn : t .Callable [[], E ]) -> Result [T , E ]:
449
449
"""Transform an option into a `Result`.
450
450
451
451
Maps `Some(v)` to `Ok(v)` or `None` to `Err(err_fn())`.
452
452
"""
453
- res : Result [T , E ] = Ok (t . cast ( T , self ._value ) )
453
+ res : Result [T , E ] = Ok (self ._value )
454
454
return res
455
455
456
456
def unwrap (self ) -> T :
@@ -515,27 +515,27 @@ def __new__(cls, _: None = None) -> "Nothing[T]":
515
515
cls ._instance = inst
516
516
return t .cast ("Nothing[T]" , cls ._instance )
517
517
518
- def and_ (self , alternative : _Option [U ]) -> _Option [U ]:
518
+ def and_ (self , alternative : Option [U ]) -> Option [U ]:
519
519
"""Return `Nothing` if `self` is `Nothing`, or the `alternative`."""
520
- return t .cast (_Option [U ], self )
520
+ return t .cast (Option [U ], self )
521
521
522
- def or_ (self , alternative : _Option [T ]) -> _Option [T ]:
522
+ def or_ (self , alternative : Option [T ]) -> Option [T ]:
523
523
"""Return option if it is `Some`, or the `alternative`."""
524
524
return alternative
525
525
526
- def xor (self , alternative : _Option [T ]) -> _Option [T ]:
526
+ def xor (self , alternative : Option [T ]) -> Option [T ]:
527
527
"""Return Some IFF exactly one of `self`, `alternative` is `Some`."""
528
528
return alternative if alternative .is_some () else self
529
529
530
- def and_then (self , fn : t .Callable [[T ], _Option [U ]]) -> _Option [U ]:
530
+ def and_then (self , fn : t .Callable [[T ], Option [U ]]) -> Option [U ]:
531
531
"""Return `Nothing`, or call `fn` with the `Some` value."""
532
- return t .cast (_Option [U ], self )
532
+ return t .cast (Option [U ], self )
533
533
534
- def flatmap (self , fn : t .Callable [[T ], "_Option [U]" ]) -> "_Option [U]" :
534
+ def flatmap (self , fn : t .Callable [[T ], Option [U ]]) -> Option [U ]:
535
535
"""Return `Nothing`, or call `fn` with the `Some` value."""
536
- return self .and_then (fn )
536
+ return t . cast ( Option [ U ], self .and_then (fn ) )
537
537
538
- def or_else (self , fn : t .Callable [[], _Option [T ]]) -> _Option [T ]:
538
+ def or_else (self , fn : t .Callable [[], Option [T ]]) -> Option [T ]:
539
539
"""Return option if it is `Some`, or calculate an alternative."""
540
540
return fn ()
541
541
@@ -547,7 +547,7 @@ def expect(self, msg: str, exc_cls: t.Type[Exception] = RuntimeError) -> T:
547
547
"""
548
548
raise exc_cls (msg )
549
549
550
- def filter (self , predicate : t .Callable [[T ], bool ]) -> _Option [T ]:
550
+ def filter (self , predicate : t .Callable [[T ], bool ]) -> Option [T ]:
551
551
"""Return `Nothing`, or an option determined by the predicate.
552
552
553
553
If `self` is `Some`, call `predicate` with the wrapped value and
@@ -571,9 +571,9 @@ def iter(self) -> t.Iterator[T]:
571
571
"""Return an iterator over the possibly contained value."""
572
572
return iter (self )
573
573
574
- def map (self , fn : t .Callable [[T ], U ]) -> _Option [U ]:
574
+ def map (self , fn : t .Callable [[T ], U ]) -> Option [U ]:
575
575
"""Apply `fn` to the contained value if any."""
576
- return t .cast (_Option [U ], self )
576
+ return t .cast (Option [U ], self )
577
577
578
578
def map_or (self , default : U , fn : t .Callable [[T ], U ]) -> U :
579
579
"""Apply `fn` to contained value, or return the default."""
0 commit comments