Add type annotations and improve type checking throughout codebase#263
Merged
Conversation
- Fix test_concurrent_roundtrip flakiness: the broker protocol mandates strictly ordered receive counters (docs/protocol.md), so concurrent workers now retry out-of-order deliveries instead of racing frame() against unframe() and failing on scheduler interleavings. - Fix a portability bug in the conformance cgroup probe: read back threading.get_native_id() (what attach_current writes) instead of os.gettid(), which is missing on some libc builds and could also disagree with the written TID. - Resolve all 74 mypy errors across the tree: annotate SandboxThread runtime state, CryptoBroker counters, watchdog ring-buffer iterator, alert callbacks, and module-proxy helpers; align optional-dependency fallback stub signatures in pyisolate/__init__; rename shadowed loop variables in policy model/compiler; type the operator sandbox map. - Pre-commit: give mypy the PyYAML stubs, and run pytest via 'python -m pytest' so the hook uses the interpreter the package is installed into rather than whatever pytest is first on PATH. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DDSofWX5HwawsrwbN2nSXR
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.
This PR improves type safety and mypy compliance across the codebase by adding explicit type annotations, fixing type inconsistencies, and adding type ignore comments where necessary.
Key Changes
Type Annotations:
runtime/thread.py,policy/model.py,policy/__init__.py,__init__.py,observability/alerts.py,operator/__init__.py,supervisor.py,watchdog.py,broker/crypto.py, andbroker/uring.pySandboxThreadto document the class contract (_backend: str,_temp_dir: Path)_reset_runtime_state()and other initialization methodsType Consistency Fixes:
Decisiontype imports and parameter types in_deny()function to use the properDecisiontype instead of string literalsAnyinstead ofIterable[str]for address parameters to match actual socket API_module_proxy()fromtypes.ModuleTypetoAnyfor better type compatibility_require_clock()return type toClockCapability | Noneto reflect actual behavioron_violationcallback type to returnobjectinstead ofNoneVariable Naming Improvements:
ruleto more specific names (tcp_rule,net_rule,str_lst) to avoid shadowing and improve clarity inpolicy/model.pyandpolicy/__init__.pyType Ignore Comments:
# type: ignore[assignment]for socket.sendto guard with explanatory comment about signature collapsing# type: ignore[misc]for SandboxedPath class definition# type: ignore[assignment]for YAML module assignmentBug Fixes and Improvements:
_merge_allowed_imports()to checkallowed_imports is not Noneinstead of just the variable name_is_native_origin()to properly check for None before calling string methodswatchdog.pyto ensurenameis a string before using ittest_crypto.pyby adding retry logic with deadline for concurrent frame deliveryconformance.pyto usethreading.get_native_id()instead ofos.gettid()for better portability.pre-commit-config.yamlto addtypes-PyYAMLdependency and usepython -m pytestfor better hook reliabilityImport Improvements:
from collections.abc import Iterable, MappingandIteratorimports where neededTYPE_CHECKINGimports for forward references inoperator/__init__.pyandsupervisor.pycastimport insupervisor.pyfor type castingThese changes improve code maintainability, enable better IDE support, and catch potential type-related bugs earlier in development.
https://claude.ai/code/session_01DDSofWX5HwawsrwbN2nSXR