library symtable.SymbolTable.is_nested() description differs from Cpython implementation.

Abierto
#99,350 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
20/100
Tipo de issue
Documentación
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
python

Línea de trabajo

Revisa primero el PR vinculado gh-151901, luego compara symtable e inspecciona la documentación con el comportamiento indicado de CPython y el flag CO_NESTED. El issue deja abierta la resolución: alinear la implementación y añadir compiler tests, o corregir la documentación y posiblemente marcar la funcionalidad como obsoleta.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

docs interpreter-core type-bug

The documentation says

is_nested()
Return True if the block is a nested class or function.

The cpython implementation of symtable.c sets the corresponding flag only if the block is nested somewhere inside a function. This would include, for example, a scope inside a class inside a function.

Thus

def f():    # false
  def g():    # true
    def h():    # true
    class C:    # true
  class B:    # true
    def h():    # true
    class C:    # true
class A:    # false
  def g():    # false !!
    def h():    # true
    class C:    # true
  class B:    # false !!
    def h():    # false !!
    class C:    # false !!

The value is the same as the CO_NESTED bit in the code object's co_flags member.
The flag is also documented (similarly incorrectly) in the inspect module documentation.

I would recommend deprecating this feature. The documentation for co_flags says that

bits 0x10 and 0x1000 were used in earlier versions of Python.

0x10 is the CO_NESTED bit. I would guess that nobody uses CO_NESTED or SymbolTable.is_nested().

Alternatives:

  • Change the behavior in Cpython to agree with the documentation. A class nested in another scope should also have CO_NESTED set. Make a test case for the compiler, to ensure that other implementations follow the same definition.
  • Correct the documentation for symtable and inspect modules. Possibly also deprecate the feature.
Linked PRs
  • gh-151901
Lenguaje dominante
Python
Estrellas
77.2k
Forks
36k
Merge medio
1 d 9 h
PR fusionados (30 d)
558

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/cpython

Todos los issues de python/cpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.