Skip to content
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

fix: onboard to use ruff #1029

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e45bd6b
infra: onboard to use ruff
Aug 28, 2024
531d724
Merge branch 'main' into ruff_test
AbeCoull Sep 3, 2024
f886b18
remove unused inputs
Sep 3, 2024
c45edd5
up007 ignore
Sep 3, 2024
8b3e5a7
Merge branch 'main' into ruff_test
AbeCoull Sep 4, 2024
d53188b
up007 ignore
Sep 4, 2024
5be9e80
fix typing issue
Sep 6, 2024
2a64ad4
fix
Sep 6, 2024
7e17365
fix dict typing
Sep 6, 2024
1c04013
fix
Sep 6, 2024
8ecf2d2
fix
Sep 6, 2024
376aba0
last one
Sep 6, 2024
f35947e
fix
Sep 6, 2024
a888b09
fix
Sep 6, 2024
83b3e20
typing issue
Sep 6, 2024
f3bef23
Merge branch 'main' into ruff_test
AbeCoull Sep 6, 2024
d3ab6e8
coverage fix
Sep 6, 2024
65cfa9d
ignore the list conversion for ascii diagrams
Sep 6, 2024
e4e0c9f
ignore the list conversion for ascii diagrams
Sep 6, 2024
913c0aa
add optional
Sep 10, 2024
324df53
enable more pydoclint
Oct 7, 2024
8e25245
Merge branch 'main' into ruff_test
AbeCoull Oct 7, 2024
89984d8
fix import from main
Oct 7, 2024
8dec2ce
fix typing
Oct 7, 2024
93d286d
Merge branch 'main' into ruff_test
AbeCoull Oct 16, 2024
ad56d22
Merge branch 'main' into ruff_test
AbeCoull Oct 18, 2024
6dea3bd
rebase main
Oct 18, 2024
911cdc9
correct datetime import
Oct 23, 2024
49bb44c
Merge branch 'main' into ruff_test
AbeCoull Oct 23, 2024
20aaeca
Merge branch 'main' into ruff_test
AbeCoull Nov 26, 2024
aaed73e
update files
Nov 25, 2024
b5c62fb
update for ruf039 and ann101, ann102
Nov 26, 2024
da699bf
Merge branch 'main' into ruff_test
AbeCoull Dec 17, 2024
6b1b9c3
rebase in main and update for new rules
Dec 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ exclude_lines =
# Avoid type checking import conditionals
if TYPE_CHECKING:



[html]
directory = build/coverage

Expand Down
85 changes: 84 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,85 @@
[tool.black]
[tool.ruff]
target-version = "py39"
line-length = 100
format.preview = true
format.docstring-code-line-length = 100
format.docstring-code-format = true
lint.select = [
"ALL",
]
lint.ignore = [
"A001", "A002", # This will be refactored out later
"ANN202", "ARG001", "ARG002", # TODO after onboarding
"ANN002", # Missing tpye annotation for `*args` in method
"ANN003", # Missing tpye annotation for `**kwargs` in method
"ANN204", # Special functions don't need return types
"ANN401", # Dynamically typed expressions (typing.Any) are disallowed in `arg`"
"B008", # Logger deinition rework
"B026", # Accepted pattern to unpack after keyword args
"BLE001", # This needs to be cleaned up later.
"COM812", # conflicts with formatter
"CPY", # No copyright header
"D", # ignore documentation for now
"D203", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
"D212", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
"DOC201", # no restructuredtext support yet
"DOC502", # Exceptions are documented in top level functions and no in the private methods
"DOC501", # broken with sphinx docs
"EM", # error string messages are fine
"ERA001", # This is used to capture math
"FA100", # Ignore until Python 3.10 is used
"FBT", # Booleans in methods are fine
"FIX", # Allow TODO statements for now.
"FURB101", # Same as PTH
"FURB103", # Same as PTH
"G004", # Let f strings in logger
"INP001", # no implicit namespaces here
"ISC001", # conflicts with formatter
"N802", # Function names are set now
"N803", # Allow uppercase
"N806", # Vars will break this rule based on technical names.
"N815", # Allowing reviwers to do a per case basis
"PLC2701", # Allow private function access in code
"PLR0913", # Too many variables in function
"PLR0914", # Too many local variables
"PLR0917", # Too many positional arguments
"PLR6301", # Ignore for dispath functions
"PLR2004", # Allow magic values
"PLW1510", # Subprocess implicitly uses this
"PLW0603", # Allow usage of global vars
"PLW1641", # Object with equal and not __hash__
"PTH", # Prefer builtin path
"PYI034", # Return class type hint here instead of self
"S104", # Possible binding to all interfaces
"S403", # Pickle is used
"S404", # Using subprocess is alright.
"S603", # Using subprocess is alright.
"S607", # Partial path
"SLF", # Private methods are allowed for access
"T201", # Print statements are used in some places. Later this should be moved inline.
"TCH", # This is good to look into later on. TODO
"TD", # Allow TODO Statements
"TRY003", # Longer messages are fine.
]
lint.per-file-ignores."tests/**/*.py" = [
"D", # don't care about documentation in tests
"FBT", # don"t care about booleans as positional arguments in tests
"INP001", # no implicit namespace
"PLR2004", # Magic value used in comparison, consider replacing with a constant variable
"S101", # asserts allowed in tests...
"S603", # `subprocess` call: check for execution of untrusted input
]
lint.isort = { known-first-party = [
"amazon-braket-sdk",
"tests",
] }
lint.preview = true

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint.flake8-annotations]
mypy-init-return = false

[tool.ruff.lint.flake8-builtins]
builtins-allowed-modules = ["operator"]
25 changes: 0 additions & 25 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,4 @@ filterwarnings=
# Ref: https://github.com/pytest-dev/pytest-cov/issues/557
ignore:The --rsyncdir command line argument and rsyncdirs config variable are deprecated.:DeprecationWarning

[isort]
line_length = 100
multi_line_output = 3
include_trailing_comma = true
profile = black

[flake8]
ignore =
# not pep8, black adds whitespace before ':'
E203,
# not pep8, https://www.python.org/dev/peps/pep-0008/#pet-peeves
E231,
# not pep8, black adds line break before binary operator
W503,
# Google Python style is not RST until after processed by Napoleon
# See https://github.com/peterjc/flake8-rst-docstrings/issues/17
RST201,RST203,RST301,
max_line_length = 100
max-complexity = 10
exclude =
__pycache__
.tox
.git
bin
build
venv
6 changes: 5 additions & 1 deletion src/braket/ahs/analog_hamiltonian_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from collections import defaultdict
from functools import singledispatch
from typing import TYPE_CHECKING

import braket.ir.ahs as ir
from braket.ahs.atom_arrangement import AtomArrangement, SiteType
Expand All @@ -25,6 +26,9 @@
from braket.device_schema import DeviceActionType
from braket.timings.time_series import TimeSeries

if TYPE_CHECKING:
from braket.aws import AwsDevice


class AnalogHamiltonianSimulation:
LOCAL_DETUNING_PROPERTY = "local_detuning"
Expand Down Expand Up @@ -124,7 +128,7 @@ def _hamiltonian_to_ir(self) -> ir.Hamiltonian:
localDetuning=terms[AnalogHamiltonianSimulation.LOCAL_DETUNING_PROPERTY],
)

def discretize(self, device: AwsDevice) -> AnalogHamiltonianSimulation: # noqa
def discretize(self, device: AwsDevice) -> AnalogHamiltonianSimulation:
"""Creates a new AnalogHamiltonianSimulation with all numerical values represented
as Decimal objects with fixed precision based on the capabilities of the device.

Expand Down
6 changes: 3 additions & 3 deletions src/braket/ahs/atom_arrangement.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from decimal import Decimal
from enum import Enum
from numbers import Number
from typing import Union

import numpy as np

Expand Down Expand Up @@ -63,7 +62,7 @@ def __init__(self):

def add(
self,
coordinate: Union[tuple[Number, Number], np.ndarray],
coordinate: tuple[Number, Number] | np.ndarray,
site_type: SiteType = SiteType.FILLED,
) -> AtomArrangement:
"""Add a coordinate to the atom arrangement.
Expand Down Expand Up @@ -124,6 +123,7 @@ def discretize(self, properties: DiscretizationProperties) -> AtomArrangement:
round(Decimal(c) / position_res) * position_res for c in site.coordinate
)
discretized_arrangement.add(new_coordinates, site.site_type)
return discretized_arrangement
except Exception as e:
raise DiscretizationError(f"Failed to discretize register {e}") from e
else:
return discretized_arrangement
12 changes: 4 additions & 8 deletions src/braket/ahs/driving_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

from __future__ import annotations

from typing import Union

from braket.ahs.discretization_types import DiscretizationProperties
from braket.ahs.field import Field
from braket.ahs.hamiltonian import Hamiltonian
Expand All @@ -24,9 +22,9 @@
class DrivingField(Hamiltonian):
def __init__(
self,
amplitude: Union[Field, TimeSeries],
phase: Union[Field, TimeSeries],
detuning: Union[Field, TimeSeries],
amplitude: Field | TimeSeries,
phase: Field | TimeSeries,
detuning: Field | TimeSeries,
) -> None:
r"""Creates a Hamiltonian term :math:`H_{drive}` for the driving field
that coherently transfers atoms from the ground state to the Rydberg state
Expand Down Expand Up @@ -181,6 +179,4 @@ def from_lists(
detuning.put(t, detuning_value)
phase.put(t, phase_value)

drive = DrivingField(amplitude=amplitude, detuning=detuning, phase=phase)

return drive
return DrivingField(amplitude=amplitude, detuning=detuning, phase=phase)
17 changes: 7 additions & 10 deletions src/braket/ahs/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class Field:
def __init__(self, time_series: TimeSeries, pattern: Optional[Pattern] = None) -> None:
def __init__(self, time_series: TimeSeries, pattern: Optional[Pattern] = None) -> None: # noqa: UP007
"""A space and time dependent parameter of a program.

Args:
Expand All @@ -37,15 +37,15 @@ def time_series(self) -> TimeSeries:
return self._time_series

@property
def pattern(self) -> Optional[Pattern]:
def pattern(self) -> Optional[Pattern]: # noqa: UP007
"""Optional[Pattern]: The local pattern of real numbers."""
return self._pattern

def discretize(
self,
time_resolution: Optional[Decimal] = None,
value_resolution: Optional[Decimal] = None,
pattern_resolution: Optional[Decimal] = None,
time_resolution: Optional[Decimal] = None, # noqa: UP007
value_resolution: Optional[Decimal] = None, # noqa: UP007
pattern_resolution: Optional[Decimal] = None, # noqa: UP007
) -> Field:
"""Creates a discretized version of the field,
where time, value and pattern are rounded to the
Expand All @@ -64,8 +64,7 @@ def discretize(
discretized_pattern = None
else:
discretized_pattern = self.pattern.discretize(pattern_resolution)
discretized_field = Field(time_series=discretized_time_series, pattern=discretized_pattern)
return discretized_field
return Field(time_series=discretized_time_series, pattern=discretized_pattern)

@staticmethod
def from_lists(times: list[Decimal], values: list[Decimal], pattern: list[Decimal]) -> Field:
Expand All @@ -90,6 +89,4 @@ def from_lists(times: list[Decimal], values: list[Decimal], pattern: list[Decima

time_series = TimeSeries.from_lists(times=times, values=values)

field = Field(time_series=time_series, pattern=Pattern(pattern))

return field
return Field(time_series=time_series, pattern=Pattern(pattern))
2 changes: 1 addition & 1 deletion src/braket/ahs/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


class Hamiltonian:
def __init__(self, terms: Optional[list[Hamiltonian]] = None):
def __init__(self, terms: Optional[list[Hamiltonian]] = None): # noqa: UP007
r"""A Hamiltonian representing a system to be simulated.

A Hamiltonian :math:`H` may be expressed as a sum of multiple terms
Expand Down
4 changes: 1 addition & 3 deletions src/braket/ahs/local_detuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def from_lists(times: list[float], values: list[float], pattern: list[float]) ->
magnitude = TimeSeries()
for t, v in zip(times, values):
magnitude.put(t, v)
shift = LocalDetuning(Field(magnitude, Pattern(pattern)))

return shift
return LocalDetuning(Field(magnitude, Pattern(pattern)))

def stitch(
self, other: LocalDetuning, boundary: StitchBoundaryCondition = StitchBoundaryCondition.MEAN
Expand Down
2 changes: 1 addition & 1 deletion src/braket/ahs/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def series(self) -> list[Number]:
"""
return self._series

def discretize(self, resolution: Optional[Decimal]) -> Pattern:
def discretize(self, resolution: Optional[Decimal]) -> Pattern: # noqa: UP007
"""Creates a discretized version of the pattern,
where each value is rounded to the closest multiple
of the resolution.
Expand Down
3 changes: 1 addition & 2 deletions src/braket/annealing/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def to_ir(self) -> Problem:
type=ir.ProblemType[self._problem_type.value],
linear=self._linear,
quadratic={
",".join((str(q1), str(q2))): self._quadratic[(q1, q2)]
for q1, q2 in self._quadratic
",".join((str(q1), str(q2))): self._quadratic[q1, q2] for q1, q2 in self._quadratic
},
)
Loading
Loading