Self-documenting expressions conversion + format-spec
Aperta
Nessuno ha ancora preso questa issue.
interpreter-core
topic-parser
type-feature
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
# initialisation
foo = "hello!"
bar = "hello, world!"
long_name = bar
print(f"{foo = }")
print(f"{bar = }")
print()
# these work as I expected
# they default to !r value conversion
print(f"{f"{foo=}" = }")
print(f"{f"{bar=}" = }")
print(f"{(f"{foo=}" == f"{foo=!r}") = }")
print()
# these do not work as I expected
# they default to !s value conversion, instead of !r
print(f"{f"{foo=:}" = }")
print(f"{f"{bar=:}" = }")
print(f"{(f"{foo=:}" == f"{foo=!r}") = }")
print(f"{(f"{foo=:}" == f"{foo=!s}") = }")
print()
# these do not work as I expected
# they apply padding to the expression value
# the expression itself is not taken into account
# as a result, the true string length is dependent on the expression length
print(f"{f"{bar=!r:20}." = }")
print(f"{len(f"{bar=!r:20}.") = }")
print(f"{f"{long_name=!r:20}." = }")
print(f"{len(f"{long_name=!r:20}.") = }")
print()
# these do not work as I expected
# they apply alignment only to the expression value
# the expression itself is always left-aligned
print(f"{f"{bar=!r:>20}" = }")
print(f"{len(f"{bar=!r:>20}") = }")
print(f"{f"{long_name=!r:>20}" = }")
print(f"{len(f"{long_name=!r:>20}") = }")
Expected results
- Default value conversion mode should be consistent regardless of whether
:is present - The whole self-documenting expression should be padded / aligned
import unittest
from secrets import token_urlsafe as random_string
class ExpectedResults(unittest.TestCase):
def test_consistent_default_value_conversion_mode(self):
my_value = random_string(16)
first_way = f"{my_value=}"
second_way = f"{my_value=:}"
self.assertEqual(first_way, second_way)
def test_pad_to_length(self):
my_value = random_string(16)
assert len(my_value) == 22
value_only = f"{my_value!r:35}."
self_documenting = f"{my_value=!r:35}."
self.assertEqual(len(value_only), len(self_documenting))
def test_alignment(self):
my_value = random_string(16)
assert len(my_value) == 22
faux_self_documenting = f"{f"my_value={my_value!r}":>35}"
assert not faux_self_documenting.startswith("my_value=")
assert len(faux_self_documenting) == 35
self_documenting = f"{my_value=!r:>35}"
self.assertEqual(faux_self_documenting, self_documenting)
def test_consistent_default_value_conversion_mode_exact(self):
my_value = "report poet ethics"
expected = "my_value='report poet ethics'"
actual = f"{my_value=:}"
self.assertEqual(expected, actual)
def test_pad_to_length_exact(self):
my_value = 'sugar lemon dilemma'
expected = "my_value='sugar lemon dilemma' "
assert len(expected) == 36
actual = f"{my_value=!r:36}"
self.assertEqual(expected, actual)
# or alternatively
self.assertEqual(36, len(actual))
def test_alignment_exact(self):
my_value = 'bonus thought skull'
expected = " my_value='bonus thought skull' "
assert len(expected) == 36
actual = f"{my_value=!r:^36}"
self.assertEqual(expected, actual)
if __name__ == "__main__":
unittest.main()
CPython versions tested on:
3.13
Operating systems tested on:
Linux
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
Riproduci i casi segnalati di conversione, padding e allineamento delle f-string su Python 3.13, quindi traccia i punti di ingresso per il parsing e la formattazione delle f-string in CPython. Aggiungi test di regressione che coprano i risultati attesi forniti; il lavoro è completato quando le espressioni auto-documentanti contenenti i due punti corrispondono alla conversione senza due punti e formattano l’espressione completa in modo coerente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100