Skip to content

DEP: Deprecate promotion of numbers and bool to string - #18116

Merged
mattip merged 1 commit into
numpy:masterfrom
seberg:futurewarn-string-promotion
Jan 26, 2021
Merged

mattip merged 1 commit into
numpy:masterfrom
seberg:futurewarn-string-promotion

Conversation

@seberg

@seberg seberg commented Jan 5, 2021 •

Copy link
Copy Markdown
Member

This deprecates promotion of strings and numbers (and bool). This an absolutely necessary step to make things sane at some point... (marking as draft for now, as it is based off the fix for promotion with half precision and string).

Some notes:

  • Promotion to string is, quite frankly, nonsense. It is so bad that pandas has to work around our behaviour (they don't even notice this change because of that, as far as I am aware). I think safe casting to string is also weird, but most places where we use "safe casting" as logic (i.e. ufuncs) should be using promotion instead, so I just don't care much anymore how we define "safe" casting. (Safe casting is just another casting level, and not a fundamental operator used for any logic.)
  • FutureWarning isn't ideal, as DeprecationWarning would be nicer, but I am not sure I want yet another special case to work around the fact that it is defined in one place and np.array uses a try/except.
  • I did add a work-around for ufunc type resolution so no warning is given there (since that will error anyway). That might turn out aannoying to keep. But probably I have to keep a similar hack in place in any case, since the alternative would be to shorten the deprecation period. (The could become akward when we start adding ufunc loops for strings, which we want to)

@seberg seberg added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jan 5, 2021
@seberg
seberg marked this pull request as draft January 5, 2021 05:31
@seberg
seberg force-pushed the futurewarn-string-promotion branch from 031f935 to 861a0fa Compare January 6, 2021 18:18
@seberg seberg removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jan 6, 2021
@seberg
seberg marked this pull request as ready for review January 6, 2021 18:18
@seberg
seberg force-pushed the futurewarn-string-promotion branch from 861a0fa to 94e9a27 Compare January 6, 2021 23:46
@mattip

mattip commented Jan 7, 2021

Copy link
Copy Markdown
Member

There is a warning in doc building:

/home/circleci/repo/doc/source/release/1.21.0-notes.rst:15: WARNING: Unexpected indentation.
/home/circleci/repo/doc/source/release/1.21.0-notes.rst:16: WARNING: Block quote ends without a blank line; unexpected unindent.

@seberg
seberg force-pushed the futurewarn-string-promotion branch from 94e9a27 to 25e5572 Compare January 7, 2021 15:01

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Comparisons and universal functions are not affected by this.
Comparisons, universal functions, and casting are not affected by this.

@seberg
seberg force-pushed the futurewarn-string-promotion branch from 25e5572 to f6a813e Compare January 7, 2021 15:55
@mattip

mattip commented Jan 7, 2021

Copy link
Copy Markdown
Member

I restarted Azure since it hit gh-18119 on the 64-bit run (failed to find nm.exe)

Comment thread numpy/core/tests/test_deprecations.py Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I randomly realized: I could change this so that it it actually raises the FutureWarning here. That is: We could differentiate between "no promotion found/possible" and an error while trying to promote).
But, I am not sure it makes things better here, and another solution for that distinction is to create a PromotionError type (assuming that invalid promotions are unlikely to be ever speed relevant).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we would want to make sure a warning is noticed when promotion fails. Should we merge as-is and enhance in another PR or add it here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just to be clear (just to be sure), this affects:

warnings.simplefilter("error", FutureWarning)
np.array([0, "a"])

which would "opt in" to the new behaviour currently. Happy to change it. But maybe the distinction for custom errors doesn't matter too much. If we introduce a PromotionTypeError to allow custom errors (if that even makes sense for an operator), we could still catch PromotionTypeError explicitly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot that this also affects, the second step, which would be the one that finds the correct unit for two different datetimes. If you think its worthwhile for this warning, I can look into it. Otherwise, I think swapping out to an explicit error type might be more elegant at some point (if we ever care). Since we should also need to say that __common_instance__ should return NotImplemented for such impossible cases, which seems not really that great to me.

@charris

charris commented Jan 15, 2021

Copy link
Copy Markdown
Member

@seberg needs rebase.

@seberg
seberg force-pushed the futurewarn-string-promotion branch from f6a813e to 5164312 Compare January 19, 2021 19:36
@seberg
seberg force-pushed the futurewarn-string-promotion branch from 5164312 to 38bda3c Compare January 25, 2021 19:21
@mattip

mattip commented Jan 25, 2021

Copy link
Copy Markdown
Member

Where do we stand with this? Has it been discussed on the mailing list? Does it need to be or is this niche enough we can merge it as is?

@seberg

seberg commented Jan 25, 2021

Copy link
Copy Markdown
Member Author

This has been mentioned multiple times on the list, just not super recently.

@mattip
mattip merged commit af51d66 into numpy:master Jan 26, 2021
@mattip

mattip commented Jan 26, 2021

Copy link
Copy Markdown
Member

Thanks @seberg

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants