From 134ab8c851b7695cd76473c836a7b8c1f630483c Mon Sep 17 00:00:00 2001 From: Mike Gouline <1960272+gouline@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:12:16 +1000 Subject: [PATCH] Migrate to Ruff (#272) --- .github/workflows/pull_request.yml | 10 ++-------- Makefile | 29 ++++++++--------------------- dbtmetabase/__main__.py | 6 +++--- dbtmetabase/_exposures.py | 4 ++-- pyproject.toml | 25 ++++++++++++++----------- requirements-test.txt | 4 +--- sandbox/entrypoint.py | 2 +- tests/_mocks.py | 2 +- tests/test_models.py | 3 --- 9 files changed, 32 insertions(+), 53 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 192975e..c924d81 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -28,14 +28,8 @@ jobs: - name: Build run: make build - - name: Formatting check (black) - run: make check-fmt - - - name: Imports ordering check (isort) - run: make check-imports - - - name: Lint Python check (pylint) - run: make check-lint-python + - name: Lint check (ruff) + run: make check-lint - name: Type check (mypy) run: make check-type diff --git a/Makefile b/Makefile index a7126aa..e47aa4a 100644 --- a/Makefile +++ b/Makefile @@ -12,34 +12,21 @@ requirements: -r requirements-test.txt .PHONY: requirements -fix-fmt: - black . -.PHONY: fix-fmt - -fix-imports: - isort . -.PHONY: fix-imports - -fix: fix-fmt fix-imports +fix: + ruff format . + ruff check --fix . .PHONY: fix -check-fmt: - black --check . -.PHONY: check-fmt - -check-imports: - isort --check . -.PHONY: check-imports - -check-lint-python: - pylint dbtmetabase -.PHONY: check-lint-python +check-lint: + ruff format --check . + ruff check . +.PHONY: check-lint check-type: mypy dbtmetabase .PHONY: check-type -check: check-fmt check-imports check-lint-python check-type +check: check-lint check-type .PHONY: check test: diff --git a/dbtmetabase/__main__.py b/dbtmetabase/__main__.py index a28a844..e7054f5 100644 --- a/dbtmetabase/__main__.py +++ b/dbtmetabase/__main__.py @@ -65,7 +65,7 @@ def cli(ctx: click.Context, config_path: str): config_path_expanded = Path(config_path).expanduser() if config_path_expanded.exists(): - with open(config_path_expanded, "r", encoding="utf-8") as f: + with open(config_path_expanded, encoding="utf-8") as f: config = yaml.safe_load(f).get("config", {}) # Propagate common configs to all commands common = {k: v for k, v in config.items() if k not in group.commands} @@ -197,7 +197,7 @@ def wrapper( skip_verify=skip_verify, cert=cert, http_timeout=http_timeout, - http_headers={k: v for k, v in http_headers}, + http_headers=dict(http_headers), ), **kwargs, ) @@ -410,4 +410,4 @@ def exposures( if __name__ == "__main__": # Executed when running locally via python3 -m dbtmetabase - cli() # pylint: disable=no-value-for-parameter + cli() diff --git a/dbtmetabase/_exposures.py b/dbtmetabase/_exposures.py index f27ec08..f4c5bbd 100644 --- a/dbtmetabase/_exposures.py +++ b/dbtmetabase/_exposures.py @@ -337,7 +337,7 @@ def __format_exposure( if native_query: # Format query into markdown code block - native_query = "\n".join(l for l in native_query.split("\n") if l.strip()) + native_query = "\n".join(x for x in native_query.split("\n") if x.strip()) native_query = f"#### Query\n\n```\n{native_query}\n```\n\n" metadata = ( @@ -393,7 +393,7 @@ def __write_exposures( path = path.joinpath(*group[:-1]) / f"{group[-1]}.yml" path.parent.mkdir(parents=True, exist_ok=True) - exps_unwrapped = map(lambda x: x["body"], exps) + exps_unwrapped = (x["body"] for x in exps) exps_sorted = sorted(exps_unwrapped, key=itemgetter("name")) with open(path, "w", encoding="utf-8") as f: diff --git a/pyproject.toml b/pyproject.toml index e55ea2f..23012ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,19 +5,22 @@ requires = ["setuptools>=60", "setuptools-scm"] [tool.setuptools_scm] version_file = "dbtmetabase/_version.py" -[tool.black] -include = '\.pyi?$' -line-length = 88 -target-version = ["py38"] - -[tool.isort] -profile = "black" -src_paths = ["dbtmetabase", "tests", "setup.py"] - [tool.mypy] check_untyped_defs = true ignore_missing_imports = true python_version = "3.8" -[tool.pylint.master] -disable = ["R", "C", "W0511"] +[tool.ruff.lint] +select = [ + "E", # pycodestyle errors + "W", # pycodestyle warnings + "F", # pyflakes + "I", # isort + "A", # flake8-builtins + "B", # flake8-bugbear + "C4", # flake8-comprehensions +] +ignore = [ + "E501", # line-too-long + "F403", # undefined-local-with-import-star +] diff --git a/requirements-test.txt b/requirements-test.txt index 8eba1f5..68abab3 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,9 +1,7 @@ pip>=23.3.1 build>=1.0.3 twine>=4.0.2 -black>=23.11.0 -isort>=5.12.0 -pylint>=3.0.2 +ruff>=0.5.5 mypy>=1.7.1 pytest>=8.3.1 molot~=1.0.0 diff --git a/sandbox/entrypoint.py b/sandbox/entrypoint.py index 46870eb..72015a3 100755 --- a/sandbox/entrypoint.py +++ b/sandbox/entrypoint.py @@ -97,7 +97,7 @@ def metabase_setup(): timeout=10, ).json() for collection in collections: - if collection.get("is_sample") == True and collection.get("archived") == False: + if collection.get("is_sample") and not collection.get("archived"): logging.info("Deleting Metabase sample collection %s", collection["id"]) requests.put( url=f"{MB_API_URL}/collection/{collection['id']}", diff --git a/tests/_mocks.py b/tests/_mocks.py index 84333d6..1bf475b 100644 --- a/tests/_mocks.py +++ b/tests/_mocks.py @@ -108,6 +108,6 @@ def __init__( self, manifest_path: Path = FIXTURES_PATH / "manifest-v12.json", metabase_url: str = f"http://localhost:{SANDBOX_ENV['MB_PORT']}", - ): # pylint: disable=super-init-not-called + ): self._manifest = MockManifest(path=manifest_path) self._metabase = MockMetabase(url=metabase_url, record=RECORD) diff --git a/tests/test_models.py b/tests/test_models.py index 4d4db60..9fee256 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -8,7 +8,6 @@ @pytest.fixture(name="core") def fixture_core() -> MockDbtMetabase: - # pylint: disable=protected-access c = MockDbtMetabase() c._ModelsMixin__SYNC_PERIOD = 1 # type: ignore return c @@ -24,7 +23,6 @@ def test_export(core: MockDbtMetabase): def test_export_hidden_table(core: MockDbtMetabase): - # pylint: disable=protected-access core._manifest.read_models() model = core._manifest.find_model("stg_customers") assert model is not None @@ -44,7 +42,6 @@ def test_export_hidden_table(core: MockDbtMetabase): def test_build_lookups(core: MockDbtMetabase): - # pylint: disable=protected-access,no-member expected = { "PUBLIC.CUSTOMERS": [ "CUSTOMER_ID",