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

Unexpected false-positive "no-value-for-parameter" from type stubs 'hidden' under 'if TYPE_CHECKING:' #8689

Open
geokala opened this issue May 15, 2023 · 2 comments
Labels
Documentation 📗 inference Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc. typing

Comments

@geokala
Copy link

geokala commented May 15, 2023

Bug description

This may not be a bug in pylint, so possibly can just be closed but as I just burned a bunch of time figuring it out, I'm raising it here.

When type stubs for a function that needs to be decorated are hidden under a TYPE_CHECKING, pylint seems to get unhappy with them- when they're in separate files, it can be a bit puzzling until one notices the issue is actually with the type stub.

"""Repro for issue that burned time due to 'missing arguments'.
Context: The original issue came from some async sqlalchemy fun, where the
'cake_type' was a DeclarativeBase type.
"""
# pylint: disable=too-few-public-methods
from typing import Any, Callable, TYPE_CHECKING


if TYPE_CHECKING:
    from typing_extensions import ParamSpec, TypeVar
    Params = ParamSpec('Params')
    Sometype = TypeVar('Sometype')


def add_an_arg(func: "Callable[..., Sometype]"):
    """Mutate a function, that's always fun."""
    def wrapper(self: "Base", *args: "Params.args",
                **kwargs: "Params.kwargs") -> "Sometype":
        return func(self, 'hello', *args, **kwargs)
    return wrapper


class CheeseCake:
    """A good cake."""


class Base:
    """Base repro class."""
    def __init__(self, cake_type: Any):
        self.cake_type = cake_type

    @add_an_arg
    def say_something_and_serve_cake(self, hello_arg: str, something_else: int):
        """Say something n times and return a cake."""
        for _ in range(something_else):
            print(hello_arg)
        return self.cake_type()


class CheesecakePlace(Base):
    """The subclass where the pain happens."""
    def __init__(self):
        super().__init__(CheeseCake)

    def new_customers_enter(self, num_customers: int):
        """Do the usual stuff when a new customer enters."""
        self.say_something_and_serve_cake(num_customers)

    if TYPE_CHECKING:
        def say_something_and_serve_cake(
            self, hello_arg: str, something_else: int,
        ) -> CheeseCake:
            ...

Configuration

No response

Command used

pylint --signature-mutators=add_an_arg testfile.py

Pylint output

************* Module testfile
testfile.py:47:8: E1120: No value for argument 'something_else' in method call (no-value-for-parameter)

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

Expected behavior

I was expecting no complaints, but I'm not sure if that was a reasonable expectation. It might've been nice to have more indication as to where the failing function call was (as this would've immediately pointed me at the type stub), but I'm not sure how feasible that is.

Pylint version

pylint 2.17.4
astroid 2.15.5
Python 3.11.3 (main, Apr  5 2023, 14:15:06) [GCC 9.4.0]

OS / Environment

No response

Additional dependencies

No response

@geokala geokala added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 15, 2023
@geokala
Copy link
Author

geokala commented May 15, 2023

Just to note: the fix (to the supplied code) is to decorate the type stub as well.

@jacobtylerwalls
Copy link
Member

Thanks for the report. Closely related to pylint-dev/astroid#1015 (overzealous filtering of possible functions to only the last one defined, here, the stub) and its many duplicates in this repo. A couple reasons to keep this issue open:

  • we could let ClassDef.igetattr be smart enough to ignore FunctionDefs guarded under TYPE_CHECKING, but we just removed those utils from astroid, so maybe has to be done on the pylint side?
  • Barring that, there's a suggestion in this issue to improve the message (we could emit a slightly different message if we detect that the called function is guarded under TYPE_CHECKING)

@jacobtylerwalls jacobtylerwalls added Documentation 📗 inference Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc. typing and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation 📗 inference Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc. typing
Projects
None yet
Development

No branches or pull requests

2 participants