Compare commits

...

5 Commits

Author SHA1 Message Date
Jeffrey Morgan 64e3723e6b Merge pull request #334 from ollama/mxyng/hasattr-none 2024-11-23 18:27:22 -08:00
jmorganca 1e22f2e118 add test case for explicit None 2024-11-23 18:22:38 -08:00
jmorganca 00c64332cc check defaults that aren't None too 2024-11-23 18:15:05 -08:00
Michael Yang 986fb4c7b3 fix: skip tests on examples, readme 2024-11-23 16:44:09 -08:00
Michael Yang c6ade633b8 fix: hasattr checks if attr is None 2024-11-23 16:44:09 -08:00
2 changed files with 37 additions and 2 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ name: test
on:
pull_request:
paths:
paths-ignore:
- 'examples/**'
- '**/README.md'
+36 -1
View File
@@ -23,7 +23,42 @@ class SubscriptableBaseModel(BaseModel):
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)