-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: split MessageDict by role for mypy narrowing #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
53379ee
refactor: split MessageDict by role for mypy narrowing
clean6378-max-it 32e8019
test: require typeddict-item on both invalid MessageDict fixture lines
clean6378-max-it 94f89de
test: harden MessageDict mypy fixtures and restore search parity
clean6378-max-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """Negative fixture: role-inappropriate MessageDict access must fail mypy strict.""" | ||
|
|
||
| from models.session import MessageDict, UserMessageDict | ||
|
|
||
| user_msg: UserMessageDict = {"role": "user", "text": "hello"} | ||
| _ = user_msg["thinking"] | ||
|
|
||
| union_msg: MessageDict = {"role": "user", "text": "hello"} | ||
| _ = union_msg["thinking"] | ||
|
clean6378-max-it marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """Positive fixture: discriminant narrowing allows role-specific fields.""" | ||
|
|
||
| from models.session import MessageDict, UserMessageDict | ||
|
|
||
| messages: list[MessageDict] = [{"role": "user", "text": "hello"}] | ||
| for msg in messages: | ||
| if msg["role"] == "user": | ||
| narrowed: UserMessageDict = msg | ||
| _ = narrowed.get("slug") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Type-level regression: MessageDict union rejects role-inappropriate access.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parent.parent | ||
| MYPY_TYPES_DIR = REPO_ROOT / "tests" / "mypy_types" | ||
|
|
||
| _INVALID_FIXTURE = "message_dict_invalid.py" | ||
| _INVALID_ERROR_LINES = (6, 9) | ||
|
|
||
|
|
||
| def _run_mypy_on(path: Path) -> subprocess.CompletedProcess[str]: | ||
| return subprocess.run( | ||
| [ | ||
| sys.executable, | ||
| "-m", | ||
| "mypy", | ||
| "--strict", | ||
| str(path), | ||
| "--config-file", | ||
| str(REPO_ROOT / "pyproject.toml"), | ||
| ], | ||
| cwd=REPO_ROOT, | ||
| capture_output=True, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
|
|
||
|
|
||
| def _typeddict_item_error_lines(output: str, fixture_name: str) -> set[int]: | ||
| pattern = rf"{re.escape(fixture_name)}:(\d+): error:.*\[typeddict-item\]" | ||
| return {int(match.group(1)) for match in re.finditer(pattern, output)} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("fixture_name", "should_pass"), | ||
| [ | ||
| (_INVALID_FIXTURE, False), | ||
| ("message_dict_valid.py", True), | ||
| ], | ||
| ) | ||
| def test_message_dict_mypy_fixtures(fixture_name: str, should_pass: bool) -> None: | ||
| result = _run_mypy_on(MYPY_TYPES_DIR / fixture_name) | ||
| output = result.stdout + result.stderr | ||
| if should_pass: | ||
| assert result.returncode == 0, output | ||
| else: | ||
| assert result.returncode != 0 | ||
| error_lines = _typeddict_item_error_lines(output, fixture_name) | ||
| assert error_lines >= set(_INVALID_ERROR_LINES), ( | ||
| f"expected typeddict-item on lines {_INVALID_ERROR_LINES}, " | ||
| f"got {sorted(error_lines)}: {output}" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.