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

Error message for typing_extensions.Self is confusing #14089

Closed
orsinium opened this issue Nov 14, 2022 · 2 comments · Fixed by #14041
Closed

Error message for typing_extensions.Self is confusing #14089

orsinium opened this issue Nov 14, 2022 · 2 comments · Fixed by #14041
Labels
bug mypy got something wrong

Comments

@orsinium
Copy link

orsinium commented Nov 14, 2022

Bug Report

It looks like mypy doesn't support typing_extensions.Self. I know that Rome wasn't built in a day and the work in #11871 is on the way. However, while it's still in progress, I think it would be great to have a user-friendly error message that better explains the nature of the error.

To Reproduce

from typing_extensions import Self

class A:
    def f(self) -> Self:
        return self

Expected Behavior

I'd expect a user-friendly error message explaining the nature of the issue, something like this:

tmp.py:5: note: "typing_extensions.Self" is not supported yet, see #11871  [misc]

Actual Behavior

tmp.py:5: error: Variable "typing_extensions.Self" is not valid as a type  [valid-type]

Your Environment

  • Mypy version used: 0.990
  • Mypy command-line flags: mypy tmp.py
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.10.6

#14089 (comment)

@orsinium orsinium added the bug mypy got something wrong label Nov 14, 2022
@cdce8p
Copy link
Collaborator

cdce8p commented Nov 14, 2022

It looks like mypy doesn't support typing_extensions.Self. I know that Rome wasn't built in a day and the work in #11871 is on the way. However, while it's still in progress, I think it would be great to have a user-friendly error message that better explains the nature of the error.

There is actually an open PR to add full support for Self: #14041. It should get merged in the next two weeks and will then be included in the next mypy release.

@orsinium
Copy link
Author

Great! Let's then link these two together, just in case the PR gets stuck. @ilevkivskyi could you add "Fixes #14089" into the description of #14041, please?

ilevkivskyi added a commit that referenced this issue 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.
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

Successfully merging a pull request may close this issue.

2 participants