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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
[case testNewNamedTupleNoUnderscoreFields]
from typing import NamedTuple
class X(NamedTuple):
x: int
_y: int # E: NamedTuple field name cannot start with an underscore: _y
_z: int # E: NamedTuple field name cannot start with an underscore: _z
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleAccessingAttributes]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
x: X
x.x
x.y
x.z # E: "X" has no attribute "z"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleAttributesAreReadOnly]
from typing import NamedTuple
class X(NamedTuple):
x: int
x: X
x.x = 5 # E: Property "x" defined in "X" is read-only
x.y = 5 # E: "X" has no attribute "y"
class A(X): pass
a: A
a.x = 5 # E: Property "x" defined in "X" is read-only
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleCreateWithPositionalArguments]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
x = X(1, '2')
x.x
x.z # E: "X" has no attribute "z"
x = X(1) # E: Missing positional argument "y" in call to "X"
x = X(1, '2', 3) # E: Too many arguments for "X"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleShouldBeSingleBase]
from typing import NamedTuple
class A: ...
class X(NamedTuple, A): # E: NamedTuple should be a single base
pass
[builtins fixtures/tuple.pyi]
[case testCreateNewNamedTupleWithKeywordArguments]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
x = X(x=1, y='x')
x = X(1, y='x')
x = X(x=1, z=1) # E: Unexpected keyword argument "z" for "X"
x = X(y='x') # E: Missing positional argument "x" in call to "X"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleCreateAndUseAsTuple]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
x = X(1, 'x')
a, b = x
a, b, c = x # E: Need more than 2 values to unpack (3 expected)
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleWithItemTypes]
from typing import NamedTuple
class N(NamedTuple):
a: int
b: str
n = N(1, 'x')
s: str = n.a # E: Incompatible types in assignment (expression has type "int", \
variable has type "str")
i: int = n.b # E: Incompatible types in assignment (expression has type "str", \
variable has type "int")
x, y = n
if int():
x = y # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleConstructorArgumentTypes]
from typing import NamedTuple
class N(NamedTuple):
a: int
b: str
n = N('x', 'x') # E: Argument 1 to "N" has incompatible type "str"; expected "int"
n = N(1, b=2) # E: Argument "b" to "N" has incompatible type "int"; expected "str"
N(1, 'x')
N(b='x', a=1)
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleAsBaseClass]
from typing import NamedTuple
class N(NamedTuple):
a: int
b: str
class X(N):
pass
x = X(1, 2) # E: Argument 2 to "X" has incompatible type "int"; expected "str"
s = ''
i = 0
if int():
s = x.a # E: Incompatible types in assignment (expression has type "int", variable has type "str")
if int():
i, s = x
if int():
s, s = x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleSelfTypeWithNamedTupleAsBase]
from typing import NamedTuple
class A(NamedTuple):
a: int
b: str
class B(A):
def f(self, x: int) -> None:
self.f(self.a)
self.f(self.b) # E: Argument 1 to "f" of "B" has incompatible type "str"; expected "int"
i = 0
s = ''
if int():
i, s = self
i, i = self # E: Incompatible types in assignment (expression has type "str", \
variable has type "int")
[builtins fixtures/tuple.pyi]
[out]
[case testNewNamedTupleTypeReferenceToClassDerivedFrom]
from typing import NamedTuple
class A(NamedTuple):
a: int
b: str
class B(A):
def f(self, x: 'B') -> None:
i = 0
s = ''
if int():
self = x
i, s = x
i, s = x.a, x.b
i, s = x.a, x.a # E: Incompatible types in assignment (expression has type "int", \
variable has type "str")
i, i = self # E: Incompatible types in assignment (expression has type "str", \
variable has type "int")
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleSubtyping]
from typing import NamedTuple, Tuple
class A(NamedTuple):
a: int
b: str
class B(A): pass
a = A(1, '')
b = B(1, '')
t: Tuple[int, str]
if int():
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a = t # E: Incompatible types in assignment (expression has type "tuple[int, str]", variable has type "A")
if int():
b = t # E: Incompatible types in assignment (expression has type "tuple[int, str]", variable has type "B")
if int():
t = a
if int():
t = (1, '')
if int():
t = b
if int():
a = b
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleSimpleTypeInference]
from typing import NamedTuple, Tuple
class A(NamedTuple):
a: int
l = [A(1), A(2)]
a = A(1)
a = l[0]
(i,) = l[0]
i, i = l[0] # E: Need more than 1 value to unpack (2 expected)
l = [A(1)]
a = (1,) # E: Incompatible types in assignment (expression has type "tuple[int]", \
variable has type "A")
[builtins fixtures/list.pyi]
[case testNewNamedTupleMissingClassAttribute]
from typing import NamedTuple
class MyNamedTuple(NamedTuple):
a: int
b: str
MyNamedTuple.x # E: "type[MyNamedTuple]" has no attribute "x"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleEmptyItems]
from typing import NamedTuple
class A(NamedTuple):
...
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleForwardRef]
from typing import NamedTuple
class A(NamedTuple):
b: 'B'
class B: ...
a = A(B())
a = A(1) # E: Argument 1 to "A" has incompatible type "int"; expected "B"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleProperty36]
from typing import NamedTuple
class A(NamedTuple):
a: int
class B(A):
@property
def b(self) -> int:
return self.a
class C(B): pass
B(1).b
C(2).b
[builtins fixtures/property.pyi]
[case testNewNamedTupleAsDict]
from typing import NamedTuple, Any
class X(NamedTuple):
x: Any
y: Any
x: X
reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any]"
[builtins fixtures/dict.pyi]
[case testNewNamedTupleReplaceTyped]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
x: X
reveal_type(x._replace()) # N: Revealed type is "tuple[builtins.int, builtins.str, fallback=__main__.X]"
x._replace(x=5)
x._replace(y=5) # E: Argument "y" to "_replace" of "X" has incompatible type "int"; expected "str"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleFields]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
reveal_type(X._fields) # N: Revealed type is "tuple[builtins.str, builtins.str]"
reveal_type(X._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]"
reveal_type(X._field_defaults) # N: Revealed type is "builtins.dict[builtins.str, Any]"
# In typeshed's stub for builtins.pyi, __annotations__ is `dict[str, Any]`,
# but it's inferred as `Mapping[str, object]` here due to the fixture we're using
reveal_type(X.__annotations__) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]"
[builtins fixtures/dict-full.pyi]
[case testNewNamedTupleUnit]
from typing import NamedTuple
class X(NamedTuple):
pass
x: X = X()
x._replace()
x._fields[0] # E: Tuple index out of range
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleJoinNamedTuple]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
class Y(NamedTuple):
x: int
y: str
reveal_type([X(3, 'b'), Y(1, 'a')]) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]"
[builtins fixtures/list.pyi]
[case testNewNamedTupleJoinTuple]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: str
reveal_type([(3, 'b'), X(1, 'a')]) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]"
reveal_type([X(1, 'a'), (3, 'b')]) # N: Revealed type is "builtins.list[tuple[builtins.int, builtins.str]]"
[builtins fixtures/list.pyi]
[case testNewNamedTupleWithTooManyArguments]
from typing import NamedTuple
class X(NamedTuple):
x: int
y = z = 2 # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
def f(self): pass
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleWithInvalidItems2]
import typing
class X(typing.NamedTuple):
x: int
y = 1 # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
x.x: int # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
z: str = 'z'
aa: int # E: Non-default NamedTuple fields cannot follow default fields
[builtins fixtures/list.pyi]
[case testNewNamedTupleWithoutTypesSpecified]
from typing import NamedTuple
class X(NamedTuple):
x: int
y = 2 # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
[builtins fixtures/tuple.pyi]
[case testTypeUsingTypeCNamedTuple]
from typing import NamedTuple, Type
class N(NamedTuple):
x: int
y: str
def f(a: Type[N]):
a() # E: Missing positional arguments "x", "y" in call to "N"
[builtins fixtures/list.pyi]
[case testNewNamedTupleWithDefaults]
from typing import List, NamedTuple, Optional
class X(NamedTuple):
x: int
y: int = 2
reveal_type(X(1)) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.X]"
reveal_type(X(1, 2)) # N: Revealed type is "tuple[builtins.int, builtins.int, fallback=__main__.X]"
X(1, 'a') # E: Argument 2 to "X" has incompatible type "str"; expected "int"
X(1, z=3) # E: Unexpected keyword argument "z" for "X"
class HasNone(NamedTuple):
x: int
y: Optional[int] = None
reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, Union[builtins.int, None], fallback=__main__.HasNone]"
class Parameterized(NamedTuple):
x: int
y: List[int] = [1] + [2]
z: List[int] = []
reveal_type(Parameterized(1)) # N: Revealed type is "tuple[builtins.int, builtins.list[builtins.int], builtins.list[builtins.int], fallback=__main__.Parameterized]"
Parameterized(1, ['not an int']) # E: List item 0 has incompatible type "str"; expected "int"
class Default:
pass
class UserDefined(NamedTuple):
x: Default = Default()
reveal_type(UserDefined()) # N: Revealed type is "tuple[__main__.Default, fallback=__main__.UserDefined]"
reveal_type(UserDefined(Default())) # N: Revealed type is "tuple[__main__.Default, fallback=__main__.UserDefined]"
UserDefined(1) # E: Argument 1 to "UserDefined" has incompatible type "int"; expected "Default"
[builtins fixtures/list.pyi]
[case testNewNamedTupleWithDefaultsStrictOptional]
from typing import List, NamedTuple, Optional
class HasNone(NamedTuple):
x: int
y: Optional[int] = None
reveal_type(HasNone(1)) # N: Revealed type is "tuple[builtins.int, Union[builtins.int, None], fallback=__main__.HasNone]"
HasNone(None) # E: Argument 1 to "HasNone" has incompatible type "None"; expected "int"
HasNone(1, y=None)
HasNone(1, y=2)
class CannotBeNone(NamedTuple):
x: int
y: int = None # E: Incompatible types in assignment (expression has type "None", variable has type "int")
[builtins fixtures/list.pyi]
[case testNewNamedTupleWrongType]
from typing import NamedTuple
class X(NamedTuple):
x: int
y: int = 'not an int' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleErrorInDefault]
from typing import NamedTuple
class X(NamedTuple):
x: int = 1 + '1' # E: Unsupported left operand type for + ("int")
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleInheritance]
from typing import NamedTuple
class X(NamedTuple):
x: str
y: int = 3
class Y(X):
def method(self) -> str:
self.y
return self.x
reveal_type(Y('a')) # N: Revealed type is "tuple[builtins.str, builtins.int, fallback=__main__.Y]"
Y(y=1, x='1').method()
class CallsBaseInit(X):
def __init__(self, x: str) -> None:
super().__init__(x) # E: Too many arguments for "__init__" of "object"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleWithMethods]
from typing import NamedTuple
class XMeth(NamedTuple):
x: int
def double(self) -> int:
return self.x
async def asyncdouble(self) -> int:
return self.x
class XRepr(NamedTuple):
x: int
y: int = 1
def __str__(self) -> str:
return 'string'
def __sub__(self, other: XRepr) -> int:
return 0
reveal_type(XMeth(1).double()) # N: Revealed type is "builtins.int"
_ = reveal_type(XMeth(1).asyncdouble()) # N: Revealed type is "typing.Coroutine[Any, Any, builtins.int]"
reveal_type(XMeth(42).x) # N: Revealed type is "builtins.int"
reveal_type(XRepr(42).__str__()) # N: Revealed type is "builtins.str"
reveal_type(XRepr(1, 2).__sub__(XRepr(3))) # N: Revealed type is "builtins.int"
[typing fixtures/typing-async.pyi]
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleOverloading]
from typing import NamedTuple, overload
class Overloader(NamedTuple):
x: int
@overload
def method(self, y: str) -> str: pass
@overload
def method(self, y: int) -> int: pass
def method(self, y):
return y
reveal_type(Overloader(1).method('string')) # N: Revealed type is "builtins.str"
reveal_type(Overloader(1).method(1)) # N: Revealed type is "builtins.int"
Overloader(1).method(('tuple',)) # E: No overload variant of "method" of "Overloader" matches argument type "tuple[str]" \
# N: Possible overload variants: \
# N: def method(self, y: str) -> str \
# N: def method(self, y: int) -> int
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleMethodInheritance]
from typing import NamedTuple, TypeVar
T = TypeVar('T')
class Base(NamedTuple):
x: int
def copy(self: T) -> T:
reveal_type(self) # N: Revealed type is "T`-1"
return self
def good_override(self) -> int:
reveal_type(self) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Base]"
reveal_type(self[0]) # N: Revealed type is "builtins.int"
self[0] = 3 # E: Unsupported target for indexed assignment ("Base")
reveal_type(self.x) # N: Revealed type is "builtins.int"
self.x = 3 # E: Property "x" defined in "Base" is read-only
self[1] # E: Tuple index out of range
reveal_type(self[T]) # N: Revealed type is "builtins.int" \
# E: No overload variant of "__getitem__" of "tuple" matches argument type "TypeVar" \
# N: Possible overload variants: \
# N: def __getitem__(self, int, /) -> int \
# N: def __getitem__(self, slice, /) -> tuple[int, ...]
return self.x
def bad_override(self) -> int:
return self.x
class Child(Base):
def new_method(self) -> int:
reveal_type(self) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Child]"
reveal_type(self[0]) # N: Revealed type is "builtins.int"
self[0] = 3 # E: Unsupported target for indexed assignment ("Child")
reveal_type(self.x) # N: Revealed type is "builtins.int"
self.x = 3 # E: Property "x" defined in "Base" is read-only
self[1] # E: Tuple index out of range
return self.x
def good_override(self) -> int:
return 0
def bad_override(self) -> str: # E: Return type "str" of "bad_override" incompatible with return type "int" in supertype "Base"
return 'incompatible'
def takes_base(base: Base) -> int:
return base.x
reveal_type(Base(1).copy()) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Base]"
reveal_type(Child(1).copy()) # N: Revealed type is "tuple[builtins.int, fallback=__main__.Child]"
reveal_type(Base(1).good_override()) # N: Revealed type is "builtins.int"
reveal_type(Child(1).good_override()) # N: Revealed type is "builtins.int"
reveal_type(Base(1).bad_override()) # N: Revealed type is "builtins.int"
reveal_type(takes_base(Base(1))) # N: Revealed type is "builtins.int"
reveal_type(takes_base(Child(1))) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]
[case testNewNamedTupleIllegalNames]
from typing import Callable, NamedTuple
class XMethBad(NamedTuple):
x: int
def _fields(self): # E: Cannot overwrite NamedTuple attribute "_fields"
return 'no chance for this'
class MagicalFields(NamedTuple):
x: int
def __slots__(self) -> None: pass # E: Cannot overwrite NamedTuple attribute "__slots__"
def __new__(cls) -> MagicalFields: pass # E: Cannot overwrite NamedTuple attribute "__new__"
def _source(self) -> int: pass # E: Cannot overwrite NamedTuple attribute "_source"
__annotations__ = {'x': float} # E: NamedTuple field name cannot start with an underscore: __annotations__ \
# E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]" \
# E: Cannot overwrite NamedTuple attribute "__annotations__"
class AnnotationsAsAMethod(NamedTuple):
x: int
# This fails at runtime because typing.py assumes that __annotations__ is a dictionary.
def __annotations__(self) -> float: # E: Cannot overwrite NamedTuple attribute "__annotations__"
return 1.0
class ReuseNames(NamedTuple):
x: int
def x(self) -> str: # E: Name "x" already defined on line 22
return ''
def y(self) -> int:
return 0
y: str # E: Name "y" already defined on line 26
class ReuseCallableNamed(NamedTuple):
z: Callable[[ReuseNames], int]
def z(self) -> int: # E: Name "z" already defined on line 31
return 0
[builtins fixtures/dict.pyi]
[case testNewNamedTupleDocString]
from typing import NamedTuple
class Documented(NamedTuple):
"""This is a docstring."""
x: int
reveal_type(Documented.__doc__) # N: Revealed type is "builtins.str"
reveal_type(Documented(1).x) # N: Revealed type is "builtins.int"
class BadDoc(NamedTuple):
x: int
def __doc__(self) -> str:
return ''
reveal_type(BadDoc(1).__doc__()) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]
[case testNewNamedTupleClassMethod]
from typing import NamedTuple
class HasClassMethod(NamedTuple):
x: str
@classmethod
def new(cls, f: str) -> 'HasClassMethod':
reveal_type(cls) # N: Revealed type is "type[tuple[builtins.str, fallback=__main__.HasClassMethod]]"
reveal_type(HasClassMethod) # N: Revealed type is "def (x: builtins.str) -> tuple[builtins.str, fallback=__main__.HasClassMethod]"
return cls(x=f)
[builtins fixtures/classmethod.pyi]
[case testNewNamedTupleStaticMethod]
from typing import NamedTuple
class HasStaticMethod(NamedTuple):
x: str
@staticmethod
def new(f: str) -> 'HasStaticMethod':
return HasStaticMethod(x=f)
[builtins fixtures/classmethod.pyi]
[case testNewNamedTupleProperty]
from typing import NamedTuple
class HasStaticMethod(NamedTuple):
x: str
@property
def size(self) -> int:
reveal_type(self) # N: Revealed type is "tuple[builtins.str, fallback=__main__.HasStaticMethod]"
return 4
[builtins fixtures/property.pyi]
[case testTypingExtensionsNamedTuple]
from typing_extensions import NamedTuple
class Point(NamedTuple):
x: int
y: int
bad_point = Point('foo') # E: Missing positional argument "y" in call to "Point" \
# E: Argument 1 to "Point" has incompatible type "str"; expected "int"
point = Point(1, 2)
x, y = point
x = point.x
reveal_type(x) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "builtins.int"
point.y = 6 # E: Property "y" defined in "Point" is read-only
[builtins fixtures/tuple.pyi]
|