1
1
[case testStrSplit]
2
- from typing import Optional, List
2
+ from typing import NewType, Optional, List, Union
3
+ NewStr = NewType("NewStr", str)
3
4
4
- def do_split(s: str, sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]:
5
+ def do_split(s: Union[ str, NewStr] , sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]:
5
6
if sep is not None:
6
7
if max_split is not None:
7
8
return s.split(sep, max_split)
8
9
else:
9
10
return s.split(sep)
10
11
return s.split()
12
+ [typing fixtures/typing-full.pyi]
11
13
[out]
12
14
def do_split(s, sep, max_split):
13
15
s :: str
56
58
57
59
58
60
[case testStrEquality]
61
+ from typing import NewType, Union
62
+ NewStr = NewType("NewStr", str)
59
63
def eq(x: str, y: str) -> bool:
60
64
return x == y
61
65
62
- def neq(x: str, y: str) -> bool:
66
+ def neq(x: str, y: Union[ str, NewStr] ) -> bool:
63
67
return x != y
64
68
69
+ [typing fixtures/typing-full.pyi]
65
70
[out]
66
71
def eq(x, y):
67
72
x, y :: str
79
84
return r1
80
85
81
86
[case testStrReplace]
82
- from typing import Optional
83
-
84
- def do_replace(s: str, old_substr: str, new_substr: str, max_count: Optional[int] = None) -> str:
87
+ from typing import NewType, Optional, Union
88
+ NewStr = NewType("NewStr", str)
89
+ def do_replace(s: Union[ str, NewStr] , old_substr: str, new_substr: str, max_count: Optional[int] = None) -> str:
85
90
if max_count is not None:
86
91
return s.replace(old_substr, new_substr, max_count)
87
92
else:
88
93
return s.replace(old_substr, new_substr)
94
+ [typing fixtures/typing-full.pyi]
89
95
[out]
90
96
def do_replace(s, old_substr, new_substr, max_count):
91
97
s, old_substr, new_substr :: str
@@ -114,17 +120,19 @@ L5:
114
120
unreachable
115
121
116
122
[case testStrStartswithEndswithTuple]
117
- from typing import Tuple
123
+ from typing import NewType, Tuple, Union
124
+ NewStr = NewType("NewStr", str)
118
125
119
- def do_startswith(s1: str, s2: Tuple[str, ...]) -> bool:
126
+ def do_startswith(s1: Union[ str, NewStr] , s2: Tuple[str, ...]) -> bool:
120
127
return s1.startswith(s2)
121
128
122
- def do_endswith(s1: str, s2: Tuple[str, ...]) -> bool:
129
+ def do_endswith(s1: Union[ str, NewStr] , s2: Tuple[str, ...]) -> bool:
123
130
return s1.endswith(s2)
124
131
125
- def do_tuple_literal_args(s1: str) -> None:
132
+ def do_tuple_literal_args(s1: Union[ str, NewStr] ) -> None:
126
133
x = s1.startswith(("a", "b"))
127
134
y = s1.endswith(("a", "b"))
135
+ [typing fixtures/typing-full.pyi]
128
136
[out]
129
137
def do_startswith(s1, s2):
130
138
s1 :: str
@@ -165,11 +173,14 @@ L0:
165
173
return 1
166
174
167
175
[case testStrToBool]
168
- def is_true(x: str) -> bool:
176
+ from typing import NewType, Union
177
+ NewStr = NewType("NewStr", str)
178
+ def is_true(x: Union[str, NewStr]) -> bool:
169
179
if x:
170
180
return True
171
181
else:
172
182
return False
183
+ [typing fixtures/typing-full.pyi]
173
184
[out]
174
185
def is_true(x):
175
186
x :: str
@@ -185,11 +196,14 @@ L3:
185
196
unreachable
186
197
187
198
[case testStringFormatMethod]
188
- def f(s: str, num: int) -> None:
199
+ from typing import NewType, Union
200
+ NewStr = NewType("NewStr", str)
201
+ def f(s: Union[str, NewStr], num: int) -> None:
189
202
s1 = "Hi! I'm {}, and I'm {} years old.".format(s, num)
190
203
s2 = ''.format()
191
204
s3 = 'abc'.format()
192
205
s4 = '}}{}{{{}}}{{{}'.format(num, num, num)
206
+ [typing fixtures/typing-full.pyi]
193
207
[out]
194
208
def f(s, num):
195
209
s :: str
@@ -217,11 +231,14 @@ L0:
217
231
return 1
218
232
219
233
[case testFStrings_64bit]
220
- def f(var: str, num: int) -> None:
234
+ from typing import NewType, Union
235
+ NewStr = NewType("NewStr", str)
236
+ def f(var: Union[str, NewStr], num: int) -> None:
221
237
s1 = f"Hi! I'm {var}. I am {num} years old."
222
238
s2 = f'Hello {var:>{num}}'
223
239
s3 = f''
224
240
s4 = f'abc'
241
+ [typing fixtures/typing-full.pyi]
225
242
[out]
226
243
def f(var, num):
227
244
var :: str
267
284
return 1
268
285
269
286
[case testStringFormattingCStyle]
270
- def f(var: str, num: int) -> None:
287
+ from typing import NewType, Union
288
+ NewStr = NewType("NewStr", str)
289
+ def f(var: Union[str, NewStr], num: int) -> None:
271
290
s1 = "Hi! I'm %s." % var
272
291
s2 = "I am %d years old." % num
273
292
s3 = "Hi! I'm %s. I am %d years old." % (var, num)
322
341
return 1
323
342
324
343
[case testEncode_64bit]
325
- def f(s: str) -> None:
344
+ from typing import NewType, Union
345
+ NewStr = NewType("NewStr", str)
346
+ def f(s: Union[str, NewStr]) -> None:
326
347
s.encode()
327
348
s.encode('utf-8')
328
349
s.encode('utf8', 'strict')
@@ -340,6 +361,7 @@ def f(s: str) -> None:
340
361
s.encode(encoding=encoding, errors=errors)
341
362
s.encode('latin2')
342
363
364
+ [typing fixtures/typing-full.pyi]
343
365
[out]
344
366
def f(s):
345
367
s :: str
410
432
return 1
411
433
412
434
[case testOrd]
413
- def str_ord(x: str) -> int:
435
+ from typing import NewType, Union
436
+ NewStr = NewType("NewStr", str)
437
+ def str_ord(x: Union[str, NewStr]) -> int:
414
438
return ord(x)
415
439
def str_ord_literal() -> int:
416
440
return ord("a")
@@ -420,6 +444,7 @@ def bytes_ord_literal() -> int:
420
444
return ord(b"a")
421
445
def any_ord(x) -> int:
422
446
return ord(x)
447
+ [typing fixtures/typing-full.pyi]
423
448
[out]
424
449
def str_ord(x):
425
450
x :: str
@@ -459,13 +484,16 @@ L0:
459
484
return r6
460
485
461
486
[case testStrip]
462
- def do_strip(s: str) -> None:
487
+ from typing import NewType, Union
488
+ NewStr = NewType("NewStr", str)
489
+ def do_strip(s: Union[str, NewStr]) -> None:
463
490
s.lstrip("x")
464
491
s.strip("y")
465
492
s.rstrip("z")
466
493
s.lstrip()
467
494
s.strip()
468
495
s.rstrip()
496
+ [typing fixtures/typing-full.pyi]
469
497
[out]
470
498
def do_strip(s):
471
499
s, r0, r1, r2, r3, r4, r5, r6, r7, r8 :: str
@@ -481,60 +509,99 @@ L0:
481
509
r8 = CPyStr_RStrip(s, 0)
482
510
return 1
483
511
484
- [case testCountAll]
512
+ [case testCountAll_64bit]
513
+ from typing import NewType, Union
514
+ NewStr = NewType("NewStr", str)
485
515
def do_count(s: str) -> int:
486
- return s.count("x") # type: ignore [attr-defined]
516
+ return s.count("x")
517
+ [typing fixtures/typing-full.pyi]
487
518
[out]
488
519
def do_count(s):
489
520
s, r0 :: str
490
521
r1 :: native_int
491
- r2 :: bit
492
- r3 :: object
493
- r4 :: int
522
+ r2, r3, r4 :: bit
523
+ r5, r6, r7 :: int
494
524
L0:
495
525
r0 = 'x'
496
526
r1 = CPyStr_Count(s, r0, 0)
497
527
r2 = r1 >= 0 :: signed
498
- r3 = box(native_int, r1)
499
- r4 = unbox(int, r3)
500
- return r4
528
+ r3 = r1 <= 4611686018427387903 :: signed
529
+ if r3 goto L1 else goto L2 :: bool
530
+ L1:
531
+ r4 = r1 >= -4611686018427387904 :: signed
532
+ if r4 goto L3 else goto L2 :: bool
533
+ L2:
534
+ r5 = CPyTagged_FromInt64(r1)
535
+ r6 = r5
536
+ goto L4
537
+ L3:
538
+ r7 = r1 << 1
539
+ r6 = r7
540
+ L4:
541
+ return r6
501
542
502
- [case testCountStart]
543
+ [case testCountStart_64bit]
544
+ from typing import NewType, Union
545
+ NewStr = NewType("NewStr", str)
503
546
def do_count(s: str, start: int) -> int:
504
- return s.count("x", start) # type: ignore [attr-defined]
547
+ return s.count("x", start)
548
+ [typing fixtures/typing-full.pyi]
505
549
[out]
506
550
def do_count(s, start):
507
551
s :: str
508
552
start :: int
509
553
r0 :: str
510
554
r1 :: native_int
511
- r2 :: bit
512
- r3 :: object
513
- r4 :: int
555
+ r2, r3, r4 :: bit
556
+ r5, r6, r7 :: int
514
557
L0:
515
558
r0 = 'x'
516
559
r1 = CPyStr_Count(s, r0, start)
517
560
r2 = r1 >= 0 :: signed
518
- r3 = box(native_int, r1)
519
- r4 = unbox(int, r3)
520
- return r4
561
+ r3 = r1 <= 4611686018427387903 :: signed
562
+ if r3 goto L1 else goto L2 :: bool
563
+ L1:
564
+ r4 = r1 >= -4611686018427387904 :: signed
565
+ if r4 goto L3 else goto L2 :: bool
566
+ L2:
567
+ r5 = CPyTagged_FromInt64(r1)
568
+ r6 = r5
569
+ goto L4
570
+ L3:
571
+ r7 = r1 << 1
572
+ r6 = r7
573
+ L4:
574
+ return r6
521
575
522
- [case testCountStartEnd]
576
+ [case testCountStartEnd_64bit]
577
+ from typing import NewType, Union
578
+ NewStr = NewType("NewStr", str)
523
579
def do_count(s: str, start: int, end: int) -> int:
524
- return s.count("x", start, end) # type: ignore [attr-defined]
580
+ return s.count("x", start, end)
581
+ [typing fixtures/typing-full.pyi]
525
582
[out]
526
583
def do_count(s, start, end):
527
584
s :: str
528
585
start, end :: int
529
586
r0 :: str
530
587
r1 :: native_int
531
- r2 :: bit
532
- r3 :: object
533
- r4 :: int
588
+ r2, r3, r4 :: bit
589
+ r5, r6, r7 :: int
534
590
L0:
535
591
r0 = 'x'
536
592
r1 = CPyStr_CountFull(s, r0, start, end)
537
593
r2 = r1 >= 0 :: signed
538
- r3 = box(native_int, r1)
539
- r4 = unbox(int, r3)
540
- return r4
594
+ r3 = r1 <= 4611686018427387903 :: signed
595
+ if r3 goto L1 else goto L2 :: bool
596
+ L1:
597
+ r4 = r1 >= -4611686018427387904 :: signed
598
+ if r4 goto L3 else goto L2 :: bool
599
+ L2:
600
+ r5 = CPyTagged_FromInt64(r1)
601
+ r6 = r5
602
+ goto L4
603
+ L3:
604
+ r7 = r1 << 1
605
+ r6 = r7
606
+ L4:
607
+ return r6
0 commit comments