REFACTOR: Promote single-class module constants to ClassVar (backend + registry + setup) - #1964
Merged
romanlutz merged 2 commits intoJun 10, 2026
Conversation
…+ 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>
This was referenced Jun 9, 2026
rlundeen2
approved these changes
Jun 9, 2026
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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Promotes module-level constants that are only consumed by a single class to
ClassVarattributes on that class, perstyle-guide.instructions.mdlines 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_PATHS→EntraAuthMiddleware._PUBLIC_PATHS: ClassVar[set[str]]pyrit/backend/middleware/security_headers.py—_DOCS_PATHS,_API_CSP,_FRONTEND_CSP→SecurityHeadersMiddlewareClassVarspyrit/backend/services/converter_service.py—_DATA_TYPE_EXTENSION→ConverterService._DATA_TYPE_EXTENSIONpyrit/backend/services/target_service.py—_AZURE_ML_SCOPE→TargetService._AZURE_ML_SCOPE(referenced viaTargetService._AZURE_ML_SCOPEbecause the call site is@staticmethod)pyrit/registry/tag_query.py—_VALID_OPS,_OP_FUNC→TagQueryClassVars (ClassVaris required here becauseTagQueryis@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_MAP→ConfigurationLoader._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_REGISTRYand_CONVERTER_CLASS_REGISTRYare 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— cleanuv run --link-mode=copy ty check— cleanuv run --link-mode=copy pytest tests/unit/backend/ tests/unit/registry/ tests/unit/setup/— 1160 passed, 5 skipped