python / python/cpython

ast.parse creates only Constant(value=0) nodes for a `case +0:` pattern, which is inconsistent.

Aperta
#152,708 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core topic-parser type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

I found the following asymmetry in the generated ast while I was working with the new cpython 3.15.

c="""
match 0:
    case +0:
        pass
    case -0:
        pass
+0
-0
"""

import ast

print(ast.dump(ast.parse(c),indent=2))

output (Python 3.15.0b3+dev):

Module(
  body=[
    Match(
      subject=Constant(value=0),
      cases=[
        match_case(
          pattern=MatchValue(
            value=Constant(value=0)),
          body=[
            Pass()]),
        match_case(
          pattern=MatchValue(
            value=UnaryOp(
              op=USub(),
              operand=Constant(value=0))),
          body=[
            Pass()])]),
    Expr(
      value=UnaryOp(
        op=UAdd(),
        operand=Constant(value=0))),
    Expr(
      value=UnaryOp(
        op=USub(),
        operand=Constant(value=0)))])

+0 is parsed to Constant(value=0) instead of UnaryOp(op=UAdd(),operand=Constant(value=0)) when it is used as a match-expression.

This was really unexpected for me and can maybe produce some problems with ast.parse/unparse roundtripps or code which works with the ast and require workarounds (like in my case)

Is it possible to implement this in a way that it generates UnaryOp(...)?

@johnslavik you implemented this #148566, maybe you can help me here.

CPython versions tested on:

3.15

Operating systems tested on:

No response

Linked PRs
  • gh-152712

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’esempio ast.parse nell’issue e confronta i nodi generati per case +0:, case -0:, +0 e -0. Traccia come viene rappresentato il match-expression, quindi fai in modo che il pattern +0 produca la forma UnaryOp prevista e verifica l’output dell’AST e il round trip di ast.parse/unparse.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.