Improve syntax error for nonlocal not in a nested function
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
Using nonlocal inside a non-nested function to refer to a global gives the syntax error "no binding for nonlocal 'name' found" even if the global 'name' exists. This is confusing. Improve the error message by making it clear that the problem is not that variable 'name' doesn't exist, but that the function is not nested inside another function containing a variable 'name'.
Suggestion: if nonlocal is used in a non-nested function where there is no surrounding function (e.g. either a global level function or one defined inside a class), rather than reporting that there is no binding found, make the error message more specific:
"nonlocal can only be used in function nested inside another function; perhaps try global instead?"
As evidence that this error message is not clear, see #92983 where the submitter @snoopyjc was mislead into thinking that the problem was the loop variable rather than the lack of nested function.
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece localizando o tratamento do compilador para erros de nonlocal e os testes de regressão para escopo inválido de função aninhada. Verifique a mensagem existente para uma função não aninhada e, em seguida, faça com que o comportamento final informe a função envolvente ausente e sugira global em seu lugar.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- compilers
- Tipo de issue
- Bug
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 45/100