Skip to content

chore: replace black with ruff-format #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/test_dependents.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ jobs:

- name: Install ome-types
run: pip install .

working-directory: ome-types

- name: Run Tests
run: |
pytest --color=yes -k "not test_known_errors_without_cleaning and not bad" \
pytest --color=yes -k \
"not test_known_errors_without_cleaning and not bad and not Imaris" \
aicsimageio/tests/readers/test_ome_tiff_reader.py \
aicsimageio/tests/writers/test_ome_tiff_writer.py \
aicsimageio/tests/readers/extra_readers/test_bioformats_reader.py \
aicsimageio/tests/readers/extra_readers/test_ome_zarr_reader.py
aicsimageio/tests/readers/extra_readers/test_ome_zarr_reader.py
test-paquo:
name: test paquo
Expand Down
25 changes: 11 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ ci:
autoupdate_commit_msg: "ci(pre-commit.ci): autoupdate"

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.16
hooks:
- id: trailing-whitespace
exclude: ^tests|^docs|.xsd
- id: end-of-file-fixer
exclude: ^tests|^docs
- id: validate-pyproject

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
- repo: https://github.com/crate-ci/typos
rev: v1.19.0
hooks:
- id: ruff
args: [--fix]
- id: typos

- repo: https://github.com/psf/black
rev: 24.1.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.0
hooks:
- id: black
exclude: ^docs
- id: ruff
args: [--fix, --unsafe-fixes]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@

**Merged pull requests:**

- intial setup config [\#1](https://github.com/tlambert03/ome-types/pull/1) ([tlambert03](https://github.com/tlambert03))
- initial setup config [\#1](https://github.com/tlambert03/ome-types/pull/1) ([tlambert03](https://github.com/tlambert03))



Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/write_v2_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<details>
<summary>List of plural name changes</summary>
These fields appear in many different classes:
- `annotation_ref` -> `annotation_refs`
Expand Down
76 changes: 38 additions & 38 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dynamic = ["version"]
dependencies = [
"pydantic >=1.9.0",
"pydantic-compat >=0.1.0",
"xsdata >=23.6, <25",
"xsdata >=23.6, <24.3",
]

[project.urls]
Expand All @@ -50,7 +50,6 @@ lxml = ["lxml >=4.8.0"]
dev = ["pre-commit", "types-lxml; python_version >= '3.8'"]
docs = ["mkdocs-material", "mkdocstrings-python"]
test = [
"black",
"lxml",
"mypy",
"numpy",
Expand All @@ -70,7 +69,7 @@ test-qt = ["qtpy", "pytest-qt"]
# requirements to run the autogen script in hatch_build.py
require-runtime-dependencies = true
# pin ruff for builds because it changes often
dependencies = ["black", "ruff ==0.1.5", "xsdata[cli]>=23.6"]
dependencies = ["ruff ==0.3.0", "xsdata[cli]==24.2.1"]


# https://hatch.pypa.io/latest/config/metadata/
Expand All @@ -92,51 +91,50 @@ exclude = ["src/ome_types/_autogenerated"]
line-length = 88
src = ["src", "tests"]
target-version = "py38"
exclude = ['src/_ome_autogen.py', 'src/ome_types/_vendor']

[tool.ruff.lint]
pydocstyle = { convention = "numpy" }
select = [
"E", # style errors
"F", # flakes
"D", # pydocstyle
"I", # isort
"UP", # pyupgrade
"S", # bandit
"C4", # flake8-comprehensions
"B", # flake8-bugbear
"A001", # flake8-builtins
"TID", # tidy
"TCH", # typechecking
"RUF", # ruff-specific rules
"E", # style errors
"W", # style warnings
"F", # flakes
"D", # pydocstyle
"D417", # Missing argument descriptions in Docstrings
"I", # isort
"UP", # pyupgrade
"S", # bandit
"C4", # flake8-comprehensions
"B", # flake8-bugbear
"A001", # flake8-builtins
"RUF", # ruff-specific rules
"SIM105", # contextlib.suppress
"TID", # tidy imports
"TCH", # flake8-type-checking
]
ignore = [
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D104", # Missing docstring in public package
"D106", # Missing docstring in public nested class
"D107", # Missing docstring in __init__
"D203", # 1 blank line required before class docstring
"D205", # 1 blank line required between summary line and description
"D212", # Multi-line docstring summary should start at the first line
"D213", # Multi-line docstring summary should start at the second line
"D400", # First line should end with a period
"D404", # First word of the docstring should not be This
"D413", # Missing blank line after last section
"D416", # Section name should end with a colon
"C901", # Function is too complex
"RUF009", # Do not perform function calls in default arguments
"S3", # xml security
"D104", # Missing docstring in public package
"D401", # First line should be in imperative mood
"D401", # First line should be in imperative mood
"D100", # Missing docstring in public module
"S3", # Using `xml` to parse untrusted data is known to be vulnerable to XML attacks
]
exclude = ['src/_ome_autogen.py', 'src/ome_types/_vendor']

[tool.ruff.flake8-tidy-imports]
[tool.ruff.lint.flake8-tidy-imports]
ban-relative-imports = "all" # Disallow all relative imports.

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"tests/*.py" = ["D", "S"]
"src/ome_autogen.py" = ["D10", "E501"]
".github/*.py" = ["D"]
"setup.py" = ["D"]
"docs/**/*.py" = ["D"]
"src/xsdata_pydantic_basemodel/**/*.py" = ["D"]

# https://docs.astral.sh/ruff/formatter/
[tool.ruff.format]
docstring-code-format = true

[tool.check-manifest]
ignore = [
"coverage.yml",
Expand All @@ -149,10 +147,6 @@ ignore = [
"typesafety/**/*",
]


[tool.black]
target-version = ['py38']

# https://docs.pytest.org/en/6.2.x/customize.html
[tool.pytest.ini_options]
minversion = "6.0"
Expand Down Expand Up @@ -218,6 +212,12 @@ omit = [
"src/ome_types/widget.py",
]

[tool.typos.default]
extend-ignore-identifiers-re = ["nd2?.*", "ND2?.*", ".*ser_schema"]

[tool.typos.files]
extend-exclude = ["*.xsd", "*.xml", "*.xsl", "*.json"]

# Entry points -- REMOVE ONCE XSDATA-PYDANTIC-BASEMODEL IS SEPARATE
[project.entry-points."xsdata.plugins.class_types"]
xsdata_pydantic_basemodel = "xsdata_pydantic_basemodel.hooks.class_type"
Expand Down
2 changes: 1 addition & 1 deletion src/ome_autogen/_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def render_module(
mod = super().render_module(resolver, classes)

# xsdata renames classes like "FillRule" (which appears as a SimpleType
# inside of the Shape ComlexType) as "Shape_FillRule".
# inside of the Shape ComplexType) as "Shape_FillRule".
# We want to make them available as "FillRule" in the corresponding
# module, (i.e. the "Shape" module in this case).
# That is, we want "Shape = Shape_FillRule" included in the module.
Expand Down
38 changes: 19 additions & 19 deletions src/ome_autogen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from ome_autogen._config import PYDANTIC_SUPPORT, get_config
from ome_autogen._transformer import OMETransformer

BLACK_LINE_LENGTH = 88
BLACK_TARGET_VERSION = "py38"
BLACK_SKIP_TRAILING_COMMA = False # use trailing commas as a reason to split lines?
RUFF_LINE_LENGTH = 88
RUFF_TARGET_VERSION = "py38"

OUTPUT_PACKAGE = "ome_types._autogenerated.ome_2016_06"
DO_MYPY = os.environ.get("OME_AUTOGEN_MYPY", "0") == "1" or "--mypy" in sys.argv
SRC_PATH = Path(__file__).parent.parent
Expand Down Expand Up @@ -62,22 +62,22 @@ def build_model(


def _fix_formatting(package_dir: str, ruff_ignore: list[str] = RUFF_IGNORE) -> None:
_print_gray("Running black and ruff ...")
_print_gray("Running ruff check...")

ruff = ["ruff", "-q", "--fix", "--unsafe-fixes", package_dir]
ruff.extend(f"--ignore={ignore}" for ignore in ruff_ignore)
subprocess.check_call(ruff) # noqa S
ruff_chk = ["ruff", "check", "-q", "--fix", "--unsafe-fixes", package_dir]
ruff_chk.extend(f"--ignore={ignore}" for ignore in ruff_ignore)
subprocess.check_call(ruff_chk) # noqa S

black = [
"black",
_print_gray("Running ruff format...")
ruff_fmt = [
"ruff",
"format",
"-q",
f"--line-length={BLACK_LINE_LENGTH}",
f"--target-version={BLACK_TARGET_VERSION}",
f"--line-length={RUFF_LINE_LENGTH}",
f"--target-version={RUFF_TARGET_VERSION}",
]
if BLACK_SKIP_TRAILING_COMMA: # pragma: no cover
black.append("--skip-magic-trailing-comma")
black.extend([str(x) for x in Path(package_dir).rglob("*.py")])
subprocess.check_call(black) # noqa S
ruff_fmt.extend([str(x) for x in Path(package_dir).rglob("*.py")])
subprocess.check_call(ruff_fmt) # noqa S


def _check_mypy(package_dir: str) -> None:
Expand Down Expand Up @@ -149,13 +149,13 @@ def _disp_type(obj: Any) -> str:
if "ForwardRef" in x:
# replace "List[ForwardRef('Map.M')]" with "List[Map.M]"
x = re.sub(r"ForwardRef\('([a-zA-Z_.]*)'\)", r"\1", x)
x = re.sub("ome_types\._autogenerated\.ome_2016_06.[^.]+.", "", x)
x = re.sub(r"ome_types\._autogenerated\.ome_2016_06.[^.]+.", "", x)
return x

# add TypedDicts for all models
module = KWARGS_MODULE
SUFFIX = "Dict"
CLASS = "class {name}(TypedDict, total=False):\n\t{fields}\n\n"
CLASS = "class {name}(TypedDict, total=False):\n {fields}\n\n"
for cls_name, m in sorted(ome_models.items()):
if cls_name.endswith("Ref"):
module += f"{cls_name}: TypeAlias = RefDict\n"
Expand All @@ -166,11 +166,11 @@ def _disp_type(obj: Any) -> str:
]
if _fields:
module += CLASS.format(
name=f"{m.__name__}{SUFFIX}", fields="\n\t".join(_fields)
name=f"{m.__name__}{SUFFIX}", fields="\n ".join(_fields)
)
else:
module += (
f"class {m.__name__}{SUFFIX}(TypedDict, total=False):\n\tpass\n\n"
f"class {m.__name__}{SUFFIX}(TypedDict, total=False):\n pass\n\n"
)

# fix name spaces
Expand Down
2 changes: 1 addition & 1 deletion src/ome_types/_mixins/_kinded.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class KindMixin(BaseModel):
"""This mixin adds a `kind` field to the dict output.
"""Mixin to a `kind` field to the dict output.
This helps for casting a dict to a specific subclass, when the fields are
otherwise identical.
Expand Down
4 changes: 4 additions & 0 deletions src/ome_types/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@


class OME2016Loader(Loader):
"""Loader to redirect imports from ome_types.model to ome_types._autogenerated."""

def __init__(self, fullname: str) -> None:
submodule = fullname.split(".", 2)[-1]
file_2016 = (_OME_2016 / submodule.replace(".", "/")).with_suffix(".py")
Expand Down Expand Up @@ -61,6 +63,8 @@ def exec_module(self, module: ModuleType) -> None:
# add a sys.meta_path hook to allow import of any modules in
# ome_types._autogenerated.ome_2016_06
class OMEMetaPathFinder(MetaPathFinder):
"""Finder to redirect imports from ome_types.model to ome_types._autogenerated."""

def find_spec(
self,
fullname: str,
Expand Down
2 changes: 1 addition & 1 deletion src/ome_types/model/simple_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""This module is only here for backwards compatibility with v<0.4.0 of ome-types.
"""Module is only here for backwards compatibility with v<0.4.0 of ome-types.
Most of the types defined here are also exported from `ome_types.model`. But for
now, you can also import them from here.
Expand Down