Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use ruff isort during codegen #972

Merged
merged 1 commit into from Mar 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -8,13 +8,13 @@ repos:
- id: end-of-file-fixer
- id: debug-statements
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.0
rev: v0.3.2
hooks:
- id: ruff
args: [ --fix, --show-fixes ]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
rev: v1.9.0
hooks:
- id: mypy
files: ^(xsdata/)
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/annotations/model.py
@@ -1,6 +1,8 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Optional

from tests.fixtures.annotations.units import unit

__NAMESPACE__ = "http://domain.org/schema/model"
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/annotations/units.py
@@ -1,4 +1,5 @@
from __future__ import annotations

from enum import Enum

__NAMESPACE__ = "http://domain.org/schema/model/units"
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/artists/metadata.py
@@ -1,5 +1,6 @@
from dataclasses import dataclass, field
from typing import List, Optional, Union

from xsdata.models.datatype import XmlDate, XmlPeriod

__NAMESPACE__ = "http://musicbrainz.org/ns/mmd-2.0#"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/books/__init__.py
@@ -1,7 +1,7 @@
from tests.fixtures.books.books import (
BookForm,
BooksForm,
Books,
BooksForm,
)

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/books/books.py
@@ -1,5 +1,6 @@
from dataclasses import dataclass, field
from typing import List, Optional

from xsdata.models.datatype import XmlDate

__NAMESPACE__ = "urn:books"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/docstrings/accessible/__init__.py
Expand Up @@ -2,9 +2,9 @@
DoubleQuotesDescription,
DoubleQuotesSummary,
Root,
RootEnum,
RootB,
RootD,
RootEnum,
)

__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/docstrings/blank/__init__.py
Expand Up @@ -2,9 +2,9 @@
DoubleQuotesDescription,
DoubleQuotesSummary,
Root,
RootEnum,
RootB,
RootD,
RootEnum,
)

__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/docstrings/google/__init__.py
Expand Up @@ -2,9 +2,9 @@
DoubleQuotesDescription,
DoubleQuotesSummary,
Root,
RootEnum,
RootB,
RootD,
RootEnum,
)

__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/docstrings/numpy/__init__.py
Expand Up @@ -2,9 +2,9 @@
DoubleQuotesDescription,
DoubleQuotesSummary,
Root,
RootEnum,
RootB,
RootD,
RootEnum,
)

__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/docstrings/rst/__init__.py
Expand Up @@ -2,9 +2,9 @@
DoubleQuotesDescription,
DoubleQuotesSummary,
Root,
RootEnum,
RootB,
RootD,
RootEnum,
)

__all__ = [
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/hello/__init__.py
@@ -1,11 +1,11 @@
from tests.fixtures.hello.hello import (
GetHelloAsString,
GetHelloAsStringResponse,
HelloByeError,
HelloError,
HelloGetHelloAsString,
HelloGetHelloAsStringInput,
HelloGetHelloAsStringOutput,
GetHelloAsString,
GetHelloAsStringResponse,
)

__all__ = [
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/primer/__init__.py
@@ -1,9 +1,9 @@
from tests.fixtures.primer.order import (
Comment,
Items,
PurchaseOrder,
PurchaseOrderType,
Usaddress,
Comment,
PurchaseOrder,
)

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/primer/order.py
@@ -1,6 +1,7 @@
from dataclasses import dataclass, field
from decimal import Decimal
from typing import List, Optional

from xsdata.models.datatype import XmlDate


Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/series/__init__.py
@@ -1,8 +1,8 @@
from tests.fixtures.series.series import (
Links,
Country,
Externals,
Image,
Links,
Network,
Previousepisode,
Rating,
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/series/series.py
@@ -1,5 +1,6 @@
from dataclasses import dataclass, field
from typing import List, Optional

from xsdata.models.datatype import XmlDate


Expand Down
11 changes: 0 additions & 11 deletions tests/formats/dataclass/test_filters.py
Expand Up @@ -899,17 +899,6 @@ def test_default_imports_with_annotations(self):
expected = "from __future__ import annotations"
self.assertEqual(expected, self.filters.default_imports(""))

def test_default_imports_ordering(self):
self.filters.postponed_annotations = True
self.filters.import_patterns["attrs"] = {"__module__": ["@attrs.s"]}

expected = (
"from __future__ import annotations\n"
"import attrs\n"
"from dataclasses import dataclass"
)
self.assertEqual(expected, self.filters.default_imports("@dataclass @attrs.s"))

def test_format_metadata(self):
data = {
"num": 1,
Expand Down
33 changes: 16 additions & 17 deletions tests/formats/dataclass/test_generator.py
Expand Up @@ -68,23 +68,22 @@ def test_render_package(self):
random.shuffle(classes)

actual = self.generator.render_package(classes, "foo.tests", Path.cwd())
expected = "\n".join(
[
"from foo.bar import A as BarA",
"from foo.tests import (",
" A as TestsA,",
" B,",
" C,",
")",
"",
"__all__ = [",
' "BarA",',
' "TestsA",',
' "B",',
' "C",',
"]",
"",
]
expected = (
"from foo.bar import A as BarA\n"
"from foo.tests import (\n"
" A as TestsA,\n"
")\n"
"from foo.tests import (\n"
" B,\n"
" C,\n"
")\n"
"\n"
"__all__ = [\n"
' "BarA",\n'
' "TestsA",\n'
' "B",\n'
' "C",\n'
"]\n"
)
self.assertEqual(expected, actual)

Expand Down
12 changes: 5 additions & 7 deletions xsdata/formats/dataclass/filters.py
Expand Up @@ -843,8 +843,7 @@ def constant_value(self, attr: Attr) -> str:

def default_imports(self, output: str) -> str:
"""Generate the default imports for the given package output."""
module_imports = set()
func_imports = set()
imports = []
for library, types in self.import_patterns.items():
names = [
name
Expand All @@ -853,15 +852,14 @@ def default_imports(self, output: str) -> str:
]

if len(names) == 1 and names[0] == "__module__":
module_imports.add(f"import {library}")
imports.append(f"import {library}")
elif names:
func_imports.add(f"from {library} import {', '.join(names)}")
imports.append(f"from {library} import {', '.join(names)}")

imports = sorted(module_imports) + sorted(func_imports)
if self.postponed_annotations:
imports.insert(0, "from __future__ import annotations")
imports.append("from __future__ import annotations")

return "\n".join(imports)
return "\n".join(collections.unique_sequence(imports))

def _get_iterable_format(self):
fmt = "Tuple[{}, ...]" if self.format.frozen else "List[{}]"
Expand Down
16 changes: 15 additions & 1 deletion xsdata/formats/dataclass/generator.py
Expand Up @@ -24,7 +24,7 @@ class DataclassGenerator(AbstractGenerator):
filters: The template filters instance
"""

__slots__ = ("env", "filters")
__slots__ = ("env", "filters", "ruff_config")

package_template = "package.jinja2"
module_template = "module.jinja2"
Expand All @@ -39,6 +39,7 @@ def __init__(self, config: GeneratorConfig):
self.env = Environment(loader=loader, autoescape=False)
self.filters = self.init_filters(config)
self.filters.register(self.env)
self.ruff_config = Path(__file__).parent / "ruff.toml"

@classmethod
def get_template_paths(cls) -> List[str]:
Expand Down Expand Up @@ -227,6 +228,19 @@ def ruff_code(self, src_code: str, file_path: Path) -> str:
"--line-length",
str(self.config.output.max_line_length),
],
[
"ruff",
"checks",
"--stdin-filename",
str(file_path),
"--line-length",
str(self.config.output.max_line_length),
"--config",
str(self.ruff_config),
"--fix",
"--unsafe-fixes",
"--exit-zero",
],
]
try:
src_code_encoded = src_code.encode()
Expand Down
8 changes: 8 additions & 0 deletions xsdata/formats/dataclass/ruff.toml
@@ -0,0 +1,8 @@
exclude = [
]

[lint]
select = ["I"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
14 changes: 5 additions & 9 deletions xsdata/models/datatype.py
Expand Up @@ -283,11 +283,9 @@ def __str__(self) -> str:
- 2001-10-26T21:32:52.126
- -2001-10-26T21:32:52.126Z
"""
return "{}T{}{}".format(
format_date(self.year, self.month, self.day),
format_time(self.hour, self.minute, self.second, self.fractional_second),
format_offset(self.offset),
)
date = format_date(self.year, self.month, self.day)
time = format_time(self.hour, self.minute, self.second, self.fractional_second)
return f"{date}T{time}{format_offset(self.offset)}"

def __repr__(self) -> str:
"""Return the instance string representation."""
Expand Down Expand Up @@ -433,10 +431,8 @@ def __str__(self) -> str:
- 21:32:52.126789
- 21:32:52.126Z
"""
return "{}{}".format(
format_time(self.hour, self.minute, self.second, self.fractional_second),
format_offset(self.offset),
)
time = format_time(self.hour, self.minute, self.second, self.fractional_second)
return f"{time}{format_offset(self.offset)}"

def __repr__(self) -> str:
"""Return the instance string representation."""
Expand Down