Merged
Conversation
|
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.



Enable ruff rule RUF012: Mutable class attributes should be annotated with
typing.ClassVarSummary
Enable the
RUF012lint rule, which requires mutable class-level attributes to be explicitly annotatedwith
ClassVar. This was previously ignored inruff.toml. All 55 violations across 8 files are resolved.Motivation
ClassVaris a type-checking annotation that explicitly marks attributes as belonging to the class ratherthan instances. Without it, type checkers may treat mutable class attributes as instance attributes, and
the shared-state semantics are not visible to readers or tooling. This change documents existing intent
without altering runtime behavior.
Alternatives considered
__init__(per-instance): Wrong for caches that intentionally share state acrossinstances, and unnecessary overhead for class-level configuration constants that are never mutated.
tupleinstead oflist): Good semantic fit for the configuration lists(
whitelist_set_attrs,add_keys, etc.) since they're never mutated, but doesn't work for cacheswhich must be mutable dicts. Would be a larger refactor for limited additional safety.
ClassVar[Final[...]](Python 3.13 supports nesting): Doesn't fit — the configuration lists areoverridden by subclasses (which
Finalprohibits), and the caches are reassigned at runtime.ClassVaris the pragmatic choice: it matches what the code already does, adds type-checking clarity,and requires minimal changes.
Changes by category
1. Cache singletons —
ClassVarannotation (3 files)Shared mutable dicts that cache database connections or Celery task resources.
ClassVarcorrectlydocuments the intentional class-level sharing.
AsyncClientCache._clientsinuserdb/db/async_db.pyMongoClientCache._clientsinuserdb/db/sync_db.pyMessageSender._cache_storeinworkers/msg/tasks.py2.
AttributeFetcherconfiguration lists (2 files, 34 violations)The base class and 17 subclasses define
whitelist_set_attrs/whitelist_unset_attrsas class-levelconfiguration. These are read at runtime but never mutated. Updated the base class in
ams/common.pyand all subclasses in
ams/__init__.py.3.
GenericFilterDictfilter lists (1 file, 14 violations)The base class and ~10 subclasses define
add_keys/remove_keysto configure dict filteringbehavior. Updated base class and all subclasses in
userdb/support/models.py.4. Test mock data (1 file, 2 violations)
MockedScimAPIMixin.get_invite_responseandpost_user_responseare class-level test fixture dicts.Annotated with
ClassVar[dict[str, Any]]incommon/clients/scim_client/testing.py.5. Pydantic false positive (1 file)
LadokConfig.dev_fake_users_in: list[str] = []is a Pydantic field, but ruff can't detect theBaseModelancestry through 3 levels of inheritance. AddingClassVarwould break Pydanticserialization. Fixed by using
Field(default_factory=list)— the idiomatic Pydantic pattern formutable defaults — which doesn't trigger the rule.
6. Dead code removal (1 file)
Removed
CacheMDB._init_collections: set[str]inworkers/msg/cache.py. This was an artifact froma 2021 refactor (commit
0c441c4a9) that modernized index management to useBaseDB.setup_indexes().The old
ensure_indices()method was removed but the tracking attribute was left behind. It had zeroreferences in the codebase.