From adb9e791c6a13f473d0df1e28d4eb4937dced834 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Wed, 20 Dec 2023 09:11:18 +0100 Subject: [PATCH] gh-113212: Document zero-arg `super()` inside nested functions and generator expressions --- Doc/library/functions.rst | 15 +++++++++++++++ ...2023-12-20-08-54-54.gh-issue-113212.62AUlw.rst | 2 ++ 2 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-12-20-08-54-54.gh-issue-113212.62AUlw.rst diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index c731b6fd3332753..027400a7707fc8c 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1845,6 +1845,21 @@ are always available. They are listed here in alphabetical order. :func:`super`, see `guide to using super() `_. + .. note:: + By default, if no second argument is provided, :func:`super` uses the first argument + of the immediately enclosing function definition, which is ``self`` under normal circumstances. + This may lead to unexpected behaviour if used inside nested functions or generator expressions + (the latter creates implicit nested functions). The following example will result in an exception:: + + class B(C): + def method(self, arg): + list(super().method(arg) for _ in range(1)) # Raises TypeError. + + In such cases, you should provide arguments explicitly:: + + class B(C): + def method(self, arg): + list(super(B, self).method(arg) for _ in range(1)) .. _func-tuple: .. class:: tuple() diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-20-08-54-54.gh-issue-113212.62AUlw.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-20-08-54-54.gh-issue-113212.62AUlw.rst new file mode 100644 index 000000000000000..f9c9d843b279fd3 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-12-20-08-54-54.gh-issue-113212.62AUlw.rst @@ -0,0 +1,2 @@ +improve :py:class:`super` error message && document zero-arg :py:class:`super` inside nested +functions and generator expressions