-
Notifications
You must be signed in to change notification settings - Fork 969
refactor: Add comprehensive type hints to backend modules #641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
refactor: Add comprehensive type hints to backend modules #641
Conversation
- Add type annotations to all functions in qiskit_backend.py - Add type annotations to all functions in cirq_backend.py - Add type annotations to all functions in amazon_braket_backend.py - Import typing.Any and numpy for proper type definitions - Use union types for flexible return types (e.g., LocalSimulator | AwsDevice) - Improve IDE support and enable static type checking This change is purely additive - no functionality is altered, only type information is added to improve code maintainability and developer experience. Closes #[issue-number]
guan404ming
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution, overall looks nice. Could you also add some tests for type check?
Yaa sure sir I had worked on it just pushing the changes. Thanks! for you suggestion. |
- Add mypy as dev dependency with strict configuration - Create test_type_checking.py with 6 comprehensive tests - Validates type hints in all backend modules - Ensures modern union syntax (int | None) works - Verifies no type errors are suppressed - Fix type checking issues in qiskit_backend.py and amazon_braket_backend.py - Add explicit type casts for library return values - Add dedicated type checking step to CI workflow - All 297 tests pass (291 existing + 6 new) - mypy validation: Success on all 5 source files
| "cirq>=1.5.0,<1.6.0", | ||
| "amazon-braket-sdk>=1.102.6,<2.0", | ||
| "sympy>=1.14.0,<2.0", | ||
| "flask (>=3.1.2,<4.0.0)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this?
| python_functions = "test_*" | ||
| addopts = ["-v", "--tb=short"] | ||
|
|
||
| [tool.mypy] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you help explain why for these changes, thanks!
ryankert01
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make sure it pass the pre commit test!
|
Sorry I'm late to the party - same comment as @guan404ming and @ryankert01 |
|
|
||
| def execute_circuit(circuit, backend, backend_config): | ||
| def execute_circuit( | ||
| circuit: qiskit.QuantumCircuit, backend: Any, backend_config: dict[str, Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| circuit: qiskit.QuantumCircuit, backend: Any, backend_config: dict[str, Any] | |
| circuit: qiskit.QuantumCircuit, backend: qiskit.providers.backend.Backend, backend_config: dict[str, Any] |
Not quite sure what exact type should be here, but better to avoid Any. WDYT?
| no_implicit_optional = true | ||
| warn_redundant_casts = true | ||
| warn_unused_ignores = true | ||
| strict_equality = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are here, maybe we can also add things like
warn_unreachable = true
strict_optional = trueto detect dead code and check if we miss Optional for args that can take None
Overview
This PR adds comprehensive type annotations to all three backend modules, improving IDE support and enabling static type checking.
closes #643
What's Changed
typing.Any,numpy)LocalSimulator | AwsDevice)Why This Change
The backend modules lacked type hints, making it harder for developers to:
This change improves developer experience without altering any functionality.
Benefits
Testing