Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of PEP 673 (typing.Self) #11666

Closed
wants to merge 24 commits into from
Closed

Conversation

Gobot1234
Copy link
Contributor

Description

An initial implementation of typing.Self type checking for PEP 673.

Couple of questions:

  • Is this implementation actually good?
  • Should think there should be a note about using the old style of this if it's detected?

Test Plan

I've tested the use cases from the PEP individually and it worked but unit test wise I'm not entirely sure what to do should I basically just copy all of https://github.com/python/mypy/blob/master/test-data/unit/check-selftype.test and add some extra stuff for recursive classes and staticmethods?

@JelleZijlstra
Copy link
Member

Did you do this off an old checkout? There's a ton of merge conflicts.

The PEP itself suggested implementing Self by desugaring into a bound TypeVar, which might be easier since it can (hopefully) be done fully in the semanal phase.

@Gobot1234
Copy link
Contributor Author

Did you do this off an old checkout? There's a ton of merge conflicts.

The PEP itself suggested implementing Self by desugaring into a bound TypeVar, which might be easier since it can (hopefully) be done fully in the semanal phase.

  1. Yeah this is quite old, I'll fix the merge conflicts.
  2. I know the PEP suggested desugaring into a bound TypeVar but I really couldn't make that work here. Something was calling deserialize and things were being replaced with FakeInfo which the only work around I found for was replacing the instance value with an unserialized one.

@github-actions

This comment has been minimized.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, very excited for this feature! 👍

Going to leave some minor early comments 🙂

mypy/types.py Outdated Show resolved Hide resolved
mypy/types.py Outdated Show resolved Hide resolved
mypy/types.py Outdated Show resolved Hide resolved
mypy/type_visitor.py Outdated Show resolved Hide resolved
mypy/type_visitor.py Outdated Show resolved Hide resolved
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@MathiasSven
Copy link

I am not completely sure it is due to the implementation, but in the piece of code bellow mypy cannot correctly understand the type of an object that uses this commit's implementation of Self.

from typing import Generic, TypeVar, Any, Optional, Protocol, Callable
from typing_extensions import Self
from abc import ABC

T = TypeVar("T")
K = TypeVar("K")

class ClassProperty(Protocol[T, K]):
    def __init__(self, getter: Callable[[T], K]) -> None: ...
    def __get__(self, obj: None, type: type[T] | None = ...) -> K: ...

def classproperty(f: Callable[[T], K]) -> ClassProperty[T, K]: ...

class ItemSet(Generic[T]):
    def first(self) -> T: ...

class BaseItem(ABC):
    @classproperty
    def set(cls) -> ItemSet[Self]: ...

class FooItem(BaseItem):
    name: str
    def test(self) -> None: ...

reveal_type(FooItem.set)
reveal_type(FooItem.set.first().test())

mypy 0.920+dev.f844fbe435719f8fd756573fcc1e95ac3a3b18cd:

t.py:8: error: Invariant type variable "T" used in protocol where contravariant one is expected  [misc]
t.py:8: error: Invariant type variable "K" used in protocol where covariant one is expected  [misc]
t.py:25: note: Revealed type is "t.ItemSet*[Self@t.BaseItem]"
t.py:26: error: "object" has no attribute "test"  [attr-defined]
t.py:26: note: Revealed type is "Any"

pyright 1.1.197:

t.py:25:13 - info: Type of "FooItem.set" is "ItemSet[FooItem]"
t.py:26:13 - info: Type of "FooItem.set.first().test()" is "None"

The contravariant and covariant issue is separate and I believe it is a problem with master too, by following the prompt it just leads to pyright complaining that T and K should be invariant and then for both it leads to "Cannot use a contravariant type variable as return type", so I assume they should be invariant.

# Conflicts:
#	mypy/constraints.py
#	mypy/semanal.py
#	mypy/subtypes.py
#	mypy/typeanal.py
@github-actions

This comment has been minimized.

@Gobot1234
Copy link
Contributor Author

I'd like a bit of advice on how to be substitute Self into a function signature, should I use a TypeVistor subclass or should I just have a function to do it?

@@ -84,6 +84,10 @@ if sys.version_info >= (3, 10):
TypeAlias: _SpecialForm = ...
TypeGuard: _SpecialForm = ...

if sys.version_info >= (3, 11):
# Self is also a (non-subscriptable) special form.
Self: object = ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't change this directly, it is just copied from typeshed

mypy/typeanal.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@NeilGirdhar
Copy link
Contributor

NeilGirdhar commented May 24, 2022

mypy will reveal it as Type[]

I'm not sure, but it seems that the documentation says nothing about cls being restricted to the type of (or even a subclass of) the enclosing class. Couldn't it be any class?

But it does mention annotating the return type of new as Type[Self]

That's also weird because the documentation doesn't promise that either: "The return value of __new__() should be the new object instance (usually an instance of cls)."

Perhaps things have changed since the docs were written, but it seems that all you can promise a priori is:

@staticmethod
def __new__(cls: type, ...) -> Any

It's not even guaranteed that the return value has the same type.

I think MyPy should automatically apply this basic assumption, and if the user wants to add additional guarantees, they should be free to do so, e.g., __new__(cls: type[T], ...) -> T. I'm not sure what the reasoning is in preventing Self in static methods.

@tmke8
Copy link
Contributor

tmke8 commented May 26, 2022

@Gobot1234
Copy link
Contributor Author

Hi @erikkemperman now that my exams are over would you be ok with me co-authoring you on this as I try to push it over the finish line?

@erikkemperman
Copy link
Contributor

@Gobot1234 That’d be wonderful, actually — I got a bit stuck to be honest, and have been too busy with work stuff to work on this.

For what it’s worth, here is my branch:

https://github.com/erikkemperman/mypy/commits/gobot-master

This is basically your PR, merged with the upstream master branch of end of May, plus some some small fixes to make some tests pass.

At that point I had the feeling that it should be possible to implement this PEP in a somewhat “smaller” approach, i.e. the desugaring strategy you also mentioned, but I did not get very far trying that for lack of time.

I also was slightly confused about some edge cases like __new__ as I mentioned above, but I guess we ought to just follow the signature currently given in typeshed.

I still think it’s a bit odd that mypy handles this method differently if a user explicitly decorates it as static, but that should probably be a separate issue.

@github-actions

This comment has been minimized.

@NeilGirdhar
Copy link
Contributor

Looks like the interaction with class methods is messed up. When Self is returned from a class method, it should mean the an instantiated object right?

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Gobot1234
Copy link
Contributor Author

Gobot1234 commented Jun 27, 2022

steam.py

+ steam/enums.py:110: error: Incompatible types in assignment (expression has type "object", variable has type "Enum")  [assignment]
+ steam/enums.py:111: error: "None" has no attribute "value"  [attr-defined]

I'm able to fix these locally I can push these changes at some point later in the week. ✅ fixed

+ steam/enums.py:237: error: Incompatible return value type (got "object", expected "object")  [return-value]
+ steam/enums.py:241: error: Incompatible return value type (got "object", expected "object")  [return-value]
+ steam/enums.py:245: error: Incompatible return value type (got "object", expected "object")  [return-value]

These are concerning but I'm not entirely what's causing them, cls.__new__ reveals the correct type (it's probably related to the other errors in discord.py)

+ steam/channel.py:185: error: Type argument "object" of "GroupChannelHistoryIterator" must be a subtype of "Union[ClanChannel, GroupChannel]"  [type-var]
+ steam/channel.py:186: error: Incompatible return value type (got "GroupChannelHistoryIterator[GroupM_co, <nothing>]", expected "GroupChannelHistoryIterator[GroupM_co, object]")  [return-value]

First error is catching a valid type error not sure what the second is on about

+ steam/clan.py:277: error: Incompatible types in assignment (expression has type "Union[User, SteamID]", variable has type "Optional[User]")  [assignment]

Is a type error on my end.

+ steam/clan.py:442: error: Incompatible return value type (got "Clan", expected "Event")  [return-value]
+ steam/clan.py:454: error: Incompatible return value type (got "Clan", expected "Announcement")  [return-value]

This error is a genieuly baffling one I'm yet to wrap my head round. These functions (fetch_event and fetch_announcement) return Event and Announcements respectively (they don't even use Self) and this type checks correctly with pyright.
The only thing that I think could be a cause for this is that all of Clan, Event and Announcement share a base class that performs some async initialisation and then returns the object in place (using Self) and that binding is somehow being overwritten but I can't reproduce this with a snippet.

+ steam/ext/commands/commands.py:861: error: Incompatible return value type (got "List[Command[Any]]", expected "List[Group[Any]]")  [return-value]

It seems like this case needs a bit of special casing for the compatiblity checker.

discord.py

+ discord/flags.py:131: error: Signatures of "__ior__" and "__or__" are incompatible
+ discord/flags.py:135: error: Signatures of "__iand__" and "__and__" are incompatible
+ discord/flags.py:139: error: Signatures of "__ixor__" and "__xor__" are incompatible

Something very funky is going on here considering (for or) they are

test.py:212: error: Signatures of "__ior__" and "__or__" are incompatible
test.py:215: note: Revealed type is "def (__main__.Flags, Self@__main__.Flags) -> Self@__main__.Flags"
test.py:216: note: Revealed type is "def (__main__.Flags, Self@__main__.Flags) -> Self@__main__.Flags"
- discord/flags.py:1470: error: No overload variant of "__new__" of "type" matches argument type "Type[Self?]"
+ discord/flags.py:1470: error: No overload variant of "__new__" of "type" matches argument type "Type[object]"
- discord/flags.py:1556: error: "Type[Self?]" has no attribute "VALID_FLAGS"
+ discord/flags.py:1556: error: "Type[object]" has no attribute "VALID_FLAGS"

Something here isn't being bound correctly. ✅ fixed

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Gobot1234
Copy link
Contributor Author

For the + steam/iterators.py:374: error: Argument 1 to "append" of "list" has incompatible type "Type[TradeOffer]"; expected "TradeOffer" [arg-type] I think the error is due TradeOffer being defined in another file which is weird.

class TradeOffer:
    @classmethod
    def _from_history(cls) -> Self:
        trade = cls()
        return trade

class AsyncIterator(Generic[T]):
    _fill: AsyncGeneratorType[T, None]
    def __aiter__(self) -> Self:
        return self

    def __anext__(self) -> Coroutine[None, None, T]:
        return self._fill.__anext__()

    async def next(self) -> T:
        return await self.__anext__()

    async def fill(self) -> AsyncGenerator[T, None]:
        raise NotImplementedError
        yield

class TradesIterator(AsyncIterator["TradeOffer"]):
    async def fill(self) -> AsyncGenerator[TradeOffer, None]:
        async def get_trades(page: int = 100) -> list[TradeOffer]:
            trades: list[TradeOffer] = []
            for trade in ():
                trades.append(TradeOffer._from_history())
            return trades
        for trade in await get_trades():
            yield trade

works.

I also haven't been able to reproduce the tornado errors with a small snippet

import unittest

class AsyncTestCase(unittest.TestCase):
    ...

class AsyncHTTPTestCase(AsyncTestCase):
    ...

class WebSocketBaseTestCase(AsyncHTTPTestCase):
    @types.coroutine
    def foo(self, path: str, **kwargs: Any):
        yield

@overload
def gen_test(
    *, timeout: Union[float, None] = None
) -> Callable[[Callable[..., Union[Generator, "Coroutine"]]], Callable[..., None]]:
    pass


@overload  # noqa: F811
def gen_test(func: Callable[..., Union[Generator, "Coroutine"]]) -> Callable[..., None]:
    pass


def gen_test(  # noqa: F811
    func = None,
    timeout=None,
):
    pass


class WSTest(WebSocketBaseTestCase):
    @gen_test
    def bar(self: Any):
        yield reveal_type(self.assertEqual())  # revealed type is Any

@github-actions
Copy link
Contributor

github-actions bot commented Jul 6, 2022

Diff from mypy_primer, showing the effect of this PR on open source code:

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/enums.py:99: error: Incompatible return value type (got "Type[Enum]", expected "object")  [return-value]
- steam/enums.py:79: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:79: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/enums.py:148: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:148: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/enums.py:177: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:177: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/enums.py:179: error: "Type[Self?]" has no attribute "_value_map_"  [attr-defined]
- steam/enums.py:181: error: No overload variant of "__new__" of "type" matches argument types "Type[Self?]", "str", "Any"  [call-overload]
- steam/enums.py:181: note: Possible overload variants:
- steam/enums.py:181: note:     def __new__(cls, cls: Type[type], object) -> type
- steam/enums.py:181: note:     def [Self] __new__(cls, cls: Type[Self], str, Tuple[type, ...], Dict[str, Any], **kwds: Any) -> Self
- steam/enums.py:199: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:199: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/enums.py:212: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:212: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/enums.py:216: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:216: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/enums.py:503: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:503: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/enums.py:910: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/enums.py:910: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/utils.py:438: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/utils.py:438: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/utils.py:445: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/utils.py:445: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/utils.py:457: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/utils.py:457: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/utils.py:464: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/utils.py:464: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/utils.py:582: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/utils.py:582: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/utils.py:586: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/utils.py:586: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/protobufs/struct_messages.py:15: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/protobufs/struct_messages.py:15: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/protobufs/struct_messages.py:32: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/protobufs/struct_messages.py:32: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/protobufs/struct_messages.py:57: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/protobufs/struct_messages.py:57: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/protobufs/struct_messages.py:70: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/protobufs/struct_messages.py:70: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/role.py:94: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/role.py:94: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/iterators.py:174: error: Self? has no attribute "__anext__"  [attr-defined]
- steam/iterators.py:189: error: Self? has no attribute "__anext__"  [attr-defined]
- steam/iterators.py:225: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/iterators.py:225: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ steam/iterators.py:374: error: Argument 1 to "append" of "list" has incompatible type "Type[TradeOffer]"; expected "TradeOffer"  [arg-type]
- steam/iterators.py:624: error: Self? has no attribute "created_at"  [attr-defined]
- steam/iterators.py:632: error: Self? has no attribute "created_at"  [attr-defined]
- steam/models.py:62: error: "__new__" must return a class instance (got Self?)  [misc]
- steam/models.py:62: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/models.py:62: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/gateway.py:284: error: Self? has no attribute "__anext__"  [attr-defined]
- steam/trade.py:568: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/trade.py:568: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/trade.py:577: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/trade.py:577: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/package.py:147: error: Unsupported left operand type for & (Self?)  [operator]
- steam/abc.py:272: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/abc.py:272: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/abc.py:293: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/abc.py:293: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/abc.py:316: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/abc.py:316: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/abc.py:985: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/abc.py:985: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/abc.py:1012: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/abc.py:1012: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:148: error: "__new__" must return a class instance (got Self?)  [misc]
- steam/manifest.py:148: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:148: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:151: error: Self? has no attribute "_mapping"  [attr-defined]
- steam/manifest.py:152: error: Self? has no attribute "_manifest"  [attr-defined]
- steam/manifest.py:165: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:165: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:171: error: Self? has no attribute "_manifest"  [attr-defined]
- steam/manifest.py:174: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:174: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:178: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:178: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:222: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:222: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:236: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:236: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:242: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:242: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/manifest.py:250: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/manifest.py:250: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/chat.py:276: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/chat.py:276: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/chat.py:321: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/chat.py:321: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/review.py:177: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/review.py:177: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/review.py:206: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/review.py:206: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ steam/clan.py:249: error: Incompatible types in assignment (expression has type "Union[User, SteamID]", variable has type "Optional[User]")  [assignment]
+ steam/clan.py:249: error: Incompatible types in assignment (expression has type "List[Union[User, SteamID]]", variable has type "List[Optional[User]]")  [assignment]
- steam/clan.py:241: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/clan.py:241: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/clan.py:244: error: Self? has no attribute "_id"  [attr-defined]
- steam/clan.py:245: error: Self? has no attribute "tagline"  [attr-defined]
- steam/clan.py:246: error: Self? has no attribute "active_member_count"  [attr-defined]
- steam/clan.py:247: error: Self? has no attribute "game"  [attr-defined]
- steam/clan.py:247: error: Self? has no attribute "_state"  [attr-defined]
- steam/clan.py:249: error: Self? has no attribute "owner"  [attr-defined]
- steam/clan.py:249: error: Self? has no attribute "top_members"  [attr-defined]
- steam/clan.py:249: error: Self? has no attribute "_state"  [attr-defined]
- steam/clan.py:253: error: Self? has no attribute "_populate_roles"  [attr-defined]
- steam/clan.py:254: error: Self? has no attribute "_default_role_id"  [attr-defined]
+ steam/clan.py:343: error: Incompatible return value type (got "Clan", expected "Event[EventType]")  [return-value]
+ steam/clan.py:355: error: Incompatible return value type (got "Clan", expected "Announcement")  [return-value]
+ steam/group.py:59: error: Return type "Coroutine[Any, Any, Group]" of "_from_proto" incompatible with return type "Coroutine[Any, Any, Clan]" in supertype "ChatGroup"  [override]
- steam/state.py:106: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/state.py:106: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ steam/state.py:314: error: Incompatible types in assignment (expression has type "Type[TradeOffer]", variable has type "TradeOffer")  [assignment]
- steam/state.py:950: error: Self? has no attribute "id"  [attr-defined]
- steam/state.py:951: error: Self? has no attribute "_id"  [attr-defined]
- steam/state.py:952: error: Self? has no attribute "_id"  [attr-defined]
- steam/state.py:988: error: Self? has no attribute "_update_channels"  [attr-defined]
- steam/state.py:992: error: Self? has no attribute "id"  [attr-defined]
- steam/state.py:993: error: Self? has no attribute "_id"  [attr-defined]
- steam/state.py:994: error: Self? has no attribute "_id"  [attr-defined]
- steam/state.py:1245: error: Self? has no attribute "__anext__"  [attr-defined]
- steam/ext/commands/cooldown.py:60: error: Item "Channel[Self?]" of "Union[Channel[Self?], Any, SteamID]" has no attribute "id"  [union-attr]
+ steam/ext/commands/cooldown.py:60: error: Item "Channel[object]" of "Union[Channel[object], Any, SteamID]" has no attribute "id"  [union-attr]
- steam/ext/commands/commands.py:234: error: Self? has no attribute "name"  [attr-defined]
- steam/ext/commands/commands.py:237: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/ext/commands/commands.py:237: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ steam/ext/commands/commands.py:841: error: Incompatible return value type (got "List[Command[Any]]", expected "List[Group[Any]]")  [return-value]
- steam/ext/commands/commands.py:828: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]
- steam/ext/commands/commands.py:828: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- steam/ext/commands/cog.py:88: error: Self? has no attribute "__original_kwargs__"  [attr-defined]
+ steam/ext/commands/cog.py:88: error: "Group[Any]" has no attribute "__original_kwargs__"  [attr-defined]
- steam/ext/commands/cog.py:200: error: Self? has no attribute "clean_params"  [attr-defined]
- steam/ext/commands/cog.py:201: error: Self? has no attribute "cog"  [attr-defined]
- steam/ext/commands/cog.py:202: error: Self? has no attribute "clean_params"  [attr-defined]
- steam/ext/commands/help.py:184: error: Self? has no attribute "name"  [attr-defined]

discord.py (https://github.com/Rapptz/discord.py)
+ discord/ui/view.py:218: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
+ https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
+ Please report a bug at https://github.com/python/mypy/issues
+ version: 0.970+dev.7f6e2fbb03b392e39472c86893b143dd71e171b7
+ discord/ui/view.py:218: : note: use --pdb to drop into pdb
- discord/enums.py:100: error: Variable "typing_extensions.Self" is not valid as a type
- discord/enums.py:100: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
+ discord/flags.py:131: error: Signatures of "__ior__" and "__or__" are incompatible
+ discord/flags.py:135: error: Signatures of "__iand__" and "__and__" are incompatible
+ discord/flags.py:139: error: Signatures of "__ixor__" and "__xor__" are incompatible
- discord/flags.py:56: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:56: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:122: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:122: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:123: error: Self? has no attribute "value"
- discord/flags.py:125: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:125: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:126: error: Self? has no attribute "value"
- discord/flags.py:128: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:128: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:129: error: Self? has no attribute "value"
- discord/flags.py:131: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:131: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:132: error: Self? has no attribute "value"
- discord/flags.py:135: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:135: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:136: error: Self? has no attribute "value"
- discord/flags.py:139: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:139: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:140: error: Self? has no attribute "value"
- discord/flags.py:143: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:143: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:1487: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:1487: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:1488: error: No overload variant of "__new__" of "type" matches argument type "Type[Self?]"
- discord/flags.py:1488: note: Possible overload variants:
- discord/flags.py:1488: note:     def __new__(cls, cls: Type[type], object) -> type
- discord/flags.py:1488: note:     def [Self] __new__(cls, cls: Type[Self], str, Tuple[type, ...], Dict[str, Any], **kwds: Any) -> Self
- discord/flags.py:1572: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:1572: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:1574: error: "Type[Self?]" has no attribute "VALID_FLAGS"
- discord/flags.py:1576: error: No overload variant of "__new__" of "type" matches argument type "Type[Self?]"
- discord/flags.py:1576: note: Possible overload variants:
- discord/flags.py:1576: note:     def __new__(cls, cls: Type[type], object) -> type
- discord/flags.py:1576: note:     def [Self] __new__(cls, cls: Type[Self], str, Tuple[type, ...], Dict[str, Any], **kwds: Any) -> Self
- discord/flags.py:1581: error: Variable "typing_extensions.Self" is not valid as a type
- discord/flags.py:1581: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/flags.py:1583: error: No overload variant of "__new__" of "type" matches argument type "Type[Self?]"
- discord/flags.py:1583: note: Possible overload variants:
- discord/flags.py:1583: note:     def __new__(cls, cls: Type[type], object) -> type
- discord/flags.py:1583: note:     def [Self] __new__(cls, cls: Type[Self], str, Tuple[type, ...], Dict[str, Any], **kwds: Any) -> Self
- discord/permissions.py:170: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:170: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:176: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:176: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:185: error: Self? has no attribute "view_channel"
- discord/permissions.py:186: error: Self? has no attribute "read_message_history"
- discord/permissions.py:187: error: Self? has no attribute "value"
- discord/permissions.py:190: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:190: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:192: error: Self? has no attribute "read_messages"
- discord/permissions.py:193: error: Self? has no attribute "send_tts_messages"
- discord/permissions.py:194: error: Self? has no attribute "manage_messages"
- discord/permissions.py:198: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:198: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:224: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:224: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:237: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:237: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:246: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:246: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:261: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:261: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:267: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:267: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:276: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:276: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:292: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:292: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:313: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:313: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/permissions.py:794: error: Variable "typing_extensions.Self" is not valid as a type
- discord/permissions.py:794: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:162: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:162: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:167: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:167: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:173: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:173: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:211: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:211: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:216: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:216: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:237: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:237: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:242: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:242: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:247: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:247: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:255: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:255: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:260: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:260: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:265: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:265: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:270: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:270: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:275: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:275: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:280: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:280: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:285: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:285: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:290: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:290: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:295: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:295: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:300: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:300: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:305: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:305: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:310: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:310: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:315: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:315: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:323: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:323: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:328: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:328: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:333: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:333: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:340: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:340: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:347: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:347: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:354: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:354: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:361: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:361: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:366: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:366: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:371: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:371: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:376: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:376: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:385: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:385: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/colour.py:393: error: Variable "typing_extensions.Self" is not valid as a type
- discord/colour.py:393: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/mentions.py:101: error: Variable "typing_extensions.Self" is not valid as a type
- discord/mentions.py:101: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
- discord/mentions.py:109: error: Variable "typing_extensions.Self" is not valid as a typ

... (truncated 33546 lines) ...

MarcelWilson pushed a commit to bandophahita/screenpy that referenced this pull request Sep 16, 2022
MarcelWilson pushed a commit to bandophahita/screenpy that referenced this pull request Sep 22, 2022
ilevkivskyi added a commit that referenced this pull request Nov 15, 2022
Ref #12840 
Fixes #11871
Fixes #14089

This is an alternative implementation to two existing PRs:
#11666,
#13133. This PR treats `typing.Self`
as pure syntactic sugar, and transforms it into a type variable early
during semantic analyzis.

This way we can re-use all the existing machinery and handled edge cases
for self-types. The only new thing is self-type for _attributes_ (as
proposed in the PEP). This required handling in several places, since
attribute access is duplicated in several places (see #7724), plus
special forms (like NamedTuples and TypedDicts) and dataclasses plugin
require additional care, since they use attribute annotations in special
ways.

I don't copy all the existing tests for "old style" self-types, but only
some common use cases, possible error conditions, and relevant new edge
cases, such as e.g. special forms mentioned above, and implicit type
variable binding for callable types.
@ilevkivskyi
Copy link
Member

Superseded by #14041

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.