Skip to content

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

arvi18
Copy link

@arvi18 arvi18 commented Apr 23, 2025

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:

  • Example 1:
from typing import List, Iterable, TypeVar, Union, reveal_type

T = TypeVar("T")


def coerce_list(x: Union[T, Iterable[T]]) -> List[T]:
    reveal_type(x)
    raise NotImplementedError()


def test() -> None:
    reveal_type(coerce_list(1))
    reveal_type(coerce_list([1, 2]))

Output before the fix:

file.py:7: note: Revealed type is "Union[T`-1, typing.Iterable[T`-1]]"
file.py:12: note: Revealed type is "builtins.list[builtins.int]"
file.py:13: note: Revealed type is "builtins.list[Never]"
file.py:13: error: Argument 1 to "coerce_list" has incompatible type "list[int]"; expected "Iterable[Never]"

Output after the fix:

file.py:7: note: Revealed type is "Union[T`-1, typing.Iterable[T`-1]]"
file.py:12: note: Revealed type is "builtins.list[builtins.int]"
file.py:13: note: Revealed type is "builtins.list[builtins.int]"
  • Example 2:
from typing import Generic, TypeVar, Union, reveal_type

T = TypeVar("T")


class GenericClass(Generic[T]):
    def __init__(self, value: T) -> None:
        self.value = value


def method_with_union(arg: Union[GenericClass[T], T]) -> GenericClass[T]:
    if not isinstance(arg, GenericClass):
        arg = GenericClass(arg)
    return arg


result_1 = method_with_union(GenericClass("test"))
reveal_type(result_1)

result_2 = method_with_union("test")
reveal_type(result_2)

Output before the fix:

file.py:17: error: Need type annotation for "result_1"  [var-annotated]
file.py:17: error: Argument 1 to "method_with_union" has incompatible type "GenericClass[str]"; expected "GenericClass[Never]"  [arg-type]
file.py:18: note: Revealed type is "file.GenericClass[Any]"
file.py:21: note: Revealed type is "file.GenericClass[builtins.str]"

Output after the fix:

file.py:18: note: Revealed type is "file.GenericClass[builtins.str]"
file.py:21: note: Revealed type is "file.GenericClass[builtins.str]"

Summary by CodeRabbit

  • New Features
    • Improved type inference for functions involving unions of generic types and their contained types, reducing redundant or conflicting type errors.
  • Bug Fixes
    • Resolved an ambiguous type inference scenario for certain function calls, eliminating unnecessary error messages.
  • Tests
    • Added new test cases to verify type inference behavior with complex unions and generics.

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

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]

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

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]

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

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.

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

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]

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

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]

Copy link

coderabbitai bot commented Apr 23, 2025

Walkthrough

The changes introduce a new parameter, can_have_union_overlapping, to the infer_constraints and _infer_constraints functions in mypy/constraints.py. This parameter enables more nuanced handling of overlapping union types during type constraint inference, particularly when unions contain types that are subtypes or containments of each other. Helper functions are added to detect and filter overlapping union items, reducing redundant or conflicting constraints. Corresponding test cases are updated and expanded in test-data/unit/check-inference.test to cover new inference scenarios involving unions of generic classes and sequences, ensuring the updated logic works as intended.

Changes

File(s) Change Summary
mypy/constraints.py Added can_have_union_overlapping parameter to infer_constraints and _infer_constraints. Implemented logic to detect and filter overlapping union types using new helper functions. Updated function signatures accordingly.
test-data/unit/check-inference.test Modified an existing ambiguous inference test case. Added three new test cases to cover inference with unions of generic classes, unions of sequences, and unions involving type variables bounded by unions.

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
Loading

Poem

In the garden of types, unions grew wild,
Overlapping branches left rabbits beguiled.
With a clever new flag, we prune as we go,
Redundant constraints now know where not to grow.
Tests bloom with new cases, so lively and bright—
Type inference hops on, with logic just right!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@arvi18
Copy link
Author

arvi18 commented Apr 23, 2025

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]

@visz11
Copy link
Collaborator

visz11 commented Apr 23, 2025

@coderabbitai full review

Copy link

coderabbitai bot commented Apr 23, 2025

✅ Actions performed

Full review triggered.

@visz11
Copy link
Collaborator

visz11 commented Apr 23, 2025

@coderabbitai full review

Copy link

coderabbitai bot commented Apr 23, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a 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 new can_have_union_overlapping parameter

The docstring still lists only skip_neg_op as an optional argument.
Please add a short description for the new can_have_union_overlapping flag so that callers (and future maintainers) understand its purpose and default behaviour.


395-419: Avoid re‑defining helpers on every call

Both _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

📥 Commits

Reviewing files that changed from the base of the PR and between 380cb8d and 4929815.

📒 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 a Never 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 type GenericClass[str] regardless of whether the input is a GenericClass[str] or just a str.


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 of sub_method(value) as Iterable[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 where T is already a union (Union[str, Sequence[int]]). The test confirms that sub_method(value) correctly infers to Iterable[Union[str, Sequence[int]]], validating that the enhanced inference logic handles complex union types properly.

Comment on lines +395 to +406
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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Suggested change
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

Copy link

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Seemingly erroneous incompatible type "list[int]"; expected "Iterable[Never]"
3 participants