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

useless-super-delegation when an override changes the return type annotation #5822

Closed
Infernio opened this issue Feb 20, 2022 · 1 comment · Fixed by #6141
Closed

useless-super-delegation when an override changes the return type annotation #5822

Infernio opened this issue Feb 20, 2022 · 1 comment · Fixed by #6141
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@Infernio
Copy link

Current problem

Similar to #1923, but for the return type annotation.
It is sometimes useful to override a method and change the return type annotation - whether to help a static type checker out or to help the IDE make better autocomplete suggestions.

Here's an example - it's a bit contrived, I actually ran into this when overriding to add typing to a method from a third-party library (ANTLR).

"""Quick test to demonstrate useless-super-declaration raised for overrides
that change return type annotations."""
import random
from dataclasses import dataclass
from typing import Any

# pylint: disable=too-few-public-methods
class _AGenerator:
    """Abstract base class for generators."""
    _valid_values: list[Any]

    def get_random(self) -> Any:
        """Generates a random value from the valid values that this generator
        can return."""
        return random.choice(self._valid_values)

@dataclass
class StrGenerator(_AGenerator):
    """Generator that accepts and returns string values."""
    _valid_values: list[str]

    # override to add typing
    def get_random(self) -> str:
        return super().get_random()

s = StrGenerator(['foo', 'bar', 'qux'])
print(s.get_random())
print(s.get_random())
print(s.get_random())

Running pylint with default config on it:

$ python -m pylint test.py 
************* Module test
test.py:24:4: W0235: Useless super delegation in method 'get_random' (useless-super-delegation)

------------------------------------------------------------------
Your code has been rated at 9.33/10 (previous run: 9.33/10, +0.00)

Desired solution

pylint should not emit a warning in this case as the override has legitimate uses.

Additional context

No response

@Infernio Infernio added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 20, 2022
@DanielNoord DanielNoord added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 21, 2022
@JonnoFTW
Copy link

I experience the same error, except that the superclass is an ABC. I would consider this a bug seeing as this behaviour contradicts what is in the changelog for 2.0

timmartin added a commit to timmartin/pylint that referenced this issue Apr 2, 2022
timmartin added a commit to timmartin/pylint that referenced this issue Apr 3, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.13.5 milestone Apr 3, 2022
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.13.5, 2.13.6 Apr 6, 2022
timmartin added a commit to timmartin/pylint that referenced this issue Apr 14, 2022
Pierre-Sassoulas pushed a commit that referenced this issue Apr 15, 2022
…6141)

* Reinstate warning if base method has no return type annotation
Pierre-Sassoulas pushed a commit that referenced this issue Apr 20, 2022
…6141)

* Reinstate warning if base method has no return type annotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants