Skip to content

Commit

Permalink
Merge pull request #770 from hkad98/ruff-improvements
Browse files Browse the repository at this point in the history
Enable some rules for ruff
  • Loading branch information
hkad98 authored Aug 15, 2024
2 parents 73e61db + a57bda4 commit acc0463
Show file tree
Hide file tree
Showing 58 changed files with 147 additions and 173 deletions.
5 changes: 1 addition & 4 deletions gooddata-dbt/gooddata_dbt/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ def set_gooddata_endpoint_args(parser: argparse.ArgumentParser) -> None:
)
# Alternative - use profile.yml file
env_profiles = os.getenv("GOODDATA_PROFILES")
if env_profiles:
env_profiles_list = env_profiles.split(" ")
else:
env_profiles_list = []
env_profiles_list = env_profiles.split(" ") if env_profiles else []
parser.add_argument(
"-gp",
"--gooddata-profiles",
Expand Down
2 changes: 1 addition & 1 deletion gooddata-dbt/tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def _read_json(path: Union[str, Path]) -> dict:
with open(path, "r") as f:
with open(path) as f:
return json.load(f)


Expand Down
5 changes: 1 addition & 4 deletions gooddata-fdw/gooddata_fdw/_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# (C) 2021 GoodData Corporation
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata # type: ignore # mypy issue #1153
from importlib import metadata

__version__: str = metadata.version("gooddata-fdw")
3 changes: 2 additions & 1 deletion gooddata-fdw/gooddata_fdw/executor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# (C) 2022 GoodData Corporation
from __future__ import annotations

from typing import Any, Generator, NamedTuple, Optional
from collections.abc import Generator
from typing import Any, NamedTuple, Optional

from gooddata_sdk import GoodDataSdk

Expand Down
2 changes: 1 addition & 1 deletion gooddata-fdw/gooddata_fdw/fdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class GoodDataForeignDataWrapper(ForeignDataWrapper):
def __init__(self, options: dict[str, str], columns: dict[str, ColumnDefinition]) -> None:
super(GoodDataForeignDataWrapper, self).__init__(options, columns)
super().__init__(options, columns)
_log_debug(f"initializing (options={options}, columns={columns})")

# Table options contain also foreign server options
Expand Down
3 changes: 2 additions & 1 deletion gooddata-fdw/gooddata_fdw/result_reader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# (C) 2022 GoodData Corporation
from __future__ import annotations

from typing import Any, Generator
from collections.abc import Generator
from typing import Any

from gooddata_sdk import ExecutionTable
from gooddata_sdk.type_converter import DBTypeConverterStore
Expand Down
2 changes: 1 addition & 1 deletion gooddata-fdw/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def pytest_addoption(parser):
@pytest.fixture(scope="session")
def test_config(request):
config_path = Path(request.config.getoption("--gd-test-config"))
with open(config_path, "rt") as f:
with open(config_path) as f:
config = yaml.safe_load(f)

return config
Expand Down
5 changes: 1 addition & 4 deletions gooddata-flight-server/gooddata_flight_server/_version.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# (C) 2024 GoodData Corporation
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata # type: ignore # mypy issue #1153
from importlib import metadata

try:
__version__ = metadata.version("gooddata-flight-server")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ def _init_names(cls: Any) -> Any:


_RetryFlagsLiteral: TypeAlias = Literal["any", "other", "here"]
_FlagsMapping = {
Literal["any"]: _RETRY_ANY_FLAG,
Literal["here"]: _RETRY_HERE_FLAG,
Literal["other"]: _RETRY_OTHER_FLAG,
}


def _get_flags(retry: Optional[_RetryFlagsLiteral] = None) -> int:
if retry == "any":
return _RETRY_ANY_FLAG
elif retry == "here":
return _RETRY_HERE_FLAG
elif retry == "other":
return _RETRY_OTHER_FLAG

return 0x0
return _FlagsMapping.get(retry, 0x0)


def _error_code(code: int, retry: Optional[_RetryFlagsLiteral] = None) -> int:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# (C) 2024 GoodData Corporation
import importlib
from typing import Iterable
from collections.abc import Iterable

import structlog

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# (C) 2024 GoodData Corporation
from typing import Iterable, Optional, Union
from collections.abc import Iterable
from typing import Optional, Union

import pyarrow
import structlog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# (C) 2024 GoodData Corporation
from typing import Generator, Optional
from collections.abc import Generator
from typing import Optional

import orjson
import pyarrow.flight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- FlightServerMethods - base class containing typed definitions of all Flight RPC Methods
"""

from typing import Generator, Optional, Union
from collections.abc import Generator
from typing import Optional, Union

import pyarrow.flight
from typing_extensions import TypeAlias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@


def _get_flight_server_locations(config: ServerConfig) -> tuple[str, str]:
if config.use_tls:
transport = "grpc+tls"
else:
transport = "grpc"
transport = "grpc+tls" if config.use_tls else "grpc"

return (
f"{transport}://{config.listen_host}:{config.listen_port}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# (C) 2024 GoodData Corporation
from typing import Generator
from collections.abc import Generator

import pyarrow.flight

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# (C) 2024 GoodData Corporation
import abc
import threading
from collections.abc import Generator, Iterable
from dataclasses import dataclass
from typing import Generator, Iterable, Optional, Union, final
from typing import Optional, Union, final

import pyarrow.flight
from readerwriterlock import rwlock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# (C) 2024 GoodData Corporation
import threading
import time
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Any, Callable, Generic, Iterator, Optional, TypeVar
from typing import Any, Callable, Generic, Optional, TypeVar

import structlog
from readerwriterlock import rwlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import abc
import threading
import time
from collections.abc import Generator
from concurrent.futures import CancelledError, Future, ThreadPoolExecutor
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Any, Generator, Optional, Union
from typing import Any, Optional, Union

import opentelemetry.context as otelctx
import pyarrow.flight
Expand Down Expand Up @@ -499,23 +500,25 @@ def _task_run_wrapper(self, task_execution: _TaskExecution) -> Any:
structlog.contextvars.clear_contextvars()
structlog.contextvars.bind_contextvars(**logging_ctx)

with task_execution.use_execution_span():
with SERVER_TRACER.start_as_current_span("task_run", attributes={TaskAttributes.TaskId: task.task_id}):
self._logger.info(
"task_run",
task_id=task.task_id,
waited=stats.run_waited_duration,
)
self._metrics.wait_time.observe(stats.run_waited_duration)
with (
task_execution.use_execution_span(),
SERVER_TRACER.start_as_current_span("task_run", attributes={TaskAttributes.TaskId: task.task_id}),
):
self._logger.info(
"task_run",
task_id=task.task_id,
waited=stats.run_waited_duration,
)
self._metrics.wait_time.observe(stats.run_waited_duration)

try:
return task.run()
finally:
stats.run_completed = time.perf_counter()
stats.completed = stats.run_completed
try:
return task.run()
finally:
stats.run_completed = time.perf_counter()
stats.completed = stats.run_completed

self._metrics.task_duration.observe(stats.run_duration)
self._metrics.task_e2e_duration.observe(stats.duration)
self._metrics.task_duration.observe(stats.run_duration)
self._metrics.task_e2e_duration.observe(stats.duration)

def _finish_task_with_result(self, task_execution: "_TaskExecution", result: TaskExecutionResult) -> None:
task = task_execution.task
Expand All @@ -533,11 +536,10 @@ def run_task(
self,
task_execution: _TaskExecution,
) -> Future:
with task_execution.use_execution_span():
with SERVER_TRACER.start_as_current_span("task_run_submit"):
task_execution.stats.run_submitted = time.perf_counter()
with task_execution.use_execution_span(), SERVER_TRACER.start_as_current_span("task_run_submit"):
task_execution.stats.run_submitted = time.perf_counter()

return self._executor.submit(self._task_run_wrapper, task_execution)
return self._executor.submit(self._task_run_wrapper, task_execution)

def process_task_result(
self,
Expand Down
5 changes: 3 additions & 2 deletions gooddata-flight-server/tests/server/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import contextlib
import os
import socket
from collections.abc import Iterable
from contextlib import closing
from pathlib import Path
from typing import Iterable, Union
from typing import Union

import pytest
from gooddata_flight_server.flexfun.flight_methods import (
Expand Down Expand Up @@ -35,7 +36,7 @@ def _find_free_port():

def _clean_env_vars():
to_drop = []
for key in os.environ.keys():
for key in os.environ:
if key.startswith("GOODDATA_FLIGHT"):
to_drop.append(key)

Expand Down
26 changes: 11 additions & 15 deletions gooddata-flight-server/tests/server/test_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# (C) 2024 GoodData Corporation
import os
from typing import Generator
from collections.abc import Generator

import pyarrow.flight
import pytest
Expand Down Expand Up @@ -60,9 +60,8 @@ def test_smoke_with_auth_fail1(tls_ca_cert):
os.environ["GOODDATA_FLIGHT_SERVER__AUTHENTICATION_METHOD"] = "token"
os.environ["GOODDATA_FLIGHT_ENUMERATED_TOKENS__TOKENS"] = '["t1", "t2"]'

with server(_TestingMethods(), tls=True) as s:
with pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
list(pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert).list_flights())
with server(_TestingMethods(), tls=True) as s, pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
list(pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert).list_flights())


def test_smoke_with_auth_fail2(tls_ca_cert):
Expand All @@ -71,9 +70,8 @@ def test_smoke_with_auth_fail2(tls_ca_cert):

# important: header names must be lowercase
opts = FlightCallOptions(headers=[(b"authorization", b"Bearer 123")])
with server(_TestingMethods(), tls=True) as s:
with pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
list(pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert).list_flights(b"", opts))
with server(_TestingMethods(), tls=True) as s, pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
list(pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert).list_flights(b"", opts))


def test_smoke_with_auth1(tls_ca_cert):
Expand Down Expand Up @@ -107,10 +105,9 @@ def test_smoke_with_custom_auth_fail1(tls_ca_cert):
os.environ["GOODDATA_FLIGHT_SERVER__TOKEN_VERIFICATION"] = "tests.server.testing_token_verifier"

opts = FlightCallOptions(headers=[(b"x-some-bad-header", b"test_token")])
with server(_TestingMethods(), tls=True) as s:
with pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
c = pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert)
tuple(c.list_flights(b"", opts))
with server(_TestingMethods(), tls=True) as s, pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
c = pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert)
tuple(c.list_flights(b"", opts))


def test_smoke_with_custom_auth_fail2(tls_ca_cert):
Expand All @@ -119,7 +116,6 @@ def test_smoke_with_custom_auth_fail2(tls_ca_cert):
os.environ["GOODDATA_FLIGHT_SERVER__TOKEN_VERIFICATION"] = "tests.server.testing_token_verifier"

opts = FlightCallOptions(headers=[(b"x-custom-token-header", b"invalid")])
with server(_TestingMethods(), tls=True) as s:
with pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
c = pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert)
tuple(c.list_flights(b"", opts))
with server(_TestingMethods(), tls=True) as s, pytest.raises(pyarrow.flight.FlightUnauthenticatedError):
c = pyarrow.flight.FlightClient(s.location, tls_root_certs=tls_ca_cert)
tuple(c.list_flights(b"", opts))
5 changes: 1 addition & 4 deletions gooddata-pandas/gooddata_pandas/_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# (C) 2021 GoodData Corporation
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata # type: ignore # mypy issue #1153
from importlib import metadata

__version__: str = metadata.version("gooddata-pandas")
18 changes: 9 additions & 9 deletions gooddata-pandas/gooddata_pandas/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ def _process_index(self, index_by: Optional[IndexDef] = None) -> None:
if index_by is None:
return

if not isinstance(index_by, dict):
_index_by = {self._DEFAULT_INDEX_NAME: index_by}
else:
_index_by = index_by
_index_by = {self._DEFAULT_INDEX_NAME: index_by} if not isinstance(index_by, dict) else index_by

for index_name, index_def in _index_by.items():
if isinstance(index_def, str) and (index_def in self._col_to_attr_idx):
Expand Down Expand Up @@ -208,11 +205,14 @@ def _update_filter_ids(self, filter_by: Optional[Union[Filter, list[Filter]]] =
raise ValueError(f"AttributeFilter instance referencing metric [{_filter.label}]")
else:
_filter.label = _str_to_obj_id(_filter.label) or _filter.label
elif isinstance(_filter, MetricValueFilter) and isinstance(_filter.metric, str):
if _filter.metric in self._col_to_metric_idx:
# Metric is referenced by local_id which was already generated during creation of columns
# When Metric filter contains ObjId reference, it does not need to be modified
_filter.metric = self._metrics[self._col_to_metric_idx[_filter.metric]].local_id
elif (
isinstance(_filter, MetricValueFilter)
and isinstance(_filter.metric, str)
and _filter.metric in self._col_to_metric_idx
):
# Metric is referenced by local_id which was already generated during creation of columns
# When Metric filter contains ObjId reference, it does not need to be modified
_filter.metric = self._metrics[self._col_to_metric_idx[_filter.metric]].local_id

return filters

Expand Down
5 changes: 1 addition & 4 deletions gooddata-pandas/gooddata_pandas/result_convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,7 @@ def _mapper(header: Any, header_idx: Optional[int]) -> Optional[str]:
if "labelValue" in header["attributeHeader"]:
label_value = header["attributeHeader"]["labelValue"]
primary_label_value = header["attributeHeader"]["primaryLabelValue"]
if use_primary_labels_in_attributes:
label = primary_label_value
else:
label = label_value
label = primary_label_value if use_primary_labels_in_attributes else label_value
if header_idx is not None:
if header_idx in primary_attribute_labels_mapping:
primary_attribute_labels_mapping[header_idx][primary_label_value] = label_value
Expand Down
2 changes: 1 addition & 1 deletion gooddata-pandas/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def _load_test_env():
test_env = find_dotenv(".env.test", raise_error_if_not_found=True)

return dotenv_values(test_env)
except IOError:
except OSError:
return dict()


Expand Down
2 changes: 1 addition & 1 deletion gooddata-pandas/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def pytest_addoption(parser):
@pytest.fixture(scope="session")
def test_config(request):
config_path = Path(request.config.getoption("--gd-test-config"))
with open(config_path, "rt") as f:
with open(config_path) as f:
config = yaml.safe_load(f)

return config
Loading

0 comments on commit acc0463

Please sign in to comment.