community[patch]: mypy ignore fix (#18483)

Relates to #17048 
Description : Applied fix to dynamodb and elasticsearch file.

Error was : `Cannot override writeable attribute with read-only
property`
Suggestion:
instead of adding 
```
@messages.setter
def messages(self, messages: List[BaseMessage]) -> None:
    raise NotImplementedError("Use add_messages instead")
```

we can change base class property
`messages: List[BaseMessage]`
to
```
@property
def messages(self) -> List[BaseMessage]:...
```

then we don't need to add `@messages.setter` in all child classes.
This commit is contained in:
Smit Parmar 2024-03-29 04:06:53 +05:30 committed by GitHub
parent dc9e9a66db
commit dfc4177b50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -103,7 +103,7 @@ class DynamoDBChatMessageHistory(BaseChatMessageHistory):
) )
@property @property
def messages(self) -> List[BaseMessage]: # type: ignore def messages(self) -> List[BaseMessage]:
"""Retrieve the messages from DynamoDB""" """Retrieve the messages from DynamoDB"""
try: try:
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
@ -129,6 +129,13 @@ class DynamoDBChatMessageHistory(BaseChatMessageHistory):
messages = messages_from_dict(items) messages = messages_from_dict(items)
return messages return messages
@messages.setter
def messages(self, messages: List[BaseMessage]) -> None:
raise NotImplementedError(
"Direct assignment to 'messages' is not allowed."
" Use the 'add_messages' instead."
)
def add_message(self, message: BaseMessage) -> None: def add_message(self, message: BaseMessage) -> None:
"""Append the message to the record in DynamoDB""" """Append the message to the record in DynamoDB"""
try: try:

View File

@ -143,7 +143,7 @@ class ElasticsearchChatMessageHistory(BaseChatMessageHistory):
return es_client return es_client
@property @property
def messages(self) -> List[BaseMessage]: # type: ignore[override] def messages(self) -> List[BaseMessage]:
"""Retrieve the messages from Elasticsearch""" """Retrieve the messages from Elasticsearch"""
try: try:
from elasticsearch import ApiError from elasticsearch import ApiError
@ -167,6 +167,13 @@ class ElasticsearchChatMessageHistory(BaseChatMessageHistory):
return messages_from_dict(items) return messages_from_dict(items)
@messages.setter
def messages(self, messages: List[BaseMessage]) -> None:
raise NotImplementedError(
"Direct assignment to 'messages' is not allowed."
" Use the 'add_messages' instead."
)
def add_message(self, message: BaseMessage) -> None: def add_message(self, message: BaseMessage) -> None:
"""Add a message to the chat session in Elasticsearch""" """Add a message to the chat session in Elasticsearch"""
try: try: