Partly fails to identify `Self` and `type(self)`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
In such a method, mypy accepts the call to __set_from_self, so it does recognize that if v has type type(self) then it has type Self. However, it does not understand that the else branch cannot get a Self, as if some objects of type Self would have failed the isinstance(v, type(self)):
def set(self, v: Union[int, str, Self]) -> None:
if isinstance(v, type(self)):
self.__set_from_self(v)
elif isinstance(v, str):
self.__set_from_int(int(v))
else:
self.__set_from_int(v)
It is unfortunate that those two expressions to describe the type of self are different in different contexts, but I believe they are supposed to be otherwise strictly equivalent.
Full test case: https://mypy-play.net/?mypy=latest&python=3.11&gist=27112299a10603d7093c01173808a566
Mypy version used: 1.3.0
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 with the linked mypy-play full test case and reproduce the behavior using mypy 1.3.0 with Python 3.11. Then trace the type-narrowing handling for isinstance(v, type(self)) and Self; done means the shown else branch is correctly rejected as unreachable for a Self value while the valid branches remain accepted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100