tgrx / oyabun

MIT License
0 stars 0 forks source link

`Update.message.from` works correctly #14

Closed tgrx closed 2 years ago

tgrx commented 2 years ago

Unable to create a Message using a from_ key arg:

from consigliere.telegram import Message, Chat, User

m = Message(
    message_id=1,
    date="2020-01-01T00:00:00Z",
    chat=Chat(id=1),
    from_=User(id=1, is_bot=True, first_name="fn"),
)

assert m.from_ is not None  # fails!

Possible solution:

class _Base(BaseModel):
    def dict(self, **kw):
        kw["by_alias"] = True
    def json(self, **kw):
        kw["by_alias"] = True

class X(_Base):
    from_: int = Field(..., alias="from")
    class Config:
        allow_population_by_field_name = True

x = X(from_=1)
y = X(**{"from": 1})
assert x.dict() == {"from": 1}
assert y.dict() == {"from": 1}