Skip to content

Keyword arguments to __str__ are rejected #16735

Open
@JelleZijlstra

Description

@JelleZijlstra
class X:
    def __str__(self, arg: int | None = None) -> str:
        return f"{arg}"

def user(x: X) -> str:
    return x.__str__(arg=42)
main.py:2: note: "__str__" of "X" defined here
main.py:6: error: Unexpected keyword argument "arg" for "__str__" of "X"  [call-arg]

https://mypy-play.net/?mypy=latest&python=3.10&gist=b0426a5fffb000335d04492959d69b34

This works fine at runtime. Interestingly, mypy allows passing additional positional arguments to __str__, e.g. x.__str__(42). I assume something is overzealously applying the general rule that dunders usually don't accept keyword arguments.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @JelleZijlstra

        Issue actions

          Keyword arguments to `__str__` are rejected · Issue #16735 · python/mypy