1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
-- String interpolation
-- --------------------
[case testStringInterpolationType]
from typing import Tuple
i: int
f: float
s: str
t: Tuple[int]
'%d' % i
'%f' % f
'%s' % s
'%d' % (f,)
'%d' % (s,) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]")
'%d' % t
'%d' % s # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]")
'%f' % s # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsFloat]")
'%x' % f # E: Incompatible types in string interpolation (expression has type "float", placeholder has type "int")
'%i' % f
'%o' % f # E: Incompatible types in string interpolation (expression has type "float", placeholder has type "int")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationSAcceptsAnyType]
from typing import Any
i: int
o: object
s: str
'%s %s %s' % (i, o, s)
[builtins fixtures/primitives.pyi]
[case testStringInterpolationSBytesVsStrErrorPy3]
xb: bytes
xs: str
'%s' % xs # OK
'%s' % xb # E: If x = b'abc' then "%s" % x produces "b'abc'", not "abc". If this is desired behavior use "%r" % x. Otherwise, decode the bytes
'%(name)s' % {'name': b'value'} # E: If x = b'abc' then "%s" % x produces "b'abc'", not "abc". If this is desired behavior use "%r" % x. Otherwise, decode the bytes
[builtins fixtures/primitives.pyi]
[case testStringInterpolationCount]
'%d %d' % 1 # E: Not enough arguments for format string
'%d %d' % (1, 2)
'%d %d' % (1, 2, 3) # E: Not all arguments converted during string formatting
t = 1, 's'
'%d %s' % t
'%s %d' % t # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsInt]")
'%d' % t # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationWithAnyType]
from typing import Any
a = None # type: Any
'%d %d' % a
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationInvalidPlaceholder]
'%W' % 1 # E: Unsupported format character "W"
'%b' % 1 # E: Format character "b" is only supported on bytes patterns
[case testStringInterpolationWidth]
'%2f' % 3.14
'%*f' % 3.14 # E: Not enough arguments for format string
'%*f' % (4, 3.14)
'%*f' % (1.1, 3.14) # E: * wants int
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationPrecision]
'%.2f' % 3.14
'%.*f' % 3.14 # E: Not enough arguments for format string
'%.*f' % (4, 3.14)
'%.*f' % (1.1, 3.14) # E: * wants int
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationWidthAndPrecision]
'%4.2f' % 3.14
'%4.*f' % 3.14 # E: Not enough arguments for format string
'%*.2f' % 3.14 # E: Not enough arguments for format string
'%*.*f' % 3.14 # E: Not enough arguments for format string
'%*.*f' % (4, 2, 3.14)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationFlagsAndLengthModifiers]
'%04hd' % 1
'%-.4ld' % 1
'%+*Ld' % (1, 1)
'% .*ld' % (1, 1)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationDoublePercentage]
'%% %d' % 1
'%3% %d' % 1
'%*%' % 1
'%*% %d' % 1 # E: Not enough arguments for format string
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationC]
'%c' % 1
'%c' % 1.0 # E: "%c" requires int or char (expression has type "float")
'%c' % 's'
'%c' % '' # E: "%c" requires int or char
'%c' % 'ab' # E: "%c" requires int or char
'%c' % b'a' # E: "%c" requires int or char (expression has type "bytes")
'%c' % b'' # E: "%c" requires int or char (expression has type "bytes")
'%c' % b'ab' # E: "%c" requires int or char (expression has type "bytes")
[builtins fixtures/primitives.pyi]
[case testStringInterpolationMappingTypes]
'%(a)d %(b)s' % {'a': 1, 'b': 's'}
'%(a)d %(b)s' % {'a': 's', 'b': 1} # E: Incompatible types in string interpolation (expression has type "str", placeholder with key 'a' has type "Union[int, float, SupportsInt]")
b'%(x)s' % {b'x': b'data'}
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationMappingKeys]
'%()d' % {'': 2}
'%(a)d' % {'a': 1, 'b': 2, 'c': 3}
'%(q)d' % {'a': 1, 'b': 2, 'c': 3} # E: Key "q" not found in mapping
'%(a)d %%' % {'a': 1}
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationMappingDictTypes]
from typing import Any, Dict, Iterable
class StringThing:
def keys(self) -> Iterable[str]:
...
def __getitem__(self, __key: str) -> str:
...
class BytesThing:
def keys(self) -> Iterable[bytes]:
...
def __getitem__(self, __key: bytes) -> str:
...
a: Any
ds: Dict[str, int]
do: Dict[object, int]
di: Dict[int, int]
'%(a)' % 1 # E: Format requires a mapping (expression has type "int", expected type for mapping is "SupportsKeysAndGetItem[str, Any]")
'%()d' % a
'%()d' % ds
'%()d' % do # E: Format requires a mapping (expression has type "dict[object, int]", expected type for mapping is "SupportsKeysAndGetItem[str, Any]")
b'%()d' % ds # E: Format requires a mapping (expression has type "dict[str, int]", expected type for mapping is "SupportsKeysAndGetItem[bytes, Any]")
'%()s' % StringThing()
b'%()s' % BytesThing()
[builtins fixtures/primitives.pyi]
[case testStringInterpolationMappingInvalidSpecifiers]
'%(a)d %d' % 1 # E: String interpolation mixes specifier with and without mapping keys
'%(b)*d' % 1 # E: String interpolation contains both stars and mapping keys
'%(b).*d' % 1 # E: String interpolation contains both stars and mapping keys
[case testStringInterpolationMappingFlagsAndLengthModifiers]
'%(a)1d' % {'a': 1}
'%(a).1d' % {'a': 1}
'%(a)#1.1ld' % {'a': 1}
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationFloatPrecision]
'%.f' % 1.2
'%.3f' % 1.2
'%.f' % 'x'
'%.3f' % 'x'
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[out]
main:3: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsFloat]")
main:4: error: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float, SupportsFloat]")
[case testStringInterpolationSpaceKey]
'%( )s' % {' ': 'foo'}
[case testStringInterpolationStarArgs]
x = (1, 2)
"%d%d" % (*x,)
[typing fixtures/typing-medium.pyi]
[builtins fixtures/tuple.pyi]
[case testStringInterpolationVariableLengthTuple]
from typing import Tuple
def f(t: Tuple[int, ...]) -> None:
'%d %d' % t
'%d %d %d' % t
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testStringInterpolationUnionType]
from typing import Tuple, Union
a: Union[Tuple[int, str], Tuple[str, int]] = ('A', 1)
'%s %s' % a
'%s' % a # E: Not all arguments converted during string formatting
b: Union[Tuple[int, str], Tuple[int, int], Tuple[str, int]] = ('A', 1)
'%s %s' % b
'%s %s %s' % b # E: Not enough arguments for format string
c: Union[Tuple[str, int], Tuple[str, int, str]] = ('A', 1)
'%s %s' % c # E: Not all arguments converted during string formatting
[builtins fixtures/tuple.pyi]
[case testStringInterpolationIterableType]
from typing import Sequence, List, Tuple, Iterable
t1: Sequence[str] = ('A', 'B')
t2: List[str] = ['A', 'B']
t3: Tuple[str, ...] = ('A', 'B')
t4: Tuple[str, str] = ('A', 'B')
t5: Iterable[str] = ('A', 'B')
'%s %s' % t1
'%s %s' % t2
'%s %s' % t3
'%s %s %s' % t3
'%s %s' % t4
'%s %s %s' % t4 # E: Not enough arguments for format string
'%s %s' % t5
[builtins fixtures/tuple.pyi]
-- Bytes interpolation
-- --------------------
[case testBytesInterpolation]
b'%b' % 1 # E: Incompatible types in string interpolation (expression has type "int", placeholder has type "bytes")
b'%b' % b'1'
b'%a' % 3
[case testBytesInterpolationC]
b'%c' % 1
b'%c' % 1.0 # E: "%c" requires an integer in range(256) or a single byte (expression has type "float")
b'%c' % 's' # E: "%c" requires an integer in range(256) or a single byte (expression has type "str")
b'%c' % '' # E: "%c" requires an integer in range(256) or a single byte (expression has type "str")
b'%c' % 'ab' # E: "%c" requires an integer in range(256) or a single byte (expression has type "str")
b'%c' % b'a'
b'%c' % b'' # E: "%c" requires an integer in range(256) or a single byte
b'%c' % b'aa' # E: "%c" requires an integer in range(256) or a single byte
[builtins fixtures/primitives.pyi]
[case testByteByteInterpolation]
def foo(a: bytes, b: bytes):
b'%s:%s' % (a, b)
foo(b'a', b'b') == b'a:b'
[builtins fixtures/tuple.pyi]
[case testBytePercentInterpolationSupported]
b'%s' % (b'xyz',)
b'%(name)s' % {'name': b'jane'} # E: Dictionary keys in bytes formatting must be bytes, not strings
b'%(name)s' % {b'name': 'jane'} # E: On Python 3 b'%s' requires bytes, not string
b'%c' % (123)
[builtins fixtures/tuple.pyi]
-- str.format() calls
-- ------------------
[case testFormatCallParseErrors]
'}'.format() # E: Invalid conversion specifier in format string: unexpected }
'{'.format() # E: Invalid conversion specifier in format string: unmatched {
'}}'.format() # OK
'{{'.format() # OK
'{{}}}'.format() # E: Invalid conversion specifier in format string: unexpected }
'{{{}}'.format() # E: Invalid conversion specifier in format string: unexpected }
'{}}{{}'.format() # E: Invalid conversion specifier in format string: unexpected }
'{{{}:{}}}'.format(0) # E: Cannot find replacement for positional format specifier 1
[builtins fixtures/primitives.pyi]
[case testFormatCallValidationErrors]
'{!}}'.format(0) # E: Invalid conversion specifier in format string: unexpected }
'{!x}'.format(0) # E: Invalid conversion type "x", must be one of "r", "s" or "a"
'{!:}'.format(0) # E: Invalid conversion specifier in format string
'{{}:s}'.format(0) # E: Invalid conversion specifier in format string: unexpected }
'{{}.attr}'.format(0) # E: Invalid conversion specifier in format string: unexpected }
'{{}[key]}'.format(0) # E: Invalid conversion specifier in format string: unexpected }
'{ {}:s}'.format() # E: Conversion value must not contain { or }
'{ {}.attr}'.format() # E: Conversion value must not contain { or }
'{ {}[key]}'.format() # E: Conversion value must not contain { or }
[builtins fixtures/primitives.pyi]
[case testFormatCallEscaping]
'{}'.format() # E: Cannot find replacement for positional format specifier 0
'{}'.format(0) # OK
'{{}}'.format() # OK
'{{}}'.format(0) # E: Not all arguments converted during string formatting
'{{{}}}'.format() # E: Cannot find replacement for positional format specifier 0
'{{{}}}'.format(0) # OK
'{{}} {} {{}}'.format(0) # OK
'{{}} {:d} {{}} {:d}'.format('a', 'b') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
'foo({}, {}) == {{}} ({{}} expected)'.format(0) # E: Cannot find replacement for positional format specifier 1
'foo({}, {}) == {{}} ({{}} expected)'.format(0, 1) # OK
'foo({}, {}) == {{}} ({{}} expected)'.format(0, 1, 2) # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]
[case testFormatCallNestedFormats]
'{:{}{}}'.format(42, '*') # E: Cannot find replacement for positional format specifier 2
'{:{}{}}'.format(42, '*', '^') # OK
'{:{}{}}'.format(42, '*', '^', 0) # E: Not all arguments converted during string formatting
# NOTE: we don't check format specifiers that contain { or } at all
'{:{{}}}'.format() # E: Cannot find replacement for positional format specifier 0
'{:{:{}}}'.format() # E: Formatting nesting must be at most two levels deep
'{:{{}:{}}}'.format() # E: Invalid conversion specifier in format string: unexpected }
'{!s:{fill:d}{align}}'.format(42, fill='*', align='^') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
[builtins fixtures/primitives.pyi]
[case testFormatCallAutoNumbering]
'{}, {{}}, {0}'.format() # E: Cannot combine automatic field numbering and manual field specification
'{0}, {1}, {}'.format() # E: Cannot combine automatic field numbering and manual field specification
'{0}, {1}, {0}'.format(1, 2, 3) # E: Not all arguments converted during string formatting
'{}, {other:+d}, {}'.format(1, 2, other='no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
'{0}, {other}, {}'.format() # E: Cannot combine automatic field numbering and manual field specification
'{:{}}, {:{:.5d}{}}'.format(1, 2, 3, 'a', 5) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
[builtins fixtures/primitives.pyi]
[case testFormatCallMatchingPositional]
'{}'.format(positional='no') # E: Cannot find replacement for positional format specifier 0 \
# E: Not all arguments converted during string formatting
'{.x}, {}, {}'.format(1, 'two', 'three') # E: "int" has no attribute "x"
'Reverse {2.x}, {1}, {0}'.format(1, 2, 'three') # E: "str" has no attribute "x"
''.format(1, 2) # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]
[case testFormatCallMatchingNamed]
'{named}'.format(0) # E: Cannot find replacement for named format specifier "named" \
# E: Not all arguments converted during string formatting
'{one.x}, {two}'.format(one=1, two='two') # E: "int" has no attribute "x"
'{one}, {two}, {.x}'.format(1, one='two', two='three') # E: "int" has no attribute "x"
''.format(stuff='yes') # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]
[case testFormatCallMatchingVarArg]
from typing import List
args: List[int] = []
'{}, {}'.format(1, 2, *args) # Don't flag this because args may be empty
strings: List[str]
'{:d}, {[0].x}'.format(*strings) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int") \
# E: "str" has no attribute "x"
# TODO: this is a runtime error, but error message is confusing
'{[0][:]:d}'.format(*strings) # E: Syntax error in format specifier "0[0]["
[builtins fixtures/primitives.pyi]
[case testFormatCallMatchingKwArg]
from typing import Dict
kwargs: Dict[str, str] = {}
'{one}, {two}'.format(one=1, two=2, **kwargs) # Don't flag this because args may be empty
'{stuff:.3d}'.format(**kwargs) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
'{stuff[0]:f}, {other}'.format(**kwargs) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")
'{stuff[0]:c}'.format(**kwargs)
[builtins fixtures/primitives.pyi]
[case testFormatCallCustomFormatSpec]
from typing import Union
class Bad:
...
class Good:
def __format__(self, spec: str) -> str: ...
'{:OMG}'.format(Good())
'{:OMG}'.format(Bad()) # E: Unrecognized format specification "OMG"
'{!s:OMG}'.format(Good()) # E: Unrecognized format specification "OMG"
'{:{}OMG{}}'.format(Bad(), 'too', 'dynamic')
x: Union[Good, Bad]
'{:OMG}'.format(x) # E: Unrecognized format specification "OMG"
[builtins fixtures/primitives.pyi]
[case testFormatCallFormatTypes]
'{:x}'.format(42)
'{:E}'.format(42)
'{:g}'.format(42)
'{:x}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
'{:E}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")
'{:g}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")
'{:n}'.format(3.14)
'{:d}'.format(3.14) # E: Incompatible types in string interpolation (expression has type "float", placeholder has type "int")
'{:s}'.format(42)
'{:s}'.format('yes')
'{:z}'.format('what') # E: Unsupported format character "z"
'{:Z}'.format('what') # E: Unsupported format character "Z"
[builtins fixtures/primitives.pyi]
[case testFormatCallFormatTypesChar]
'{:c}'.format(42)
'{:c}'.format('no') # E: ":c" requires int or char
'{:c}'.format('c')
class C:
...
'{:c}'.format(C()) # E: Incompatible types in string interpolation (expression has type "C", placeholder has type "Union[int, str]")
x: str
'{:c}'.format(x)
[builtins fixtures/primitives.pyi]
[case testFormatCallFormatTypesCustomFormat]
from typing import Union
class Bad:
...
class Good:
def __format__(self, spec: str) -> str: ...
x: Union[Good, Bad]
y: Union[Good, int]
z: Union[Bad, int]
t: Union[Good, str]
'{:d}'.format(x) # E: Incompatible types in string interpolation (expression has type "Bad", placeholder has type "int")
'{:d}'.format(y)
'{:d}'.format(z) # E: Incompatible types in string interpolation (expression has type "Bad", placeholder has type "int")
'{:d}'.format(t) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
[builtins fixtures/primitives.pyi]
[case testFormatCallFormatTypesBytes]
from typing import Union, TypeVar, NewType, Generic
A = TypeVar('A', str, bytes)
B = TypeVar('B', bound=bytes)
x: Union[str, bytes]
a: str
b: bytes
N = NewType('N', bytes)
n: N
'{}'.format(a)
'{}'.format(b) # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
'{}'.format(x) # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
'{}'.format(n) # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
f'{b}' # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
f'{x}' # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
f'{n}' # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
class C(Generic[B]):
x: B
def meth(self) -> None:
'{}'.format(self.x) # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
def func(x: A) -> A:
'{}'.format(x) # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
return x
'{!r}'.format(a)
'{!r}'.format(b)
'{!r}'.format(x)
'{!r}'.format(n)
f'{a}'
f'{a!r}'
f'{b!r}'
f'{x!r}'
f'{n!r}'
class D(bytes):
def __str__(self) -> str:
return "overrides __str__ of bytes"
'{}'.format(D())
[builtins fixtures/primitives.pyi]
[case testNoSpuriousFormattingErrorsDuringFailedOverlodMatch]
from typing import overload, Callable
@overload
def sub(pattern: str, repl: Callable[[str], str]) -> str: ...
@overload
def sub(pattern: bytes, repl: Callable[[bytes], bytes]) -> bytes: ...
def sub(pattern: object, repl: object) -> object:
pass
def better_snakecase(text: str) -> str:
# Mypy used to emit a spurious error here
# warning about interpolating bytes into an f-string:
text = sub(r"([A-Z])([A-Z]+)([A-Z](?:[^A-Z]|$))", lambda match: f"{match}")
return text
[builtins fixtures/primitives.pyi]
[case testFormatCallFinal]
from typing import Final
FMT: Final = '{.x}, {:{:d}}'
FMT.format(1, 2, 'no') # E: "int" has no attribute "x" \
# E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
[builtins fixtures/primitives.pyi]
[case testFormatCallFinalChar]
from typing import Final
GOOD: Final = 'c'
BAD: Final = 'no'
OK: Final[str] = '...'
'{:c}'.format(GOOD)
'{:c}'.format(BAD) # E: ":c" requires int or char
'{:c}'.format(OK)
[builtins fixtures/primitives.pyi]
[case testFormatCallForcedConversions]
'{!r}'.format(42)
'{!s}'.format(42)
'{!s:d}'.format(42) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int")
'{!s:s}'.format('OK')
'{} and {!x}'.format(0, 1) # E: Invalid conversion type "x", must be one of "r", "s" or "a"
[builtins fixtures/primitives.pyi]
[case testFormatCallAccessorsBasic]
from typing import Any
x: Any
'{.x:{[0]}}'.format('yes', 42) # E: "str" has no attribute "x" \
# E: Value of type "int" is not indexable
'{.1+}'.format(x) # E: Syntax error in format specifier "0.1+"
'{name.x[x]()[x]:.2f}'.format(name=x) # E: Only index and member expressions are allowed in format field accessors; got "name.x[x]()[x]"
[builtins fixtures/primitives.pyi]
[case testFormatCallAccessorsIndices]
from typing import TypedDict
class User(TypedDict):
id: int
name: str
u: User
'{user[name]:.3f}'.format(user=u) # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")
def f() -> str: ...
'{[f()]}'.format(u) # E: Invalid index expression in format field accessor "[f()]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-typeddict.pyi]
[case testFormatCallFlags]
from typing import Union
class Good:
def __format__(self, spec: str) -> str: ...
'{:#}'.format(42)
'{:#}'.format('no') # E: Numeric flags are only allowed for numeric types
'{!s:#}'.format(42) # E: Numeric flags are only allowed for numeric types
'{:#s}'.format(42) # E: Numeric flags are only allowed for numeric types
'{:+s}'.format(42) # E: Numeric flags are only allowed for numeric types
'{:+d}'.format(42)
'{:#d}'.format(42)
x: Union[float, Good]
'{:+f}'.format(x)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testFormatCallSpecialCases]
'{:08b}'.format(int('3'))
class S:
def __int__(self) -> int: ...
'{:+d}'.format(S()) # E: Incompatible types in string interpolation (expression has type "S", placeholder has type "int")
'%d' % S() # This is OK however
'{:%}'.format(0.001)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-medium.pyi]
[case testEnumWithStringToFormatValue]
from enum import Enum
class Responses(str, Enum):
TEMPLATED = 'insert {} here'
TEMPLATED_WITH_KW = 'insert {value} here'
NORMAL = 'something'
Responses.TEMPLATED.format(42)
Responses.TEMPLATED_WITH_KW.format(value=42)
Responses.TEMPLATED.format() # E: Cannot find replacement for positional format specifier 0
Responses.TEMPLATED_WITH_KW.format() # E: Cannot find replacement for named format specifier "value"
Responses.NORMAL.format(42) # E: Not all arguments converted during string formatting
Responses.NORMAL.format(value=42) # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]
[case testNonStringEnumToFormatValue]
from enum import Enum
class Responses(Enum):
TEMPLATED = 'insert {value} here'
Responses.TEMPLATED.format(value=42) # E: "Responses" has no attribute "format"
[builtins fixtures/primitives.pyi]
[case testStrEnumWithStringToFormatValue]
# flags: --python-version 3.11
from enum import StrEnum
class Responses(StrEnum):
TEMPLATED = 'insert {} here'
TEMPLATED_WITH_KW = 'insert {value} here'
NORMAL = 'something'
Responses.TEMPLATED.format(42)
Responses.TEMPLATED_WITH_KW.format(value=42)
Responses.TEMPLATED.format() # E: Cannot find replacement for positional format specifier 0
Responses.TEMPLATED_WITH_KW.format() # E: Cannot find replacement for named format specifier "value"
Responses.NORMAL.format(42) # E: Not all arguments converted during string formatting
Responses.NORMAL.format(value=42) # E: Not all arguments converted during string formatting
[builtins fixtures/primitives.pyi]
|