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

PYI034 false positive if Self is a quoted annotation #12928

Closed
BenGale93 opened this issue Aug 16, 2024 · 5 comments · Fixed by #12951
Closed

PYI034 false positive if Self is a quoted annotation #12928

BenGale93 opened this issue Aug 16, 2024 · 5 comments · Fixed by #12951
Labels
accepted Ready for implementation

Comments

@BenGale93
Copy link

List of keywords searched:

  • PYI034

Code example:

import typing as t

if t.TYPE_CHECKING:
    from typing_extensions import Self


class Foo:
    def __init__(self, x: int) -> None:
        self.x = x

    def __iadd__(self, other: "Self") -> "Self":
        """Adds two Foo objects together."""
        self.x += other.x
        return self

Command: ruff check file.py --isolated --select=PYI034

Ruff Version: 0.5.7

I'd be happy to try and fix this as I have some Rust experience. Please let me know if I can be of help.

@AlexWaygood AlexWaygood added help wanted Contributions especially welcome accepted Ready for implementation labels Aug 16, 2024
@AlexWaygood
Copy link
Member

I'd be happy to try and fix this as I have some Rust experience. Please let me know if I can be of help.

Please feel free! We have logic elsewhere for handling quoted annotations, so I'd definitely go and check how other rules do this kind of thing first

@BenGale93
Copy link
Author

I think this might be more of a fundamental issue.

If I extend the example:

import typing as t

if t.TYPE_CHECKING:
    from typing import Any

    from typing_extensions import Self


class Foo:
    def __init__(self, x: int) -> None:
        self.x = x

    def __iadd__(self, other: "Self") -> "Self":
        self.x += other.x
        return self

    def __eq__(self, value: "Any") -> bool:
        return True

PYI031 should pick up the Any in the __eq__ but it doesn't. Appreciate the example is a little contrived.

I've been combing through the code on how to resolve it but I'm struggling. Any suggestions?

@AlexWaygood
Copy link
Member

Hmm, yeah, this was a bit more complicated than I realised. Unfortunately in order to figure out how to fix it, I've basically had to go ahead and implement the fix locally, so I might just go ahead and file a PR myself if that's okay :/ Sorry -- I thought this was a good contributor issue but I think I was mistaken.

@AlexWaygood
Copy link
Member

I've filed #12951

@AlexWaygood AlexWaygood removed the help wanted Contributions especially welcome label Aug 17, 2024
@BenGale93
Copy link
Author

Thanks @AlexWaygood! No worries about taking over, I'm certain I would have never come up with that fix.

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

Successfully merging a pull request may close this issue.

2 participants