-
Notifications
You must be signed in to change notification settings - Fork 0
Fix union inference of generic class and its generic type #5
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
base: master
Are you sure you want to change the base?
Fix union inference of generic class and its generic type #5
Conversation
…the generic type of the other
Diff from mypy_primer, showing the effect of this PR on open source code: aioredis (https://github.com/aio-libs/aioredis)
+ aioredis/client.py:1921: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2160: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2173: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2608: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2616: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2621: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2629: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2666: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:2674: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:3176: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
+ aioredis/client.py:3190: error: Value of type variable "_KeyT" of "list_or_args" cannot be "int | bytes | str | memoryview" [type-var]
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]
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]
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/backends/flink/ddl.py:96: error: Argument 1 to "promote_list" has incompatible type "str | Sequence[str] | None"; expected "Sequence[None] | None" [arg-type]
|
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]
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]
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]
|
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. |
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]
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]
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]
|
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]
|
WalkthroughThe changes introduce a new parameter, Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant Constraints
participant Helpers
Caller->>Constraints: infer_constraints(template, actual, direction, skip_neg_op, can_have_union_overlapping)
Constraints->>Helpers: _can_have_overlapping / _is_item_overlapping_actual_type (if union types detected)
Helpers-->>Constraints: Overlap information
Constraints->>Constraints: Filter union items if overlapping detected and flag is True
Constraints-->>Caller: List of inferred constraints
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Diff from mypy_primer, showing the effect of this PR on open source code: static-frame (https://github.com/static-frame/static-frame)
+ static_frame/core/archive_npy.py:418: error: Unused "type: ignore" comment [unused-ignore]
Tanjun (https://github.com/FasterSpeeding/Tanjun): 1.40x faster (124.3s -> 88.7s in a single noisy sample)
- tanjun/dependencies/data.py:220: error: Argument "callback" to "inject" has incompatible type "Callable[..., Coroutine[Any, Any, _T]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/dependencies/data.py:347: error: Argument "callback" to "inject" has incompatible type "Callable[..., Coroutine[Any, Any, _T]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:347: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "_CallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:373: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "Callable[..., Coroutine[Any, Any, None]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:396: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "Callable[..., Coroutine[Any, Any, None]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:1067: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "_CallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/commands/slash.py:3191: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "_SlashCallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/commands/slash.py:3235: 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[..., 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[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/commands/menu.py:689: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "_AnyMenuCallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/clients.py:2294: error: "Never" has no attribute "__iter__" (not iterable) [attr-defined]
- tanjun/clients.py:2294: 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[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
django-stubs (https://github.com/typeddjango/django-stubs): 1.71x slower (22.9s -> 39.1s in a single noisy sample)
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:694: error: Need type annotation for "arg" [var-annotated]
- ibis/expr/types/relations.py:696: 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:407: 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]
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
@coderabbitai full review |
✅ Actions performedFull review triggered. |
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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
mypy/constraints.py (2)
303-306
: Document the newcan_have_union_overlapping
parameterThe docstring still lists only
skip_neg_op
as an optional argument.
Please add a short description for the newcan_have_union_overlapping
flag so that callers (and future maintainers) understand its purpose and default behaviour.
395-419
: Avoid re‑defining helpers on every callBoth
_can_have_overlapping
and_is_item_overlapping_actual_type
are defined
inside the hot path of_infer_constraints
, causing a fresh function object
to be created for every invocation. Moving them to module scope (or at least
to the outer function) will:
- reduce per‑call overhead,
- simplify unit testing,
- make the code easier to read.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
mypy/constraints.py
(4 hunks)test-data/unit/check-inference.test
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
- GitHub Check: Run mypy_primer (4)
- GitHub Check: Run mypy_primer (3)
- GitHub Check: Run mypy_primer (1)
- GitHub Check: Test suite with py39-windows-64
- GitHub Check: Run mypy_primer (0)
- GitHub Check: Run mypy_primer (2)
🔇 Additional comments (4)
test-data/unit/check-inference.test (4)
876-876
: Improved test case by removing redundant error expectation.This change correctly removes the error expectation for
g(['a'])
which was previously flagged as ambiguous. The fix to union inference now allows mypy to correctly determine the type in this case without resulting in aNever
type.
888-909
: Good test case for validating union inference with generic classes.This test case verifies the core functionality addressed in the PR: properly inferring types when dealing with unions involving a generic class and its generic type parameter. Both calls to
method_with_union()
correctly resolve to the same return typeGenericClass[str]
regardless of whether the input is aGenericClass[str]
or just astr
.
910-922
: Good test for complex union inference with nested sequences.This test case validates that the type inference can properly handle
Union[Sequence[T], Sequence[Sequence[T]]]
and correctly infer the return type ofsub_method(value)
asIterable[Sequence[T]]
. This tests an important scenario where nested generic containers participate in union inference.
923-933
: Good test for union inference with complex bounds and sequence types.This test case checks inference with a bound type variable
S bound=T
whereT
is already a union (Union[str, Sequence[int]]
). The test confirms thatsub_method(value)
correctly infers toIterable[Union[str, Sequence[int]]]
, validating that the enhanced inference logic handles complex union types properly.
def _can_have_overlapping(_item: Type, _actual: UnionType) -> bool: | ||
# There is a special overlapping case, where we have a Union of where two types | ||
# are the same, but one of them contains the other. | ||
# For example, we have Union[Sequence[T], Sequence[Sequence[T]]] | ||
# In this case, only the second one can have overlapping because it contains the other. | ||
# So, in case of list[list[int]], second one would be chosen. | ||
if isinstance(p_item := get_proper_type(_item), Instance) and p_item.args: | ||
other_items = [o_item for o_item in _actual.items if o_item is not a_item] | ||
|
||
if len(other_items) == 1 and other_items[0] in p_item.args: | ||
return 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.
NameError: undefined variable a_item
in _can_have_overlapping
a_item
is not defined in this scope – the comprehension intends to compare each
o_item
with the _item
currently being inspected.
- other_items = [o_item for o_item in _actual.items if o_item is not a_item]
+ other_items = [o_item for o_item in _actual.items if o_item is not _item]
Without this fix the helper will raise at runtime and break constraint inference
whenever SUPERTYPE_OF
union handling is triggered.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
def _can_have_overlapping(_item: Type, _actual: UnionType) -> bool: | |
# There is a special overlapping case, where we have a Union of where two types | |
# are the same, but one of them contains the other. | |
# For example, we have Union[Sequence[T], Sequence[Sequence[T]]] | |
# In this case, only the second one can have overlapping because it contains the other. | |
# So, in case of list[list[int]], second one would be chosen. | |
if isinstance(p_item := get_proper_type(_item), Instance) and p_item.args: | |
other_items = [o_item for o_item in _actual.items if o_item is not a_item] | |
if len(other_items) == 1 and other_items[0] in p_item.args: | |
return True | |
def _can_have_overlapping(_item: Type, _actual: UnionType) -> bool: | |
# There is a special overlapping case, where we have a Union of where two types | |
# are the same, but one of them contains the other. | |
# For example, we have Union[Sequence[T], Sequence[Sequence[T]]] | |
# In this case, only the second one can have overlapping because it contains the other. | |
# So, in case of list[list[int]], second one would be chosen. | |
if isinstance(p_item := get_proper_type(_item), Instance) and p_item.args: | |
- other_items = [o_item for o_item in _actual.items if o_item is not a_item] | |
+ other_items = [o_item for o_item in _actual.items if o_item is not _item] | |
if len(other_items) == 1 and other_items[0] in p_item.args: | |
return True |
Diff from mypy_primer, showing the effect of this PR on open source code: static-frame (https://github.com/static-frame/static-frame)
+ static_frame/core/archive_npy.py:419: error: Unused "type: ignore" comment [unused-ignore]
Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/dependencies/data.py:220: error: Argument "callback" to "inject" has incompatible type "Callable[..., Coroutine[Any, Any, _T]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/dependencies/data.py:347: error: Argument "callback" to "inject" has incompatible type "Callable[..., Coroutine[Any, Any, _T]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:347: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "_CallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:373: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "Callable[..., Coroutine[Any, Any, None]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:396: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "Callable[..., Coroutine[Any, Any, None]]"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/schedules.py:1067: error: Argument 1 to "call_with_async_di" of "Client" has incompatible type "_CallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/commands/slash.py:3191: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "_SlashCallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/commands/slash.py:3235: 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[..., 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[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/commands/menu.py:689: error: Argument 1 to "call_with_async_di" of "Context" has incompatible type "_AnyMenuCallbackSigT"; expected "Callable[..., Coroutine[Any, Any, Never] | Never]" [arg-type]
- tanjun/clients.py:2294: error: "Never" has no attribute "__iter__" (not iterable) [attr-defined]
- tanjun/clients.py:2294: 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[..., 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:694: error: Need type annotation for "arg" [var-annotated]
- ibis/expr/types/relations.py:696: 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:407: 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 python#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:
Summary by CodeRabbit