Skip to content

REFACTOR: Promote single-class module constants to ClassVar (backend + registry + setup) - #1964

Merged
romanlutz merged 2 commits into
microsoft:mainfrom
romanlutz:romanlutz/audit-constants-backend-registry-setup
Jun 10, 2026
Merged

REFACTOR: Promote single-class module constants to ClassVar (backend + registry + setup)#1964
romanlutz merged 2 commits into
microsoft:mainfrom
romanlutz:romanlutz/audit-constants-backend-registry-setup

Conversation

@romanlutz

Copy link
Copy Markdown
Contributor

Promotes module-level constants that are only consumed by a single class to ClassVar attributes on that class, per style-guide.instructions.md lines 222-237 ("Define constants as class attributes, not module-level. Use UPPER_CASE naming.").

This is Part A, PR 1 of a planned series cleaning up constant placement across PyRIT. See the audit notes for the full scope.

Files touched

  • pyrit/backend/middleware/auth.py_PUBLIC_PATHSEntraAuthMiddleware._PUBLIC_PATHS: ClassVar[set[str]]
  • pyrit/backend/middleware/security_headers.py_DOCS_PATHS, _API_CSP, _FRONTEND_CSPSecurityHeadersMiddleware ClassVars
  • pyrit/backend/services/converter_service.py_DATA_TYPE_EXTENSIONConverterService._DATA_TYPE_EXTENSION
  • pyrit/backend/services/target_service.py_AZURE_ML_SCOPETargetService._AZURE_ML_SCOPE (referenced via TargetService._AZURE_ML_SCOPE because the call site is @staticmethod)
  • pyrit/registry/tag_query.py_VALID_OPS, _OP_FUNCTagQuery ClassVars (ClassVar is required here because TagQuery is @dataclass(frozen=True); without the annotation, the dict/set values would be misinterpreted as default dataclass fields).
  • pyrit/setup/configuration_loader.py_MEMORY_DB_TYPE_MAPConfigurationLoader._MEMORY_DB_TYPE_MAP: ClassVar[dict[str, str]] (also a @dataclass).

Behavior

No value changes. No behaviour changes. This is a pure relocation of where a constant lives — same value, same usage, just moved onto the class that uses it.

Out of scope (deferred)

_TARGET_CLASS_REGISTRY and _CONVERTER_CLASS_REGISTRY are explicitly left at module scope: they're populated by side-effect at import time via decorators, and moving them onto a class would change evaluation timing. They'll be revisited as a separate cleanup.

Verified

  • uv run --link-mode=copy ruff check — clean
  • uv run --link-mode=copy ty check — clean
  • uv run --link-mode=copy pytest tests/unit/backend/ tests/unit/registry/ tests/unit/setup/ — 1160 passed, 5 skipped

…+ registry + setup)

Promotes module-level constants that are only consumed by a single class
in the same file to ClassVar attributes on that class, per the style guide
(.github/instructions/style-guide.instructions.md lines 222-237: "Define
constants as class attributes, not module-level").

Files touched (Part A "bucket-(a)" candidates from the constants audit):

- pyrit/backend/middleware/auth.py
  _PUBLIC_PATHS -> EntraAuthMiddleware._PUBLIC_PATHS
- pyrit/backend/middleware/security_headers.py
  _DOCS_PATHS, _API_CSP, _FRONTEND_CSP -> SecurityHeadersMiddleware
- pyrit/backend/services/converter_service.py
  _DATA_TYPE_EXTENSION -> ConverterService._DATA_TYPE_EXTENSION
- pyrit/backend/services/target_service.py
  _AZURE_ML_SCOPE -> TargetService._AZURE_ML_SCOPE
- pyrit/registry/tag_query.py
  _VALID_OPS, _OP_FUNC -> TagQuery (ClassVar required: frozen dataclass)
- pyrit/setup/configuration_loader.py
  _MEMORY_DB_TYPE_MAP -> ConfigurationLoader (ClassVar required: dataclass)

The import-time _TARGET_CLASS_REGISTRY and _CONVERTER_CLASS_REGISTRY are
deferred to a separate audit pass because moving them inside a class body
changes import-time evaluation semantics.

No value changes. No behaviour changes. All references updated to use
self._FOO (or TargetService._FOO for the @staticmethod call site).

Verified: ruff check passes; ty check passes; tests/unit/{backend,registry,setup}
1064 passed, 4 skipped.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The diff_cover check on PR 1964 required >=90% coverage for changed lines.
Added tests that exercise the dispatch branches in SecurityHeadersMiddleware
and the data-URI / raw-base64 fallback branches in
ConverterService.preview_conversion_async that now reference
self._DATA_TYPE_EXTENSION.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@romanlutz
romanlutz added this pull request to the merge queue Jun 10, 2026
Merged via the queue into microsoft:main with commit 16f18aa Jun 10, 2026
53 checks passed
@romanlutz
romanlutz deleted the romanlutz/audit-constants-backend-registry-setup branch June 10, 2026 04:35
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.

3 participants