Skip to content

Commit

Permalink
Merge pull request #334 from ollama/mxyng/hasattr-none
Browse files Browse the repository at this point in the history
  • Loading branch information
jmorganca authored Nov 24, 2024
2 parents 2e05cde + 1e22f2e commit 64e3723
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: test

on:
pull_request:
paths:
paths-ignore:
- 'examples/**'
- '**/README.md'

Expand Down
37 changes: 36 additions & 1 deletion ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,42 @@ 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
>>> msg['tool_calls'] = None
>>> 'tool_calls' in msg
True
>>> tool = Tool()
>>> 'type' in tool
True
"""
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)
Expand Down

0 comments on commit 64e3723

Please sign in to comment.