Like `operator.index`, but emits a deprecation warning when passed a float Parameters ---------- x : int-like, or float with integral value Value to interpret as an integer desc : str description to include in any error message Raises ------ TypeErr
(x, desc)
| 718 | |
| 719 | |
| 720 | def _deprecate_as_int(x, desc): |
| 721 | """ |
| 722 | Like `operator.index`, but emits a deprecation warning when passed a float |
| 723 | |
| 724 | Parameters |
| 725 | ---------- |
| 726 | x : int-like, or float with integral value |
| 727 | Value to interpret as an integer |
| 728 | desc : str |
| 729 | description to include in any error message |
| 730 | |
| 731 | Raises |
| 732 | ------ |
| 733 | TypeError : if x is a non-integral float or non-numeric |
| 734 | DeprecationWarning : if x is an integral float |
| 735 | """ |
| 736 | try: |
| 737 | return operator.index(x) |
| 738 | except TypeError as e: |
| 739 | # Numpy 1.17.0, 2019-03-11 |
| 740 | try: |
| 741 | ix = int(x) |
| 742 | except TypeError: |
| 743 | pass |
| 744 | else: |
| 745 | if ix == x: |
| 746 | warnings.warn( |
| 747 | f"In future, this will raise TypeError, as {desc} will " |
| 748 | "need to be an integer not just an integral float.", |
| 749 | DeprecationWarning, |
| 750 | stacklevel=3 |
| 751 | ) |
| 752 | return ix |
| 753 | |
| 754 | raise TypeError(f"{desc} must be an integer") from e |
| 755 | |
| 756 | |
| 757 | def format_float(x, parens=False): |