Skip to content

int64 //= uint64 succeeds in 1.16.1 #12927

Closed

Description

@toslunar

The //= operation between an int64-array and a uint64-array fails in 1.16.0 but it succeeds in 1.16.1. It seems the result changed unintentionally (see below). I'm not sure which is the desired behavior.

Reproducing code example:

import numpy as np
print(np.__version__)
a = np.array([1], np.int64)
b = np.array([1], np.uint64)
a //= b
print('ok')

Results (with Numpy version information)

1.16.1
ok
1.16.0
Traceback (most recent call last):
  File "a.py", line 5, in <module>
    a //= b
TypeError: ufunc 'floor_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''
1.9.3
ok

I also tested with master + revert #12683, #12308

% git log --oneline | head -n 3
68d4a8397 Revert "Merge pull request #12308 from tylerjereddy/timedelta64_floordiv"
0e97167ee Revert "Merge pull request #12683 from tylerjereddy/timedelta64_divmod"
c009e1b98 Merge pull request #12894 from eric-wieser/distutils-broken-import
1.17.0.dev0+c009e1b
ok
1.17.0.dev0+0e97167
ok
1.17.0.dev0+68d4a83
Traceback (most recent call last):
  File "a.py", line 5, in <module>
    a //= b
TypeError: ufunc 'floor_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''

Python version information:

3.6.7 (default, Nov  7 2018, 12:26:42)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)]

Activity

  1. eric-wieser commented on Feb 4, 2019

    @eric-wieser
    Member
  2. eric-wieser commented on Feb 4, 2019

    @eric-wieser
    Member

    @toslunar: So to be clear, your conclusion is that #12308 is the cause of the behavior change?

  3. toslunar commented on Feb 4, 2019

    @toslunar
    ContributorAuthor

    Yes. My guess is that #12308 is the cause, while I've not located which lines in the PR changed the behavior.

  4. tylerjereddy commented on Feb 5, 2019

    @tylerjereddy
    Contributor

    Commenting out TypeDescription('m', FullTypeDescr, 'mm', 'q'), restores the original behavior, but obviously reverts the enhancement to m8. I spent a few hours debugging through linear_search_type_resolver and ufunc_loop_matches, and it looks like
    types[i] == NPY_OBJECT becomes true for the original case & not for the new case. I can sort of patch to restore the TypeError conditionally on input types to the division type resolver, but actually we do have unit tests that fail if I do that it seems.

    I'm guessing integer-like types can kind of follow timedelta64 through control flow in surprising ways because of their similarity.

    What is clear is that the behavior that changed silently as far as unit tests are considered needs to be tested.

  5. eric-wieser commented on Feb 5, 2019

    @eric-wieser
    Member

    Nice detective work @tylerjereddy!

  6. eric-wieser commented on Feb 6, 2019

    @eric-wieser
    Member

    Added some debug prints to the C code - we're hitting the dtype('<m8'),dtype('<m8')->dtype('int64') loop (mm->q) in this case.

    I think the rationale is that for qQ->q, we allow q->m and Q->m conversion. So as a result, mm->q is a matching loop.

    Some options we should consider:

    1. We shouldn't allow Q->m casting, it's lossy
    2. We shouldn't allow either Q->m or q->m casting, it's sloppy from a unit perspective
    3. We need to mark some type of upcasts as ineligible for automatic conversion in ufuncs

    Personally, i think 2 is the right direction to move in

  7. tylerjereddy commented on Feb 6, 2019

    @tylerjereddy
    Contributor

    Just one other thought here -- how disruptive to end users / large downstream libraries do we think a prohibition on the Q->m + q->m casting would be? If it sneaks through in pandas or somewhere like that I suppose we may have to use a little caution.

  8. eric-wieser commented on Feb 6, 2019

    @eric-wieser
    Member

    We could start by emitting a warning in that case, and see how disruptive it ends up being.

  9. seberg commented on Feb 12, 2019

    @seberg
    Member

    I agree with Eric here, 2. seems like a good option (making it an unsafe cast). Certainly will need a deprecation period (if we get lucky, a short one), I guess only trying will tell how tricky doing that actually is. I suppose the type resolver could be fixed up to "skip" the deprecation for this specific function.

  10. seberg commented on Feb 14, 2019

    @seberg
    Member

    Not sure it helps, but if maybe an easier start for someone confused about the casting logic in numpy than digging through the code paths: https://gist.github.com/seberg/925ee5ecb312a467ce822c90adbf5659

  11. seberg commented on Feb 15, 2019

    @seberg
    Member

    I tried removing the safe cast, the fallout in our testsuit is pretty limited (mostly specific tests and tests for addition subtraction). Also a few tests of true divide in the polynomial, which is probably only tested because timedelta's are also numbers.Number (another point that I am a bit unsure about).

    I think we can probably deprecate it in a proper way, but will need to add new ufunc loops (unless they are supposed to deprecated as well) with some care.

    The other thing I am wondering about is whether we should disallow "same_kind" casting for out-of-kind casts. Unlike the documentation suggests, the current logic allows many unsafe out of kind casts.

  12. eric-wieser commented on Oct 15, 2019

    @eric-wieser
    Member

    Hmm, I'm torn on this now. The point that "it's sloppy from a unit perspective" isn't really valid - we have a "generic" timedelta type that is deliberately sloppy, and allowing to convert between that an an int64 seems pretty reasonable.

    Perhaps we should just disable uint64 <-> timedelta64, in the same way that today we disable uint64 <-> int64?

  13. added this to the 1.18.0 release milestone on Oct 15, 2019
  14. bsipocz commented on Oct 15, 2019

    @bsipocz
    Member

    This currently works on master:

    1.18.0.dev0+2138cb1
    ok
    
  15. eric-wieser commented on Oct 15, 2019

    @eric-wieser
    Member

    Yep - unfortunately, the intent was that this should not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions