Skip to content

Allow self: Self for backwards compatability #14608

Closed as not planned
Closed as not planned
@SimpleArt

Description

@SimpleArt

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions