Skip to content

Commit 557ebf1

Browse files
committed
ruff format
1 parent 869c4af commit 557ebf1

File tree

2 files changed

+33
-48
lines changed

2 files changed

+33
-48
lines changed

pyjavapoet/java_file.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,80 +94,77 @@ def emit_to(self, out: TextIO) -> None:
9494
self.emit(writer)
9595
# Write to the output
9696
out.write(str(writer))
97-
97+
9898
def _extract_wildcard_imports(self) -> set[str]:
9999
"""Extract wildcard package imports from additional imports.
100-
100+
101101
Returns:
102102
Set of package names that have wildcard imports (e.g., 'java.util' for 'java.util.*')
103103
"""
104104
wildcard_packages = set()
105-
105+
106106
for import_name in self.additional_imports:
107107
if import_name.endswith(".*"):
108108
# Wildcard import - extract package name
109109
package = import_name[:-2] # Remove ".*"
110110
wildcard_packages.add(package)
111-
111+
112112
return wildcard_packages
113-
113+
114114
def _merge_all_specific_imports(
115-
self,
116-
collected_imports: dict[str, set[str]],
117-
wildcard_packages: set[str]
115+
self, collected_imports: dict[str, set[str]], wildcard_packages: set[str]
118116
) -> dict[str, set[str]]:
119117
"""Merge all specific imports (collected + additional) while excluding wildcard-covered packages.
120-
118+
121119
Args:
122120
collected_imports: Imports collected from type usage in the code
123121
wildcard_packages: Packages with wildcard imports to exclude
124-
122+
125123
Returns:
126124
Final resolved specific imports (excluding those covered by wildcards)
127125
"""
128126
final_imports = {}
129-
127+
130128
# Add collected imports, but skip those covered by wildcards
131129
for package, simple_names in collected_imports.items():
132130
if package not in wildcard_packages:
133131
if package not in final_imports:
134132
final_imports[package] = set()
135133
final_imports[package].update(simple_names)
136-
134+
137135
# Process additional specific imports and add them
138136
for import_name in self.additional_imports:
139137
if not import_name.endswith(".*") and "." in import_name:
140138
# This is a specific import, not a wildcard
141139
package, simple_name = import_name.rsplit(".", 1)
142-
140+
143141
# Only add if not covered by wildcard
144142
if package not in wildcard_packages:
145143
if package not in final_imports:
146144
final_imports[package] = set()
147145
final_imports[package].add(simple_name)
148-
146+
149147
return final_imports
150-
148+
151149
def _emit_all_imports(
152-
self,
153-
code_writer: CodeWriter,
154-
final_imports: dict[str, set[str]],
155-
wildcard_packages: set[str]
150+
self, code_writer: CodeWriter, final_imports: dict[str, set[str]], wildcard_packages: set[str]
156151
) -> None:
157152
"""Emit all import statements in the correct order.
158-
153+
159154
Args:
160155
code_writer: The CodeWriter to emit to
161156
final_imports: The resolved specific imports to emit
162157
wildcard_packages: The wildcard packages to emit
163158
"""
164159
# Emit static imports first
165-
static_imports = sorted([
166-
f"import static {type_name.canonical_name}.{member};"
167-
for type_name, members in self.static_imports.items()
168-
for member in sorted(members)
169-
])
170-
160+
static_imports = sorted(
161+
[
162+
f"import static {type_name.canonical_name}.{member};"
163+
for type_name, members in self.static_imports.items()
164+
for member in sorted(members)
165+
]
166+
)
167+
171168
if static_imports:
172169
for static_import in static_imports:
173170
code_writer.emit(static_import)
@@ -176,16 +173,16 @@ def _emit_all_imports(
176173

177174
# Combine wildcard and specific imports, then sort them together
178175
all_imports = []
179-
176+
180177
# Add wildcard imports
181178
for package in wildcard_packages:
182179
all_imports.append(f"import {package}.*;")
183-
180+
184181
# Add specific imports
185182
for package in final_imports:
186183
for simple_name in final_imports[package]:
187184
all_imports.append(f"import {package}.{simple_name};")
188-
185+
189186
# Sort all imports alphabetically and emit them
190187
for import_statement in sorted(all_imports):
191188
code_writer.emit(import_statement)
@@ -294,10 +291,10 @@ def add_static_import(self, constant_class: Union[ClassName, str], constant_name
294291

295292
def add_additional_import(self, import_name: str) -> "JavaFile.Builder":
296293
"""Add an additional import that will be included in the generated file.
297-
294+
298295
Args:
299296
import_name: The import to add (e.g., "java.util.List" or "java.util.*")
300-
297+
301298
Returns:
302299
This builder for method chaining
303300
"""

tests/test_java_file.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,10 @@ def test_add_additional_import_arbitrary(self):
509509
)
510510

511511
# Build JavaFile and add an arbitrary import
512-
java_file = (
513-
JavaFile.builder("com.example", type_spec)
514-
.add_additional_import("java.util.Collections")
515-
.build()
516-
)
512+
java_file = JavaFile.builder("com.example", type_spec).add_additional_import("java.util.Collections").build()
517513

518514
result = str(java_file)
519-
515+
520516
# Should contain the additional import even though it's not used
521517
self.assertIn("import java.util.Collections;", result)
522518
# Should still contain the class
@@ -529,23 +525,15 @@ def test_add_additional_import_wildcard_covers_existing(self):
529525
type_spec = (
530526
TypeSpec.class_builder("ListClass")
531527
.add_modifiers(Modifier.PUBLIC)
532-
.add_field(
533-
FieldSpec.builder(list_type, "items")
534-
.add_modifiers(Modifier.PRIVATE)
535-
.build()
536-
)
528+
.add_field(FieldSpec.builder(list_type, "items").add_modifiers(Modifier.PRIVATE).build())
537529
.build()
538530
)
539531

540532
# Build JavaFile and add wildcard import that covers the specific one
541-
java_file = (
542-
JavaFile.builder("com.example", type_spec)
543-
.add_additional_import("java.util.*")
544-
.build()
545-
)
533+
java_file = JavaFile.builder("com.example", type_spec).add_additional_import("java.util.*").build()
546534

547535
result = str(java_file)
548-
536+
549537
# Should contain the wildcard import
550538
self.assertIn("import java.util.*;", result)
551539
# Should NOT contain the specific List import since it's covered by wildcard

0 commit comments

Comments
 (0)