@aygumov

Почему возникает «TypeError: __init__() takes 2 positional arguments but 4 were given»?

Код:
from typing import Union, Dict, Any

from aiogram import BaseMiddleware
from aiogram.types import Message


class CounterMiddleware(BaseMiddleware):
	def __init__(self, latency: Union[int, float] = 0.1):
		# инициализуем промежуточное программное обеспечение

		self.latency = latency
		self.data = {}

	async def __call__(self, handler, event: Message, data: Dict[str, Any]) -> Any:
		return await handler(event, data)


Ошибка:
spoiler

Cause exception while process update id="убрал ID" by bot id="убрал ID"
TypeError: __init__() takes from 1 to 2 positional arguments but 4 were given
Traceback (most recent call last):
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 308, in _process_update
    response = await self.feed_update(bot, update, **kwargs)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 157, in feed_update
    response = await self.update.wrap_outer_middleware(
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\middlewares\error.py", line 25, in __call__
    return await handler(event, data)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\middlewares\user_context.py", line 27, in __call__
    return await handler(event, data)
  File "E:\Python38\lib\site-packages\aiogram\fsm\middleware.py", line 41, in __call__
    return await handler(event, data)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\event\telegram.py", line 120, in trigger
    return await wrapped_inner(event, kwargs)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\event\handler.py", line 42, in call
    return await wrapped()
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 275, in _listen_update
    return await self.propagate_event(update_type=update_type, event=event, **kwargs)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\router.py", line 116, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\router.py", line 111, in _wrapped
    return await self._propagate_event(
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\router.py", line 144, in _propagate_event
    response = await router.propagate_event(update_type=update_type, event=event, **kwargs)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\router.py", line 116, in propagate_event
    return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\router.py", line 111, in _wrapped
    return await self._propagate_event(
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\router.py", line 136, in _propagate_event
    response = await observer.trigger(event, **kwargs)
  File "E:\Python38\lib\site-packages\aiogram\dispatcher\event\telegram.py", line 120, in trigger
    return await wrapped_inner(event, kwargs)
TypeError: __init__() takes from 1 to 2 positional arguments but 4 were given

  • Вопрос задан
  • 600 просмотров
Решения вопроса 1
febday
@febday
исправьте это:
router = Router()
router.message.middleware(
  CounterMiddleware
)

На это:
router = Router()
router.message.middleware(
  CounterMiddleware()
)
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@hardux2821
неправильный формат, у меня была проблема такая же с примерно таким кодом :
openai.api_key = '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'
client = OpenAI(openai.api_key)


Исправил так:

api_key = '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'
client = OpenAI(api_key=api_key)
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы