From c6ade633b873a46bb0a24d90038110700e56e650 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Sat, 23 Nov 2024 16:28:45 -0800 Subject: [PATCH 1/4] fix: hasattr checks if attr is None --- ollama/_types.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ollama/_types.py b/ollama/_types.py index bcf8896..28fb06c 100644 --- a/ollama/_types.py +++ b/ollama/_types.py @@ -23,7 +23,30 @@ def __setitem__(self, key: str, value: Any) -> None: setattr(self, key, value) def __contains__(self, key: str) -> bool: - return hasattr(self, key) + """ + >>> msg = Message(role='user') + >>> 'nonexistent' in msg + False + >>> 'role' in msg + True + >>> 'content' in msg + False + >>> msg.content = 'hello!' + >>> 'content' in msg + True + >>> msg = Message(role='user', content='hello!') + >>> 'content' in msg + True + >>> 'tool_calls' in msg + False + >>> msg['tool_calls'] = [] + >>> 'tool_calls' in msg + True + >>> msg['tool_calls'] = [Message.ToolCall(function=Message.ToolCall.Function(name='foo', arguments={}))] + >>> 'tool_calls' in msg + True + """ + return key in self.model_fields_set def get(self, key: str, default: Any = None) -> Any: return getattr(self, key, default) From 986fb4c7b3f5dd7b59874e674e28ecdd0ab03b59 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Sat, 23 Nov 2024 16:35:34 -0800 Subject: [PATCH 2/4] fix: skip tests on examples, readme --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c35a470..19c3fdc 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -2,7 +2,7 @@ name: test on: pull_request: - paths: + paths-ignore: - 'examples/**' - '**/README.md' From 00c64332cc4ddd9e2ffd6f6839a1a55179e68f98 Mon Sep 17 00:00:00 2001 From: jmorganca Date: Sat, 23 Nov 2024 18:15:05 -0800 Subject: [PATCH 3/4] check defaults that aren't None too --- ollama/_types.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ollama/_types.py b/ollama/_types.py index 28fb06c..89a74f0 100644 --- a/ollama/_types.py +++ b/ollama/_types.py @@ -45,8 +45,17 @@ def __contains__(self, key: str) -> bool: >>> msg['tool_calls'] = [Message.ToolCall(function=Message.ToolCall.Function(name='foo', arguments={}))] >>> 'tool_calls' in msg True + >>> tool = Tool() + >>> 'type' in tool + True """ - return key in self.model_fields_set + if key in self.model_fields_set: + return True + + if key in self.model_fields: + return self.model_fields[key].default is not None + + return False def get(self, key: str, default: Any = None) -> Any: return getattr(self, key, default) From 1e22f2e118a895b0a098ca64ad4757548970775b Mon Sep 17 00:00:00 2001 From: jmorganca Date: Sat, 23 Nov 2024 18:22:38 -0800 Subject: [PATCH 4/4] add test case for explicit None --- ollama/_types.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ollama/_types.py b/ollama/_types.py index 89a74f0..5be4850 100644 --- a/ollama/_types.py +++ b/ollama/_types.py @@ -45,6 +45,9 @@ def __contains__(self, key: str) -> bool: >>> msg['tool_calls'] = [Message.ToolCall(function=Message.ToolCall.Function(name='foo', arguments={}))] >>> 'tool_calls' in msg True + >>> msg['tool_calls'] = None + >>> 'tool_calls' in msg + True >>> tool = Tool() >>> 'type' in tool True