77import functools
88import itertools
99import pickle
10- from string .templatelib import Template
10+ from string .templatelib import Template , Interpolation
1111import typing
1212import unittest
1313from annotationlib import (
@@ -282,6 +282,7 @@ def f(
282282 a : t "a{b}c{d}e{f}g" ,
283283 b : t "{a:{1}}" ,
284284 c : t "{a | b * c}" ,
285+ gh138558 : t "{ 0}" ,
285286 ): pass
286287
287288 annos = get_annotations (f , format = Format .STRING )
@@ -293,6 +294,7 @@ def f(
293294 # interpolations in the format spec are eagerly evaluated so we can't recover the source
294295 "b" : "t'{a:1}'" ,
295296 "c" : "t'{a | b * c}'" ,
297+ "gh138558" : "t'{ 0}'" ,
296298 })
297299
298300 def g (
@@ -1350,6 +1352,20 @@ def nested():
13501352 self .assertEqual (type_repr ("1" ), "'1'" )
13511353 self .assertEqual (type_repr (Format .VALUE ), repr (Format .VALUE ))
13521354 self .assertEqual (type_repr (MyClass ()), "my repr" )
1355+ # gh138558 tests
1356+ self .assertEqual (type_repr (t '''{ 0
1357+ & 1
1358+ | 2
1359+ }''' ), 't"""{ 0\n & 1\n | 2}"""' )
1360+ self .assertEqual (
1361+ type_repr (Template ("hi" , Interpolation (42 , "42" ))), "t'hi{42}'"
1362+ )
1363+ self .assertEqual (
1364+ type_repr (Template ("hi" , Interpolation (42 ))),
1365+ "Template('hi', Interpolation(42, '', None, ''))" ,
1366+ )
1367+ # gh138558: perhaps in the future, we can improve this behavior:
1368+ self .assertEqual (type_repr (Template (Interpolation (42 , "99" ))), "t'{99}'" )
13531369
13541370
13551371class TestAnnotationsToString (unittest .TestCase ):
0 commit comments