Skip to content

Commit

Permalink
feat(ruff): enable flake8-pyi, flake8-pytest-style, flake8-quotes, fl…
Browse files Browse the repository at this point in the history
…ake8-return, flake8-raise, flake8-self, flake8-slots (#22654)
  • Loading branch information
hongbo-miao authored Jan 12, 2025
1 parent 6b5752e commit 787635a
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 50 deletions.
13 changes: 10 additions & 3 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,14 @@ select = [
"C4", # flake8-comprehensions
"C90", # mccabe
"COM", # flake8-commas
"CPY", # flake8-copyright
"D", # pydocstyle
"DTZ", # flake8-datetimez
"E", # pycodestyle
"EM", # flake8-errmsg
"EXE", # flake8-executable
"F", # Pyflakes
"FBT", # flake8-boolean-trap
"FAST", # FastAPI
"FBT", # flake8-boolean-trap
"FLY", # flynt
"FURB", # Refurb
"G", # flake8-logging-format
Expand All @@ -201,17 +200,25 @@ select = [
"N", # pep8-naming
"NPY", # NumPy
"PERF", # Perflint
"PIE", # flake8-pie
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # Pylint
"PYI", # flake8-pyi
"PT", # flake8-pytest-style
"Q", # flake8-quotes
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # Ruff
"S", # flake8-bandit
"SLF", # flake8-self
"SLOT", # flake8-slots
"T10", # flake8-debugger
"T20", # flake8-print
"TRY", # tryceratops
"UP", # pyupgrade
"W", # pycodestyle
"YTT", # flake8-2020
# "CPY", # flake8-copyright
# "DJ", # flake8-django
# "FA", # flake8-future-annotations
# "INP", # flake8-no-pep420
Expand Down
20 changes: 9 additions & 11 deletions authorization/hm-opal-client/opal_fetcher_postgres/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ async def _fetch_(self) -> list[asyncpg.Record]:
return []
row = await self._connection.fetchrow(self._event.config.query)
return [row]
else:
if self._connection is None:
return []
return await self._connection.fetch(self._event.config.query)
if self._connection is None:
return []
return await self._connection.fetch(self._event.config.query)

async def _process_(
self,
Expand All @@ -123,10 +122,9 @@ async def _process_(
if records and len(records) > 0:
return dict(records[0])
return {}
else:
if self._event.config.dict_key is None:
return [dict(record) for record in records]
return {
dict(record)[self._event.config.dict_key]: dict(record)
for record in records
}
if self._event.config.dict_key is None:
return [dict(record) for record in records]
return {
dict(record)[self._event.config.dict_key]: dict(record)
for record in records
}
8 changes: 3 additions & 5 deletions computer-vision/hm-imagebind/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ def download_file(client: httpx.Client, url: str, is_audio: bool) -> Path:
file.write(response.content)
logger.info(f"Downloaded file: {local_file_path}")
return local_file_path
else:
msg = f"Download failed: {response}"
raise RuntimeError(msg)
msg = f"Download failed: {response}"
raise RuntimeError(msg)

@staticmethod
def download_all_files() -> tuple[list[Path], list[Path]]:
Expand Down Expand Up @@ -156,11 +155,10 @@ def create_gradio_interface(
examples=audio_paths,
flagging_mode="never",
)
gradio_interface = gr.TabbedInterface(
return gr.TabbedInterface(
[image_to_text_audio, text_to_image_audio, audio_to_image_text],
["Image to Text/Audio", "Text to Image/Audio", "Audio to Image/Text"],
)
return gradio_interface


def main() -> None:
Expand Down
3 changes: 1 addition & 2 deletions data-orchestration/hm-airflow/dags/s3_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
@task
def download_from_s3(key: str, bucket: str, local_path: str) -> str:
hook = S3Hook("s3_connection")
file_path = hook.download_file(
return hook.download_file(
key=key,
bucket_name=bucket,
local_path=local_path,
)
return file_path

@task
def rename_file(file_path: str, new_file_name: str) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires-python = "~=3.13.0"
dependencies = [
"pandas==2.2.3",
"pyarrow==18.1.0",
"pywin32==308",
"pywin32==308; sys_platform=='win32'",
]

[tool.uv]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from zoneinfo import ZoneInfo

import pandas as pd
import pythoncom
import win32com.client

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,15 +77,15 @@ def get_irig_times(iads_metadata_path: Path) -> tuple[str, str, int]:
):
msg = "Invalid time format in archive info file"
raise ValueError(msg)
elif irig_start_time > max_irig_time:
if irig_start_time > max_irig_time:
msg = (
f"Start time '{irig_start_time}' cannot be bigger than {max_irig_time}"
)
raise ValueError(msg)
elif irig_end_time == "000:00:00:00.000" or irig_end_time > max_irig_time:
if irig_end_time == "000:00:00:00.000" or irig_end_time > max_irig_time:
msg = f"End time '{irig_end_time}' cannot be 000:00:00:00.000 or bigger than {max_irig_time}"
raise ValueError(msg)
elif irig_end_time <= irig_start_time:
if irig_end_time <= irig_start_time:
msg = f"End time '{irig_end_time}' must be greater than start time '{irig_start_time}'"
raise ValueError(msg)

Expand Down Expand Up @@ -129,9 +127,8 @@ def export_to_parquet(
result = subprocess.run(cmd, capture_output=True, text=True, check=False) # noqa: S603
if result.returncode == 0:
return parquet_file_path
else:
msg = f"Error output: {result}"
raise ValueError(msg)
msg = f"Error output: {result}"
raise ValueError(msg)

@staticmethod
def convert_irig_to_unix_time_ns(
Expand All @@ -152,6 +149,9 @@ def get_iads_dataframe(
iads_data_path: Path,
timezone: str,
) -> pd.DataFrame:
import pythoncom
import win32com.client

iads_config_path = iads_data_path / Path(IadsUtil.IADS_CONFIG_FILE_NAME)
iads_metadata_path = iads_data_path / Path(IadsUtil.IADS_METADATA_FILE_NAME)
iads_config: Any | None = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def test_get_irig_times_invalid_date_format(self, tmp_path: Path) -> None:
FlightDate = 2025-01-01
"""
test_file.write_text(test_content)
with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match="time data '2025-01-01' does not match format '%m/%d/%y'",
):
IadsUtil.get_irig_times(test_file)

def test_get_irig_times_invalid_time_range(self, tmp_path: Path) -> None:
Expand Down Expand Up @@ -113,7 +116,7 @@ def test_get_irig_times_invalid_end_time_too_big(self, tmp_path: Path) -> None:
IadsUtil.get_irig_times(test_file)

@pytest.mark.parametrize(
"start_time,end_time",
("start_time", "end_time"),
[
("---:--:--:--.---", "---:--:--:--.---"),
("abc:12:34:56.789", "def:12:34:56.789"),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
return self.fc3(x)
3 changes: 1 addition & 2 deletions machine-learning/graph-neural-network/src/model/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ def __init__(self, emb_dim: int) -> None:

def forward(self, x: Tensor, edge_index: Tensor, edge_attr: Tensor) -> Tensor:
edge_embedding = self.bond_encoder(edge_attr)
out = self.mlp(
return self.mlp(
(1 + self.eps) * x
+ self.propagate(edge_index, x=x, edge_attr=edge_embedding),
)
return out

def message(self, x_j: Tensor, edge_attr: Tensor) -> Tensor:
return F.relu(x_j + edge_attr)
Expand Down
3 changes: 1 addition & 2 deletions machine-learning/hm-flax/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
x = x.reshape((x.shape[0], -1)) # flatten
x = nn.Dense(features=256)(x)
x = nn.relu(x)
x = nn.Dense(features=10)(x)
return x
return nn.Dense(features=10)(x)


def create_train_state(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def predict(image_tensor: torch.Tensor) -> dict[str, float]:
image_tensor = transforms.ToTensor()(image_tensor).unsqueeze(0)
with torch.no_grad():
prediction = torch.nn.functional.softmax(model(image_tensor)[0], dim=0)
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
return confidences
return {labels[i]: float(prediction[i]) for i in range(1000)}

gr.Interface(
fn=predict,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def __init__(self) -> None:
)

def forward(self, x: torch.Tensor) -> torch.Tensor:
embedding = self.encoder(x)
return embedding
return self.encoder(x)

def training_step(
self,
Expand All @@ -45,8 +44,7 @@ def training_step(
return loss

def configure_optimizers(self) -> torch.optim.Optimizer:
optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)
return optimizer
return torch.optim.Adam(self.parameters(), lr=1e-3)

dataset = torchvision.datasets.MNIST(
"data/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def __init__(self) -> None:
)

def forward(self, x: torch.Tensor) -> torch.Tensor:
embedding = self.encoder(x)
return embedding
return self.encoder(x)

def training_step(
self,
Expand All @@ -41,8 +40,7 @@ def training_step(
return loss

def configure_optimizers(self) -> torch.optim.Optimizer:
optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)
return optimizer
return torch.optim.Adam(self.parameters(), lr=1e-3)


def main() -> None:
Expand Down

0 comments on commit 787635a

Please sign in to comment.