Skip to content

Commit 653691f

Browse files
authored
Improve Slot Tests (python-telegram-bot#2541)
1 parent 92ff6a8 commit 653691f

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

tests/test_slots.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
19+
import os
1920
import importlib
2021
import importlib.util
2122
from glob import iglob
@@ -34,11 +35,15 @@
3435

3536

3637
def test_class_has_slots_and_dict(mro_slots):
37-
tg_paths = [p for p in iglob("../telegram/**/*.py", recursive=True) if '/vendor/' not in p]
38+
tg_paths = [p for p in iglob("telegram/**/*.py", recursive=True) if 'vendor' not in p]
3839

3940
for path in tg_paths:
40-
split_path = path.split('/')
41-
mod_name = f"telegram{'.ext.' if split_path[2] == 'ext' else '.'}{split_path[-1][:-3]}"
41+
# windows uses backslashes:
42+
if os.name == 'nt':
43+
split_path = path.split('\\')
44+
else:
45+
split_path = path.split('/')
46+
mod_name = f"telegram{'.ext.' if split_path[1] == 'ext' else '.'}{split_path[-1][:-3]}"
4247
spec = importlib.util.spec_from_file_location(mod_name, path)
4348
module = importlib.util.module_from_spec(spec)
4449
spec.loader.exec_module(module) # Exec module to get classes in it.
@@ -55,4 +60,20 @@ def test_class_has_slots_and_dict(mro_slots):
5560

5661

5762
def get_slots(_class):
58-
return [attr for cls in _class.__mro__ if hasattr(cls, '__slots__') for attr in cls.__slots__]
63+
slots = [attr for cls in _class.__mro__ if hasattr(cls, '__slots__') for attr in cls.__slots__]
64+
65+
# We're a bit hacky here to handle cases correctly, where we can't read the parents slots from
66+
# the mro
67+
if '__dict__' not in slots:
68+
try:
69+
70+
class Subclass(_class):
71+
__slots__ = ('__dict__',)
72+
73+
except TypeError as exc:
74+
if '__dict__ slot disallowed: we already got one' in str(exc):
75+
slots.append('__dict__')
76+
else:
77+
raise exc
78+
79+
return slots

0 commit comments

Comments
 (0)