Commit 6d2694d8 authored by Kirill Smelkov's avatar Kirill Smelkov

kpi: Robustify logic in _newscalar

_newscalar creates a scalar value of type typ with specified dtype.
Inside it works via creating dtype that will have its .type = typ and
that's how NumPy knows to instantiate scalars of such dtype via
specified typ. But at the end _newscalar was checking only that .dtype
of creatged scalar == original dtype, while we can insist that
scalar.dtype _is_ the created dtype which itself should be == to
originally passed dtype.

No change in behaviour, only make the logic more robust and explicit.
parent ca4300ed
...@@ -974,10 +974,13 @@ def _i2pc(x: Interval): # -> Interval ...@@ -974,10 +974,13 @@ def _i2pc(x: Interval): # -> Interval
# _newscalar creates new NumPy scalar instance with specified type and dtype. # _newscalar creates new NumPy scalar instance with specified type and dtype.
def _newscalar(typ, dtype): def _newscalar(typ, dtype):
_ = np.zeros(shape=(), dtype=(typ, dtype)) dtyp = np.dtype((typ, dtype)) # dtype with .type adjusted to be typ
assert dtyp == dtype
assert dtyp.type is typ
_ = np.zeros(shape=(), dtype=dtyp)
s = _[()] s = _[()]
assert type(s) is typ assert type(s) is typ
assert s.dtype == dtype assert s.dtype is dtyp
return s return s
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment