Skip to content

Commit

Permalink
Split SupportsRealImag into SupportsRealImagProperties and ``…
Browse files Browse the repository at this point in the history
…SupportsRealImagAsMethod``

Now ``sympy.core.numbers`` primitives are adequately supported (excepting general false positives; see #5).

Fixes #4.
  • Loading branch information
posita committed Nov 14, 2021
1 parent 3600886 commit 1ebeea0
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 172 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,24 @@ True
True
>>> hasattr(pants_on_fire, "real") or hasattr(pants_on_fire, "imag") # somebody's tellin' stories
False
>>> from numerary.types import SupportsRealImag
>>> real_imag: SupportsRealImag = pants_on_fire # fails to detect the lie
>>> from numerary.types import SupportsRealImagProperties
>>> real_imag: SupportsRealImagProperties = pants_on_fire # fails to detect the lie
>>> real_imag.real
Traceback (most recent call last):
...
AttributeError: 'One' object has no attribute 'real'

```

In this particular case, ``numerary`` provides us with a defensive mechanism.

``` python
>>> from numerary.types import SupportsRealImagMixedU, real, imag
>>> real_imag_defense: SupportsRealImagMixedU = pants_on_fire
>>> real(real_imag_defense)
1
>>> imag(real_imag)
0

```

Expand Down
2 changes: 2 additions & 0 deletions docs/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

## [0.1.2](https://github.com/posita/numerary/releases/tag/v0.1.2)

* Splits ``SupportsRealImag`` into [``SupportsRealImagProperties``][numerary.types.SupportsRealImagProperties] and [``SupportsRealImagAsMethod``][numerary.types.SupportsRealImagAsMethod] and provides the [``real``][numerary.types.real] and [``imag``][numerary.types.imag] helper functions for better support of ``sympy.core.numbers`` primitives.

## [0.1.1](https://github.com/posita/numerary/releases/tag/v0.1.1)

* Removes obsoleted ``…SCT`` aliases.
Expand Down
10 changes: 8 additions & 2 deletions docs/numerary.types.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ from numerary.bt import beartype # will resolve to the identity decorator if be
- "SupportsIndex"
- "SupportsRound"
- "SupportsConjugate"
- "SupportsRealImag"
- "SupportsRealImagProperties"
- "SupportsRealImagAsMethod"
- "SupportsTrunc"
- "SupportsFloorCeil"
- "SupportsDivmod"
Expand All @@ -77,6 +78,8 @@ from numerary.bt import beartype # will resolve to the identity decorator if be
- "SupportsRealOps"
- "SupportsIntegralOps"
- "SupportsIntegralPow"
- "real"
- "imag"
- "trunc"
- "floor"
- "ceil"
Expand All @@ -102,7 +105,10 @@ from numerary.bt import beartype # will resolve to the identity decorator if be
- "_SupportsIndex"
- "_SupportsRound"
- "_SupportsConjugate"
- "_SupportsRealImag"
- "_SupportsRealImagProperties"
- "_SupportsRealImagAsMethod"
- "SupportsRealImagMixedT"
- "SupportsRealImagMixedU"
- "_SupportsTrunc"
- "_SupportsFloorCeil"
- "_SupportsDivmod"
Expand Down
4 changes: 2 additions & 2 deletions docs/perf_rational_big_protocol.ipy
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from numerary.types import ( # "raw" (non-caching) versions
_SupportsConjugate,
_SupportsFloorCeil,
_SupportsDivmod,
_SupportsRealImag,
_SupportsRealImagProperties,
_SupportsRealOps,
_SupportsTrunc,
)
Expand Down Expand Up @@ -33,7 +33,7 @@ class SupportsLotsOfNumberStuff(
_SupportsTrunc,
_SupportsFloorCeil,
_SupportsConjugate,
_SupportsRealImag,
_SupportsRealImagProperties,
SupportsAbs,
SupportsFloat,
SupportsComplex,
Expand Down
6 changes: 3 additions & 3 deletions docs/perf_rational_big_protocol.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%timeit isinstance(builtins.int(1), SupportsLotsOfNumberStuff)
132 µs ± 1.15 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
135 µs ± 1.02 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit isinstance(fractions.Fraction(2), SupportsLotsOfNumberStuff)
139 µs ± 2.01 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
146 µs ± 14.4 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit isinstance(builtins.float(3.0), SupportsLotsOfNumberStuff)
131 µs ± 1.13 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
134 µs ± 1.84 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Loading

0 comments on commit 1ebeea0

Please sign in to comment.