Skip to content

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

@github-actions

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
@github-actions

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.

@github-actions

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

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
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: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]

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/states.py:369: error: Argument 1 to "StateGroup" has incompatible type "Collection[Never]"; expected "list[State[Any]]"  [arg-type]
+ src/prefect/states.py:369: error: Argument 1 to "StateGroup" has incompatible type "Collection[Any]"; expected "list[State[Any]]"  [arg-type]

ibis (https://github.com/ibis-project/ibis)
+ ibis/common/egraph.py:733: error: Item "function" of "Callable[..., Any] | Pattern | Variable" has no attribute "substitute"  [union-attr]
- ibis/common/egraph.py:731: error: Need type annotation for "rewrite"  [var-annotated]
- ibis/common/egraph.py:731: error: Argument 1 to "promote_list" has incompatible type "list[Rewrite]"; expected "Iterable[Never]"  [arg-type]
- ibis/expr/types/strings.py:960: error: Need type annotation for "pattern"  [var-annotated]
- ibis/expr/types/strings.py:962: error: Argument 1 to "promote_list" has incompatible type "str | StringValue | Iterable[str | StringValue]"; expected "Iterable[Never]"  [arg-type]
+ ibis/expr/types/strings.py:961: error: Argument 2 to "StringSQLLike" has incompatible type "str | StringValue"; expected "Value[String, Any]"  [arg-type]
- ibis/expr/types/strings.py:1004: error: Need type annotation for "pattern"  [var-annotated]
- ibis/expr/types/strings.py:1006: error: Argument 1 to "promote_list" has incompatible type "str | StringValue | Iterable[str | StringValue]"; expected "Iterable[Never]"  [arg-type]
+ ibis/expr/types/strings.py:1005: error: Argument 2 to "StringSQLILike" has incompatible type "str | StringValue"; expected "Value[String, Any]"  [arg-type]
- ibis/expr/types/relations.py:704: error: Need type annotation for "arg"  [var-annotated]
- ibis/expr/types/relations.py:706: error: Argument 1 to "promote_list" has incompatible type "Sequence[str | int]"; expected "Iterable[Never]"  [arg-type]

static-frame (https://github.com/static-frame/static-frame)
+ static_frame/core/archive_npy.py:439: error: Unused "type: ignore" comment  [unused-ignore]

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

2 participants