Skip to content

Commit 64e3723

Browse files
authored
Merge pull request #334 from ollama/mxyng/hasattr-none
2 parents 2e05cde + 1e22f2e commit 64e3723

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: test
22

33
on:
44
pull_request:
5-
paths:
5+
paths-ignore:
66
- 'examples/**'
77
- '**/README.md'
88

ollama/_types.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,42 @@ def __setitem__(self, key: str, value: Any) -> None:
2323
setattr(self, key, value)
2424

2525
def __contains__(self, key: str) -> bool:
26-
return hasattr(self, key)
26+
"""
27+
>>> msg = Message(role='user')
28+
>>> 'nonexistent' in msg
29+
False
30+
>>> 'role' in msg
31+
True
32+
>>> 'content' in msg
33+
False
34+
>>> msg.content = 'hello!'
35+
>>> 'content' in msg
36+
True
37+
>>> msg = Message(role='user', content='hello!')
38+
>>> 'content' in msg
39+
True
40+
>>> 'tool_calls' in msg
41+
False
42+
>>> msg['tool_calls'] = []
43+
>>> 'tool_calls' in msg
44+
True
45+
>>> msg['tool_calls'] = [Message.ToolCall(function=Message.ToolCall.Function(name='foo', arguments={}))]
46+
>>> 'tool_calls' in msg
47+
True
48+
>>> msg['tool_calls'] = None
49+
>>> 'tool_calls' in msg
50+
True
51+
>>> tool = Tool()
52+
>>> 'type' in tool
53+
True
54+
"""
55+
if key in self.model_fields_set:
56+
return True
57+
58+
if key in self.model_fields:
59+
return self.model_fields[key].default is not None
60+
61+
return False
2762

2863
def get(self, key: str, default: Any = None) -> Any:
2964
return getattr(self, key, default)

0 commit comments

Comments
 (0)