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

Allow self: Self for backwards compatability #14608

Closed
SimpleArt opened this issue Feb 4, 2023 · 3 comments
Closed

Allow self: Self for backwards compatability #14608

SimpleArt opened this issue Feb 4, 2023 · 3 comments
Labels
bug mypy got something wrong

Comments

@SimpleArt
Copy link

Bug Report

Using self: Self or cls: type[Self] results in an error.

To Reproduce

from typing import Self


class A:

    def foo(self: Self) -> Self:
        return self

Expected Behavior

The behavior should be equivalent to the workaround explained in PEP 673 before we had typing.Self. Users which previously used the old code would otherwise have to change all of the code which used the previous workaround to work with the new change or continue to use the old workaround.

# Should be a simple change to `from typing import Self`.
from typing import TypeVar
Self = TypeVar("Self", bound="A")


class A:

    def foo(self: Self) -> Self:
        return self

Actual Behavior

main.py:6: error: Variable "typing.Self" is not valid as a type  [valid-type]
main.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

Requires self: Self to be changed to just self everywhere.

Your Environment

mypy Playground

@SimpleArt SimpleArt added the bug mypy got something wrong label Feb 4, 2023
@AlexWaygood
Copy link
Member

Are you using mypy master? Mypy doesn't support typing.Self yet in any released version, so if you're using mypy 0.991 or older, that's why you're seeing the error. It will support typing.Self in the way you'd like it to in the next release (mypy 1.0).

@AlexWaygood
Copy link
Member

(Follow #13685 for updates on the release schedule.)

@SimpleArt
Copy link
Author

Thanks, I was testing in the linked mypy Playground. Checking back on it with the other versions, you're right in that the master branch works as expected, while every other version failed.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants