Skip to content
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

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

Conversation

roberfi
Copy link
Contributor

@roberfi roberfi commented Jun 1, 2024

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:

  • 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]"

This comment has been minimized.

@roberfi roberfi marked this pull request as ready for review June 1, 2024 13:23
@roberfi roberfi marked this pull request as draft June 2, 2024 11:43

This comment has been minimized.

@roberfi roberfi marked this pull request as ready for review June 2, 2024 15:50
@LevBernstein
Copy link

LevBernstein commented Oct 23, 2024

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.

actual: Type,
direction: int,
skip_neg_op: bool = False,
can_have_union_overlaping: bool = True,

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.

Copy link
Contributor Author

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

Copy link
Contributor

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]

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]"
2 participants