2021-05-29 16:18:16 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
2021-05-29 16:18:16 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
import importlib
|
2022-05-05 09:27:54 +02:00
|
|
|
import inspect
|
2021-06-06 11:48:48 +02:00
|
|
|
import os
|
2021-12-05 09:42:14 +01:00
|
|
|
from pathlib import Path
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-08-19 22:01:10 +02:00
|
|
|
included = { # These modules/classes intentionally have __dict__.
|
2022-05-05 17:40:22 +02:00
|
|
|
"CallbackContext",
|
2021-08-19 22:01:10 +02:00
|
|
|
}
|
2021-05-29 16:18:16 +02:00
|
|
|
|
|
|
|
|
2021-08-19 22:01:10 +02:00
|
|
|
def test_class_has_slots_and_no_dict():
|
2022-05-05 17:40:22 +02:00
|
|
|
tg_paths = Path("telegram").rglob("*.py")
|
2021-05-29 16:18:16 +02:00
|
|
|
|
|
|
|
for path in tg_paths:
|
2022-05-05 17:40:22 +02:00
|
|
|
if "__" in str(path): # Exclude __init__, __main__, etc
|
2021-12-05 09:42:14 +01:00
|
|
|
continue
|
2022-05-05 17:40:22 +02:00
|
|
|
mod_name = str(path)[:-3].replace(os.sep, ".")
|
2021-12-05 09:42:14 +01:00
|
|
|
module = importlib.import_module(mod_name) # import module to get classes in it.
|
2021-05-29 16:18:16 +02:00
|
|
|
|
|
|
|
for name, cls in inspect.getmembers(module, inspect.isclass):
|
|
|
|
if cls.__module__ != module.__name__ or any( # exclude 'imported' modules
|
2023-06-29 11:38:09 +02:00
|
|
|
x in name for x in ("__class__", "__init__", "Queue", "Webhook")
|
2021-05-29 16:18:16 +02:00
|
|
|
):
|
|
|
|
continue
|
2021-08-19 22:01:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
assert "__slots__" in cls.__dict__, f"class '{name}' in {path} doesn't have __slots__"
|
2021-08-19 22:01:10 +02:00
|
|
|
# if the class slots is a string, then mro_slots() iterates through that string (bad).
|
|
|
|
assert not isinstance(cls.__slots__, str), f"{name!r}s slots shouldn't be strings"
|
|
|
|
|
|
|
|
# specify if a certain module/class/base class should have dict-
|
2023-06-29 11:38:09 +02:00
|
|
|
if any(i in included for i in (cls.__module__, name, cls.__base__.__name__)):
|
2022-05-05 17:40:22 +02:00
|
|
|
assert "__dict__" in get_slots(cls), f"class {name!r} ({path}) has no __dict__"
|
2021-05-29 16:18:16 +02:00
|
|
|
continue
|
2021-08-19 22:01:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
assert "__dict__" not in get_slots(cls), f"class '{name}' in {path} has __dict__"
|
2021-05-29 16:18:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_slots(_class):
|
2023-03-25 19:18:04 +01:00
|
|
|
return [attr for cls in _class.__mro__ if hasattr(cls, "__slots__") for attr in cls.__slots__]
|