From c27d7dfc66e557b1c14767c55c21ffad51b6bd05 Mon Sep 17 00:00:00 2001 From: Elton Cardoso do Nascimento <43186596+EltonCN@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:38:07 -0300 Subject: [PATCH] Type check --- src/cst_python/core/entities/codelet.py | 25 +++---------- src/cst_python/core/entities/memory.py | 4 +-- src/cst_python/core/entities/memory_object.py | 8 +++-- src/cst_python/core/entities/mind.py | 9 ++--- src/cst_python/python/alias.py | 2 ++ tests/test_typecheck.py | 36 +++++++++++++++++++ 6 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 tests/test_typecheck.py diff --git a/src/cst_python/core/entities/codelet.py b/src/cst_python/core/entities/codelet.py index a47d5ba..eed248e 100644 --- a/src/cst_python/core/entities/codelet.py +++ b/src/cst_python/core/entities/codelet.py @@ -188,7 +188,7 @@ def threshold(self, value:float): #@alias.alias("get_time_step", "getTime_step") @property - def time_step(self) -> float: + def time_step(self) -> int: ''' If the proc() method is set to be called automatically in a loop, this variable stores the time step for such a loop. A timeStep of value 0 @@ -200,7 +200,7 @@ def time_step(self) -> float: #@alias.alias("set_time_step", "setTime_step") @time_step.setter - def time_step(self, value:float): + def time_step(self, value:int): self._time_step = value @@ -471,23 +471,6 @@ def get_inputs_of_type(self, type:str) -> List[Memory]: return inputs_of_type - #@alias.alias("getBroadcast") - def get_broadcast(self, name:str) -> Memory: - ''' - Returns a specific memory (with the given name) from the broadcast list - of the Codelet. - - Args: - name (str): the name of a memory to be retrieved at the broadcast list. - - Returns: - Memory: the memory. - ''' - if self._broadcast is not None: - for m in self._broadcast: - if m.compare_name(name): - return m - #@alias.alias("addBroadcast") def add_broadcast(self, memory:Memory) -> None: ''' @@ -551,7 +534,7 @@ def __str__(self) -> str: return result def _get_memory(self, search_list:List[Memory], type:Optional[str]=None, - index:Optional[int]=None, name:Optional[str]=None) -> Memory: + index:Optional[int]=None, name:Optional[str]=None) -> Memory|None: ''' This method returns an memory from a list. If it couldn't find the given M, it sets this codelet as not able to perform proc(), and @@ -597,7 +580,7 @@ def _get_memory(self, search_list:List[Memory], type:Optional[str]=None, return found_MO #@alias.alias("getInput") - def get_input(self, type:Optional[str]=None, index:Optional[int]=None, name:Optional[str]=None) -> Memory: + def get_input(self, type:Optional[str]=None, index:Optional[int]=None, name:Optional[str]=None) -> Memory|None: ''' This method returns an input memory from its input list. If it couldn't find the given M, it sets this codelet as not able to perform proc(), and diff --git a/src/cst_python/core/entities/memory.py b/src/cst_python/core/entities/memory.py index f12b58c..eab496a 100644 --- a/src/cst_python/core/entities/memory.py +++ b/src/cst_python/core/entities/memory.py @@ -152,8 +152,8 @@ def compare_name(self, other_name:str) -> bool: Returns: bool: True if is the same name. ''' - if self._name is None: + if self.get_name() is None: return False - return self._name.lower() == other_name.lower() + return self.get_name().lower() == other_name.lower() \ No newline at end of file diff --git a/src/cst_python/core/entities/memory_object.py b/src/cst_python/core/entities/memory_object.py index cf12eab..199bc83 100644 --- a/src/cst_python/core/entities/memory_object.py +++ b/src/cst_python/core/entities/memory_object.py @@ -1,7 +1,7 @@ from __future__ import annotations import time -from typing import Any, Set +from typing import Any, Set, cast from cst_python.python import alias from .memory import Memory @@ -131,7 +131,11 @@ def __hash__(self) -> int: return result #@alias.alias("equals") - def __eq__(self, value: MemoryObject) -> bool: + def __eq__(self, value: object) -> bool: + if not isinstance(value, MemoryObject): + return False + value = cast(MemoryObject, value) + if id(self) == id(value): return True if value is None: diff --git a/src/cst_python/core/entities/mind.py b/src/cst_python/core/entities/mind.py index 6b2d3e2..e52b364 100644 --- a/src/cst_python/core/entities/mind.py +++ b/src/cst_python/core/entities/mind.py @@ -99,7 +99,7 @@ def create_memory_container(self, name:str) -> Optional[MemoryContainer]: ''' mc = None - if self._raw is not None: + if self._raw_memory is not None: mc = self._raw_memory.create_memory_container(name) return mc @@ -128,7 +128,7 @@ def create_rest_memory_object(self, name:str, mo = None if self._raw_memory is not None: - mo = self._raw_memory.create_rest_memory_object(name, hostname, port) + mo = self._raw_memory.create_rest_memory_object(name, port, hostname) return mo @@ -155,7 +155,7 @@ def create_rest_memory_container(self, name:str, mc = None if self._raw_memory is not None: - mc = self._raw_memory.create_rest_memory_container(name, hostname, port) + mc = self._raw_memory.create_rest_memory_container(name, port, hostname) return mc @@ -195,7 +195,8 @@ def insert_codelet(self, co:Codelet, group_name:Optional[str]=None) -> Codelet: if self._code_rack is not None: self._code_rack.add_codelet(co) - self.register_codelet(co, group_name) + if group_name is not None: + self.register_codelet(co, group_name) return co diff --git a/src/cst_python/python/alias.py b/src/cst_python/python/alias.py index acd74cf..8c63dda 100644 --- a/src/cst_python/python/alias.py +++ b/src/cst_python/python/alias.py @@ -1,5 +1,6 @@ import warnings import functools +import typing from typing import Any, Callable, Type class aliased: @@ -26,6 +27,7 @@ class alias: def __init__(self, *aliases:str) -> None: self._aliases = set(aliases) + @typing.no_type_check def __call__(self, method:Callable) -> Callable: method._aliases = self._aliases diff --git a/tests/test_typecheck.py b/tests/test_typecheck.py new file mode 100644 index 0000000..60cb081 --- /dev/null +++ b/tests/test_typecheck.py @@ -0,0 +1,36 @@ +import os +import glob +import subprocess +import unittest +import pathlib +import sys +from typing import List + + + + +class TestTypeCheck(unittest.TestCase): + + def test_run_mypy_module(self): + """Run mypy on all module sources""" + mypy_call: List[str] = self.base_call + [self.pkg_path] + subprocess.check_call(mypy_call) + + #def test_run_mypy_tests(self): + # """Run mypy on all tests in module under the tests directory""" + # mypy_call: List[str] = self.base_call + [self.tests_path] + # subprocess.check_call(mypy_call) + + def __init__(self, *args, **kwargs) -> None: + super(TestTypeCheck, self).__init__(*args, **kwargs) + + self.tests_path = pathlib.Path(__file__).parent.resolve() + + + self.pkg_path = os.path.join(self.tests_path, "../src/cst_python") + + self.mypy_opts: List[str] = ['--ignore-missing-imports'] + + self.base_call : List[str] = [sys.executable, "-m", "mypy"] + self.mypy_opts + + \ No newline at end of file