Skip to content

Commit 1780999

Browse files
committed
ENH: improve base interface hinting w/circular ref
1 parent 3a6827a commit 1780999

File tree

4 files changed

+74
-70
lines changed

4 files changed

+74
-70
lines changed

setup.cfg

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ disallow_untyped_defs = True
1212
disallow_incomplete_defs = True
1313
disallow_untyped_decorators = True
1414
strict_optional = True
15-
warn_unused_ignores = True
15+
warn_redundant_casts = True
1616
warn_return_any = True
17+
warn_unused_ignores = True
1718

1819
[mypy-tests.*]
1920
disallow_untyped_decorators = False

src/safetywrap/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Typesafe python versions of Rust-inspired result types."""
22

33
__all__ = ("Option", "Result", "Ok", "Err", "Some", "Nothing")
4-
__version__ = "1.0.0"
4+
__version__ = "1.0.1"
55
__version_info__ = tuple(map(int, __version__.split(".")))
66

77

src/safetywrap/_impl.py

+43-43
Original file line numberDiff line numberDiff line change
@@ -103,37 +103,37 @@ def __init__(self, result: T) -> None:
103103
"""Wrap a result."""
104104
self._value: T = result
105105

106-
def and_(self, res: "_Result[U, E]") -> "_Result[U, E]":
106+
def and_(self, res: "Result[U, E]") -> "Result[U, E]":
107107
"""Return `res` if the result is `Ok`, otherwise return `self`."""
108108
return res
109109

110-
def or_(self, res: "_Result[T, F]") -> "_Result[T, F]":
110+
def or_(self, res: "Result[T, F]") -> "Result[T, F]":
111111
"""Return `res` if the result is `Err`, otherwise `self`."""
112112
return t.cast(Result[T, F], self)
113113

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]":
115115
"""Call `fn` if Ok, or ignore an error.
116116
117117
This can be used to chain functions that return results.
118118
"""
119119
return fn(self._value)
120120

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]":
122122
"""Call `fn` if Ok, or ignore an error.
123123
124124
This can be used to chain functions that return results.
125125
"""
126126
return self.and_then(fn)
127127

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]":
129129
"""Return `self` if `Ok`, or call `fn` with `self` if `Err`."""
130130
return t.cast(Result[T, F], self)
131131

132-
def err(self) -> _Option[E]:
132+
def err(self) -> Option[E]:
133133
"""Return Err value if result is Err."""
134134
return Nothing()
135135

136-
def ok(self) -> _Option[T]:
136+
def ok(self) -> Option[T]:
137137
"""Return OK value if result is Ok."""
138138
return Some(self._value)
139139

@@ -170,11 +170,11 @@ def iter(self) -> t.Iterator[T]:
170170
"""
171171
return iter(self)
172172

173-
def map(self, fn: t.Callable[[T], U]) -> "_Result[U, E]":
173+
def map(self, fn: t.Callable[[T], U]) -> "Result[U, E]":
174174
"""Map a function onto an okay result, or ignore an error."""
175175
return Ok(fn(self._value))
176176

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]":
178178
"""Map a function onto an error, or ignore a success."""
179179
return t.cast(Result[T, F], self)
180180

@@ -232,37 +232,37 @@ def __init__(self, result: E) -> None:
232232
"""Wrap a result."""
233233
self._value = result
234234

235-
def and_(self, res: "_Result[U, E]") -> "_Result[U, E]":
235+
def and_(self, res: "Result[U, E]") -> "Result[U, E]":
236236
"""Return `res` if the result is `Ok`, otherwise return `self`."""
237237
return t.cast(Result[U, E], self)
238238

239-
def or_(self, res: "_Result[T, F]") -> "_Result[T, F]":
239+
def or_(self, res: "Result[T, F]") -> "Result[T, F]":
240240
"""Return `res` if the result is `Err`, otherwise `self`."""
241241
return res
242242

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]":
244244
"""Call `fn` if Ok, or ignore an error.
245245
246246
This can be used to chain functions that return results.
247247
"""
248248
return t.cast(Result[U, E], self)
249249

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]":
251251
"""Call `fn` if Ok, or ignore an error.
252252
253253
This can be used to chain functions that return results.
254254
"""
255-
return self.and_then(fn)
255+
return t.cast(Result[U, E], self.and_then(fn))
256256

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]":
258258
"""Return `self` if `Ok`, or call `fn` with `self` if `Err`."""
259259
return fn(self._value)
260260

261-
def err(self) -> _Option[E]:
261+
def err(self) -> Option[E]:
262262
"""Return Err value if result is Err."""
263263
return Some(self._value)
264264

265-
def ok(self) -> _Option[T]:
265+
def ok(self) -> Option[T]:
266266
"""Return OK value if result is Ok."""
267267
return Nothing()
268268

@@ -299,11 +299,11 @@ def iter(self) -> t.Iterator[T]:
299299
"""
300300
return iter(self)
301301

302-
def map(self, fn: t.Callable[[T], U]) -> "_Result[U, E]":
302+
def map(self, fn: t.Callable[[T], U]) -> "Result[U, E]":
303303
"""Map a function onto an okay result, or ignore an error."""
304304
return t.cast(Result[U, E], self)
305305

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]":
307307
"""Map a function onto an error, or ignore a success."""
308308
return Err(fn(self._value))
309309

@@ -363,29 +363,29 @@ def __init__(self, value: T) -> None:
363363
# not sure why pylint things _value is not in __slots__
364364
self._value = value # pylint: disable=assigning-non-slot
365365

366-
def and_(self, alternative: _Option[U]) -> _Option[U]:
366+
def and_(self, alternative: Option[U]) -> Option[U]:
367367
"""Return `Nothing` if `self` is `Nothing`, or the `alternative`."""
368368
return alternative
369369

370-
def or_(self, alternative: _Option[T]) -> _Option[T]:
370+
def or_(self, alternative: Option[T]) -> Option[T]:
371371
"""Return option if it is `Some`, or the `alternative`."""
372372
return self
373373

374-
def xor(self, alternative: _Option[T]) -> _Option[T]:
374+
def xor(self, alternative: Option[T]) -> Option[T]:
375375
"""Return Some IFF exactly one of `self`, `alternative` is `Some`."""
376376
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()
378378
)
379379

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]:
381381
"""Return `Nothing`, or call `fn` with the `Some` value."""
382382
return fn(self._value)
383383

384-
def flatmap(self, fn: t.Callable[[T], "_Option[U]"]) -> "_Option[U]":
384+
def flatmap(self, fn: t.Callable[[T], Option[U]]) -> Option[U]:
385385
"""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))
387387

388-
def or_else(self, fn: t.Callable[[], _Option[T]]) -> _Option[T]:
388+
def or_else(self, fn: t.Callable[[], Option[T]]) -> Option[T]:
389389
"""Return option if it is `Some`, or calculate an alternative."""
390390
return self
391391

@@ -397,7 +397,7 @@ def expect(self, msg: str, exc_cls: t.Type[Exception] = RuntimeError) -> T:
397397
"""
398398
return self._value
399399

400-
def filter(self, predicate: t.Callable[[T], bool]) -> _Option[T]:
400+
def filter(self, predicate: t.Callable[[T], bool]) -> Option[T]:
401401
"""Return `Nothing`, or an option determined by the predicate.
402402
403403
If `self` is `Some`, call `predicate` with the wrapped value and
@@ -423,7 +423,7 @@ def iter(self) -> t.Iterator[T]:
423423
"""Return an iterator over the possibly contained value."""
424424
return iter(self)
425425

426-
def map(self, fn: t.Callable[[T], U]) -> _Option[U]:
426+
def map(self, fn: t.Callable[[T], U]) -> Option[U]:
427427
"""Apply `fn` to the contained value if any."""
428428
return Some(fn(self._value))
429429

@@ -442,15 +442,15 @@ def ok_or(self, err: E) -> Result[T, E]:
442442
443443
Maps `Some(v)` to `Ok(v)` or `None` to `Err(err)`.
444444
"""
445-
res: Result[T, E] = Ok(t.cast(T, self._value))
445+
res: Result[T, E] = Ok(self._value)
446446
return res
447447

448448
def ok_or_else(self, err_fn: t.Callable[[], E]) -> Result[T, E]:
449449
"""Transform an option into a `Result`.
450450
451451
Maps `Some(v)` to `Ok(v)` or `None` to `Err(err_fn())`.
452452
"""
453-
res: Result[T, E] = Ok(t.cast(T, self._value))
453+
res: Result[T, E] = Ok(self._value)
454454
return res
455455

456456
def unwrap(self) -> T:
@@ -515,27 +515,27 @@ def __new__(cls, _: None = None) -> "Nothing[T]":
515515
cls._instance = inst
516516
return t.cast("Nothing[T]", cls._instance)
517517

518-
def and_(self, alternative: _Option[U]) -> _Option[U]:
518+
def and_(self, alternative: Option[U]) -> Option[U]:
519519
"""Return `Nothing` if `self` is `Nothing`, or the `alternative`."""
520-
return t.cast(_Option[U], self)
520+
return t.cast(Option[U], self)
521521

522-
def or_(self, alternative: _Option[T]) -> _Option[T]:
522+
def or_(self, alternative: Option[T]) -> Option[T]:
523523
"""Return option if it is `Some`, or the `alternative`."""
524524
return alternative
525525

526-
def xor(self, alternative: _Option[T]) -> _Option[T]:
526+
def xor(self, alternative: Option[T]) -> Option[T]:
527527
"""Return Some IFF exactly one of `self`, `alternative` is `Some`."""
528528
return alternative if alternative.is_some() else self
529529

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]:
531531
"""Return `Nothing`, or call `fn` with the `Some` value."""
532-
return t.cast(_Option[U], self)
532+
return t.cast(Option[U], self)
533533

534-
def flatmap(self, fn: t.Callable[[T], "_Option[U]"]) -> "_Option[U]":
534+
def flatmap(self, fn: t.Callable[[T], Option[U]]) -> Option[U]:
535535
"""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))
537537

538-
def or_else(self, fn: t.Callable[[], _Option[T]]) -> _Option[T]:
538+
def or_else(self, fn: t.Callable[[], Option[T]]) -> Option[T]:
539539
"""Return option if it is `Some`, or calculate an alternative."""
540540
return fn()
541541

@@ -547,7 +547,7 @@ def expect(self, msg: str, exc_cls: t.Type[Exception] = RuntimeError) -> T:
547547
"""
548548
raise exc_cls(msg)
549549

550-
def filter(self, predicate: t.Callable[[T], bool]) -> _Option[T]:
550+
def filter(self, predicate: t.Callable[[T], bool]) -> Option[T]:
551551
"""Return `Nothing`, or an option determined by the predicate.
552552
553553
If `self` is `Some`, call `predicate` with the wrapped value and
@@ -571,9 +571,9 @@ def iter(self) -> t.Iterator[T]:
571571
"""Return an iterator over the possibly contained value."""
572572
return iter(self)
573573

574-
def map(self, fn: t.Callable[[T], U]) -> _Option[U]:
574+
def map(self, fn: t.Callable[[T], U]) -> Option[U]:
575575
"""Apply `fn` to the contained value if any."""
576-
return t.cast(_Option[U], self)
576+
return t.cast(Option[U], self)
577577

578578
def map_or(self, default: U, fn: t.Callable[[T], U]) -> U:
579579
"""Apply `fn` to contained value, or return the default."""

0 commit comments

Comments
 (0)