ast.parse creates only Constant(value=0) nodes for a `case +0:` pattern, which is inconsistent.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem ast.parse-Beispiel im Issue und vergleiche die erzeugten Knoten für case +0:, case -0:, +0 und -0. Verfolge, wie der match-expression dargestellt wird, sorge dann dafür, dass das +0-Muster die erwartete UnaryOp-Form erzeugt, und überprüfe die AST-Ausgabe sowie den ast.parse/unparse-Roundtrip.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100