diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index c66b14dd..e11c3600 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -75,6 +75,5 @@ jobs: wget -O tmp/test.input.001.bin https://github.com/ceccopierangiolieugenio/binaryRepo/raw/master/pyTermTk/tests/test.input.001.bin wget -O tmp/test.input.002.bin https://github.com/ceccopierangiolieugenio/binaryRepo/raw/master/pyTermTk/tests/test.input.002.bin wget -O tmp/test.input.003.bin https://github.com/ceccopierangiolieugenio/binaryRepo/raw/master/pyTermTk/tests/test.input.003.bin - pytest ${DDDD}/tests/pytest/test_003_string.py - pytest ${DDDD}/tests/pytest/test_002_textedit.py - pytest ${DDDD}/tests/pytest/test_001_demo.py + pytest ${DDDD}/tests/pytest/test_* + pytest ${DDDD}/tests/pytest/run_* diff --git a/.vscode/settings.json b/.vscode/settings.json index bc987018..bdc20ae7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,11 @@ "-vv", "--color=yes", "-s" ], "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true + "python.testing.pytestEnabled": true, + "mypy-type-checker.args": [ + "--config-file=pyproject.toml" + ], + "python.analysis.extraPaths": [ + "./libs/pyTermTk", + ] } \ No newline at end of file diff --git a/Makefile b/Makefile index b54117f5..930bc092 100644 --- a/Makefile +++ b/Makefile @@ -141,9 +141,6 @@ test: .venv . .venv/bin/activate ; \ flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude .venv,build,tmp,experiments ; . .venv/bin/activate ; \ - pytest tests/pytest/test_003_string.py ; + pytest tests/pytest/test_* . .venv/bin/activate ; \ - pytest tests/pytest/test_002_textedit.py ; - . .venv/bin/activate ; \ - pytest -v tests/pytest/test_001_demo.py ; - + pytest tests/pytest/run_* diff --git a/libs/pyTermTk/TermTk/TTkCore/signal.py b/libs/pyTermTk/TermTk/TTkCore/signal.py index b69acf32..dcf6b0de 100644 --- a/libs/pyTermTk/TermTk/TTkCore/signal.py +++ b/libs/pyTermTk/TermTk/TTkCore/signal.py @@ -60,6 +60,7 @@ __all__ = ['pyTTkSlot', 'pyTTkSignal'] # from typing import TypeVar, TypeVarTuple, Generic, List from inspect import getfullargspec, iscoroutinefunction from types import LambdaType +from typing import Union, get_origin, get_args from threading import Lock import asyncio @@ -82,6 +83,29 @@ else: def _run_coroutines(coros): Thread(target=_async_runner, args=(coros,)).start() +def _check_types(slot_type, signal_type): + ''' Comparing the signal and slot types + + return True if the slot_type is compatible with the signal_type + ''' + if signal_type==slot_type: return True + + if get_origin(slot_type) is Union: + _sl_ts = get_args(slot_type) + else: + _sl_ts = (slot_type,) + + if get_origin(signal_type) is Union: + _sig_ts = get_args(signal_type) + else: + _sig_ts = (signal_type,) + + for _sig_t in _sig_ts: + if not any( issubclass(_sl_t, _sig_t) for _sl_t in _sl_ts): + return False + + return True + def pyTTkSlot(*args): def pyTTkSlot_d(func): # Add signature attributes to the function @@ -139,10 +163,9 @@ class pyTTkSignal(): error = "Decorated slot has no signature compatible: "+slot.__name__+str(slot._TTkslot_attr)+" != signal"+str(self._types) raise TypeError(error) else: - for a,b in zip(slot._TTkslot_attr, self._types): - if a!=b and not issubclass(a,b): - error = "Decorated slot has no signature compatible: "+slot.__name__+str(slot._TTkslot_attr)+" != signal"+str(self._types) - raise TypeError(error) + for slot_type,signal_type in zip(slot._TTkslot_attr, self._types): + if not _check_types(slot_type, signal_type): + raise TypeError("Decorated slot has no signature compatible: "+slot.__name__+str(slot._TTkslot_attr)+" != signal"+str(self._types)) if iscoroutinefunction(slot): if slot not in self._connected_async_slots: self._connected_async_slots[slot]=slice(nargs) diff --git a/libs/pyTermTk/TermTk/TTkCore/string.py b/libs/pyTermTk/TermTk/TTkCore/string.py index 18a6814e..b9fd58bb 100644 --- a/libs/pyTermTk/TermTk/TTkCore/string.py +++ b/libs/pyTermTk/TermTk/TTkCore/string.py @@ -22,7 +22,7 @@ from __future__ import annotations -__all__ = ['TTkString'] +__all__ = ['TTkString','TTkStringType'] import os import re @@ -712,4 +712,6 @@ class TTkString(): if os.environ.get("TERMTK_GPM",False): _getDataW = _getDataW_tty else: - _getDataW = _getDataW_pts \ No newline at end of file + _getDataW = _getDataW_pts + +TTkStringType = Union[str, TTkString] \ No newline at end of file diff --git a/libs/pyTermTk/pyproject.toml b/libs/pyTermTk/pyproject.toml index bdf79224..10073c9a 100644 --- a/libs/pyTermTk/pyproject.toml +++ b/libs/pyTermTk/pyproject.toml @@ -1,43 +1,49 @@ [build-system] -requires = ["setuptools>=42", "wheel"] -build-backend = "setuptools.build_meta" + requires = ["setuptools>=42", "wheel"] + build-backend = "setuptools.build_meta" [project] -name = "pyTermTk" -dynamic = ["version"] -requires-python = ">=3.9" -description = "Python Terminal Toolkit" -readme = {file = "README.md", content-type = "text/markdown"} -license = { text = "MIT License" } -authors = [ - { name = "Eugenio Parodi", email = "ceccopierangiolieugenio@googlemail.com" } -] -classifiers = [ - "Programming Language :: Python :: 3", - "License :: OSI Approved :: MIT License", - "Operating System :: OS Independent", - "Development Status :: 3 - Alpha", - "Environment :: Console", - "Intended Audience :: Education", - "Intended Audience :: Developers", - "Intended Audience :: Information Technology", - "Topic :: Terminals", - "Topic :: Text Editors :: Text Processing", - "Topic :: Software Development :: User Interfaces", - "Topic :: Software Development :: Libraries", - "Topic :: Software Development :: Libraries :: Python Modules", - "Topic :: Software Development :: Libraries :: Application Frameworks" -] + name = "pyTermTk" + dynamic = ["version"] + requires-python = ">=3.9" + description = "Python Terminal Toolkit" + readme = {file = "README.md", content-type = "text/markdown"} + license = { text = "MIT License" } + authors = [ + { name = "Eugenio Parodi", email = "ceccopierangiolieugenio@googlemail.com" } + ] + classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Development Status :: 3 - Alpha", + "Environment :: Console", + "Intended Audience :: Education", + "Intended Audience :: Developers", + "Intended Audience :: Information Technology", + "Topic :: Terminals", + "Topic :: Text Editors :: Text Processing", + "Topic :: Software Development :: User Interfaces", + "Topic :: Software Development :: Libraries", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: Software Development :: Libraries :: Application Frameworks" + ] [project.urls] -Homepage = "https://github.com/ceccopierangiolieugenio/pyTermTk" -Documentation = "https://ceccopierangiolieugenio.github.io/pyTermTk-Docs/" -Repository = "https://github.com/ceccopierangiolieugenio/pyTermTk.git" -Issues = "https://github.com/ceccopierangiolieugenio/pyTermTk/issues" -Changelog = "https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/libs/pyTermTk/CHANGELOG.md" + Homepage = "https://github.com/ceccopierangiolieugenio/pyTermTk" + Documentation = "https://ceccopierangiolieugenio.github.io/pyTermTk-Docs/" + Repository = "https://github.com/ceccopierangiolieugenio/pyTermTk.git" + Issues = "https://github.com/ceccopierangiolieugenio/pyTermTk/issues" + Changelog = "https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/libs/pyTermTk/CHANGELOG.md" [tool.setuptools.packages.find] -where = ["."] + where = ["."] [tool.setuptools.dynamic] -version = {attr = "TermTk.__version__"} + version = {attr = "TermTk.__version__"} + +[tool.mypy] + ignore_missing_imports = true + warn_unused_configs = true + disallow_any_generics = true + disallow_subclassing_any = true \ No newline at end of file diff --git a/tests/pytest/test_001_demo.py b/tests/pytest/run_test_001_demo.py similarity index 100% rename from tests/pytest/test_001_demo.py rename to tests/pytest/run_test_001_demo.py diff --git a/tests/pytest/test_002_textedit.py b/tests/pytest/run_test_002_textedit.py similarity index 100% rename from tests/pytest/test_002_textedit.py rename to tests/pytest/run_test_002_textedit.py diff --git a/tests/pytest/test_004_slots.py b/tests/pytest/test_004_slots.py new file mode 100644 index 00000000..d041a6ba --- /dev/null +++ b/tests/pytest/test_004_slots.py @@ -0,0 +1,133 @@ +# MIT License +# +# Copyright (c) 2025 Eugenio Parodi +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import sys, os +import pytest +from typing import Union, Optional + +sys.path.append(os.path.join(sys.path[0],'../../libs/pyTermTk')) + +import TermTk as ttk + +def test_slots_stringType(): + signal1 = ttk.pyTTkSignal(str) + signal2 = ttk.pyTTkSignal(ttk.TTkString) + signal3 = ttk.pyTTkSignal(ttk.TTkStringType) + + @ttk.pyTTkSlot(str) + def _test_slot1(txt:str): + print("slot 1", txt) + + @ttk.pyTTkSlot(ttk.TTkString) + def _test_slot2(txt:ttk.TTkString): + print("slot 2", txt) + + @ttk.pyTTkSlot(ttk.TTkStringType) + def _test_slot3(txt:ttk.TTkStringType): + print("slot 3", txt) + + signal1.connect(_test_slot1) + signal2.connect(_test_slot2) + signal3.connect(_test_slot3) + + signal1.connect(_test_slot3) + signal2.connect(_test_slot3) + + with pytest.raises(TypeError): + signal1.connect(_test_slot2) + with pytest.raises(TypeError): + signal2.connect(_test_slot1) + with pytest.raises(TypeError): + signal3.connect(_test_slot1) + with pytest.raises(TypeError): + signal3.connect(_test_slot2) + + print('OKKK') + + signal1.emit('Eugenio1') + signal2.emit('Eugenio2') + signal3.emit('Eugenio3') + +def test_slots_unionType(): + t1 = ttk.TTkWidget + t2 = ttk.TTkContainer + t3 = ttk.TTkFrame + # TTkContainer extends TTkWidget + # TTkWindow extends TTkFrame + # TTkFileButtonPicker extends TTkButton + t4 = Union[ttk.TTkFrame, ttk.TTkButton] + t5 = Union[ttk.TTkWindow, ttk.TTkFileButtonPicker] + + signal1 = ttk.pyTTkSignal(t1) + signal2 = ttk.pyTTkSignal(t2) + signal3 = ttk.pyTTkSignal(t3) + signal4 = ttk.pyTTkSignal(t4) + signal5 = ttk.pyTTkSignal(t5) + + @ttk.pyTTkSlot(t1) + def _test_slot1(_): pass + @ttk.pyTTkSlot(t2) + def _test_slot2(_): pass + @ttk.pyTTkSlot(t3) + def _test_slot3(_): pass + @ttk.pyTTkSlot(t4) + def _test_slot4(_): pass + @ttk.pyTTkSlot(t5) + def _test_slot5(_): pass + + signal1.connect(_test_slot1) + signal1.connect(_test_slot2) + signal1.connect(_test_slot3) + signal1.connect(_test_slot4) + signal1.connect(_test_slot5) + + signal2.connect(_test_slot2) + signal2.connect(_test_slot3) + signal2.connect(_test_slot4) + signal2.connect(_test_slot5) + + signal3.connect(_test_slot3) + signal3.connect(_test_slot4) + signal3.connect(_test_slot5) + + signal4.connect(_test_slot4) + signal4.connect(_test_slot5) + + signal5.connect(_test_slot5) + + with pytest.raises(TypeError): + signal5.connect(_test_slot4) + with pytest.raises(TypeError): + signal5.connect(_test_slot1) + + with pytest.raises(TypeError): + @ttk.pyTTkSlot(float) + def _slot(_): pass + ttk.pyTTkSignal(int).connect(_slot) + with pytest.raises(TypeError): + @ttk.pyTTkSlot(int) + def _slot(_): pass + ttk.pyTTkSignal(Union[int,float]).connect(_slot) + with pytest.raises(TypeError): + @ttk.pyTTkSlot(Union[int,str]) + def _slot(_): pass + ttk.pyTTkSignal(Union[int,float]).connect(_slot) \ No newline at end of file