Keyword arguments to `__str__` are rejected
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example in the linked mypy playground and inspect the type-checking path for calls to str. Compare keyword-argument handling with the already accepted positional-argument case; done means the keyword call is accepted without weakening unrelated dunder-call checks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100