Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Fixing mypy issues for syft/service/ and syft/util/ #8492

Merged
merged 53 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d188d42
[refactor] done fixing mypy issues for `syft/service/request`
khoaguin Feb 7, 2024
71dc2c3
[refactor] checking if `attr` has both `fget` and `fset` methods,
khoaguin Feb 7, 2024
cd0197a
Merge branch 'dev' into fix-mypy-issues-service-request
khoaguin Feb 12, 2024
c1f39e9
Merge branch 'dev' into fix-mypy-issues-service-request
khoaguin Feb 14, 2024
231a2e3
Merge branch 'dev' into fix-mypy-issues-service-request
khoaguin Feb 15, 2024
39b14bb
Merge branch 'dev' into fix-mypy-issues-service-dir
kiendang Feb 15, 2024
4a38df5
[refactor] done fixing mypy issues for `syft/service/code_history`
khoaguin Feb 15, 2024
b6bd489
[refactor] done fixing mypy issues for `service/project/project_servi…
khoaguin Feb 15, 2024
de9fee3
[refactor] done fixing mypy issues for `syft/util/`
khoaguin Feb 15, 2024
a9f6983
Merge branch 'dev' into fix-mypy-issues-service-dir
khoaguin Feb 15, 2024
86ca8f1
[refactor] done fixing mypy issues for `syft/service/action`
khoaguin Feb 16, 2024
22b4c39
[tests] skip `test_transfer_request_nonblocking` on windows
khoaguin Feb 16, 2024
123104b
Merge branch 'dev' into fix-mypy-issues-service-dir
khoaguin Feb 18, 2024
2d32d24
[refactor] on fixing mypy issues for `service/queue/`
khoaguin Feb 19, 2024
70e21eb
[refactor] done fixing mypy issues for
khoaguin Feb 19, 2024
f40e407
[refactor] fix cicular import error in queue.py
khoaguin Feb 19, 2024
6ce490e
delete unnecessary protocol json files
khoaguin Feb 19, 2024
ecdd36c
Merge branch 'dev' into fix-mypy-issues-service-dir
khoaguin Feb 19, 2024
1778070
[refactor] done fixing mypy issues for `service/queue`
khoaguin Feb 19, 2024
5f23d1a
[refactor] continue fixing mypy issues in
khoaguin Feb 19, 2024
3c63ccf
[refactor] done fixing mypy issues for `service/code`
khoaguin Feb 20, 2024
0f33092
[refactor] fix mypy issues in code related to user code and action se…
khoaguin Feb 20, 2024
4ae4742
[refactor] remove 'Execution denied' from Err: input policy is None
khoaguin Feb 20, 2024
b55676d
Merge branch 'dev' into fix-mypy-issues-service-dir
khoaguin Feb 20, 2024
06cce86
[refactor] continue fixing mypy issues for `syft/service`
khoaguin Feb 21, 2024
a080be6
[refactor] fix circular import errors
khoaguin Feb 21, 2024
f5fa929
[refactor] done fixing mypy issues in `syft/service/`
khoaguin Feb 21, 2024
2331c79
Merge branch 'dev' into fix-mypy-issues-service-dir
khoaguin Feb 21, 2024
65f7564
[refactor] remove unnecessary type checkings
khoaguin Feb 22, 2024
8e5a836
[bugfix] fix `test_code_accept_deny` not passed due to
khoaguin Feb 22, 2024
52a1443
Fix instrument typing
kiendang Feb 23, 2024
beab7ca
Improve noop and instrument compatibility
kiendang Feb 23, 2024
5482d81
[refactor] reenable checking `union-attr` mypy errors
khoaguin Feb 26, 2024
bda83b4
[refactor] using `cast(AbstractNode, context.node)` to
khoaguin Feb 26, 2024
39eb66b
Merge branch 'dev' into fix-mypy-issues-service-dir
khoaguin Feb 26, 2024
74dfe4d
[security] update `bandit` to `1.7.7`
khoaguin Feb 27, 2024
bf6fb88
Merge branch 'dev' into fix-mypy-issues-service-dir
khoaguin Feb 28, 2024
353c3d9
action_graph: fix type: ignore[call-arg] issue in action graph
shubham3121 Feb 28, 2024
dd52fb2
add missing typing to TraceResult and _set_obj_location method
shubham3121 Feb 28, 2024
7a8d08f
action_object: fix typing for syft_internal_type variable
shubham3121 Feb 28, 2024
1e952fd
action_object: fix _repr_markdown_ for ActionObject when syft_action_…
shubham3121 Feb 28, 2024
4961d8b
action_object: fix storing of data repr when value is None
shubham3121 Feb 28, 2024
5e95f00
[refactor] fixing mypy issues according
khoaguin Feb 28, 2024
8fcb738
fix typing in trace_decorator
shubham3121 Feb 28, 2024
27277dd
[refactor] using `cast(AbstractNode, context.node` to
khoaguin Feb 28, 2024
760db95
remove some type ignores
shubham3121 Feb 28, 2024
f420232
remove type ignores in registry.py
shubham3121 Feb 28, 2024
6d31e21
worker_pool_service: cast context.node to AbstractNode
shubham3121 Feb 28, 2024
6ada1dd
remove all occurences of type ignores from registry.py
shubham3121 Feb 28, 2024
9c91768
fix return type of handle_api_call_with_unsigned_result
shubham3121 Feb 28, 2024
51700dd
remove type ignore from user code
shubham3121 Feb 28, 2024
fd31708
cast context.node to AbstractNode in user and setting service
shubham3121 Feb 28, 2024
ebda550
fix syntax for type casting of context.node to AbstractNode
shubham3121 Feb 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ repos:
- id: mypy
name: "mypy: syft"
always_run: true
files: "^packages/syft/src/syft/serde|^packages/syft/src/syft/util/env.py|^packages/syft/src/syft/util/logger.py|^packages/syft/src/syft/util/markdown.py|^packages/syft/src/syft/util/notebook_ui/notebook_addons.py|^packages/syft/src/syft/service/warnings.py|^packages/syft/src/syft/service/dataset|^packages/syft/src/syft/service/worker|^packages/syft/src/syft/service/user"
#files: "^packages/syft/src/syft/serde"
# files: "^packages/syft/src/syft/serde|^packages/syft/src/syft/util|^packages/syft/src/syft/service"
files: "^packages/syft/src/syft/service/action"
args: [
"--follow-imports=skip",
"--ignore-missing-imports",
Expand Down
4 changes: 3 additions & 1 deletion packages/syft/src/syft/client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def set_api_for(
cls.__api_registry__[key] = api

@classmethod
def api_for(cls, node_uid: UID, user_verify_key: SyftVerifyKey) -> SyftAPI:
def api_for(
cls, node_uid: UID, user_verify_key: SyftVerifyKey
) -> Optional[SyftAPI]:
key = (node_uid, user_verify_key)
return cls.__api_registry__.get(key, None)

Expand Down
2 changes: 1 addition & 1 deletion packages/syft/src/syft/service/action/action_data_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ActionDataEmpty(SyftObject):
__canonical_name__ = "ActionDataEmpty"
__version__ = SYFT_OBJECT_VERSION_1

syft_internal_type: Optional[Type] = NoneType
syft_internal_type: Optional[Type] = NoneType # type: ignore

def __repr__(self) -> str:
return f"{type(self).__name__} <{self.syft_internal_type}>"
Expand Down
68 changes: 36 additions & 32 deletions packages/syft/src/syft/service/action/action_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from result import Err
from result import Ok
from result import Result
from typing_extensions import Self

# relative
from ...node.credentials import SyftVerifyKey
Expand Down Expand Up @@ -79,7 +78,7 @@ def make_created_at(cls, v: Optional[DateTime]) -> DateTime:
return DateTime.now() if v is None else v

@staticmethod
def from_action(action: Action, credentials: SyftVerifyKey):
def from_action(action: Action, credentials: SyftVerifyKey) -> "NodeActionData":
khoaguin marked this conversation as resolved.
Show resolved Hide resolved
is_mutagen = action.remote_self is not None and (
action.remote_self == action.result_id
)
Expand All @@ -91,24 +90,26 @@ def from_action(action: Action, credentials: SyftVerifyKey):
)

@staticmethod
def from_action_obj(action_obj: ActionObject, credentials: SyftVerifyKey):
def from_action_obj(
action_obj: ActionObject, credentials: SyftVerifyKey
) -> "NodeActionData":
khoaguin marked this conversation as resolved.
Show resolved Hide resolved
return NodeActionData(
id=action_obj.id,
type=NodeType.ACTION_OBJECT,
user_verify_key=credentials,
)

def __hash__(self):
def __hash__(self) -> int:
return hash(self.id)

def __eq__(self, other: Self):
def __eq__(self, other: Any) -> bool:
if not isinstance(other, NodeActionData):
raise NotImplementedError(
"Comparisions can be made with NodeActionData type objects only."
)
return hash(self) == hash(other)

def __repr__(self):
def __repr__(self) -> str:
return self._repr_debug_()


Expand Down Expand Up @@ -148,7 +149,7 @@ def get(self, uid: Any) -> Any:
def delete(self, uid: Any) -> None:
raise NotImplementedError

def find_neighbors(self, uid: Any) -> List[Any]:
def find_neighbors(self, uid: Any) -> Optional[List[Any]]:
khoaguin marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError

def update(self, uid: Any, data: Any) -> None:
Expand Down Expand Up @@ -229,7 +230,9 @@ def lock(self) -> SyftLock:
def db(self) -> nx.Graph:
return self._db

def _thread_safe_cbk(self, cbk: Callable, *args, **kwargs):
def _thread_safe_cbk(
self, cbk: Callable, *args: Any, **kwargs: Any
) -> Result[Any, str]:
# TODO copied method from document_store, have it in one place and reuse?
locked = self.lock.acquire(blocking=True)
if not locked:
Expand Down Expand Up @@ -267,10 +270,11 @@ def _delete(self, uid: UID) -> None:
self.db.remove_node(uid)
self.save()

def find_neighbors(self, uid: UID) -> Optional[Iterable]:
def find_neighbors(self, uid: UID) -> Optional[List[Any]]:
if self.exists(uid=uid):
neighbors = self.db.neighbors(uid)
return neighbors
return None

def update(self, uid: UID, data: Any) -> None:
self._thread_safe_cbk(self._update, uid=uid, data=data)
Expand All @@ -294,7 +298,7 @@ def _remove_edge(self, parent: Any, child: Any) -> None:
self.db.remove_edge(parent, child)
self.save()

def visualize(self, seed: int = 3113794652, figsize=(20, 10)) -> None:
def visualize(self, seed: int = 3113794652, figsize: tuple = (20, 10)) -> None:
plt.figure(figsize=figsize)
pos = nx.spring_layout(self.db, seed=seed)
return nx.draw_networkx(self.db, pos=pos, with_labels=True)
Expand All @@ -305,10 +309,10 @@ def nodes(self) -> Iterable:
def edges(self) -> Iterable:
return self.db.edges()

def get_predecessors(self, uid: UID) -> Iterable:
def get_predecessors(self, uid: UID) -> List:
return self.db.predecessors(uid)

def get_successors(self, uid: UID) -> Iterable:
def get_successors(self, uid: UID) -> List:
return self.db.successors(uid)

def is_parent(self, parent: Any, child: Any) -> bool:
Expand Down Expand Up @@ -372,10 +376,10 @@ def set(
credentials: SyftVerifyKey,
parent_uids: Optional[List[UID]] = None,
) -> Result[NodeActionData, str]:
if self.graph.exists(uid=node.id):
if self.graph.exists(uid=node.id): # type: ignore[call-arg]
return Err(f"Node already exists in the graph: {node}")

self.graph.set(uid=node.id, data=node)
self.graph.set(uid=node.id, data=node) # type: ignore[call-arg]

if parent_uids is None:
parent_uids = []
Expand All @@ -397,8 +401,8 @@ def get(
credentials: SyftVerifyKey,
) -> Result[NodeActionData, str]:
# 🟡 TODO: Add permission check
if self.graph.exists(uid=uid):
node_data = self.graph.get(uid=uid)
if self.graph.exists(uid=uid): # type: ignore[call-arg]
node_data = self.graph.get(uid=uid) # type: ignore[call-arg]
return Ok(node_data)
return Err(f"Node does not exists with id: {uid}")

Expand All @@ -408,8 +412,8 @@ def delete(
credentials: SyftVerifyKey,
) -> Result[bool, str]:
# 🟡 TODO: Add permission checks
if self.graph.exists(uid=uid):
self.graph.delete(uid=uid)
if self.graph.exists(uid=uid): # type: ignore[call-arg]
self.graph.delete(uid=uid) # type: ignore[call-arg]
return Ok(True)
return Err(f"Node does not exists with id: {uid}")

Expand All @@ -420,11 +424,11 @@ def update(
credentials: SyftVerifyKey,
) -> Result[NodeActionData, str]:
# 🟡 TODO: Add permission checks
node_data = self.graph.get(uid=uid)
node_data = self.graph.get(uid=uid) # type: ignore[call-arg]
if node_data is not None:
for key, val in data.to_dict(exclude_empty=True).items():
setattr(node_data, key, val)
self.graph.update(uid=uid, data=node_data)
self.graph.update(uid=uid, data=node_data) # type: ignore[call-arg]
return Ok(node_data)
return Err(f"Node does not exists for uid: {uid}")

Expand All @@ -438,7 +442,7 @@ def update_non_mutated_successor(
Used when a node is a mutagen and to update non-mutated
successor for all nodes between node_id and nm_successor_id
"""
node_data = self.graph.get(uid=node_id)
node_data = self.graph.get(uid=node_id) # type: ignore[call-arg]

data = NodeActionDataUpdate(
next_mutagen_node=nm_successor_id,
Expand All @@ -453,7 +457,7 @@ def update_non_mutated_successor(
# loop through successive mutagen nodes and
# update their last_nm_mutagen_node id
while node_id != nm_successor_id:
node_data = self.graph.get(uid=node_id)
node_data = self.graph.get(uid=node_id) # type: ignore[call-arg]

# If node is the last added mutagen node,
# then in that case its `next_mutagen_node` will be None
Expand Down Expand Up @@ -483,7 +487,7 @@ def update_non_mutated_successor(
def _get_last_non_mutated_mutagen(
self, credentials: SyftVerifyKey, uid: UID
) -> Result[UID, str]:
node_data = self.graph.get(uid=uid)
node_data = self.graph.get(uid=uid) # type: ignore[call-arg]
if node_data.is_mutated:
return Ok(node_data.last_nm_mutagen_node)

Expand All @@ -495,10 +499,10 @@ def add_edge(
child: UID,
credentials: SyftVerifyKey,
) -> Result[bool, str]:
if not self.graph.exists(parent):
if not self.graph.exists(parent): # type: ignore[call-arg]
return Err(f"Node does not exists for uid (parent): {parent}")

if not self.graph.exists(child):
if not self.graph.exists(child): # type: ignore[call-arg]
return Err(f"Node does not exists for uid (child): {child}")

result = self._get_last_non_mutated_mutagen(
Expand All @@ -511,13 +515,13 @@ def add_edge(

new_parent = result.ok()

self.graph.add_edge(parent=new_parent, child=child)
self.graph.add_edge(parent=new_parent, child=child) # type: ignore[call-arg]

return Ok(True)

def is_parent(self, parent: UID, child: UID) -> Result[bool, str]:
if self.graph.exists(child):
parents = self.graph.get_predecessors(child)
if self.graph.exists(child): # type: ignore[call-arg]
parents = self.graph.get_predecessors(child) # type: ignore[call-arg]
result = parent in parents
return Ok(result)
return Err(f"Node doesn't exists for id: {child}")
Expand All @@ -529,11 +533,11 @@ def query(
) -> Result[List[NodeActionData], str]:
if isinstance(qks, QueryKey):
qks = QueryKeys(qks=[qks])
subgraph = self.graph.subgraph(qks=qks)
return Ok(self.graph.topological_sort(subgraph=subgraph))
subgraph = self.graph.subgraph(qks=qks) # type: ignore[call-arg]
return Ok(self.graph.topological_sort(subgraph=subgraph)) # type: ignore[call-arg]

def nodes(self, credentials: SyftVerifyKey) -> Result[List, str]:
return Ok(self.graph.nodes())
return Ok(self.graph.nodes()) # type: ignore[call-arg]

def edges(self, credentials: SyftVerifyKey) -> Result[List, str]:
return Ok(self.graph.edges())
return Ok(self.graph.edges()) # type: ignore[call-arg]
13 changes: 10 additions & 3 deletions packages/syft/src/syft/service/action/action_graph_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# stdlib
from typing import List
from typing import Tuple
from typing import Optional
from typing import Union

# third party
Expand Down Expand Up @@ -60,6 +60,8 @@ def add_action(
if action_node.is_mutagen:
# updated non-mutated successor for all nodes between
# node_id and nm_successor_id
if action.remote_self is None:
return SyftError(message=f"action {action}'s remote_self is None")
result = self.store.update_non_mutated_successor(
node_id=action.remote_self.id,
nm_successor_id=action_node.id,
Expand Down Expand Up @@ -102,7 +104,9 @@ def add_action_obj(

return result.ok()

def _extract_input_and_output_from_action(self, action: Action) -> Tuple[UID]:
def _extract_input_and_output_from_action(
self, action: Action
) -> tuple[set[UID], Optional[UID]]:
input_uids = set()

if action.remote_self is not None:
Expand All @@ -114,7 +118,10 @@ def _extract_input_and_output_from_action(self, action: Action) -> Tuple[UID]:
for _, kwarg in action.kwargs.items():
input_uids.add(kwarg.id)

output_uid = action.result_id.id
if action.result_id is not None:
output_uid = action.result_id.id
else:
output_uid = None

return input_uids, output_uid

Expand Down
Loading
Loading