Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Type Hinting] Application.add_handlers is not properly annotated. #4530

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

Closed
roast-lord opened this issue Oct 21, 2024 · 0 comments · Fixed by #4531
Closed

[Type Hinting] Application.add_handlers is not properly annotated. #4530

roast-lord opened this issue Oct 21, 2024 · 0 comments · Fixed by #4531

Comments

@roast-lord
Copy link
Contributor

roast-lord commented Oct 21, 2024

Steps to Reproduce

  1. Create a main.py file like:
from telegram import Update
from telegram.ext import (
    ApplicationBuilder,
    CommandHandler,
    ContextTypes,
    MessageHandler,
    filters,
)

# pyright: strict


async def hello(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    if update.message and update.effective_user:
        await update.message.reply_text(f"Hello {update.effective_user.first_name}")


async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    if update.message and update.message.text:
        await update.message.reply_text(update.message.text)


async def hi(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    if update.message:
        await update.message.reply_text("Hi")


async def welcome(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    if update.message:
        await update.message.reply_text("Welcome")


mixed_handlers = [
    CommandHandler("hello", hello),
    MessageHandler(filters.TEXT & ~filters.COMMAND, echo),
]


command_handlers = [
    CommandHandler("hi", hi),
    CommandHandler("welcome", welcome),
]


app = ApplicationBuilder().token("YOUR TOKEN HERE").build()


app.add_handlers(mixed_handlers)

app.add_handlers(command_handlers)


app.run_polling()
  1. Run pyright and mypy on the file:
 pyright main.py && mypy main.py

Expected behaviour

Code is properly annotated so pyright and mypy doesn't throw any error.

Actual behaviour

Both mypy and pyright in strict mode will complain about invariance of

Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]],

Errors:

1. Argument 1 to "add_handlers" of "Application" has incompatible type "list[CommandHandler[CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], None]]"; expected "list[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]] | tuple[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]] | dict[int, list[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]] | tuple[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]]]"  [arg-type]

2. "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance

3. Consider using "Sequence" instead, which is covariant

4. Argument 1 to "add_handlers" of "Application" has incompatible type "list[CommandHandler[CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], None]]"; expected "list[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]] | tuple[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]] | dict[int, list[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]] | tuple[BaseHandler[Any, CallbackContext[ExtBot[None], dict[Any, Any], dict[Any, Any], dict[Any, Any]], Any]]]"  [arg-type]

5. Argument of type "list[CommandHandler[DEFAULT_TYPE, None]]" cannot be assigned to parameter "handlers" of type "List[BaseHandler[Any, DEFAULT_TYPE, Any]] | Tuple[BaseHandler[Any, DEFAULT_TYPE, Any]] | Dict[int, List[BaseHandler[Any, DEFAULT_TYPE, Any]] | Tuple[BaseHandler[Any, DEFAULT_TYPE, Any]]]" in function "add_handlers"
Type "list[CommandHandler[DEFAULT_TYPE, None]]" is not assignable to type "List[BaseHandler[Any, DEFAULT_TYPE, Any]] | Tuple[BaseHandler[Any, DEFAULT_TYPE, Any]] | Dict[int, List[BaseHandler[Any, DEFAULT_TYPE, Any]] | Tuple[BaseHandler[Any, DEFAULT_TYPE, Any]]]"
	"list[CommandHandler[DEFAULT_TYPE, None]]" is not assignable to "List[BaseHandler[Any, DEFAULT_TYPE, Any]]"
Type parameter "_T@list" is invariant, but "CommandHandler[DEFAULT_TYPE, None]" is not the same as "BaseHandler[Any, DEFAULT_TYPE, Any]"
Consider switching from "list" to "Sequence" which is covariant
"list[CommandHandler[DEFAULT_TYPE, None]]" is not assignable to "Tuple[BaseHandler[Any, DEFAULT_TYPE, Any]]"
"list[CommandHandler[DEFAULT_TYPE, None]]" is not assignable to "Dict[int, List[BaseHandler[Any, DEFAULT_TYPE, Any]] | Tuple[BaseHandler[Any, DEFAULT_TYPE, Any]]]" [reportArgumentType]

Operating System

Arch Linux

Version of Python, python-telegram-bot & dependencies

python 3.12.7

python-telegram-bot 21.6

mypy 1.12.1

pyright 1.1.385

Relevant log output

No response

Additional Context

Changing List[BaseHandler[Any, CCT, Any] to Sequence[BaseHandler[Any, CCT, Any] seems to fix the issue.

     def add_handlers(
        self,
        handlers: Union[
-          Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]],
+          Sequence[BaseHandler[Any, CCT, Any]],
            Dict[int, Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]]],
        ],
        group: Union[int, DefaultValue[int]] = _DEFAULT_0,
    ) -> None:
@github-actions github-actions bot locked and limited conversation to collaborators Oct 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants