- Notifications
You must be signed in to change notification settings - Fork5.7k
Description
Steps to Reproduce
- Create a main.py file like:
fromtelegramimportUpdatefromtelegram.extimport (ApplicationBuilder,CommandHandler,ContextTypes,MessageHandler,filters,)# pyright: strictasyncdefhello(update:Update,context:ContextTypes.DEFAULT_TYPE)->None:ifupdate.messageandupdate.effective_user:awaitupdate.message.reply_text(f"Hello{update.effective_user.first_name}")asyncdefecho(update:Update,context:ContextTypes.DEFAULT_TYPE)->None:ifupdate.messageandupdate.message.text:awaitupdate.message.reply_text(update.message.text)asyncdefhi(update:Update,context:ContextTypes.DEFAULT_TYPE)->None:ifupdate.message:awaitupdate.message.reply_text("Hi")asyncdefwelcome(update:Update,context:ContextTypes.DEFAULT_TYPE)->None:ifupdate.message:awaitupdate.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()
- 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#variance3. Consider using "Sequence" instead, which is covariant4. 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:
Metadata
Metadata
Assignees
Labels
No labels