Skip to content

Commit

Permalink
Several Smaller Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibo-Joshi committed Sep 2, 2023
1 parent e5761c8 commit 147e760
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
python-version: '3.11'

- name: Install dependencies
run: |
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ repos:
# run pylint across multiple cpu cores to speed it up-
- --jobs=0 # See https://pylint.pycqa.org/en/latest/user_guide/run.html?#parallel-execution to know more
additional_dependencies:
- python-telegram-bot~=20.0
- plotly~=5.11
- python-telegram-bot~=20.4
- plotly~=5.16
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
hooks:
- id: mypy
files: ^ptbstats/.*\.py$
additional_dependencies:
- python-telegram-bot~=20.0
- plotly~=5.11
- python-telegram-bot~=20.4
- plotly~=5.16
- repo: https://github.com/asottile/pyupgrade
rev: v3.10.1
hooks:
Expand All @@ -52,5 +52,5 @@ repos:
hooks:
- id: ruff
additional_dependencies:
- python-telegram-bot~=20.0
- plotly~=5.11
- python-telegram-bot~=20.4
- plotly~=5.16
31 changes: 16 additions & 15 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ Here is an example setup using the very basic `SimpleStats <https://Bibo-Joshi.g

.. code-block:: python
#!/usr/bin/env python3
from telegram.ext import Application, PicklePersistence, filters, MessageHandler
from ptbstats import set_application, register_stats, SimpleStats
def main():
"""Start the bot."""
persistence = PicklePersistence("persistence.pickle")
application = Application.builder().token("TOKEN").persistence(persistence).build()
# Set up stats
set_application(application)
# Count number of text messages
register_stats(
SimpleStats(
"text", lambda u: bool(u.message and (filters.TEXT & ~filters.COMMAND).check_update(u))
async def post_init(app):
# Set up stats
set_application(application)
# Count number of text messages
register_stats(
SimpleStats(
"text", lambda u: bool(u.message and (filters.TEXT & ~filters.COMMAND).check_update(u))
)
)
)
# Count number of inline queries
register_stats(SimpleStats("ilq", lambda u: bool(u.inline_query and u.inline_query.query)))
# Count number of inline queries
register_stats(SimpleStats("ilq", lambda u: bool(u.inline_query and u.inline_query.query)))
persistence = PicklePersistence("persistence.pickle")
application = Application.builder().token("TOKEN").persistence(persistence).post_init(post_init).build()
# Register handlers
async def callback(u, c):
Expand All @@ -66,8 +67,8 @@ Here is an example setup using the very basic `SimpleStats <https://Bibo-Joshi.g
application.run_polling()
if __name__ == "__main__":
main()
if __name__ == "__main__":
main()

Advanced Usage
--------------
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# Use intersphinx to reference the python-telegram-bot docs
intersphinx_mapping = {
"telegram": ("https://python-telegram-bot.readthedocs.io/en/stable/", None),
"https://docs.python.org/": None,
"python": ("https://docs.python.org/", None),
}

# Fail on warnings & unresolved references etc
Expand Down
11 changes: 6 additions & 5 deletions ptbstats/_basestats.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env python
"""Base class for storing and displaying statistics."""
from abc import ABC, abstractmethod
from typing import Any, Optional, TypeVar
from typing import Any, Optional, Type, TypeVar

from telegram import Update
from telegram.ext import Application, BaseHandler, CallbackContext

_CCT = TypeVar("_CCT", bound=CallbackContext)
_T = TypeVar("_T", bound="BaseStats")


class BaseStats(BaseHandler[Update, _CCT], ABC):
Expand All @@ -28,14 +29,14 @@ class BaseStats(BaseHandler[Update, _CCT], ABC):
block_command: Whether :meth:`reply_statistics` should be run blocking.
"""

def __new__(cls, *args, **kwargs): # type: ignore
instance = super().__new__(cls, *args, **kwargs)
def __new__(cls: Type[_T], *_: Any, **__: Any) -> _T:
instance = super().__new__(cls)
orig_check_update = instance.check_update

def check_update(update: Update) -> Optional[bool]:
def check_update(update: object) -> Optional[bool]:
return isinstance(update, Update) and orig_check_update(update)

instance.check_update = check_update
instance.check_update = check_update # type: ignore[method-assign]
return instance

def __init__(self, command: str, block_stats: bool = True, block_command: bool = True):
Expand Down
2 changes: 1 addition & 1 deletion ptbstats/_simplestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def store_data(self, context: _CCT) -> None:
Assumes that ``context.bot_data`` is a dict and stores the data in
``context.bot_data['SimpleStats'][self.command]``. Override to customize the behavior.
"""
context.bot_data["SimpleStats"][self.command] = self.records
context.bot_data.setdefault("SimpleStats", {})[self.command] = self.records

def load_data(self, application: Application[Any, _CCT, Any, Any, Any, Any]) -> None:
"""Loads the data stored by :meth:`store_data`.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
python-telegram-bot~=20.0
python-telegram-bot~=20.4
plotly~=5.16

0 comments on commit 147e760

Please sign in to comment.