Commit 25cbf38
authored
## Summary
We use classes like
[`_typeshed._type_checker_internals.NamedTupleFallback`](https://github.com/python/typeshed/blob/d9c76e1d9f0c0000c1971c3e936a69dd55f49ce1/stdlib/_typeshed/_type_checker_internals.pyi#L54-L75)
to tack on additional attributes/methods to instances of user-defined
`NamedTuple`s (or `TypedDict`s), even though these classes are not
present in the MRO of those types.
The problem is that those classes use implicit and explicit `Self`
annotations which refer to `NamedTupleFallback` itself, instead of to
the actual type that we're adding those methods to:
```py
class NamedTupleFallback(tuple[Any, ...]):
# […]
def _replace(self, **kwargs: Any) -> typing_extensions.Self: ...
```
In effect, when we access `_replace` on an instance of a custom
`NamedTuple` instance, its `self` parameter and return type refer to the
wrong `Self`. This leads to incorrect *"Argument to bound method
`_replace` is incorrect: Argument type `Person` does not satisfy upper
bound `NamedTupleFallback` of type variable `Self`"* errors on #18007.
It would also lead to similar errors on `TypedDict`s, if they would
already implement assignability properly.
## Test Plan
I applied the following patch to typeshed and verified that no errors
appear anymore.
<details>
```diff
diff --git a/crates/ty_vendored/vendor/typeshed/stdlib/_typeshed/_type_checker_internals.pyi b/crates/ty_vendored/vendor/typeshed/stdlib/_typeshed/_type_checker_internals.pyi
index feb22aa..8e41034f19 100644
--- a/crates/ty_vendored/vendor/typeshed/stdlib/_typeshed/_type_checker_internals.pyi
+++ b/crates/ty_vendored/vendor/typeshed/stdlib/_typeshed/_type_checker_internals.pyi
@@ -29,27 +29,27 @@ class TypedDictFallback(Mapping[str, object], metaclass=ABCMeta):
__readonly_keys__: ClassVar[frozenset[str]]
__mutable_keys__: ClassVar[frozenset[str]]
- def copy(self) -> typing_extensions.Self: ...
+ def copy(self: typing_extensions.Self) -> typing_extensions.Self: ...
# Using Never so that only calls using mypy plugin hook that specialize the signature
# can go through.
- def setdefault(self, k: Never, default: object) -> object: ...
+ def setdefault(self: typing_extensions.Self, k: Never, default: object) -> object: ...
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
- def pop(self, k: Never, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
- def update(self, m: typing_extensions.Self, /) -> None: ...
- def __delitem__(self, k: Never) -> None: ...
- def items(self) -> dict_items[str, object]: ...
- def keys(self) -> dict_keys[str, object]: ...
- def values(self) -> dict_values[str, object]: ...
+ def pop(self: typing_extensions.Self, k: Never, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
+ def update(self: typing_extensions.Self, m: typing_extensions.Self, /) -> None: ...
+ def __delitem__(self: typing_extensions.Self, k: Never) -> None: ...
+ def items(self: typing_extensions.Self) -> dict_items[str, object]: ...
+ def keys(self: typing_extensions.Self) -> dict_keys[str, object]: ...
+ def values(self: typing_extensions.Self) -> dict_values[str, object]: ...
@overload
- def __or__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
+ def __or__(self: typing_extensions.Self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
@overload
- def __or__(self, value: dict[str, Any], /) -> dict[str, object]: ...
+ def __or__(self: typing_extensions.Self, value: dict[str, Any], /) -> dict[str, object]: ...
@overload
- def __ror__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
+ def __ror__(self: typing_extensions.Self, value: typing_extensions.Self, /) -> typing_extensions.Self: ...
@overload
- def __ror__(self, value: dict[str, Any], /) -> dict[str, object]: ...
+ def __ror__(self: typing_extensions.Self, value: dict[str, Any], /) -> dict[str, object]: ...
# supposedly incompatible definitions of __or__ and __ior__
- def __ior__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ... # type: ignore[misc]
+ def __ior__(self: typing_extensions.Self, value: typing_extensions.Self, /) -> typing_extensions.Self: ... # type: ignore[misc]
# Fallback type providing methods and attributes that appear on all `NamedTuple` types.
class NamedTupleFallback(tuple[Any, ...]):
@@ -61,18 +61,18 @@ class NamedTupleFallback(tuple[Any, ...]):
__orig_bases__: ClassVar[tuple[Any, ...]]
@overload
- def __init__(self, typename: str, fields: Iterable[tuple[str, Any]], /) -> None: ...
+ def __init__(self: typing_extensions.Self, typename: str, fields: Iterable[tuple[str, Any]], /) -> None: ...
@overload
@typing_extensions.deprecated(
"Creating a typing.NamedTuple using keyword arguments is deprecated and support will be removed in Python 3.15"
)
- def __init__(self, typename: str, fields: None = None, /, **kwargs: Any) -> None: ...
+ def __init__(self: typing_extensions.Self, typename: str, fields: None = None, /, **kwargs: Any) -> None: ...
@classmethod
def _make(cls, iterable: Iterable[Any]) -> typing_extensions.Self: ...
- def _asdict(self) -> dict[str, Any]: ...
- def _replace(self, **kwargs: Any) -> typing_extensions.Self: ...
+ def _asdict(self: typing_extensions.Self) -> dict[str, Any]: ...
+ def _replace(self: typing_extensions.Self, **kwargs: Any) -> typing_extensions.Self: ...
if sys.version_info >= (3, 13):
- def __replace__(self, **kwargs: Any) -> typing_extensions.Self: ...
+ def __replace__(self: typing_extensions.Self, **kwargs: Any) -> typing_extensions.Self: ...
# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter.
_S = TypeVar("_S")
```
</details>
1 parent 9a9ebc3 commit 25cbf38
File tree
6 files changed
+188
-7
lines changed- crates/ty_python_semantic
- resources/mdtest
- src
- types
6 files changed
+188
-7
lines changedLines changed: 20 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | | - | |
| 275 | + | |
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
280 | 280 | | |
281 | | - | |
| 281 | + | |
282 | 282 | | |
283 | 283 | | |
284 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
285 | 303 | | |
286 | 304 | | |
287 | 305 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
627 | 627 | | |
628 | 628 | | |
629 | 629 | | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
630 | 642 | | |
631 | 643 | | |
632 | 644 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5971 | 5971 | | |
5972 | 5972 | | |
5973 | 5973 | | |
| 5974 | + | |
| 5975 | + | |
| 5976 | + | |
| 5977 | + | |
| 5978 | + | |
| 5979 | + | |
| 5980 | + | |
| 5981 | + | |
| 5982 | + | |
| 5983 | + | |
| 5984 | + | |
| 5985 | + | |
| 5986 | + | |
5974 | 5987 | | |
5975 | 5988 | | |
5976 | 5989 | | |
| |||
5994 | 6007 | | |
5995 | 6008 | | |
5996 | 6009 | | |
5997 | | - | |
| 6010 | + | |
| 6011 | + | |
5998 | 6012 | | |
5999 | 6013 | | |
6000 | 6014 | | |
| |||
6008 | 6022 | | |
6009 | 6023 | | |
6010 | 6024 | | |
| 6025 | + | |
6011 | 6026 | | |
6012 | 6027 | | |
6013 | 6028 | | |
| |||
6116 | 6131 | | |
6117 | 6132 | | |
6118 | 6133 | | |
| 6134 | + | |
6119 | 6135 | | |
6120 | 6136 | | |
6121 | 6137 | | |
| |||
6127 | 6143 | | |
6128 | 6144 | | |
6129 | 6145 | | |
| 6146 | + | |
6130 | 6147 | | |
6131 | 6148 | | |
6132 | 6149 | | |
| |||
6662 | 6679 | | |
6663 | 6680 | | |
6664 | 6681 | | |
| 6682 | + | |
| 6683 | + | |
6665 | 6684 | | |
6666 | 6685 | | |
6667 | 6686 | | |
| |||
6683 | 6702 | | |
6684 | 6703 | | |
6685 | 6704 | | |
| 6705 | + | |
| 6706 | + | |
| 6707 | + | |
6686 | 6708 | | |
6687 | 6709 | | |
6688 | 6710 | | |
| |||
6704 | 6726 | | |
6705 | 6727 | | |
6706 | 6728 | | |
| 6729 | + | |
| 6730 | + | |
| 6731 | + | |
6707 | 6732 | | |
6708 | 6733 | | |
6709 | 6734 | | |
| |||
6728 | 6753 | | |
6729 | 6754 | | |
6730 | 6755 | | |
| 6756 | + | |
| 6757 | + | |
| 6758 | + | |
6731 | 6759 | | |
6732 | 6760 | | |
6733 | 6761 | | |
| |||
6736 | 6764 | | |
6737 | 6765 | | |
6738 | 6766 | | |
| 6767 | + | |
| 6768 | + | |
| 6769 | + | |
| 6770 | + | |
| 6771 | + | |
| 6772 | + | |
| 6773 | + | |
| 6774 | + | |
| 6775 | + | |
| 6776 | + | |
| 6777 | + | |
| 6778 | + | |
| 6779 | + | |
| 6780 | + | |
| 6781 | + | |
| 6782 | + | |
| 6783 | + | |
| 6784 | + | |
| 6785 | + | |
| 6786 | + | |
| 6787 | + | |
| 6788 | + | |
| 6789 | + | |
| 6790 | + | |
| 6791 | + | |
| 6792 | + | |
| 6793 | + | |
| 6794 | + | |
| 6795 | + | |
| 6796 | + | |
| 6797 | + | |
6739 | 6798 | | |
6740 | 6799 | | |
6741 | 6800 | | |
| |||
7663 | 7722 | | |
7664 | 7723 | | |
7665 | 7724 | | |
| 7725 | + | |
| 7726 | + | |
| 7727 | + | |
| 7728 | + | |
| 7729 | + | |
| 7730 | + | |
| 7731 | + | |
| 7732 | + | |
| 7733 | + | |
| 7734 | + | |
| 7735 | + | |
| 7736 | + | |
| 7737 | + | |
| 7738 | + | |
| 7739 | + | |
| 7740 | + | |
| 7741 | + | |
| 7742 | + | |
| 7743 | + | |
| 7744 | + | |
| 7745 | + | |
7666 | 7746 | | |
7667 | 7747 | | |
7668 | 7748 | | |
| |||
10836 | 10916 | | |
10837 | 10917 | | |
10838 | 10918 | | |
| 10919 | + | |
| 10920 | + | |
| 10921 | + | |
| 10922 | + | |
| 10923 | + | |
| 10924 | + | |
| 10925 | + | |
| 10926 | + | |
| 10927 | + | |
| 10928 | + | |
| 10929 | + | |
| 10930 | + | |
| 10931 | + | |
| 10932 | + | |
| 10933 | + | |
| 10934 | + | |
| 10935 | + | |
| 10936 | + | |
10839 | 10937 | | |
10840 | 10938 | | |
10841 | 10939 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
1975 | 1976 | | |
1976 | 1977 | | |
1977 | 1978 | | |
1978 | | - | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
1979 | 1993 | | |
1980 | 1994 | | |
1981 | 1995 | | |
| |||
2256 | 2270 | | |
2257 | 2271 | | |
2258 | 2272 | | |
| 2273 | + | |
| 2274 | + | |
| 2275 | + | |
| 2276 | + | |
| 2277 | + | |
| 2278 | + | |
| 2279 | + | |
| 2280 | + | |
| 2281 | + | |
| 2282 | + | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
2259 | 2289 | | |
2260 | 2290 | | |
2261 | 2291 | | |
| |||
2578 | 2608 | | |
2579 | 2609 | | |
2580 | 2610 | | |
| 2611 | + | |
| 2612 | + | |
| 2613 | + | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
2581 | 2617 | | |
2582 | 2618 | | |
2583 | 2619 | | |
| |||
2815 | 2851 | | |
2816 | 2852 | | |
2817 | 2853 | | |
2818 | | - | |
| 2854 | + | |
| 2855 | + | |
| 2856 | + | |
| 2857 | + | |
| 2858 | + | |
| 2859 | + | |
| 2860 | + | |
| 2861 | + | |
| 2862 | + | |
| 2863 | + | |
| 2864 | + | |
| 2865 | + | |
2819 | 2866 | | |
2820 | 2867 | | |
2821 | 2868 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
72 | 76 | | |
73 | 77 | | |
74 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
470 | 470 | | |
471 | 471 | | |
472 | 472 | | |
473 | | - | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
474 | 476 | | |
475 | 477 | | |
476 | 478 | | |
| |||
0 commit comments