Skip to content

Commit

Permalink
Python: updated pre-commits (#9739)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Put some of the pre-commit steps to their latest versions.
Improve the setup of the uv lock hook.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
  • Loading branch information
eavanvalkenburg and moonbox3 authored Nov 19, 2024
1 parent 8da553d commit 93bf06e
Show file tree
Hide file tree
Showing 17 changed files with 26 additions and 20 deletions.
8 changes: 5 additions & 3 deletions python/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ repos:
- id: pyupgrade
args: [--py310-plus]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.1
rev: v0.7.4
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- id: ruff-format
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
rev: 0.4.30
rev: 0.5.2
hooks:
# Update the uv lockfile
- id: uv-lock
files: python/pyproject.toml
args: [--project, python]
- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
rev: 1.7.10
hooks:
- id: bandit
args: ["-c", "python/pyproject.toml"]
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ dev-dependencies = [
"snoop ~= 0.4",
"mypy >= 1.10",
"types-PyYAML ~= 6.0.12.20240311",
"ruff ~= 0.5"
"ruff ~= 0.7"
]
environments = [
"sys_platform == 'darwin'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ async def main() -> None:

openai_spec = load_and_update_openai_spec()

http_client = httpx.AsyncClient()
http_client = httpx.AsyncClient(timeout=5)

await kernel.add_plugin_from_openai(
plugin_name="AzureKeyVaultPlugin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def create_client(self) -> httpx.AsyncClient:
"Authorization": f"Bearer {self.settings.token}",
"X-GitHub-Api-Version": "2022-11-28",
}
return httpx.AsyncClient(base_url=self.settings.base_url, headers=headers)
return httpx.AsyncClient(base_url=self.settings.base_url, headers=headers, timeout=5)

@staticmethod
def build_query(path: str, key: str, value: str) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async def make_request(client: httpx.AsyncClient):

if hasattr(self, "http_client") and self.http_client is not None:
return await make_request(self.http_client)
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(timeout=5) as client:
return await make_request(client)

return await fetch()
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async def _inner_search(self, query: str, options: TextSearchOptions) -> BingSea
"user_agent": SEMANTIC_KERNEL_USER_AGENT,
}
try:
async with AsyncClient() as client:
async with AsyncClient(timeout=5) as client:
response = await client.get(url, headers=headers, params=params)
response.raise_for_status()
return BingSearchResponse.model_validate_json(response.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async def _inner_search(self, query: str, options: TextSearchOptions) -> GoogleS
full_url = f"{CUSTOM_SEARCH_URL}{self._build_query(query, options)}"
headers = {"user_agent": SEMANTIC_KERNEL_USER_AGENT}
try:
async with AsyncClient() as client:
async with AsyncClient(timeout=5) as client:
response = await client.get(full_url, headers=headers)
response.raise_for_status()
return GoogleSearchResponse.model_validate_json(response.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def search(self, query: str, num_results: int = 1, offset: int = 0) -> lis
headers = {"Ocp-Apim-Subscription-Key": self._settings.api_key.get_secret_value()}

try:
async with AsyncClient() as client:
async with AsyncClient(timeout=5) as client:
response = await client.get(request_url, headers=headers)
response.raise_for_status()
data = response.json()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def search(self, query: str, num_results: int = 1, offset: int = 0) -> lis
logger.info("Sending GET request to Google Search API.")

try:
async with AsyncClient() as client:
async with AsyncClient(timeout=5) as client:
response = await client.get(request_url)
response.raise_for_status()
data = response.json()
Expand Down
2 changes: 1 addition & 1 deletion python/semantic_kernel/connectors/utils/document_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DocumentLoader:
async def from_uri(
url: str,
http_client: AsyncClient,
auth_callback: Callable[..., None | Awaitable[dict[str, str]]] | None,
auth_callback: Callable[..., Awaitable[dict[str, str]] | None] | None,
user_agent: str | None = HTTP_USER_AGENT,
):
"""Load the manifest from the given URL."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(
settings = SessionsPythonSettings()

if not http_client:
http_client = AsyncClient()
http_client = AsyncClient(timeout=5)

if auth_callback is None:
auth_callback = self._default_auth_callback(aca_settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ def __init__(
template_format: TEMPLATE_FORMAT_TYPES = KERNEL_TEMPLATE_FORMAT_NAME,
prompt_template: PromptTemplateBase | None = None,
prompt_template_config: PromptTemplateConfig | None = None,
prompt_execution_settings: None
| (PromptExecutionSettings | list[PromptExecutionSettings] | dict[str, PromptExecutionSettings]) = None,
prompt_execution_settings: PromptExecutionSettings
| list[PromptExecutionSettings]
| dict[str, PromptExecutionSettings]
| None = None,
) -> None:
"""Initializes a new instance of the KernelFunctionFromPrompt class.
Expand Down
4 changes: 3 additions & 1 deletion python/semantic_kernel/functions/kernel_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ async def from_openai(
openai_manifest = plugin_str
elif plugin_url is not None:
# Load plugin from the URL
http_client = execution_parameters.http_client if execution_parameters.http_client else httpx.AsyncClient()
http_client = (
execution_parameters.http_client if execution_parameters.http_client else httpx.AsyncClient(timeout=5)
)
openai_manifest = await DocumentLoader.from_uri(
url=plugin_url, http_client=http_client, auth_callback=None, user_agent=execution_parameters.user_agent
)
Expand Down
2 changes: 1 addition & 1 deletion python/semantic_kernel/kernel_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
T = TypeVar("T")

OneOrMany = Union[T, Sequence[T]]
OptionalOneOrMany = Union[None, T, Sequence[T]]
OptionalOneOrMany = Union[T, Sequence[T], None]

__all__ = ["AI_SERVICE_CLIENT_TYPE", "OneOrMany", "OptionalOneOrMany"]
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def check_input_variables(self):
@classmethod
def rewrite_execution_settings(
cls,
settings: None | (PromptExecutionSettings | list[PromptExecutionSettings] | dict[str, PromptExecutionSettings]),
settings: PromptExecutionSettings | list[PromptExecutionSettings] | dict[str, PromptExecutionSettings] | None,
) -> dict[str, PromptExecutionSettings]:
"""Rewrite execution settings to a dictionary."""
if not settings:
Expand Down
4 changes: 2 additions & 2 deletions python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93bf06e

Please sign in to comment.