Skip to content

MAINT: Enable the ty unsound-assignment rule with staged overrides (UA-0) - #4407

Open
Mohammad Saeed (mss06s) wants to merge 2 commits into
microsoft:mainfrom
mss06s:maint/ty-unsound-assignment-ua0
Open

Mohammad Saeed (mss06s) wants to merge 2 commits into
microsoft:mainfrom
mss06s:maint/ty-unsound-assignment-ua0

Conversation

@mss06s

Copy link
Copy Markdown

Description

UA-0 for #4402: turns on ty's unsound-assignment rule 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.

  • Removed unsound-assignment = "ignore" from [tool.ty.rules].
  • Added a permanent [[tool.ty.overrides]] for pyrit/memory/memory_models.py (the SQLAlchemy Mapped[T] = mapped_column(...) / relationship(...) idiom, 196 findings), with a comment explaining why.
  • Added a temporary [[tool.ty.overrides]] for the files that still have findings, with a comment pointing to MAINT: Enable the ty unsound-assignment rule in stages (267 findings) #4402.

One difference from the issue: main has moved since fdeb9c5, 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) and gcg/default_implementations.py (6), and attack_manager.py now has 17. I added the three files to the temporary override next to attack_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 pyrit passes, and a file outside the list is flagged by the rule.

…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).
Copilot AI balanced review requested due to automatic review settings November 18, 2026 22:35
@mss06s

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

Comment thread pyproject.toml Resolved
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.
@Steeve-Crypto

Copy link
Copy Markdown
Contributor

Heads up: pyrit/backend/services/prompt_generation_service.py (added in #4134) has an unsound-assignment finding that isn't in the temporary override list here, so make ty will likely fail on this branch after a rebase. You could add it to the override for now. My UA-4 PR for pyrit/backend fixes it at the source, so it can come out again once that lands.

Comment thread pyproject.toml
"pyrit/backend/routes/targets.py",
"pyrit/backend/routes/version.py",
"pyrit/backend/services/attack_service.py",
"pyrit/backend/services/finding_export.py",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Suggested change
"pyrit/backend/services/finding_export.py",
"pyrit/backend/services/finding_export.py",
"pyrit/backend/services/prompt_generation_service.py",

Comment thread pyproject.toml
Comment on lines +305 to +307
# 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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread pyproject.toml
Comment on lines +309 to +313
# 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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".

Suggested change
# 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.

This branch has not been deployed

No deployments
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.

4 participants