Skip to content

Sourcery refactored main branch#2

Open
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main
Open

Sourcery refactored main branch#2
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main

Conversation

@sourcery-ai
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot commented Jan 18, 2024

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai Bot requested a review from AInodeth January 18, 2024 01:20
Copy link
Copy Markdown
Author

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to GitHub API limits, only the first 60 comments can be shown.

Comment thread backend/celery_worker.py

sentry_dsn = os.getenv("SENTRY_DSN")
if sentry_dsn:
if sentry_dsn := os.getenv("SENTRY_DSN"):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 23-24 refactored with the following changes:

Comment thread backend/celery_worker.py
Comment on lines -77 to +76
tmp_file_name = "tmp-file-" + file_name
tmp_file_name = f"tmp-file-{file_name}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function process_file_and_notify refactored with the following changes:

Comment thread backend/conftest.py
missing_vars = [var for var in required_vars if not os.getenv(var)]

if missing_vars:
if missing_vars := [var for var in required_vars if not os.getenv(var)]:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function verify_env_variables refactored with the following changes:

Comment thread backend/conftest.py
Comment on lines -39 to -44
API_KEY = os.getenv("CI_TEST_API_KEY")
if not API_KEY:
if API_KEY := os.getenv("CI_TEST_API_KEY"):
return API_KEY
else:
raise ValueError(
"CI_TEST_API_KEY environment variable not set. Cannot run tests."
)
return API_KEY
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function api_key refactored with the following changes:

Comment thread backend/main.py

sentry_dsn = os.getenv("SENTRY_DSN")
if sentry_dsn:
if sentry_dsn := os.getenv("SENTRY_DSN"):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 40-41 refactored with the following changes:

Comment on lines -12 to -13
headers = {}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_api_call_response_as_text refactored with the following changes:

sanitized_string = re.sub(r"[^a-zA-Z0-9_-]", "", string)

return sanitized_string
return re.sub(r"[^a-zA-Z0-9_-]", "", string)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function sanitize_function_name refactored with the following changes:

Comment on lines -22 to +23
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
to_encode["exp"] = expire
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_access_token refactored with the following changes:

Comment thread backend/models/files.py
return False

return True
return len(self.vectors_ids) != 0
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function File.file_already_exists refactored with the following changes:

This removes the following comments ( why? ):

# pyright: ignore reportPrivateUsage=none

Comment thread backend/models/files.py
Comment on lines -122 to +119
if len(response.data) == 0:
return False

return True
return len(response.data) != 0
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function File.file_already_exists_in_brain refactored with the following changes:

Comment on lines -132 to +138
if settings.ollama_api_base_url:
embeddings = OllamaEmbeddings(
return (
OllamaEmbeddings(
base_url=settings.ollama_api_base_url,
) # pyright: ignore reportPrivateUsage=none
else:
embeddings = OpenAIEmbeddings() # pyright: ignore reportPrivateUsage=none
return embeddings
)
if settings.ollama_api_base_url
else OpenAIEmbeddings()
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_embeddings refactored with the following changes:

Comment on lines -147 to -150
documents_vector_store = SupabaseVectorStore(
return SupabaseVectorStore(
supabase_client, embeddings, table_name="vectors"
)
return documents_vector_store
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_documents_vector_store refactored with the following changes:

request = self.supabase_db.get_user_usage(self.id)

return request
return self.supabase_db.get_user_usage(self.id)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UserUsage.get_user_usage refactored with the following changes:

Comment on lines -31 to +29
request = self.supabase_db.get_model_settings()

return request
return self.supabase_db.get_model_settings()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function UserUsage.get_model_settings refactored with the following changes:

Comment on lines -33 to -41
response = (
return (
self.db.table("brain_subscription_invitations")
.select("*")
.eq("brain_id", str(brain_id))
.eq("email", user_email)
.execute()
)

return response
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BrainSubscription.get_subscription_invitations_by_brain_id_and_email refactored with the following changes:

Comment on lines -66 to -72
response = (
return (
self.db.table("api_keys")
.select("user_id")
.filter("api_key", "eq", api_key)
.execute()
)
return response
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ApiKeys.get_user_id_by_api_key refactored with the following changes:

headers={
"Authorization": "Bearer " + api_key,
},
"/api-key", headers={"Authorization": f"Bearer {api_key}"}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_create_and_delete_api_key refactored with the following changes:

if response.data == []:
return None
return BrainEntity(**response.data[0])
return None if response.data == [] else BrainEntity(**response.data[0])
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Brains.get_brain_details refactored with the following changes:

Comment on lines -74 to -78
results = (
self.db.table("brains").delete().match({"brain_id": brain_id}).execute()
return (
self.db.table("brains")
.delete()
.match({"brain_id": brain_id})
.execute()
)

return results
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Brains.delete_brain refactored with the following changes:

Comment on lines -104 to +103
if len(response) == 0:
return None

return BrainEntity(**response[0])
return None if len(response) == 0 else BrainEntity(**response[0])
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Brains.get_brain_by_id refactored with the following changes:

Comment on lines -84 to -91
results = (
return (
self.db.table("brains_users")
.delete()
.match({"brain_id": brain_id})
.execute()
)

return results
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BrainsUsers.delete_brain_users refactored with the following changes:

Comment on lines -94 to +92
response = (
return (
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BrainsUsers.create_brain_user refactored with the following changes:

Comment on lines -118 to +115
if len(response) == 0:
return None
return UUID(response[0].get("brain_id"))
return None if len(response) == 0 else UUID(response[0].get("brain_id"))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BrainsUsers.get_user_default_brain_id refactored with the following changes:

Comment on lines -133 to -141
results = (
return (
self.db.table("brains_users")
.delete()
.match({"brain_id": str(brain_id)})
.match({"rights": "Viewer"})
.execute()
).data

return results
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BrainsUsers.delete_brain_subscribers refactored with the following changes:

return []

return vector_ids
return [] if not vector_ids else vector_ids
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BrainsVectors.get_brain_vector_ids refactored with the following changes:

Comment on lines -109 to +105
"/brains/",
headers={"Authorization": "Bearer " + api_key},
"/brains/", headers={"Authorization": f"Bearer {api_key}"}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_delete_all_brains refactored with the following changes:

Comment on lines -149 to +143
"/brains/",
headers={"Authorization": "Bearer " + api_key},
"/brains/", headers={"Authorization": f"Bearer {api_key}"}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_delete_all_brains_and_get_default_brain refactored with the following changes:

Comment on lines -187 to +179
headers={"Authorization": "Bearer " + api_key},
headers={"Authorization": f"Bearer {api_key}"},
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_set_as_default_brain_endpoint refactored with the following changes:

Comment on lines -241 to +233
headers={"Authorization": "Bearer " + api_key},
headers={"Authorization": f"Bearer {api_key}"},
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_public_brain_retrieve_and_then_delete refactored with the following changes:

if brain_id:
brain = brain_service.get_brain_by_id(brain_id)
if brain:
if brain := brain_service.get_brain_by_id(brain_id):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function create_question_handler refactored with the following changes:

@sweep-ai-deprecated
Copy link
Copy Markdown

Apply Sweep Rules to your PR?

  • Apply: All new business logic should have corresponding unit tests.
  • Apply: Refactor large functions to be more modular.
  • Apply: Add docstrings to all functions and file headers.

This is an automated message generated by Sweep AI.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants