ast.parse creates only Constant(value=0) nodes for a `case +0:` pattern, which is inconsistent.
Nessuno ha ancora preso questa issue.
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- 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