Skip to content

Only allow splatting iterables if function takes at least one positional arg #12206

Open
@piotrfilipiuk

Description

@piotrfilipiuk

Bug Report

At runtime the following code snippet:

def f(*, x: int = 9, y:int = 8) -> None:
    pass

kwargs = {"x": 1, "y": 2}
f(*kwargs)

results in TypeError: f() takes 0 positional arguments but 2 were given. However, mypy does not report any issues.

To Reproduce

  1. Execute python interpreter for the code snipped above.
  2. Run mypy for the code snippet above.

Expected Behavior

Mypy should yield an error. For example pytype yields the following error:

Function f expects 0 arg(s), got 2 [wrong-arg-count]
         Expected: (*, x, y)
  Actually passed: (_, _)

Actual Behavior

Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: None. Simply mypy <FILENAME>.
  • Mypy configuration options from mypy.ini (and other config files): N/A (defaults)
  • Python version used: 3.8.2
  • Operating system and version: Mac OS Big Sur

Activity

ktbarrett

ktbarrett commented on Feb 18, 2022

@ktbarrett

As I stated on the gitter, pytype is doing a lot keeping the number of elements in the literal dict around, I think the suggestion is overkill for such a corner case (though it might be a good long term goal) and only works for this minimal example and not in the common case. An appropriate solution might be to check when splatting that the function being called has no positional (or keyword in the other case) arguments and error, since it is likely not what the user intended.

changed the title [-]False negative for keyword only arguments with default values[/-] [+]Only allow splatting iterables if function takes at least one positional arg[/+] on Feb 19, 2022
hauntsaninja

hauntsaninja commented on Feb 19, 2022

@hauntsaninja
Collaborator

Changing the title to Kaleb's suggested action item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @JelleZijlstra@piotrfilipiuk@hauntsaninja@ktbarrett

        Issue actions

          Only allow splatting iterables if function takes at least one positional arg · Issue #12206 · python/mypy