Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-104003: Implement PEP 702 #104004

Merged
merged 25 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
93e11d6
gh-104003: Implement PEP 702
JelleZijlstra Apr 30, 2023
d8d3157
blurb
JelleZijlstra Apr 30, 2023
342f28b
Add more tests
JelleZijlstra Apr 30, 2023
9b167e8
describe runtime behavior
JelleZijlstra May 6, 2023
0ed9d41
steal the typing-extensions wording instead, it's better
JelleZijlstra May 6, 2023
2f69b4e
Merge remote-tracking branch 'upstream/main' into pep702
JelleZijlstra Nov 8, 2023
815591b
Put it in warnings, copy over from typing-extensions
JelleZijlstra Nov 8, 2023
4098e62
docs
JelleZijlstra Nov 8, 2023
d6625b9
fix test
JelleZijlstra Nov 8, 2023
0870a16
Update Doc/library/warnings.rst
JelleZijlstra Nov 8, 2023
2b48983
Merge branch 'main' into pep702
JelleZijlstra Nov 8, 2023
b9507bd
Code review
JelleZijlstra Nov 8, 2023
2f23654
Merge branch 'main' into pep702
JelleZijlstra Nov 8, 2023
ed9a10c
make it a class
JelleZijlstra Nov 27, 2023
6ab4584
Merge remote-tracking branch 'upstream/main' into pep702
JelleZijlstra Nov 27, 2023
e60ad5c
Merge remote-tracking branch 'upstream/main' into pep702
JelleZijlstra Nov 29, 2023
d528dc8
Forward-port typing-extensions change
JelleZijlstra Nov 29, 2023
e323502
Add test suggested by Alex
JelleZijlstra Nov 29, 2023
acf49b3
Adjust docs
JelleZijlstra Nov 29, 2023
1d663ef
Add to Whats New
JelleZijlstra Nov 29, 2023
b94255d
Now that this is semi-public, use a better name
JelleZijlstra Nov 29, 2023
e7a361b
sync docstring
JelleZijlstra Nov 29, 2023
08efe4d
Update error msg
JelleZijlstra Nov 29, 2023
f4eb075
Apply suggestions from code review
AlexWaygood Nov 29, 2023
c02f66f
Update Lib/warnings.py
JelleZijlstra Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,11 @@ Glossary
an :term:`expression` or one of several constructs with a keyword, such
as :keyword:`if`, :keyword:`while` or :keyword:`for`.

static type checker
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
An external tool that reads Python code and analyzes it, looking for
issues such as incorrect types. See also :term:`type hints <type hint>`
and the :mod:`typing` module.

strong reference
In Python's C API, a strong reference is a reference to an object
which is owned by the code holding the reference. The strong
Expand Down Expand Up @@ -1214,8 +1219,8 @@ Glossary
attribute, or a function parameter or return value.

Type hints are optional and are not enforced by Python but
they are useful to static type analysis tools, and aid IDEs with code
completion and refactoring.
they are useful to :term:`static type checkers <static type checker>`,
and aid IDEs with code completion and refactoring.

Type hints of global variables, class attributes, and functions,
but not local variables, can be accessed using
Expand Down
46 changes: 46 additions & 0 deletions Doc/library/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,52 @@ Available Functions
and calls to :func:`simplefilter`.


.. decorator:: deprecated(msg, *, category=DeprecationWarning, stacklevel=1)

Decorator to indicate that a class, function or overload is deprecated.

Usage::
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved

from warnings import deprecated
from typing import overload

@deprecated("Use B instead")
class A:
pass

@deprecated("Use g instead")
def f():
pass

@overload
@deprecated("int support is deprecated")
def g(x: int) -> int: ...
@overload
def g(x: str) -> int: ...

When this decorator is applied to an object,
:term:`static type checkers <static type checker>`
will generate a diagnostic on usage of the deprecated object.
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved

The warning specified by ``category`` will be emitted on use
of deprecated objects. For functions, that happens on calls;
for classes, on instantiation. If the ``category`` is ``None``,
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
no warning is emitted. The ``stacklevel`` determines where the
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
warning is emitted. If it is ``1`` (the default), the warning
is emitted at the direct caller of the deprecated object; if it
is higher, it is emitted further up the stack.

The decorator sets the ``__deprecated__``
attribute on the decorated object to the deprecation message
passed to the decorator. If applied to an overload, the decorator
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
must be after the :func:`@overload <typing.overload>` decorator
for the attribute to exist on the overload as returned by
:func:`typing.get_overloads`.

.. versionadded:: 3.13
See :pep:`702`.


Available Context Managers
--------------------------

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ venv
(using ``--without-scm-ignore-files``). (Contributed by Brett Cannon in
:gh:`108125`.)

warnings
--------

* The new :func:`warnings.deprecated` decorator provides a way to communicate
deprecations to :term:`static type checkers <static type checker>`. See
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
:pep:`702`. (Contributed by Jelle Zijlstra in :gh:`104003`.)

Optimizations
=============

Expand Down
260 changes: 259 additions & 1 deletion Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import re
import sys
import textwrap
import types
from typing import overload, get_overloads
import unittest
from test import support
from test.support import import_helper
Expand All @@ -16,6 +18,7 @@
from test.test_warnings.data import stacklevel as warning_tests

import warnings as original_warnings
from warnings import deprecated


py_warnings = import_helper.import_fresh_module('warnings',
Expand Down Expand Up @@ -90,7 +93,7 @@ def test_module_all_attribute(self):
self.assertTrue(hasattr(self.module, '__all__'))
target_api = ["warn", "warn_explicit", "showwarning",
"formatwarning", "filterwarnings", "simplefilter",
"resetwarnings", "catch_warnings"]
"resetwarnings", "catch_warnings", "deprecated"]
self.assertSetEqual(set(self.module.__all__),
set(target_api))

Expand Down Expand Up @@ -1377,6 +1380,261 @@ def test_late_resource_warning(self):
self.assertTrue(err.startswith(expected), ascii(err))


class DeprecatedTests(unittest.TestCase):
def test_dunder_deprecated(self):
@deprecated("A will go away soon")
class A:
pass

self.assertEqual(A.__deprecated__, "A will go away soon")
self.assertIsInstance(A, type)

@deprecated("b will go away soon")
def b():
pass

self.assertEqual(b.__deprecated__, "b will go away soon")
self.assertIsInstance(b, types.FunctionType)

@overload
@deprecated("no more ints")
def h(x: int) -> int: ...
@overload
def h(x: str) -> str: ...
def h(x):
return x

overloads = get_overloads(h)
self.assertEqual(len(overloads), 2)
self.assertEqual(overloads[0].__deprecated__, "no more ints")

def test_class(self):
@deprecated("A will go away soon")
class A:
pass

with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"):
A()
with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"):
with self.assertRaises(TypeError):
A(42)

def test_class_with_init(self):
@deprecated("HasInit will go away soon")
class HasInit:
def __init__(self, x):
self.x = x

with self.assertWarnsRegex(DeprecationWarning, "HasInit will go away soon"):
instance = HasInit(42)
self.assertEqual(instance.x, 42)

def test_class_with_new(self):
has_new_called = False

@deprecated("HasNew will go away soon")
class HasNew:
def __new__(cls, x):
nonlocal has_new_called
has_new_called = True
return super().__new__(cls)

def __init__(self, x) -> None:
self.x = x

with self.assertWarnsRegex(DeprecationWarning, "HasNew will go away soon"):
instance = HasNew(42)
self.assertEqual(instance.x, 42)
self.assertTrue(has_new_called)

def test_class_with_inherited_new(self):
new_base_called = False

class NewBase:
def __new__(cls, x):
nonlocal new_base_called
new_base_called = True
return super().__new__(cls)

def __init__(self, x) -> None:
self.x = x

@deprecated("HasInheritedNew will go away soon")
class HasInheritedNew(NewBase):
pass

with self.assertWarnsRegex(DeprecationWarning, "HasInheritedNew will go away soon"):
instance = HasInheritedNew(42)
self.assertEqual(instance.x, 42)
self.assertTrue(new_base_called)

def test_class_with_new_but_no_init(self):
new_called = False

@deprecated("HasNewNoInit will go away soon")
class HasNewNoInit:
def __new__(cls, x):
nonlocal new_called
new_called = True
obj = super().__new__(cls)
obj.x = x
return obj

with self.assertWarnsRegex(DeprecationWarning, "HasNewNoInit will go away soon"):
instance = HasNewNoInit(42)
self.assertEqual(instance.x, 42)
self.assertTrue(new_called)

def test_mixin_class(self):
@deprecated("Mixin will go away soon")
class Mixin:
pass

class Base:
def __init__(self, a) -> None:
self.a = a

with self.assertWarnsRegex(DeprecationWarning, "Mixin will go away soon"):
class Child(Base, Mixin):
pass

instance = Child(42)
self.assertEqual(instance.a, 42)

def test_existing_init_subclass(self):
@deprecated("C will go away soon")
class C:
def __init_subclass__(cls) -> None:
cls.inited = True

with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
C()

with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
class D(C):
pass

self.assertTrue(D.inited)
self.assertIsInstance(D(), D) # no deprecation

def test_existing_init_subclass_in_base(self):
class Base:
def __init_subclass__(cls, x) -> None:
cls.inited = x

@deprecated("C will go away soon")
class C(Base, x=42):
pass

self.assertEqual(C.inited, 42)

with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
C()

with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
class D(C, x=3):
pass

self.assertEqual(D.inited, 3)

def test_init_subclass_has_correct_cls(self):
init_subclass_saw = None

@deprecated("Base will go away soon")
class Base:
def __init_subclass__(cls) -> None:
nonlocal init_subclass_saw
init_subclass_saw = cls

self.assertIsNone(init_subclass_saw)

with self.assertWarnsRegex(DeprecationWarning, "Base will go away soon"):
class C(Base):
pass

self.assertIs(init_subclass_saw, C)

def test_init_subclass_with_explicit_classmethod(self):
init_subclass_saw = None

@deprecated("Base will go away soon")
class Base:
@classmethod
def __init_subclass__(cls) -> None:
nonlocal init_subclass_saw
init_subclass_saw = cls

self.assertIsNone(init_subclass_saw)

with self.assertWarnsRegex(DeprecationWarning, "Base will go away soon"):
class C(Base):
pass

self.assertIs(init_subclass_saw, C)

def test_function(self):
@deprecated("b will go away soon")
def b():
pass

with self.assertWarnsRegex(DeprecationWarning, "b will go away soon"):
b()

def test_method(self):
class Capybara:
@deprecated("x will go away soon")
def x(self):
pass

instance = Capybara()
with self.assertWarnsRegex(DeprecationWarning, "x will go away soon"):
instance.x()

def test_property(self):
class Capybara:
@property
@deprecated("x will go away soon")
def x(self):
pass

@property
def no_more_setting(self):
return 42

@no_more_setting.setter
@deprecated("no more setting")
def no_more_setting(self, value):
pass

instance = Capybara()
with self.assertWarnsRegex(DeprecationWarning, "x will go away soon"):
instance.x

with py_warnings.catch_warnings():
py_warnings.simplefilter("error")
self.assertEqual(instance.no_more_setting, 42)

with self.assertWarnsRegex(DeprecationWarning, "no more setting"):
instance.no_more_setting = 42

def test_category(self):
@deprecated("c will go away soon", category=RuntimeWarning)
def c():
pass

with self.assertWarnsRegex(RuntimeWarning, "c will go away soon"):
c()

def test_turn_off_warnings(self):
@deprecated("d will go away soon", category=None)
def d():
pass

with py_warnings.catch_warnings():
py_warnings.simplefilter("error")
d()


def setUpModule():
py_warnings.onceregistry.clear()
c_warnings.onceregistry.clear()
Expand Down