Skip to content

Commit 2b72bee

Browse files
committed
fix a bunch and please me.
1 parent 11bf2cf commit 2b72bee

File tree

117 files changed

+262
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+262
-116
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Remove lower-case built-in generics imported from `typing`.
4+
"""
5+
6+
from __future__ import annotations
7+
8+
import argparse
9+
import sys
10+
from pathlib import Path
11+
from typing import Iterable, Iterator, Sequence
12+
13+
14+
try:
15+
import libcst as cst
16+
except ImportError as exc: # pragma: no cover - dependency guard
17+
raise SystemExit("This script requires `libcst`. Install it via `pip install libcst` and retry.") from exc
18+
19+
20+
BUILTIN_TYPING_NAMES = frozenset({"callable", "dict", "frozenset", "list", "set", "tuple", "type"})
21+
22+
23+
class TypingBuiltinImportRemover(cst.CSTTransformer):
24+
def __init__(self) -> None:
25+
self.changed = False
26+
self.removed: list[str] = []
27+
self.warnings: list[str] = []
28+
29+
def leave_ImportFrom(self, original_node: cst.ImportFrom, updated_node: cst.ImportFrom) -> cst.BaseStatement:
30+
module_name = self._module_name(updated_node.module)
31+
if module_name != "typing":
32+
return updated_node
33+
34+
names = updated_node.names
35+
if isinstance(names, cst.ImportStar):
36+
self.warnings.append("encountered `from typing import *` (skipped)")
37+
return updated_node
38+
39+
new_aliases = []
40+
removed_here: list[str] = []
41+
for alias in names:
42+
if isinstance(alias, cst.ImportStar):
43+
self.warnings.append("encountered `from typing import *` (skipped)")
44+
return updated_node
45+
if not isinstance(alias.name, cst.Name):
46+
new_aliases.append(alias)
47+
continue
48+
imported_name = alias.name.value
49+
if imported_name in BUILTIN_TYPING_NAMES:
50+
removed_here.append(imported_name)
51+
continue
52+
new_aliases.append(alias)
53+
54+
if not removed_here:
55+
return updated_node
56+
57+
self.changed = True
58+
self.removed.extend(removed_here)
59+
60+
if not new_aliases:
61+
return cst.RemoveFromParent()
62+
# Ensure trailing commas are removed.
63+
formatted_aliases = []
64+
for alias in new_aliases:
65+
if alias.comma is not None and alias is new_aliases[-1]:
66+
formatted_aliases.append(alias.with_changes(comma=None))
67+
else:
68+
formatted_aliases.append(alias)
69+
70+
return updated_node.with_changes(names=tuple(formatted_aliases))
71+
72+
def _module_name(self, node: cst.BaseExpression | None) -> str | None:
73+
if node is None:
74+
return None
75+
if isinstance(node, cst.Name):
76+
return node.value
77+
if isinstance(node, cst.Attribute):
78+
prefix = self._module_name(node.value)
79+
if prefix is None:
80+
return node.attr.value
81+
return f"{prefix}.{node.attr.value}"
82+
return None
83+
84+
85+
def iter_python_files(paths: Iterable[Path]) -> Iterator[Path]:
86+
for path in paths:
87+
if path.is_dir():
88+
yield from (p for p in path.rglob("*.py") if not p.name.startswith("."))
89+
yield from (p for p in path.rglob("*.pyi") if not p.name.startswith("."))
90+
elif path.suffix in {".py", ".pyi"}:
91+
yield path
92+
93+
94+
def process_file(path: Path, dry_run: bool) -> tuple[bool, TypingBuiltinImportRemover]:
95+
source = path.read_text(encoding="utf-8")
96+
module = cst.parse_module(source)
97+
transformer = TypingBuiltinImportRemover()
98+
updated = module.visit(transformer)
99+
100+
if not transformer.changed or source == updated.code:
101+
return False, transformer
102+
103+
if not dry_run:
104+
path.write_text(updated.code, encoding="utf-8")
105+
return True, transformer
106+
107+
108+
def main(argv: Sequence[str] | None = None) -> int:
109+
parser = argparse.ArgumentParser(description="Remove lower-case built-in generics imported from typing.")
110+
parser.add_argument(
111+
"paths",
112+
nargs="*",
113+
type=Path,
114+
default=[Path("src")],
115+
help="Files or directories to rewrite (default: src).",
116+
)
117+
parser.add_argument(
118+
"--dry-run",
119+
action="store_true",
120+
help="Only report files that would change without writing them.",
121+
)
122+
args = parser.parse_args(argv)
123+
124+
files = sorted(set(iter_python_files(args.paths)))
125+
if not files:
126+
print("No Python files matched the provided paths.", file=sys.stderr)
127+
return 1
128+
129+
changed_any = False
130+
for path in files:
131+
changed, transformer = process_file(path, dry_run=args.dry_run)
132+
if changed:
133+
changed_any = True
134+
action = "Would update" if args.dry_run else "Updated"
135+
removed = ", ".join(sorted(set(transformer.removed)))
136+
print(f"{action}: {path} (removed typing imports: {removed})")
137+
for warning in transformer.warnings:
138+
print(f"Warning: {path}: {warning}", file=sys.stderr)
139+
140+
if not changed_any:
141+
print("No changes needed.")
142+
return 0
143+
144+
145+
if __name__ == "__main__":
146+
raise SystemExit(main())

src/diffusers/loaders/textual_inversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Optional, list
14+
from typing import Optional
1515

1616
import safetensors
1717
import torch

src/diffusers/models/attention_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
import inspect
1515
import math
16-
from typing import Callable, Optional, list
16+
from typing import Callable, Optional
1717

1818
import torch
1919
import torch.nn.functional as F

src/diffusers/models/autoencoders/consistency_decoder_vae.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
from dataclasses import dataclass
15-
from typing import Optional, tuple
15+
from typing import Optional
1616

1717
import torch
1818
import torch.nn.functional as F

src/diffusers/models/autoencoders/vae.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
from dataclasses import dataclass
15-
from typing import Optional, tuple
15+
from typing import Optional
1616

1717
import numpy as np
1818
import torch

src/diffusers/models/controlnets/controlnet_flux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from dataclasses import dataclass
16-
from typing import Any, Optional, tuple
16+
from typing import Any, Optional
1717

1818
import torch
1919
import torch.nn as nn

src/diffusers/models/controlnets/controlnet_qwenimage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from dataclasses import dataclass
16-
from typing import Any, Optional, tuple
16+
from typing import Any, Optional
1717

1818
import torch
1919
import torch.nn as nn

src/diffusers/models/controlnets/controlnet_sd3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
from dataclasses import dataclass
17-
from typing import Any, Optional, tuple
17+
from typing import Any, Optional
1818

1919
import torch
2020
import torch.nn as nn

src/diffusers/models/controlnets/controlnet_xs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
from dataclasses import dataclass
1515
from math import gcd
16-
from typing import Any, Dict, Optional, tuple
16+
from typing import Any, Dict, Optional
1717

1818
import torch
1919
from torch import Tensor, nn

src/diffusers/models/controlnets/multicontrolnet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Any, Callable, Optional, tuple
2+
from typing import Any, Callable, Optional
33

44
import torch
55
from torch import nn

0 commit comments

Comments
 (0)