Skip to content

Commit

Permalink
Type check
Browse files Browse the repository at this point in the history
  • Loading branch information
EltonCN committed Nov 26, 2024
1 parent a9d6ae2 commit c27d7df
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
25 changes: 4 additions & 21 deletions src/cst_python/core/entities/codelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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:
'''
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/cst_python/core/entities/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

8 changes: 6 additions & 2 deletions src/cst_python/core/entities/memory_object.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions src/cst_python/core/entities/mind.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/cst_python/python/alias.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings
import functools
import typing
from typing import Any, Callable, Type

class aliased:
Expand All @@ -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

Expand Down
36 changes: 36 additions & 0 deletions tests/test_typecheck.py
Original file line number Diff line number Diff line change
@@ -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


0 comments on commit c27d7df

Please sign in to comment.