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

Doing variable: typing.Optional[typing.Self] = self will fail #16418

Closed
McDic opened this issue Nov 7, 2023 · 1 comment
Closed

Doing variable: typing.Optional[typing.Self] = self will fail #16418

McDic opened this issue Nov 7, 2023 · 1 comment
Labels
bug mypy got something wrong topic-self-types Types for self

Comments

@McDic
Copy link

McDic commented Nov 7, 2023

Bug Report

If you do variable: typing.Optional[typing.Self] = self in method of your class, you will get a mypy error.

To Reproduce

import typing

class TestClass:
    def __init__(self, parent: typing.Optional[typing.Self]) -> None:
        self.parent = parent

    def get_depth(self) -> int:
        current: typing.Optional[typing.Self] = self
        iteration = 0
        while current is not None:
            iteration += 1
            current = current.parent
        return iteration - 1

Expected Behavior

There should be no error.

Actual Behavior

You will get following error on current = self part.

error: Incompatible types in assignment (expression has type "TestClass", variable has type "Self | None")  [assignment]

Your Environment

  • Mypy version used: 1.6.1
  • Mypy command-line flags: mypy (filename)
  • Mypy configuration options from mypy.ini (and other config files): Nothing
  • Python version used: 3.11.6
@ilevkivskyi
Copy link
Member

This is duplicate of #14075

@ilevkivskyi ilevkivskyi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 9, 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 topic-self-types Types for self
Projects
None yet
Development

No branches or pull requests

3 participants