gh-119698: fix symtable.Class.get_methods and document its behaviour correctly (#120151)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Bénédikt Tran 2024-06-20 05:49:30 +02:00 committed by GitHub
parent 656a1c8108
commit b8a8e04fec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 186 additions and 5 deletions

View file

@ -180,8 +180,39 @@ Examining Symbol Tables
.. method:: get_methods()
Return a tuple containing the names of methods declared in the class.
Return a tuple containing the names of method-like functions declared
in the class.
Here, the term 'method' designates *any* function defined in the class
body via :keyword:`def` or :keyword:`async def`.
Functions defined in a deeper scope (e.g., in an inner class) are not
picked up by :meth:`get_methods`.
For example:
>>> import symtable
>>> st = symtable.symtable('''
... def outer(): pass
...
... class A:
... def f():
... def w(): pass
...
... def g(self): pass
...
... @classmethod
... async def h(cls): pass
...
... global outer
... def outer(self): pass
... ''', 'test', 'exec')
>>> class_A = st.get_children()[2]
>>> class_A.get_methods()
('f', 'g', 'h')
Although ``A().f()`` raises :exc:`TypeError` at runtime, ``A.f`` is still
considered as a method-like function.
.. class:: Symbol