Closed
Description
Summary
Organize imports
code action not working when one of the function in the file is deeply nested.
Checkout the code sample and screen recording.
models.py
class LinkedinAccountIdValidationReq:
pass
class LinkedinAccountIdValidationRes:
pass
class LinkedinAdsAccount:
pass
class LinkedinAdsCampaignGroup:
pass
main.py
from models import LinkedinAdsAccount, LinkedinAdsCampaignGroup
from models import LinkedinAccountIdValidationReq, LinkedinAccountIdValidationRes
from typing import Any
import polars as pl
async def validate_account_access(
accounts: list[LinkedinAccountIdValidationReq],
) -> list[LinkedinAccountIdValidationRes]:
"""Validate whether Account IDs has access on Access Token"""
# Validations
validations: list[LinkedinAccountIdValidationRes] = []
print(accounts)
return validations
async def get_accounts(access_token: str) -> list[LinkedinAdsAccount]:
"""Get Accounts"""
accounts: list[LinkedinAdsAccount] = []
print(access_token)
return accounts
async def get_campaign_groups(
access_token: str, account_id: int
) -> list[LinkedinAdsCampaignGroup]:
"""Get Campaign Groups"""
campaign_groups = []
print(access_token, account_id)
return campaign_groups
async def transform_response_rows(
rows: list[dict[str, Any]],
account_id: str | None,
access_token: str,
column_configs: dict[str, dict[str, Any]],
) -> pl.DataFrame:
"""Transform Response rows"""
# No data found
if len(rows) == 0:
return pl.DataFrame(
schema={
col: pl.Float64 if config["type"] == "METRIC" else pl.Utf8
for col, config in column_configs.items()
}
)
# Rows to dataframe first
df = pl.from_dicts(rows, infer_schema_length=None)
# Transform Pivot values
if "pivotValues" in df.columns:
df = df.with_row_index("row_nr")
df = df.join(df.pivot("entityType", index="row_nr", values="entityId"), on="row_nr")
df = df.drop(["pivotValues", "row_nr", "entityType", "entityId"], strict=False)
# Entity-wise data e.g {sponsoredCampaign: pl.Dataframe, ...}
entity_data: dict[str, pl.DataFrame] = {}
# For each column config with pivot
for col, col_config in column_configs.items():
# Entity type and key
entity_type, entity_key = col_config["type"], col_config["key"]
# ID column already exists in df, for other keys fetch data from API
if entity_key == "id":
df = df.with_columns(pl.col(entity_type).alias(col))
elif account_id:
entity_df = entity_data.get(entity_type)
if entity_df is None:
entities = []
args = [
access_token,
int(account_id.split(":")[-1]),
]
match entity_type:
case "sponsoredAccount":
entities = await get_accounts(args[0])
case "sponsoredCampaign":
entities = await get_campaign_groups(*args)
entity_df = (
pl.from_dicts([{"key": "val"} for _ in entities])
.cast(pl.Utf8)
.select(pl.all().name.prefix(f"{entity_type}."))
)
# Rename column
if col_config["res_key"] in entity_df.columns:
entity_df = entity_df.rename({col_config["res_key"]: col_config["id"]})
# Set with dict
entity_data[entity_type] = entity_df
# Join dataframes
for entity_type, entity_df in entity_data.items():
df = df.join(entity_df, left_on=entity_type, right_on=f"{entity_type}.id")
return df
pyproject.toml
[tool.ruff]
line-length = 99
lint.ignore = ["E722"]
[tool.poetry]
name = "ruffdemo"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
polars = "^1.25.2"
[tool.poetry.group.dev.dependencies]
ruff = "^0.11.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Screen recording showing code action behaviout with and without deeply nested code.
screenrec-20250319103904.mp4
Version
ruff 0.11.0