python-telegram-bot/python-telegram-bot · error · AttributeError

You can not assign a new value to disable_notification after

Error message

You can not assign a new value to disable_notification after initialization.

What it means

telegram.ext.Defaults disables assignment to its disable_notification property after construction by defining a setter that always raises AttributeError. Defaults objects are meant to be immutable once created; changing a default requires building a new Defaults instance and passing it to the Bot/Application. The library does this to guarantee that handlers and send methods observe consistent default settings.

Source

Thrown at src/telegram/ext/_defaults.py:292

        """
        return self._parse_mode

    @question_parse_mode.setter
    def question_parse_mode(self, _: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to question_parse_mode after initialization."
        )

    @property
    def disable_notification(self) -> bool | None:
        """:obj:`bool`: Optional. Sends the message silently. Users will
        receive a notification with no sound.
        """
        return self._disable_notification

    @disable_notification.setter
    def disable_notification(self, _: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to disable_notification after initialization."
        )

    @property
    def allow_sending_without_reply(self) -> bool | None:
        """:obj:`bool`: Optional. Pass :obj:`True`, if the message
        should be sent even if the specified replied-to message is not found.
        """
        return self._allow_sending_without_reply

    @allow_sending_without_reply.setter
    def allow_sending_without_reply(self, _: object) -> NoReturn:
        raise AttributeError(
            "You can not assign a new value to allow_sending_without_reply after initialization."
        )

    @property
    def tzinfo(self) -> dtm.tzinfo:

View on GitHub (pinned to d3b69d2e9f)

Solutions

  1. Create a new Defaults instance with the desired value and pass it when constructing ext.Bot/ext.Application: Defaults(disable_notification=True).
  2. Pass disable_notification explicitly per API call (e.g. bot.send_message(..., disable_notification=True)) instead of mutating defaults.
  3. If you need mutable global defaults, keep your own config object and construct a fresh Defaults from it whenever you rebuild the bot.

Example fix

// before
defaults = Defaults()
bot = Bot(token=TOKEN, defaults=default)
defaults.disable_notification = True  # AttributeError

// after
defaults = Defaults(disable_notification=True)
bot = Bot(token=TOKEN, defaults=default)
Defensive patterns

Strategy: type-guard

Validate before calling

from telegram.ext import Defaults

def can_set(d: Defaults) -> bool:
    return False  # Defaults properties are always read-only after init

Type guard

from telegram.ext import Defaults

def is_defaults(obj: object) -> bool:
    return isinstance(obj, Defaults)  # all its props are immutable; never setattr

Try / catch

try:
    defaults.disable_notification = value
except AttributeError:
    defaults = Defaults(disable_notification=value)  # rebuild instead

Prevention

When it happens

Trigger: Assigning `bot.defaults.disable_notification = True` (or mutating any Defaults instance property) after Defaults(...) has been instantiated and handed to ext.Bot/ext.Application. Accessing the property itself is fine; only the setter raises.

Common situations: Developers trying to tweak notification behavior at runtime, porting code from older PTB versions where defaults were plain attributes, or writing config reload logic that mutates a Defaults object in place.

Related errors


AI-assisted analysis of python-telegram-bot/python-telegram-bot@d3b69d2e9f (2026-08-28). Data as JSON: /api/errors/eb016d31cf7a96a5. Report an issue: GitHub.