Skip to content

Commit

Permalink
Python: fix mypy pre-commit setup, mypy fixes (#9498)
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.
-->
Fixes the mypy pre-commit job, and subsequent fixes from there.
Moved all python tests to use the uv setup task instead of manual cache,
python install and uv install.

### 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 😄
  • Loading branch information
eavanvalkenburg authored Nov 7, 2024
1 parent 801436c commit c9ef719
Show file tree
Hide file tree
Showing 75 changed files with 593 additions and 494 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/merge-gatekeeper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
timeout: 3600
interval: 30
ignored: "python-tests-coverage"
ignored: python-tests-coverage
11 changes: 7 additions & 4 deletions .github/workflows/python-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ jobs:
runs-on: ubuntu-latest
permissions:
contents: write
env:
UV_PYTHON: "3.10"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/install.sh | sh
uses: astral-sh/setup-uv@v3
with:
version: "0.4.30"
enable-cache: true
cache-suffix: ${{ runner.os }}-${{ matrix.python-version }}
- name: Check version
run: |
echo "Building and uploading Python package version: ${{ github.event.release.tag_name }}"
Expand Down
40 changes: 9 additions & 31 deletions .github/workflows/python-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,11 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up uv
if: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' }}
run: curl -LsSf https://astral.sh/uv/install.sh | sh
- name: Set up uv
if: ${{ matrix.os == 'windows-latest' }}
run: irm https://astral.sh/uv/install.ps1 | iex
shell: powershell
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Restore uv cache
id: cache
uses: actions/cache@v4
uses: astral-sh/setup-uv@v3
with:
path: ${{ env.UV_CACHE_DIR }}
key: uv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/uv.lock') }}
version: "0.4.30"
enable-cache: true
cache-suffix: ${{ runner.os }}-${{ matrix.python-version }}
- name: Install dependencies with hnswlib native disabled
if: matrix.os == 'macos-latest' && matrix.python-version == '3.11'
run: |
Expand Down Expand Up @@ -267,22 +256,11 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up uv
if: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' }}
run: curl -LsSf https://astral.sh/uv/install.sh | sh
- name: Set up uv
if: ${{ matrix.os == 'windows-latest' }}
run: irm https://astral.sh/uv/install.ps1 | iex
shell: powershell
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: astral-sh/setup-uv@v3
with:
python-version: ${{ matrix.python-version }}
- name: Restore uv cache
id: cache
uses: actions/cache@v4
with:
path: ${{ env.UV_CACHE_DIR }}
key: uv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/uv.lock') }}
version: "0.4.30"
enable-cache: true
cache-suffix: ${{ runner.os }}-${{ matrix.python-version }}
- name: Install dependencies with hnswlib native disabled
if: matrix.os == 'macos-latest' && matrix.python-version == '3.11'
run: |
Expand Down Expand Up @@ -438,4 +416,4 @@ jobs:
dry_run: ${{ env.run_type != 'Daily' && env.run_type != 'Manual'}}
job: ${{ toJson(job) }}
steps: ${{ toJson(steps) }}
overwrite: "{title: ` ${{ env.run_type }}: ${{ env.date }} `, text: ` ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}`}"
overwrite: "{title: ` ${{ env.run_type }}: ${{ env.date }} `, text: ` ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}`}"
16 changes: 6 additions & 10 deletions .github/workflows/python-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ jobs:
env:
# Configure a constant location for the uv cache
UV_CACHE_DIR: /tmp/.uv-cache
UV_PYTHON: ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v4
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/install.sh | sh
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: astral-sh/setup-uv@v3
with:
python-version: ${{ matrix.python-version }}
- name: Restore uv cache
uses: actions/cache@v4
with:
path: ${{ env.UV_CACHE_DIR }}
key: uv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/uv.lock') }}
version: "0.4.30"
enable-cache: true
cache-suffix: ${{ runner.os }}-${{ matrix.python-version }}
- name: Install the project
run: uv sync --all-extras --dev
- uses: pre-commit/action@v3.0.1
with:
extra_args: --config python/.pre-commit-config.yaml
extra_args: --config python/.pre-commit-config.yaml --all-files
- name: Minimize uv cache
run: uv cache prune --ci
60 changes: 28 additions & 32 deletions .github/workflows/python-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@ jobs:
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
experimental: [false]
include:
- python-version: "3.13.0-beta.4"
os: "ubuntu-latest"
experimental: true
# include:
# - python-version: "3.13"
# os: "ubuntu-latest"
# experimental: true
# - python-version: "3.13t"
# os: "ubuntu-latest"
# experimental: true
# gil: 0
# - python-version: "3.13t"
# os: "ubuntu-latest"
# experimental: true
# gil: 1
env:
UV_PYTHON: ${{ matrix.python-version }}
permissions:
contents: write
defaults:
Expand All @@ -32,28 +42,19 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up uv
if: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' }}
run: curl -LsSf https://astral.sh/uv/install.sh | sh
- name: Set up uv
if: ${{ matrix.os == 'windows-latest' }}
run: irm https://astral.sh/uv/install.ps1 | iex
shell: powershell
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Restore uv cache
id: cache
uses: actions/cache@v4
uses: astral-sh/setup-uv@v3
with:
path: ${{ env.UV_CACHE_DIR }}
key: uv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/uv.lock') }}
version: "0.4.30"
enable-cache: true
cache-suffix: ${{ runner.os }}-${{ matrix.python-version }}
- name: Install the project
run: uv sync --all-extras --dev
- name: Test with pytest
run: uv run pytest --junitxml=pytest.xml ./tests/unit
env:
PYTHON_GIL: ${{ matrix.gil }}
run: uv run --frozen pytest --junitxml=pytest.xml ./tests/unit
- name: Surface failing tests
if: always()
if: ${{ !matrix.experimental }}
uses: pmeier/pytest-results-action@main
with:
path: python/pytest.xml
Expand All @@ -73,26 +74,21 @@ jobs:
run:
working-directory: python
env:
PYTHON_VERSION: "3.10"
UV_PYTHON: "3.10"
steps:
- uses: actions/checkout@v4
- name: Setup filename variables
run: echo "FILE_ID=${{ github.event.number }}" >> $GITHUB_ENV
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/install.sh | sh
- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Restore uv cache
uses: actions/cache@v4
uses: astral-sh/setup-uv@v3
with:
path: ${{ env.UV_CACHE_DIR }}
key: uv-${{ runner.os }}-${{ env.PYTHON_VERSION }}-${{ hashFiles('**/uv.lock') }}
version: "0.4.30"
enable-cache: true
cache-suffix: ${{ runner.os }}-${{ env.UV_PYTHON }}
- name: Install the project
run: uv sync --all-extras --dev
- name: Test with pytest
run: uv run pytest -q --junitxml=pytest.xml --cov=semantic_kernel --cov-report=term-missing:skip-covered ./tests/unit | tee python-coverage.txt
run: uv run --frozen pytest -q --junitxml=pytest.xml --cov=semantic_kernel --cov-report=term-missing:skip-covered ./tests/unit | tee python-coverage.txt
- name: Upload coverage
if: always()
uses: actions/upload-artifact@v4
Expand Down
12 changes: 6 additions & 6 deletions python/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@ repos:
- id: pyupgrade
args: [--py310-plus]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.1
rev: v0.7.1
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.3.3
rev: 0.4.29
hooks:
# Update the uv lockfile
- id: uv-lock
files: ^\./python/(uv\.lock|pyproject\.toml)$
- repo: local
hooks:
- id: mypy
files: ^python/semantic_kernel/
name: mypy
entry: cd python && uv run mypy -p semantic_kernel --config-file mypy.ini
entry: bash -c 'cd python && uv run mypy -p semantic_kernel --config-file mypy.ini'
language: system
types: [python]
pass_filenames: false
pass_filenames: true
- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
hooks:
- id: bandit
args: ["-c", "python/pyproject.toml"]
additional_dependencies: [ "bandit[toml]" ]
additional_dependencies: [ "bandit[toml]" ]

3 changes: 3 additions & 0 deletions python/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ warn_untyped_fields = true
[mypy-semantic_kernel]
no_implicit_reexport = true

[mypy-semantic_kernel.data.*]
disable_error_code = method-assign

[mypy-semantic_kernel.memory.*]
ignore_errors = true
# TODO (eavanvalkenburg): remove this
Expand Down
7 changes: 5 additions & 2 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = [{ name = "Microsoft", email = "SK-Support@microsoft.com"}]
readme = "pip/README.md"
# Version read from __version__ field in __init__.py by Flit
dynamic = ["version"]
requires-python = ">=3.10,<3.13"
requires-python = ">=3.10"
license = {file = "LICENSE"}
urls.homepage = "https://learn.microsoft.com/en-us/semantic-kernel/overview/"
urls.source = "https://github.com/microsoft/semantic-kernel/tree/main/python"
Expand Down Expand Up @@ -50,7 +50,7 @@ dependencies = [
### Optional dependencies
[project.optional-dependencies]
azure = [
"azure-ai-inference >= 1.0.0b3",
"azure-ai-inference >= 1.0.0b4",
"azure-search-documents >= 11.6.0b4",
"azure-identity ~= 1.13",
"azure-cosmos ~= 4.7"
Expand Down Expand Up @@ -141,6 +141,9 @@ environments = [
[tool.pytest.ini_options]
addopts = "-ra -q -r fEX"
asyncio_default_fixture_loop_scope = "function"
filterwarnings = [
'ignore:.*FunctionChoiceBehavior.*:DeprecationWarning'
]

[tool.ruff]
line-length = 120
Expand Down
36 changes: 16 additions & 20 deletions python/samples/concepts/auto_function_calling/nexus_raven.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,17 @@ async def get_chat_message_contents(
try:
function_call, function_result = await self._execute_function_calls(part, chat_history, **kwargs)
if function_call:
messages.extend(
[
ChatMessageContent(
role="assistant", items=[function_call], metadata={"ai_model_id": self.ai_model_id}
),
ChatMessageContent(
role="tool",
items=[function_result],
name="nexus",
metadata={"ai_model_id": self.ai_model_id},
),
]
)
messages.extend([
ChatMessageContent(
role="assistant", items=[function_call], metadata={"ai_model_id": self.ai_model_id}
),
ChatMessageContent(
role="tool",
items=[function_result],
name="nexus",
metadata={"ai_model_id": self.ai_model_id},
),
])
else:
messages.append(ChatMessageContent(role="assistant", content=part, ai_model_id=self.ai_model_id))
except Exception as e:
Expand Down Expand Up @@ -175,13 +173,11 @@ async def _execute_function_calls(
}
call_stack[current_idx] = call
while any(call["result"] is None for call in call_stack.values()):
await asyncio.gather(
*[
self._execute_function_call(call, chat_history, kwargs.get("kernel"))
for call in call_stack.values()
if not any(isinstance(arg, tuple) for arg in call["args"].values()) and call["result"] is None
]
)
await asyncio.gather(*[
self._execute_function_call(call, chat_history, kwargs.get("kernel"))
for call in call_stack.values()
if not any(isinstance(arg, tuple) for arg in call["args"].values()) and call["result"] is None
])
for call in call_stack.values():
if call["result"] is None:
for name, arg in call["args"].items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def input_output_filter(
await next(context)

if context.result:
logger.info(f'Usage: {context.result.metadata.get("usage")}')
logger.info(f"Usage: {context.result.metadata.get('usage')}")
context.arguments["chat_history"].add_message(context.result.value[0])
print(f"Mosscap:> {context.result!s}")

Expand Down
Loading

0 comments on commit c9ef719

Please sign in to comment.