Improve error message for partial None with `--local-partial-types`
Description
Suggest an annotation of the form Optional[<type>] when we show an error for an inferred partial None type (which I think should only happen when --local-partial-types is set).
Should help with merging #10169 (see https://github.com/python/mypy/pull/10169#issuecomment-792758982).
Test Plan
The error message in question appeared in a few tests which got changed. I think that should be sufficient to test this since this isn't a particularly complicated change.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
Diff from mypy_primer, showing the effect of this PR on open source code:
paasta (https://github.com/yelp/paasta)
- paasta_tools/metrics/metastatus_lib.py:672: error: Argument "key" to "sorted" has incompatible type "Callable[[Any], Sequence[Tuple[str, str]]]"; expected "Callable[[_SlaveT], Union[SupportsDunderLT, SupportsDunderGT]]"
+ paasta_tools/metrics/metastatus_lib.py:672: error: Argument "key" to "sorted" has incompatible type "Callable[[Any], _KeyFuncRetT]"; expected "Callable[[_SlaveT], SupportsRichComparison]"
- paasta_tools/metrics/metastatus_lib.py:672: error: Argument "key" to "sorted" has incompatible type "Callable[[Any], Sequence[Tuple[str, str]]]"; expected "Callable[[Any], Union[SupportsDunderLT, SupportsDunderGT]]"
+ paasta_tools/metrics/metastatus_lib.py:672: error: Argument "key" to "sorted" has incompatible type "Callable[[Any], _KeyFuncRetT]"; expected "Callable[[Any], SupportsRichComparison]"
rclip (https://github.com/yurijmikhalevich/rclip)
- rclip/utils.py:45: error: Argument 1 to "makedirs" has incompatible type "Optional[str]"; expected "Union[str, bytes, PathLike[str], PathLike[bytes]]"
+ rclip/utils.py:45: error: Argument 1 to "makedirs" has incompatible type "Optional[str]"; expected "StrOrBytesPath"
aioredis (https://github.com/aio-libs/aioredis)
- aioredis/client.py:4114: error: Incompatible types in assignment (expression has type "Dict[Union[bytes, str], Optional[Any]]", variable has type "Dict[Union[bytes, str, memoryview], Callable[[Dict[str, str]], Awaitable[None]]]") [assignment]
+ aioredis/client.py:4114: error: Incompatible types in assignment (expression has type "Dict[Union[bytes, str], Optional[Any]]", variable has type "Dict[ChannelT, PubSubHandler]") [assignment]
- aioredis/client.py:4172: error: Incompatible types in assignment (expression has type "Dict[Union[bytes, str, memoryview], Callable[[Dict[str, str]], Awaitable[None]]]", variable has type "Dict[Any, Optional[Any]]") [assignment]
+ aioredis/client.py:4172: error: Incompatible types in assignment (expression has type "Dict[ChannelT, PubSubHandler]", variable has type "Dict[Any, Optional[Any]]") [assignment]
pip (https://github.com/pypa/pip)
- src/pip/_internal/network/session.py:406: error: Argument 1 to "ip_address" has incompatible type "Optional[str]"; expected "Union[int, str, bytes, IPv4Address, IPv6Address]"
+ src/pip/_internal/network/session.py:406: error: Argument 1 to "ip_address" has incompatible type "Optional[str]"; expected "_RawIPAddress"
vision (https://github.com/pytorch/vision)
- torchvision/datasets/utils.py:70: error: Argument 1 to "md5" has incompatible type "**Dict[str, bool]"; expected "Union[bytes, Union[bytearray, memoryview, array[Any], mmap, _CData, PickleBuffer]]" [arg-type]
+ torchvision/datasets/utils.py:70: error: Argument 1 to "md5" has incompatible type "**Dict[str, bool]"; expected "ReadableBuffer" [arg-type]
cwltool (https://github.com/common-workflow-language/cwltool)
- cwltool/pack.py:323:5: error: Returning Any from function declared to return "MutableMapping[str, Union[bool, str, int, float, MutableSequence[Union[None, bool, str, int, float, MutableSequence[Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]], MutableMapping[str, Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]]]], MutableMapping[str, Union[None, bool, str, int, float, MutableSequence[Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]], MutableMapping[str, Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]]]], None]]" [no-any-return]
+ cwltool/pack.py:323:5: error: Returning Any from function declared to return "MutableMapping[str, Union[bool, str, int, float, MutableSequence[CWLOutputAtomType], MutableMapping[str, CWLOutputAtomType], None]]" [no-any-return]
- cwltool/main.py:337:5: error: Returning Any from function declared to return "MutableMapping[str, Union[bool, str, int, float, MutableSequence[Union[None, bool, str, int, float, MutableSequence[Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]], MutableMapping[str, Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]]]], MutableMapping[str, Union[None, bool, str, int, float, MutableSequence[Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]], MutableMapping[str, Union[None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]]]]], None]]" [no-any-return]
+ cwltool/main.py:337:5: error: Returning Any from function declared to return "MutableMapping[str, Union[bool, str, int, float, MutableSequence[CWLOutputAtomType], MutableMapping[str, CWLOutputAtomType], None]]" [no-any-return]
steam.py (https://github.com/Gobot1234/steam.py)
- steam/image.py:88: error: Argument 1 to "open" has incompatible type "Union[ImageIO, str, bytes, PathLike[str], PathLike[bytes], int]"; expected "Union[Union[str, bytes, PathLike[str], PathLike[bytes]], int]" [arg-type]
+ steam/image.py:88: error: Argument 1 to "open" has incompatible type "Union[ImageIO, str, bytes, PathLike[str], PathLike[bytes], int]"; expected "_OpenFile" [arg-type]
core (https://github.com/home-assistant/core)
- homeassistant/components/simplisafe/__init__.py:256: error: Redundant cast to "Union[Any, Any]" [redundant-cast]
+ homeassistant/components/simplisafe/__init__.py:256: error: Redundant cast to "SystemType" [redundant-cast]
- homeassistant/components/ridwell/sensor.py:93: error: Returning Any from function declared to return "Union[Union[None, str, int, float], date, datetime]" [no-any-return]
+ homeassistant/components/ridwell/sensor.py:93: error: Returning Any from function declared to return "Union[StateType, date, datetime]" [no-any-return]
dragonchain (https://github.com/dragonchain/dragonchain)
- dragonchain/lib/database/redis.py:399:36: error: Argument 2 to "zadd" of "SortedSetCommands" has incompatible type "Dict[str, int]"; expected "Mapping[Union[str, bytes], Union[bytes, float, int, str]]"
+ dragonchain/lib/database/redis.py:399:36: error: Argument 2 to "zadd" of "SortedSetCommands" has incompatible type "Dict[str, int]"; expected "Mapping[_Key, _Value]"
- dragonchain/lib/database/redis_utest.py:102:9: error: "Callable[[Union[str, bytes], Union[bytes, float, int, str], Union[None, int, timedelta], Union[None, int, timedelta], bool, bool, bool, bool, Optional[Any], Optional[Any]], Optional[bool]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:102:9: error: "Callable[[_Key, _Value, Union[None, int, timedelta], Union[None, int, timedelta], bool, bool, bool, bool, Optional[Any], Optional[Any]], Optional[bool]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:106:9: error: "Callable[[Union[str, bytes], Union[bytes, float, int, str], Union[None, int, timedelta], Union[None, int, timedelta], bool, bool, bool, bool, Optional[Any], Optional[Any]], Optional[bool]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:106:9: error: "Callable[[_Key, _Value, Union[None, int, timedelta], Union[None, int, timedelta], bool, bool, bool, bool, Optional[Any], Optional[Any]], Optional[bool]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:110:9: error: "Callable[[Union[str, bytes]], Optional[Any]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:110:9: error: "Callable[[_Key], Optional[Any]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:114:9: error: "Callable[[VarArg(Union[str, bytes])], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:114:9: error: "Callable[[VarArg(_Key)], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:122:9: error: "Callable[[bool, KwArg(Union[Any, Any])], bool]" has no attribute "assert_called_once"
+ dragonchain/lib/database/redis_utest.py:122:9: error: "Callable[[bool, KwArg(_CommandOptions)], bool]" has no attribute "assert_called_once"
- dragonchain/lib/database/redis_utest.py:126:9: error: "Callable[[Union[str, bytes], VarArg(Union[str, bytes])], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:126:9: error: "Callable[[_Key, VarArg(_Key)], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:130:9: error: "Callable[[Union[bytes, float, int, str], VarArg(Union[bytes, float, int, str])], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:130:9: error: "Callable[[_Value, VarArg(_Value)], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:134:9: error: "Callable[[Union[bytes, float, int, str], VarArg(Union[bytes, float, int, str])], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:134:9: error: "Callable[[_Value, VarArg(_Value)], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:138:9: error: "Callable[[VarArg(Union[str, bytes])], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:138:9: error: "Callable[[VarArg(_Key)], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:154:9: error: "Callable[[Union[str, bytes]], Optional[Any]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:154:9: error: "Callable[[_Key], Optional[Any]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:158:9: error: "Callable[[Union[str, bytes], int], Optional[Any]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:158:9: error: "Callable[[_Key, int], Optional[Any]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:162:9: error: "Callable[[Union[str, bytes], Union[bytes, float, int, str], Union[None, int, timedelta], Union[None, int, timedelta], bool, bool, bool, bool, Optional[Any], Optional[Any]], Optional[bool]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:162:9: error: "Callable[[_Key, _Value, Union[None, int, timedelta], Union[None, int, timedelta], bool, bool, bool, bool, Optional[Any], Optional[Any]], Optional[bool]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:166:9: error: "Callable[[Union[str, bytes], int, int], bool]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:166:9: error: "Callable[[_Key, int, int], bool]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:170:9: error: "Callable[[Union[str, bytes], Union[str, bytes]], Optional[Any]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:170:9: error: "Callable[[_Key, _Key], Optional[Any]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:174:9: error: "Callable[[Union[str, bytes], VarArg(Union[bytes, float, int, str])], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:174:9: error: "Callable[[_Key, VarArg(_Value)], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:178:9: error: "Callable[[Union[str, bytes], Union[bytes, float, int, str]], bool]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:178:9: error: "Callable[[_Key, _Value], bool]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:182:9: error: "Callable[[Union[str, bytes]], Set[Any]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:182:9: error: "Callable[[_Key], Set[Any]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:186:9: error: "Callable[[Union[str, bytes], VarArg(Union[bytes, float, int, str])], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:186:9: error: "Callable[[_Key, VarArg(_Value)], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:190:9: error: "Callable[[Union[str, bytes], int, int], List[Any]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:190:9: error: "Callable[[_Key, int, int], List[Any]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:198:9: error: "Callable[[Union[str, bytes]], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:198:9: error: "Callable[[_Key], int]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:210:9: error: "Callable[[Union[str, bytes]], Dict[Any, Any]]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:210:9: error: "Callable[[_Key], Dict[Any, Any]]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:214:9: error: "Callable[[Union[str, bytes], Union[str, bytes]], bool]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:214:9: error: "Callable[[_Key, _Key], bool]" has no attribute "assert_called_once_with"
- dragonchain/lib/database/redis_utest.py:218:9: error: "Callable[[Union[str, bytes], Mapping[Union[str, bytes], Union[bytes, float, int, str]], bool, bool, bool, bool, Optional[Any], Optional[Any]], int]" has no attribute "assert_called_once_with"
+ dragonchain/lib/database/redis_utest.py:218:9: error: "Callable[[_Key, Mapping[_Key, _Value], bool, bool, bool, bool, Optional[Any], Optional[Any]], int]" has no attribute "assert_called_once_with"
Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/context/slash.py:853: note: def respond(self, content: Union[Any, UndefinedType] = ..., *, ensure_result: Literal[True], delete_after: Union[timedelta, float, int, None] = ..., component: Union[ComponentBuilder, UndefinedType] = ..., components: Union[Sequence[ComponentBuilder], UndefinedType] = ..., embed: Union[Embed, UndefinedType] = ..., embeds: Union[Sequence[Embed], UndefinedType] = ..., mentions_everyone: Union[bool, UndefinedType] = ..., user_mentions: Union[Sequence[Union[PartialUser, Union[Snowflake, int]]], bool, UndefinedType] = ..., role_mentions: Union[Sequence[Union[PartialRole, Union[Snowflake, int]]], bool, UndefinedType] = ...) -> Coroutine[Any, Any, Message]
+ tanjun/context/slash.py:853: note: def respond(self, content: UndefinedOr = ..., *, ensure_result: Literal[True], delete_after: Union[timedelta, float, int, None] = ..., component: UndefinedOr = ..., components: UndefinedOr = ..., embed: UndefinedOr = ..., embeds: UndefinedOr = ..., mentions_everyone: UndefinedOr = ..., user_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ..., role_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ...) -> Coroutine[Any, Any, Message]
- tanjun/context/slash.py:853: note: def respond(self, content: Union[Any, UndefinedType] = ..., *, ensure_result: bool = ..., delete_after: Union[timedelta, float, int, None] = ..., component: Union[ComponentBuilder, UndefinedType] = ..., components: Union[Sequence[ComponentBuilder], UndefinedType] = ..., embed: Union[Embed, UndefinedType] = ..., embeds: Union[Sequence[Embed], UndefinedType] = ..., mentions_everyone: Union[bool, UndefinedType] = ..., user_mentions: Union[Sequence[Union[PartialUser, Union[Snowflake, int]]], bool, UndefinedType] = ..., role_mentions: Union[Sequence[Union[PartialRole, Union[Snowflake, int]]], bool, UndefinedType] = ...) -> Coroutine[Any, Any, Optional[Message]]
+ tanjun/context/slash.py:853: note: def respond(self, content: UndefinedOr = ..., *, ensure_result: bool = ..., delete_after: Union[timedelta, float, int, None] = ..., component: UndefinedOr = ..., components: UndefinedOr = ..., embed: UndefinedOr = ..., embeds: UndefinedOr = ..., mentions_everyone: UndefinedOr = ..., user_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ..., role_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ...) -> Coroutine[Any, Any, Optional[Message]]
- tanjun/context/slash.py:853: note: def respond(self, content: Union[Any, UndefinedType] = ..., *, ensure_result: Literal[False] = ..., delete_after: Union[timedelta, float, int, None] = ..., component: Union[ComponentBuilder, UndefinedType] = ..., components: Union[Sequence[ComponentBuilder], UndefinedType] = ..., embed: Union[Embed, UndefinedType] = ..., embeds: Union[Sequence[Embed], UndefinedType] = ..., mentions_everyone: Union[bool, UndefinedType] = ..., user_mentions: Union[Sequence[Union[PartialUser, Union[Snowflake, int]]], bool, UndefinedType] = ..., role_mentions: Union[Sequence[Union[PartialRole, Union[Snowflake, int]]], bool, UndefinedType] = ...) -> Coroutine[Any, Any, Optional[Message]]
+ tanjun/context/slash.py:853: note: def respond(self, content: UndefinedOr = ..., *, ensure_result: Literal[False] = ..., delete_after: Union[timedelta, float, int, None] = ..., component: UndefinedOr = ..., components: UndefinedOr = ..., embed: UndefinedOr = ..., embeds: UndefinedOr = ..., mentions_everyone: UndefinedOr = ..., user_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ..., role_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ...) -> Coroutine[Any, Any, Optional[Message]]
- tanjun/context/slash.py:853: note: def respond(self, content: Union[Any, UndefinedType] = ..., *, ensure_result: Literal[True], delete_after: Union[timedelta, float, int, None] = ..., component: Union[ComponentBuilder, UndefinedType] = ..., components: Union[Sequence[ComponentBuilder], UndefinedType] = ..., embed: Union[Embed, UndefinedType] = ..., embeds: Union[Sequence[Embed], UndefinedType] = ..., mentions_everyone: Union[bool, UndefinedType] = ..., user_mentions: Union[Sequence[Union[PartialUser, Union[Snowflake, int]]], bool, UndefinedType] = ..., role_mentions: Union[Sequence[Union[PartialRole, Union[Snowflake, int]]], bool, UndefinedType] = ...) -> Coroutine[Any, Any, Message]
+ tanjun/context/slash.py:853: note: def respond(self, content: UndefinedOr = ..., *, ensure_result: Literal[True], delete_after: Union[timedelta, float, int, None] = ..., component: UndefinedOr = ..., components: UndefinedOr = ..., embed: UndefinedOr = ..., embeds: UndefinedOr = ..., mentions_everyone: UndefinedOr = ..., user_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ..., role_mentions: Union[SnowflakeishSequence, bool, UndefinedType] = ...) -> Coroutine[Any, Any, Message]
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉