Repository navigation
MAINT: Enable the ty unsound-assignment rule with staged overrides (UA-0) - #4407
Mohammad Saeed (mss06s) wants to merge 2 commits into
Conversation
…A-0) Remove `unsound-assignment = "ignore"` from [tool.ty.rules] so the rule runs at `all = "error"` like the others, and add two overrides: - a permanent one for pyrit/memory/memory_models.py, where every finding is the SQLAlchemy 2.0 `Mapped[T] = mapped_column(...)` / `relationship(...)` idiom (196 findings, one root cause); - a temporary one for the 47 files that still have findings, to be emptied by chunks UA-1 to UA-7. Since the issue was measured, the GCG refactor added findings in candidate_proposer.py, gcg_attack.py and default_implementations.py; they are in the temporary list with attack_manager.py, so UA-1 now covers 28 findings in 4 files. `ty check pyrit` passes, and every file not listed is checked by the rule. Part of microsoft#4402 (UA-0).
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
🟡 Changes recommended
Removing the global ignore enables unsound-assignment in unit tests, where existing Any-to-concrete assignments cause the full CI typing check to fail.
1 open issue
What changed in this PR
Implements UA-0 of #4402 by enabling ty’s unsound-assignment rule while deferring existing findings through scoped overrides. This is a configuration-only change.
Changes:
- Removes the global rule suppression.
- Adds a permanent exemption for SQLAlchemy declarations in
memory_models.py. - Adds a temporary exemption for 47 files pending UA-1 through UA-7, including three newly affected GCG files.
| File | Description |
|---|---|
pyproject.toml |
Enables the rule globally and adds permanent and staged file-level exemptions. |
Review effort: Balanced
make ty also checks tests/unit, where fixtures read through request.getfixturevalue(...) and other Any-typed helpers into annotated locals give 32 unsound-assignment findings. microsoft#4402 covers pyrit/ only, so add the rule to the existing tests/unit override. make ty passes.
|
Heads up: |
| "pyrit/backend/routes/targets.py", | ||
| "pyrit/backend/routes/version.py", | ||
| "pyrit/backend/services/attack_service.py", | ||
| "pyrit/backend/services/finding_export.py", |
There was a problem hiding this comment.
Required. #4134 added pyrit/backend/services/prompt_generation_service.py to main after this branch was cut, and it has one finding:
pyrit/backend/services/prompt_generation_service.py:850:35: error[unsound-assignment] Unsound assignment: `Any | None` is not a subtype of `PromptTarget | None`
With this PR applied to current main, that is the only thing ty check pyrit and make ty report (same on Python 3.11 and 3.14), so it needs to be in the list. UA-4 will take it out again.
| "pyrit/backend/services/finding_export.py", | |
| "pyrit/backend/services/finding_export.py", | |
| "pyrit/backend/services/prompt_generation_service.py", |
| # Tests read fixtures through `request.getfixturevalue(...)` and other Any-typed helpers into | ||
| # annotated locals (32 findings). #4402 covers pyrit/ only, so the rule stays off for tests. | ||
| unsound-assignment = "ignore" |
There was a problem hiding this comment.
This is one of the two conflict spots: #4412 appended missing-type-argument = "ignore" (with its own comment) at the same place. Keep both when you merge main.
Keeping the rule off for tests/unit is the right call: make ty checks tests/unit, and #4402 only measured pyrit/.
Optional: drop the (32 findings) count. It is 33 on current main and will keep drifting.
| # SQLAlchemy 2.0 declarative models are written as `col: Mapped[T] = mapped_column(...)` | ||
| # and `rel: Mapped[T] = relationship(...)`. Both calls are typed as returning Any-like | ||
| # values, so unsound-assignment flags every column and relationship (196 findings with | ||
| # one root cause). This is the documented idiom, not an unchecked boundary, and the | ||
| # migrations depend on this file, so the rule stays off here permanently. |
There was a problem hiding this comment.
Optional wording fix. On current main this file has 214 findings, not 196, and one of them is not the declarative idiom:
pyrit/memory/memory_models.py:2798:33: error[unsound-assignment] Unsound assignment: `Any` is not a subtype of `dict[str, str] | None`
That is display_group_map = json.loads(self.display_group_map_json). The override is file-wide, so it is exempt too. That follows from the approach #4402 chose and is not something to fix in this PR, but the comment should not say "one root cause" or carry a count that goes stale. The two calls also return MappedColumn[Any] and _RelationshipDeclared[Any], which is more precise than "Any-like".
| # SQLAlchemy 2.0 declarative models are written as `col: Mapped[T] = mapped_column(...)` | |
| # and `rel: Mapped[T] = relationship(...)`. Both calls are typed as returning Any-like | |
| # values, so unsound-assignment flags every column and relationship (196 findings with | |
| # one root cause). This is the documented idiom, not an unchecked boundary, and the | |
| # migrations depend on this file, so the rule stays off here permanently. | |
| # SQLAlchemy 2.0 declarative models are written as `col: Mapped[T] = mapped_column(...)` | |
| # and `rel: Mapped[T] = relationship(...)`. Those calls return `MappedColumn[Any]` and | |
| # `_RelationshipDeclared[Any]`, so unsound-assignment flags every column and relationship. | |
| # This is the documented idiom, not an unchecked boundary, and the migrations depend on | |
| # this file, so the rule stays off here permanently. The override is file-wide: other | |
| # assignments in this file are not checked by the rule either. |

Description
UA-0 for #4402: turns on ty's
unsound-assignmentrule so new code is checked from day one, with the remaining findings fenced off for UA-1 to UA-7. Configuration only, no source changes.unsound-assignment = "ignore"from[tool.ty.rules].[[tool.ty.overrides]]forpyrit/memory/memory_models.py(the SQLAlchemyMapped[T] = mapped_column(...)/relationship(...)idiom, 196 findings), with a comment explaining why.[[tool.ty.overrides]]for the files that still have findings, with a comment pointing to MAINT: Enable the tyunsound-assignmentrule in stages (267 findings) #4402.One difference from the issue:
mainhas moved sincefdeb9c5, and the GCG refactor added findings in three files that weren't on the list:gcg/attack/gcg/candidate_proposer.py(4),gcg/attack/gcg/gcg_attack.py(1) andgcg/default_implementations.py(6), andattack_manager.pynow has 17. I added the three files to the temporary override next toattack_manager.py, so UA-1 now covers 28 findings in 4 files (283 in total, not 267). It may be worth updating the issue's table.ty check pyritpasses, and a file outside the list is flagged by the rule.