``Optional[Type]`` ignores (implicit) None check, resulting in misstyping of ``Type`` to ``Optional[Type]``
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
It seems that when the check for None is implicit mypy misstype Type to Optional[Type]. I checked #9428 which look really similar, but think the problem is that the control flow in my example is a little convoluted and that the check is not really explicit. (This is pylint's https://github.com/PyCQA/pylint/blob/master/pylint/message/message_id_store.py#L79)
To Reproduce
class MessageIdStore:
def __init__(self):
self.__msgid_to_symbol: Dict[str, str] = {}
self.__symbol_to_msgid: Dict[str, str] = {}
def check_msgid_and_symbol(self, msgid: str, symbol: str) -> None:
existing_msgid: Optional[str] = self.__symbol_to_msgid.get(symbol)
existing_symbol: Optional[str] = self.__msgid_to_symbol.get(msgid)
if existing_symbol is None and existing_msgid is None:
return # both symbol and msgid are usable
if existing_msgid is not None:
if existing_msgid != msgid:
self._raise_duplicate_msgid(symbol, msgid, existing_msgid)
if existing_symbol != symbol:
# This will emit: 'Argument 3 to "_raise_duplicate_symbol" of "MessageIdStore"
# has incompatible type "Optional[str]"; expected "str"'
self._raise_duplicate_symbol(msgid, symbol, existing_symbol)
@staticmethod
def _raise_duplicate_symbol(msgid: str, symbol: str, other_symbol: str) -> None:
...
@staticmethod
def _raise_duplicate_msgid(symbol: str, msgid: str, other_msgid: str) -> None:
...
Actual Behavior
On line self._raise_duplicate_symbol(msgid, symbol, existing_symbol) we have an Argument 3 to "_raise_duplicate_symbol" of "MessageIdStore" has incompatible type "Optional[str]"; expected "str".
Expected Behavior
No error because if not(existing_symbol is None and existing_msgid is None) and not(existing_msgid is not None then we can deduce that existing_symbol is not None
Your Environment
- Mypy version used: v0.812
- Mypy command-line flags: :
["--ignore-missing-imports", "--scripts-are-modules"]
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 running the provided reproducer with the stated mypy version and flags, then trace how its control-flow checks narrow existing_symbol and existing_msgid. Done means the final _raise_duplicate_symbol call accepts existing_symbol without an Optional[str] error while preserving the existing checks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100