|
2 | 2 |
|
3 | 3 | import datetime |
4 | 4 | import decimal |
| 5 | +import warnings |
5 | 6 | from enum import Enum, auto |
6 | 7 | from typing import Optional, Sequence, Any, TYPE_CHECKING |
7 | 8 |
|
@@ -640,15 +641,21 @@ def __init__( |
640 | 641 |
|
641 | 642 | if self.scale is not None and self.precision is not None and self.scale > self.precision: |
642 | 643 | # Earlier releases documented these arguments the other way round |
643 | | - # (scale = total digits, precision = fractional digits). Fail loudly |
644 | | - # instead of silently reinterpreting a call written that way. |
645 | | - raise ValueError( |
646 | | - "DecimalParameter scale ({}) must not exceed precision ({}). `precision` is " |
647 | | - "the total number of digits and `scale` is the number of digits after the " |
648 | | - "decimal point, as in SQL DECIMAL(precision, scale).".format( |
649 | | - self.scale, self.precision |
650 | | - ) |
| 644 | + # (scale = total digits, precision = fractional digits). A valid |
| 645 | + # DECIMAL can never have scale > precision, so this can only be a call |
| 646 | + # written for the old convention: keep it working for now (swap the |
| 647 | + # arguments) but warn so callers migrate before this becomes an error. |
| 648 | + warnings.warn( |
| 649 | + "DecimalParameter received scale ({}) > precision ({}); interpreting the " |
| 650 | + "arguments using the legacy (swapped) convention as DECIMAL({},{}). " |
| 651 | + "`precision` is the total number of digits and `scale` is the number of " |
| 652 | + "digits after the decimal point, as in SQL DECIMAL(precision, scale). " |
| 653 | + "Swap your arguments; this will raise ValueError in a future " |
| 654 | + "release.".format(self.scale, self.precision, self.scale, self.precision), |
| 655 | + DeprecationWarning, |
| 656 | + stacklevel=2, |
651 | 657 | ) |
| 658 | + self.scale, self.precision = self.precision, self.scale |
652 | 659 |
|
653 | 660 | def valid_scale_and_precision(self): |
654 | 661 | if (self.scale is None and self.precision is None) or ( |
|
0 commit comments