X Tutup
Skip to content

User.to_dict() returns absent fields with empty strings '' values #460

@lisitsky

Description

@lisitsky

Steps to reproduce

  1. Register any bot in Telegram. Here I will use one with smudged sensitive information.
    Get its own information using getMe telegram api call.
    For example (output pretty-printed):
$ http https://api.telegram.org/bot17XXXXX3:AXXcXnXXXXXXXXQgcXXXXc/getMe
HTTP/1.1 200 OK
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
Connection: keep-alive
Content-Length: 89
Content-Type: application/json
Date: Sun, 06 Nov 2016 22:00:54 GMT
Server: nginx/1.10.1
Strict-Transport-Security: max-age=31536000; includeSubdomains

{
    "ok": true,
    "result": {
        "first_name": "test",
        "id": 17XXXXX3,
        "username": "test_bot"
    }
}
  1. Try to get the same information using Python Telegram Bot.
    Version 3.4.0 (prior to 4.0.0):
$ bpython
bpython version 0.16 on top of Python 3.5.1 /Users/el/.pyenv/versions/3.5.1/bin/python3.5
>>> import telegram
>>> bot=telegram.Bot('bot17XXXXX3:AXXcXnXXXXXXXXQgcXXXXc')
>>> user=bot.getMe()
>>> user.to_dict()
{'first_name': 'test', 'id': 17XXXXX3, 'username': 'test_bot'}

Version after 4+ (for example 5.2.0):

$ bpython
bpython version 0.16 on top of Python 3.5.1 /Users/el/.pyenv/versions/3.5.1/bin/python3.5
>>> import telegram
>>> bot=telegram.Bot('bot17XXXXX3:AXXcXnXXXXXXXXQgcXXXXc')
>>> user=bot.getMe()
>>> user.to_dict()
{'first_name': 'test', 'last_name': '', 'type': '', 'id': 17XXXXX3, 'username': 'test_bot'}```

Expected behaviour

Should return only fields got with Telegram API (id, first_name, username), do not add empty optional fields (last_name, type).

Actual behaviour

Returned dict is populated with absent optional fields (last_name, type) with empty string values.

Configuration

Operating System:
MaxOS 10.12.1
Also tested on Linux boxes in different CI platforms (like Travis-CI, Codeship)

Version of Python, python-telegram-bot & dependencies:
Reproduced on 3.4.5, 3.5.1, 3.5.2, 3.6b4+

$ python -m telegram
/Users/el/.pyenv/versions/3.5.1/bin/python: No module named telegram.__main__; 'telegram' is a package and cannot be directly executed

Logs

Insert logs here (if necessary)

Issue was found after upgrading python-telegram-bot from 3.2 to 5.2.0 used as an intermediate dependency in my project.

Reason

One of the reasons to emerge the issue can be a changes between versions. While dumping user data to dict, old version ignored empty field checking them (if field: ...) thus 0, false and some other values could be lost. New version checks them (if field is not None: ...) in this case we ignore only real empty (None) fields, but method User.__init__ uses empty string as default values for absent parameters. So User object gets '' as values.

Possible impact

Can break some other products dependant on returned values of getMe method.

Proposition

Change signature of User constructor to use None as default value for absent optional parameters.

If you wish I can make a PR with tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup