]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-103636: issue warning for deprecated calendar constants (#103833)
authorPrince Roshan <princekrroshan01@gmail.com>
Sat, 29 Apr 2023 07:16:46 +0000 (12:46 +0530)
committerGitHub <noreply@github.com>
Sat, 29 Apr 2023 07:16:46 +0000 (01:16 -0600)
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Éric <merwok@netwok.org>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Doc/library/calendar.rst
Doc/whatsnew/3.12.rst
Lib/calendar.py
Lib/test/test_calendar.py
Misc/NEWS.d/next/Library/2023-04-26-18-12-13.gh-issue-103636.-KvCgO.rst [new file with mode: 0644]

index 66f59f0e2ced27515193bc6231467b7ce527145c..07d04a1c7b582af34e071c2eead089bb835279e3 100644 (file)
@@ -28,6 +28,58 @@ interpreted as prescribed by the ISO 8601 standard.  Year 0 is 1 BC, year -1 is
 2 BC, and so on.
 
 
+.. class:: Day
+
+   Enumeration defining the days of the week as integer constants, from 0 to 6.
+
+   .. attribute:: MONDAY
+
+   .. attribute:: TUESDAY
+
+   .. attribute:: WEDNESDAY
+
+   .. attribute:: THURSDAY
+
+   .. attribute:: FRIDAY
+
+   .. attribute:: SATURDAY
+
+   .. attribute:: SUNDAY
+
+   .. versionadded:: 3.12
+
+
+.. class:: Month
+
+   Enumeration defining months of the year as integer constants, from 1 to 12.
+
+   .. attribute:: JANUARY
+
+   .. attribute:: FEBRUARY
+
+   .. attribute:: MARCH
+
+   .. attribute:: APRIL
+
+   .. attribute:: MAY
+
+   .. attribute:: JUNE
+
+   .. attribute:: JULY
+
+   .. attribute:: AUGUST
+
+   .. attribute:: SEPTEMBER
+
+   .. attribute:: OCTOBER
+
+   .. attribute:: NOVEMBER
+
+   .. attribute:: DECEMBER
+
+   .. versionadded:: 3.12
+
+
 .. class:: Calendar(firstweekday=0)
 
    Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the
index a75e88c2615acaf1e452c803903991f742c69a79..908cf3bb3656919b177b5283a89bb8f1a6cbc853 100644 (file)
@@ -300,6 +300,12 @@ asyncio
   yielding tasks.
   (Contributed by Kumar Aditya in :gh:`78530`.)
 
+calendar
+--------
+
+* Add enums :data:`~calendar.Month` and :data:`~calendar.Day`.
+  (Contributed by Prince Roshan in :gh:`103636`.)
+
 csv
 ---
 
@@ -692,6 +698,9 @@ Deprecated
   Python 3.14, when ``'data'`` filter will become the default.
   See :ref:`tarfile-extraction-filter` for details.
 
+* ``calendar.January`` and ``calendar.February`` constants are deprecated and
+  replaced by :data:`calendar.Month.JANUARY` and :data:`calendar.Month.FEBRUARY`.
+  (Contributed by Prince Roshan in :gh:`103636`.)
 
 Pending Removal in Python 3.13
 ------------------------------
index c219f0c218d9fa788d1ba4acd8c9b4ab70e3f041..bbd4fea3b88ca41889ba84853d1edf065f55c382 100644 (file)
@@ -10,6 +10,7 @@ import datetime
 from enum import IntEnum, global_enum
 import locale as _locale
 from itertools import repeat
+import warnings
 
 __all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
            "firstweekday", "isleap", "leapdays", "weekday", "monthrange",
@@ -41,6 +42,18 @@ class IllegalWeekdayError(ValueError):
         return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
 
 
+def __getattr__(name):
+    if name in ('January', 'February'):
+        warnings.warn(f"The '{name}' attribute is deprecated, use '{name.upper()}' instead",
+                      DeprecationWarning, stacklevel=2)
+        if name == 'January':
+            return 1
+        else:
+            return 2
+
+    raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
+
+
 # Constants for months
 @global_enum
 class Month(IntEnum):
index ccfbeede0be9496a471a952e30d3cf56b0bf7cd6..03388e8c55d5a808a18b483c644854b4d6d50856 100644 (file)
@@ -8,6 +8,7 @@ import locale
 import sys
 import datetime
 import os
+import warnings
 
 # From https://en.wikipedia.org/wiki/Leap_year_starting_on_Saturday
 result_0_02_text = """\
@@ -490,6 +491,14 @@ class OutputTestCase(unittest.TestCase):
             self.assertEqual(out.getvalue().strip(), "1   2   3")
 
 class CalendarTestCase(unittest.TestCase):
+
+    def test_deprecation_warning(self):
+        with warnings.catch_warnings(record=True) as w:
+            calendar.January
+        self.assertEqual(len(w), 1)
+        self.assertEqual(w[0].category, DeprecationWarning)
+        self.assertIn("The 'January' attribute is deprecated, use 'JANUARY' instead", str(w[0].message))
+
     def test_isleap(self):
         # Make sure that the return is right for a few years, and
         # ensure that the return values are 1 or 0, not just true or
diff --git a/Misc/NEWS.d/next/Library/2023-04-26-18-12-13.gh-issue-103636.-KvCgO.rst b/Misc/NEWS.d/next/Library/2023-04-26-18-12-13.gh-issue-103636.-KvCgO.rst
new file mode 100644 (file)
index 0000000..a05a6f5
--- /dev/null
@@ -0,0 +1 @@
+Module-level attributes ``January`` and ``February`` are deprecated from :mod:`calendar`.