Skip to content

AttributeError: 'Message' object has no attribute 'id' because of naming conflicts #1161

@beeirl

Description

@beeirl

Steps to reproduce

Folder structure (everything in the same package):

|- bot_test_project
    |- bot.py
    |- message.py

bot.py:

import logging

from telegram.ext import Updater, MessageHandler, Filters, CommandHandler

API_KEY = "please enter your api key here"

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__)

class Bot:
    def __init__(self):
        updater = Updater(API_KEY)
        self.__bot = updater.bot

        dispatcher = updater.dispatcher
        dispatcher.add_handler(CommandHandler("start", self.receive_command, pass_user_data=True))
        dispatcher.add_handler(MessageHandler(Filters.text, self.receive_message, pass_user_data=True))

        updater.start_polling()

    def receive_command(self, bot, update, user_data):
        print(update.message.id)

    def receive_message(self, bot, update, user_data):
        print(update.message.id)

Bot()

message.py:

class Message:
    def __init__(self, id=None, text=None, photo=None):
        self.__id = id
        self.__text = text
        self.__photo = photo

    @property
    def id(self):
        return self.__id

    @property
    def text(self):
        return self.__text

    @property
    def photo(self):
        return self.__photo

Expected behaviour

I want to access python-telegram-bot update.message object.

Actual behaviour

I am currently facing the issue, that my Python script is accessing an attribute of a wrong class. This is regarding to some namespace conflicts within my project imo.

Configuration

Operating System:
MacOS HighSierra

Version of Python, python-telegram-bot & dependencies:
Python: 3.6
python-telegram-bot: 10.0.2

Logs

2018-07-13 23:13:35,915 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telegram/ext/dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telegram/ext/commandhandler.py", line 173, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
  File "/bot.py", line 23, in receive_command
    print(update.message.id)
AttributeError: 'Message' object has no attribute 'id'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions