patchedast: visitors that omit sub-nodes leave unconsumed text that later token searches mis-match

Aberta
#912 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 7 dias

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
46/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
devtools

Direção de pesquisa

Start in rope/base/ast.py and trace _PatchingASTWalker plus the visitors named in the issue, including _arguments, _arg, _handle_function_def_node, _JoinedStr, _ClassDef and _TryFinally. Reproduce the listed cases with patchedast.get_patched_ast(src, sorted_children=True), then add a regression check that relevant AST nodes and string literals receive regions covering their AST spans. Done means the cases no longer raise or leave source text without a region.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

_PatchingASTWalker finds each token with a forward source.index, so any source text a visitor does not
list is skipped as whitespace; a (, ), = or quote inside it is then matched as a later token. The
walk raises MismatchedTokenError / AttributeError: 'NoneType' object has no attribute 'group', or it
silently produces wrong regions, which Scope.get_region uses for scope lookup. On rope 1.15.0, each
repro below makes patchedast.get_patched_ast(src, sorted_children=True) either raise or finish with part
of the source left without a region:

  1. posonly / kw-only args and kw_defaults (_arguments): def f(*, m: str = "(host)"): pass
  2. parameter annotations (_arg): def f(p: A[F(d="t")] = None): pass
  3. return annotations (_handle_function_def_node): def f() -> A[F(d="(")]: pass
  4. *args / **kwargs annotations: def f(*a: F("("), **k: F(")")): pass
  5. f-string implicitly concatenated with a later literal (_JoinedStr stops at the f-string's quote):
    c = g(f"{d}" "(")
  6. class keywords (_ClassDef): class A(B, metaclass=M, x=")"): pass
  7. else of try/except/else/finally (_TryFinally): an else: body holding x = "("
  8. rb/Rb prefixes (string pattern [uUbB]?[rR]?): x = rb"("; the region starts after r
  9. 3.14 t-strings: TemplateStr/Interpolation are unknown nodes: f(t"{a}(", ")")
  10. 3.12+ nested f-strings that reuse the outer quote: x = f"{f"{a}("}" ")" (the string regex stops at
    the inner quote)

Suggested regression test: after walking any module, every AST node except context/operator nodes,
withitem and f-string internals has a region, and every string literal's region covers its
ast span. With that check, CPython's stdlib and a 1,200-file project pass once the visitors above are
fixed. We carry these patches in living-architecture's rope_patches.py and will drop each when it
lands upstream.

Linguagem predominante
Python
Estrelas
2.2k
Forks
197
Merge médio
2d 13h
PRs com merge (30d)
5

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python-rope/rope

Todas as issues de python-rope/rope

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.