Skip to content

Commit

Permalink
🚸 introduce importlib.resources compatibility module (#414)
Browse files Browse the repository at this point in the history
## Description

This PR adds a compatibility module for easier managing of code related
to `importlib.resources` inspired by scikit-build-core.
It also updates some of the mypy and ruff configuration.

## Checklist:

<!---
This checklist serves as a reminder of a couple of things that ensure
your pull request will be merged swiftly.
-->

- [x] The pull request only contains commits that are related to it.
- [x] I have added appropriate tests and documentation.
- [x] I have made sure that all CI jobs on GitHub pass.
- [x] The pull request introduces no new warnings and follows the
project's style guidelines.

---------

Signed-off-by: burgholzer <[email protected]>
  • Loading branch information
burgholzer authored Jun 27, 2024
1 parent 5e4ad8f commit 5061002
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
23 changes: 21 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ report.exclude_also = [


[tool.mypy]
files = ["src/mqt", "test/python"]
files = ["src/mqt", "test/python", "noxfile.py"]
mypy_path = ["$MYPY_CONFIG_FILE_DIR/src"]
python_version = "3.8"
warn_unused_configs = true
Expand All @@ -158,10 +158,16 @@ warn_unreachable = true
module = ["qiskit.*"]
ignore_missing_imports = true


[tool.check-wheel-contents]
ignore = ["W002"] # Triggers on __init__.py's


[tool.ruff]
line-length = 120
extend-include = ["*.ipynb"]
src = ["src"]
namespace-packages = ["mqt"]
preview = true
unsafe-fixes = true

Expand Down Expand Up @@ -219,10 +225,23 @@ ignore = [
]
isort.required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"typing.Callable".msg = "Use collections.abc.Callable instead."
"typing.Iterator".msg = "Use collections.abc.Iterator instead."
"typing.Mapping".msg = "Use collections.abc.Mapping instead."
"typing.Sequence".msg = "Use collections.abc.Sequence instead."
"typing.Set".msg = "Use collections.abc.Set instead."
"importlib.resources".msg = "Use mqt.qcec._compat.importlib.resources instead."
"importlib_resources".msg = "Use mqt.qcec._compat.importlib.resources instead."

[tool.ruff.lint.per-file-ignores]
"test/python/**" = ["T20", "ANN"]
"test/python/**" = [
"T20", # allow print statements in tests
"PLC2701" # allow private name imports in tests
]
"docs/**" = ["T20"]
"noxfile.py" = ["T20", "TID251"]
"src/mqt/qcec/_compat/**.py" = ["TID251"]
"*.pyi" = ["D"] # pydocstyle
"*.ipynb" = [
"D", # pydocstyle
Expand Down
3 changes: 3 additions & 0 deletions src/mqt/qcec/_compat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from __future__ import annotations

__all__: list[str] = []
3 changes: 3 additions & 0 deletions src/mqt/qcec/_compat/importlib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from __future__ import annotations

__all__: list[str] = []
14 changes: 14 additions & 0 deletions src/mqt/qcec/_compat/importlib/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

import sys

if sys.version_info < (3, 10):
from importlib_resources import as_file, files
else:
from importlib.resources import as_file, files

__all__ = ["as_file", "files"]


def __dir__() -> list[str]:
return __all__
7 changes: 1 addition & 6 deletions src/mqt/qcec/verify_compilation_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import sys
import warnings
from typing import TYPE_CHECKING

Expand All @@ -11,15 +10,11 @@

from .configuration import ConfigurationOptions

if TYPE_CHECKING or sys.version_info < (3, 10, 0):
import importlib_resources as resources
else:
from importlib import resources

from qiskit import QuantumCircuit
from qiskit.transpiler.passes import ContainsInstruction

from . import ApplicationScheme, Configuration, EquivalenceCheckingManager
from ._compat.importlib import resources
from .compilation_flow_profiles import AncillaMode, generate_profile_name
from .configuration import augment_config_from_kwargs
from .verify import verify
Expand Down
17 changes: 6 additions & 11 deletions test/python/test_compilation_flow_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,29 @@

from __future__ import annotations

import difflib
import filecmp
import locale
import os
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast

if TYPE_CHECKING or sys.version_info < (3, 9, 0):
import importlib_resources as resources
else:
from importlib import resources

import difflib
import locale
from typing import Any, cast

import pytest

from mqt import qcec
from mqt.qcec._compat.importlib import resources
from mqt.qcec.compilation_flow_profiles import generate_profile_name


@pytest.fixture(params=[0, 1, 2, 3])
def optimization_level(request: Any) -> int:
def optimization_level(request: Any) -> int: # noqa: ANN401
"""Fixture for optimization levels."""
return cast(int, request.param)


@pytest.fixture(params=[qcec.AncillaMode.NO_ANCILLA, qcec.AncillaMode.RECURSION, qcec.AncillaMode.V_CHAIN])
def ancilla_mode(request: Any) -> qcec.AncillaMode:
def ancilla_mode(request: Any) -> qcec.AncillaMode: # noqa: ANN401
"""Fixture for ancilla modes."""
return cast(qcec.AncillaMode, request.param)

Expand Down

0 comments on commit 5061002

Please sign in to comment.