-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix union inference of generic class and its generic type #17310
base: master
Are you sure you want to change the base?
Fix union inference of generic class and its generic type #17310
Conversation
This comment has been minimized.
This comment has been minimized.
…the generic type of the other
This comment has been minimized.
This comment has been minimized.
Bumping this for visibility--I'm seeing the same error in my own project, caused by calling a nextcord.Command with a keyword argument: bb_test.py:2276: error: Argument "words" to "__call__" of "Command" has incompatible type "str"; expected "Never" [arg-type]
...
ch = MockChannel(guild=MockGuild())
ctx = MockContext(Bot.BeardlessBot, channel=ch)
assert await Bot.cmdDefine(ctx, words="f") == 1
...
@BeardlessBot.command(name="define") # type: ignore[arg-type]
async def cmdDefine(ctx: misc.BotContext, *, words: str = "") -> int:
if misc.ctxCreatedThread(ctx):
return -1
emb = await misc.define(words)
await ctx.send(embed=emb)
return 1 The command decorator in that snippet also has a type-ignore comment, because every use of the decorator sees the same "Never" issue. I would love for this to be merged. |
This comment has been minimized.
This comment has been minimized.
mypy/constraints.py
Outdated
actual: Type, | ||
direction: int, | ||
skip_neg_op: bool = False, | ||
can_have_union_overlaping: bool = True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overlapping is spelled with two "p"s; same issue in all occurrences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes. You are right. Thank you for the advice! I have fixed it
Diff from mypy_primer, showing the effect of this PR on open source code: Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/dependencies/data.py:221: error: Argument "callback" to "inject" has incompatible type "Callable[..., Coroutine[Any, Any, _T]]"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/dependencies/data.py:351: error: Argument "callback" to "inject" has incompatible type "Callable[..., Coroutine[Any, Any, _T]]"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/schedules.py:338: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "_CallbackSigT"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/schedules.py:364: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "Callable[..., Coroutine[Any, Any, None]]"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/schedules.py:387: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "Callable[..., Coroutine[Any, Any, None]]"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/schedules.py:1050: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "_CallbackSigT"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/commands/slash.py:3171: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "_SlashCallbackSigT"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/commands/slash.py:3213: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "Callable[[AutocompleteContext, str, VarArg(Any), KwArg(Any)], Coroutine[Any, Any, None]]"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/commands/message.py:346: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "_MessageCallbackSigT"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/commands/menu.py:685: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "_AnyMenuCallbackSigT"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
- tanjun/clients.py:2291: error: "Never" has no attribute "__iter__" (not iterable) [attr-defined]
- tanjun/clients.py:2291: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "Callable[[MessageContext, VarArg(Any), KwArg(Any)], Coroutine[Any, Any, Iterable[str]]]"; expected "Callable[..., Union[Coroutine[Any, Any, Never], Never]]" [arg-type]
ibis (https://github.com/ibis-project/ibis)
+ ibis/common/egraph.py:731: error: Item "function" of "Callable[..., Any] | Pattern | Variable" has no attribute "substitute" [union-attr]
- ibis/common/egraph.py:729: error: Need type annotation for "rewrite" [var-annotated]
- ibis/common/egraph.py:729: error: Argument 1 to "promote_list" has incompatible type "list[Rewrite]"; expected "Sequence[Never]" [arg-type]
- ibis/expr/types/relations.py:678: error: Need type annotation for "arg" [var-annotated]
- ibis/expr/types/relations.py:680: error: Argument 1 to "promote_list" has incompatible type "Sequence[str | int]"; expected "Sequence[Never]" [arg-type]
discord.py (https://github.com/Rapptz/discord.py)
- discord/app_commands/commands.py:1003: error: Argument 1 to "maybe_coroutine" has incompatible type "Callable[[Interaction[Client]], Coroutine[Any, Any, bool]]"; expected "Callable[[Interaction[Client]], Never | Awaitable[Never]]" [arg-type]
- discord/ext/commands/cog.py:706: error: Argument 1 to "maybe_coroutine" has incompatible type "Callable[[], Coroutine[Any, Any, None]]"; expected "Callable[[], Never | Awaitable[Never]]" [arg-type]
- discord/ext/commands/cog.py:723: error: Argument 1 to "maybe_coroutine" has incompatible type "Callable[[], Coroutine[Any, Any, None]]"; expected "Callable[[], Never | Awaitable[Never]]" [arg-type]
- discord/ext/commands/cog.py:776: error: Argument 1 to "maybe_coroutine" has incompatible type "Callable[[], Coroutine[Any, Any, None]]"; expected "Callable[[], Never | Awaitable[Never]]" [arg-type]
- discord/ext/commands/hybrid.py:401: error: Argument 1 to "maybe_coroutine" has incompatible type "Callable[[Interaction[Client]], Coroutine[Any, Any, bool]]"; expected "Callable[[Interaction[Client]], Never | Awaitable[Never]]" [arg-type]
|
Fixes #16788
The fix consists in discarding one possible type item of the union if it is an argument of other item that is subtype of the actual type.
Code examples:
Output before the fix:
Output after the fix:
Output before the fix:
Output after the fix: