| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9a10967 commit 43eecdb
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -532,17 +532,6 @@ def instance_alias_type(alias: TypeAlias, | |||
| 532 | 532 | return expand_type_by_instance(tp, target) | |
| 533 | 533 | ||
| 534 | 534 | ||
| 535 | - def is_instance_var(var: Var, info: TypeInfo) -> bool: | ||
| 536 | - """Return if var is an instance variable according to PEP 526.""" | ||
| 537 | - return ( | ||
| 538 | - # check the type_info node is the var (not a decorated function, etc.) | ||
| 539 | - var.name in info.names and info.names[var.name].node is var | ||
| 540 | - and not var.is_classvar | ||
| 541 | - # variables without annotations are treated as classvar | ||
| 542 | - and not var.is_inferred | ||
| 543 | - ) | ||
| 544 | - | ||
| 545 | - | ||
| 546 | 535 | def analyze_var(name: str, | |
| 547 | 536 | var: Var, | |
| 548 | 537 | itype: Instance, | |
@@ -571,12 +560,7 @@ def analyze_var(name: str, | |||
| 571 | 560 | t = get_proper_type(expand_type_by_instance(typ, itype)) | |
| 572 | 561 | result: Type = t | |
| 573 | 562 | typ = get_proper_type(typ) | |
| 574 | - if ( | ||
| 575 | - var.is_initialized_in_class | ||
| 576 | - and not is_instance_var(var, info) | ||
| 577 | - and isinstance(typ, FunctionLike) | ||
| 578 | - and not typ.is_type_obj() | ||
| 579 | - ): | ||
| 563 | + if var.is_initialized_in_class and isinstance(typ, FunctionLike) and not typ.is_type_obj(): | ||
| 580 | 564 | if mx.is_lvalue: | |
| 581 | 565 | if var.is_property: | |
| 582 | 566 | if not var.is_settable_property: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1263,45 +1263,81 @@ reveal_type(A.__dataclass_fields__) # N: Revealed type is "builtins.dict[builti | |||
| 1263 | 1263 | ||
| 1264 | 1264 | [builtins fixtures/dict.pyi] | |
| 1265 | 1265 | ||
| 1266 | - [case testDataclassCallableFieldAccess] | ||
| 1266 | + [case testDataclassCallableProperty] | ||
| 1267 | 1267 | # flags: --python-version 3.7 | |
| 1268 | 1268 | from dataclasses import dataclass | |
| 1269 | 1269 | from typing import Callable | |
| 1270 | 1270 | ||
| 1271 | 1271 | @dataclass | |
| 1272 | 1272 | class A: | |
| 1273 | - x: Callable[[int], int] | ||
| 1274 | - y: Callable[[int], int] = lambda i: i | ||
| 1273 | + foo: Callable[[int], int] | ||
| 1275 | 1274 | ||
| 1276 | - a = A(lambda i:i) | ||
| 1277 | - x: int = a.x(0) | ||
| 1278 | - y: str = a.y(0) # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
| 1279 | - reveal_type(a.x) # N: Revealed type is "def (builtins.int) -> builtins.int" | ||
| 1280 | - reveal_type(a.y) # N: Revealed type is "def (builtins.int) -> builtins.int" | ||
| 1281 | - reveal_type(A.y) # N: Revealed type is "def (builtins.int) -> builtins.int" | ||
| 1275 | + def my_foo(x: int) -> int: | ||
| 1276 | + return x | ||
| 1282 | 1277 | ||
| 1278 | + a = A(foo=my_foo) | ||
| 1279 | + a.foo(1) | ||
| 1280 | + reveal_type(a.foo) # N: Revealed type is "def (builtins.int) -> builtins.int" | ||
| 1281 | + reveal_type(A.foo) # N: Revealed type is "def (builtins.int) -> builtins.int" | ||
| 1282 | + [typing fixtures/typing-medium.pyi] | ||
| 1283 | 1283 | [builtins fixtures/dataclasses.pyi] | |
| 1284 | 1284 | ||
| 1285 | - [case testDataclassCallableFieldAssignment] | ||
| 1285 | + [case testDataclassCallableAssignment] | ||
| 1286 | 1286 | # flags: --python-version 3.7 | |
| 1287 | 1287 | from dataclasses import dataclass | |
| 1288 | 1288 | from typing import Callable | |
| 1289 | 1289 | ||
| 1290 | 1290 | @dataclass | |
| 1291 | 1291 | class A: | |
| 1292 | - x: Callable[[int], int] | ||
| 1292 | + foo: Callable[[int], int] | ||
| 1293 | + | ||
| 1294 | + def my_foo(x: int) -> int: | ||
| 1295 | + return x | ||
| 1293 | 1296 | ||
| 1294 | - def x(i: int) -> int: | ||
| 1295 | - return i | ||
| 1296 | - def x2(s: str) -> str: | ||
| 1297 | - return s | ||
| 1297 | + a = A(foo=my_foo) | ||
| 1298 | 1298 | ||
| 1299 | - a = A(lambda i:i) | ||
| 1300 | - a.x = x | ||
| 1301 | - a.x = x2 # E: Incompatible types in assignment (expression has type "Callable[[str], str]", variable has type "Callable[[int], int]") | ||
| 1299 | + def another_foo(x: int) -> int: | ||
| 1300 | + return x | ||
| 1301 | + | ||
| 1302 | + a.foo = another_foo | ||
| 1303 | + [builtins fixtures/dataclasses.pyi] | ||
| 1302 | 1304 | ||
| 1305 | + [case testDataclassCallablePropertyWrongType] | ||
| 1306 | + # flags: --python-version 3.7 | ||
| 1307 | + from dataclasses import dataclass | ||
| 1308 | + from typing import Callable | ||
| 1309 | + | ||
| 1310 | + @dataclass | ||
| 1311 | + class A: | ||
| 1312 | + foo: Callable[[int], int] | ||
| 1313 | + | ||
| 1314 | + def my_foo(x: int) -> str: | ||
| 1315 | + return "foo" | ||
| 1316 | + | ||
| 1317 | + a = A(foo=my_foo) # E: Argument "foo" to "A" has incompatible type "Callable[[int], str]"; expected "Callable[[int], int]" | ||
| 1318 | + [typing fixtures/typing-medium.pyi] | ||
| 1303 | 1319 | [builtins fixtures/dataclasses.pyi] | |
| 1304 | 1320 | ||
| 1321 | + [case testDataclassCallablePropertyWrongTypeAssignment] | ||
| 1322 | + # flags: --python-version 3.7 | ||
| 1323 | + from dataclasses import dataclass | ||
| 1324 | + from typing import Callable | ||
| 1325 | + | ||
| 1326 | + @dataclass | ||
| 1327 | + class A: | ||
| 1328 | + foo: Callable[[int], int] | ||
| 1329 | + | ||
| 1330 | + def my_foo(x: int) -> int: | ||
| 1331 | + return x | ||
| 1332 | + | ||
| 1333 | + a = A(foo=my_foo) | ||
| 1334 | + | ||
| 1335 | + def another_foo(x: int) -> str: | ||
| 1336 | + return "foo" | ||
| 1337 | + | ||
| 1338 | + a.foo = another_foo # E: Incompatible types in assignment (expression has type "Callable[[int], str]", variable has type "Callable[[int], int]") | ||
| 1339 | + [typing fixtures/typing-medium.pyi] | ||
| 1340 | + [builtins fixtures/dataclasses.pyi] | ||
| 1305 | 1341 | ||
| 1306 | 1342 | [case testDataclassFieldDoesNotFailOnKwargsUnpacking] | |
| 1307 | 1343 | # flags: --python-version 3.7 | |
@@ -1351,7 +1387,6 @@ class Foo: | |||
| 1351 | 1387 | reveal_type(Foo(bar=1.5)) # N: Revealed type is "__main__.Foo" | |
| 1352 | 1388 | [builtins fixtures/dataclasses.pyi] | |
| 1353 | 1389 | ||
| 1354 | - | ||
| 1355 | 1390 | [case testDataclassWithSlotsArg] | |
| 1356 | 1391 | # flags: --python-version 3.10 | |
| 1357 | 1392 | from dataclasses import dataclass | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -599,51 +599,39 @@ A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "i | |||
| 599 | 599 | ||
| 600 | 600 | ||
| 601 | 601 | [case testMethodAsDataAttribute] | |
| 602 | - from typing import Any, Callable, ClassVar | ||
| 602 | + from typing import Any, Callable | ||
| 603 | 603 | class B: pass | |
| 604 | 604 | x = None # type: Any | |
| 605 | 605 | class A: | |
| 606 | - f = x # type: ClassVar[Callable[[A], None]] | ||
| 607 | - g = x # type: ClassVar[Callable[[A, B], None]] | ||
| 606 | + f = x # type: Callable[[A], None] | ||
| 607 | + g = x # type: Callable[[A, B], None] | ||
| 608 | 608 | a = None # type: A | |
| 609 | 609 | a.f() | |
| 610 | 610 | a.g(B()) | |
| 611 | 611 | a.f(a) # E: Too many arguments | |
| 612 | 612 | a.g() # E: Too few arguments | |
| 613 | 613 | ||
| 614 | 614 | [case testMethodWithInvalidMethodAsDataAttribute] | |
| 615 | - from typing import Any, Callable, ClassVar | ||
| 615 | + from typing import Any, Callable | ||
| 616 | 616 | class B: pass | |
| 617 | 617 | x = None # type: Any | |
| 618 | 618 | class A: | |
| 619 | - f = x # type: ClassVar[Callable[[], None]] | ||
| 620 | - g = x # type: ClassVar[Callable[[B], None]] | ||
| 619 | + f = x # type: Callable[[], None] | ||
| 620 | + g = x # type: Callable[[B], None] | ||
| 621 | 621 | a = None # type: A | |
| 622 | 622 | a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument | |
| 623 | 623 | a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]" | |
| 624 | 624 | ||
| 625 | 625 | [case testMethodWithDynamicallyTypedMethodAsDataAttribute] | |
| 626 | - from typing import Any, Callable, ClassVar | ||
| 626 | + from typing import Any, Callable | ||
| 627 | 627 | class B: pass | |
| 628 | 628 | x = None # type: Any | |
| 629 | 629 | class A: | |
| 630 | - f = x # type: ClassVar[Callable[[Any], Any]] | ||
| 630 | + f = x # type: Callable[[Any], Any] | ||
| 631 | 631 | a = None # type: A | |
| 632 | 632 | a.f() | |
| 633 | 633 | a.f(a) # E: Too many arguments | |
| 634 | 634 | ||
| 635 | - [case testMethodWithInferredMethodAsDataAttribute] | ||
| 636 | - from typing import Any | ||
| 637 | - def m(self: "A") -> int: ... | ||
| 638 | - | ||
| 639 | - class A: | ||
| 640 | - n = m | ||
| 641 | - | ||
| 642 | - a = A() | ||
| 643 | - reveal_type(a.n()) # N: Revealed type is "builtins.int" | ||
| 644 | - reveal_type(A.n(a)) # N: Revealed type is "builtins.int" | ||
| 645 | - A.n() # E: Too few arguments | ||
| 646 | - | ||
| 647 | 635 | [case testOverloadedMethodAsDataAttribute] | |
| 648 | 636 | from foo import * | |
| 649 | 637 | [file foo.pyi] | |
@@ -685,35 +673,35 @@ a.g(B()) | |||
| 685 | 673 | a.g(a) # E: Argument 1 has incompatible type "A[B]"; expected "B" | |
| 686 | 674 | ||
| 687 | 675 | [case testInvalidMethodAsDataAttributeInGenericClass] | |
| 688 | - from typing import Any, TypeVar, Generic, Callable, ClassVar | ||
| 676 | + from typing import Any, TypeVar, Generic, Callable | ||
| 689 | 677 | t = TypeVar('t') | |
| 690 | 678 | class B: pass | |
| 691 | 679 | class C: pass | |
| 692 | 680 | x = None # type: Any | |
| 693 | 681 | class A(Generic[t]): | |
| 694 | - f = x # type: ClassVar[Callable[[A[B]], None]] | ||
| 682 | + f = x # type: Callable[[A[B]], None] | ||
| 695 | 683 | ab = None # type: A[B] | |
| 696 | 684 | ac = None # type: A[C] | |
| 697 | 685 | ab.f() | |
| 698 | 686 | ac.f() # E: Invalid self argument "A[C]" to attribute function "f" with type "Callable[[A[B]], None]" | |
| 699 | 687 | ||
| 700 | 688 | [case testPartiallyTypedSelfInMethodDataAttribute] | |
| 701 | - from typing import Any, TypeVar, Generic, Callable, ClassVar | ||
| 689 | + from typing import Any, TypeVar, Generic, Callable | ||
| 702 | 690 | t = TypeVar('t') | |
| 703 | 691 | class B: pass | |
| 704 | 692 | class C: pass | |
| 705 | 693 | x = None # type: Any | |
| 706 | 694 | class A(Generic[t]): | |
| 707 | - f = x # type: ClassVar[Callable[[A], None]] | ||
| 695 | + f = x # type: Callable[[A], None] | ||
| 708 | 696 | ab = None # type: A[B] | |
| 709 | 697 | ac = None # type: A[C] | |
| 710 | 698 | ab.f() | |
| 711 | 699 | ac.f() | |
| 712 | 700 | ||
| 713 | 701 | [case testCallableDataAttribute] | |
| 714 | - from typing import Callable, ClassVar | ||
| 702 | + from typing import Callable | ||
| 715 | 703 | class A: | |
| 716 | - g = None # type: ClassVar[Callable[[A], None]] | ||
| 704 | + g = None # type: Callable[[A], None] | ||
| 717 | 705 | def __init__(self, f: Callable[[], None]) -> None: | |
| 718 | 706 | self.f = f | |
| 719 | 707 | a = A(None) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,12 +25,12 @@ Ord() >= 1 # E: Unsupported operand types for >= ("Ord" and "int") | |||
| 25 | 25 | ||
| 26 | 26 | [case testTotalOrderingLambda] | |
| 27 | 27 | from functools import total_ordering | |
| 28 | - from typing import Any, Callable, ClassVar | ||
| 28 | + from typing import Any, Callable | ||
| 29 | 29 | ||
| 30 | 30 | @total_ordering | |
| 31 | 31 | class Ord: | |
| 32 | - __eq__: ClassVar[Callable[[Any, object], bool]] = lambda self, other: False | ||
| 33 | - __lt__: ClassVar[Callable[[Any, "Ord"], bool]] = lambda self, other: False | ||
| 32 | + __eq__: Callable[[Any, object], bool] = lambda self, other: False | ||
| 33 | + __lt__: Callable[[Any, "Ord"], bool] = lambda self, other: False | ||
| 34 | 34 | ||
| 35 | 35 | reveal_type(Ord() < Ord()) # N: Revealed type is "builtins.bool" | |
| 36 | 36 | reveal_type(Ord() <= Ord()) # N: Revealed type is "builtins.bool" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -366,16 +366,16 @@ reveal_type(x.f) # N: Revealed type is "builtins.int" | |||
| 366 | 366 | [builtins fixtures/property.pyi] | |
| 367 | 367 | ||
| 368 | 368 | [case testSelfTypeProperSupertypeAttribute] | |
| 369 | - from typing import Callable, TypeVar, ClassVar | ||
| 369 | + from typing import Callable, TypeVar | ||
| 370 | 370 | class K: pass | |
| 371 | 371 | T = TypeVar('T', bound=K) | |
| 372 | 372 | class A(K): | |
| 373 | 373 | @property | |
| 374 | 374 | def g(self: K) -> int: return 0 | |
| 375 | 375 | @property | |
| 376 | 376 | def gt(self: T) -> T: return self | |
| 377 | - f: ClassVar[Callable[[object], int]] | ||
| 378 | - ft: ClassVar[Callable[[T], T]] | ||
| 377 | + f: Callable[[object], int] | ||
| 378 | + ft: Callable[[T], T] | ||
| 379 | 379 | ||
| 380 | 380 | class B(A): | |
| 381 | 381 | pass | |
@@ -392,15 +392,15 @@ reveal_type(B().ft()) # N: Revealed type is "__main__.B*" | |||
| 392 | 392 | [builtins fixtures/property.pyi] | |
| 393 | 393 | ||
| 394 | 394 | [case testSelfTypeProperSupertypeAttributeTuple] | |
| 395 | - from typing import Callable, TypeVar, Tuple, ClassVar | ||
| 395 | + from typing import Callable, TypeVar, Tuple | ||
| 396 | 396 | T = TypeVar('T') | |
| 397 | 397 | class A(Tuple[int, int]): | |
| 398 | 398 | @property | |
| 399 | 399 | def g(self: object) -> int: return 0 | |
| 400 | 400 | @property | |
| 401 | 401 | def gt(self: T) -> T: return self | |
| 402 | - f: ClassVar[Callable[[object], int]] | ||
| 403 | - ft: ClassVar[Callable[[T], T]] | ||
| 402 | + f: Callable[[object], int] | ||
| 403 | + ft: Callable[[T], T] | ||
| 404 | 404 | ||
| 405 | 405 | class B(A): | |
| 406 | 406 | pass | |
@@ -450,7 +450,7 @@ reveal_type(X1.ft()) # N: Revealed type is "Type[__main__.X]" | |||
| 450 | 450 | [builtins fixtures/property.pyi] | |
| 451 | 451 | ||
| 452 | 452 | [case testSelfTypeProperSupertypeAttributeGeneric] | |
| 453 | - from typing import Callable, TypeVar, Generic, ClassVar | ||
| 453 | + from typing import Callable, TypeVar, Generic | ||
| 454 | 454 | Q = TypeVar('Q', covariant=True) | |
| 455 | 455 | class K(Generic[Q]): | |
| 456 | 456 | q: Q | |
@@ -460,8 +460,8 @@ class A(K[Q]): | |||
| 460 | 460 | def g(self: K[object]) -> int: return 0 | |
| 461 | 461 | @property | |
| 462 | 462 | def gt(self: K[T]) -> T: return self.q | |
| 463 | - f: ClassVar[Callable[[object], int]] | ||
| 464 | - ft: ClassVar[Callable[[T], T]] | ||
| 463 | + f: Callable[[object], int] | ||
| 464 | + ft: Callable[[T], T] | ||
| 465 | 465 | ||
| 466 | 466 | class B(A[Q]): | |
| 467 | 467 | pass | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -361,7 +361,8 @@ a.a = 1 | |||
| 361 | 361 | a.b = custom_obj | |
| 362 | 362 | a.c = custom_obj | |
| 363 | 363 | a.d = custom_obj | |
| 364 | - a.e = custom_obj | ||
| 364 | + # TODO: Should this be allowed? | ||
| 365 | + a.e = custom_obj # E: Cannot assign to a method | ||
| 365 | 366 | [out] | |
| 366 | 367 | [builtins fixtures/tuple.pyi] | |
| 367 | 368 | [builtins fixtures/dict.pyi] | |
| Back | FazBrowse Home | New Git URL |
0 commit comments