Skip to content

Commit 3dc2e9f

Browse files
committed
Fix ruff linter warnings
1 parent 57e1eaf commit 3dc2e9f

4 files changed

Lines changed: 25 additions & 92 deletions

File tree

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
[project]
2+
name = "ultimate-python"
3+
version = "1.0.0"
4+
requires-python = ">= 3.11"
5+
16
[tool.ruff]
27
line-length = 160
38

ultimatepython/advanced/exception_groups.py

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ def raising_exception_groups():
1616
exceptions. You create it with a message and a sequence of exceptions.
1717
"""
1818
# Create a simple exception group with multiple exceptions
19-
exceptions = [
20-
ValueError("Invalid value"),
21-
TypeError("Wrong type"),
22-
KeyError("Missing key")
23-
]
19+
exceptions = [ValueError("Invalid value"), TypeError("Wrong type"), KeyError("Missing key")]
2420

2521
try:
2622
# Raise an ExceptionGroup containing multiple exceptions
@@ -34,7 +30,6 @@ def raising_exception_groups():
3430
return "Caught exception group"
3531

3632

37-
3833
def handling_with_except_star():
3934
"""Demonstrate the except* syntax for handling exception groups.
4035
@@ -44,11 +39,7 @@ def handling_with_except_star():
4439
results = []
4540

4641
try:
47-
exceptions = [
48-
ValueError("Invalid value 1"),
49-
ValueError("Invalid value 2"),
50-
TypeError("Wrong type 1")
51-
]
42+
exceptions = [ValueError("Invalid value 1"), ValueError("Invalid value 2"), TypeError("Wrong type 1")]
5243
raise ExceptionGroup("Processing errors", exceptions)
5344
except* ValueError as eg:
5445
# This catches all ValueError instances from the group
@@ -72,12 +63,7 @@ def handling_multiple_types():
7263
results = []
7364

7465
try:
75-
exceptions = [
76-
ValueError("Bad value"),
77-
TypeError("Bad type"),
78-
RuntimeError("Runtime issue"),
79-
ValueError("Another bad value")
80-
]
66+
exceptions = [ValueError("Bad value"), TypeError("Bad type"), RuntimeError("Runtime issue"), ValueError("Another bad value")]
8167
raise ExceptionGroup("Various errors", exceptions)
8268
except* ValueError as eg:
8369
# Handles all ValueErrors (there are 2)
@@ -100,12 +86,7 @@ def nested_exception_groups():
10086
unless you explicitly unwrap them.
10187
"""
10288
# Create a flat exception group with mixed exception types
103-
exceptions = [
104-
ValueError("Value error 1"),
105-
ValueError("Value error 2"),
106-
TypeError("Type error 1"),
107-
RuntimeError("Runtime error")
108-
]
89+
exceptions = [ValueError("Value error 1"), ValueError("Value error 2"), TypeError("Type error 1"), RuntimeError("Runtime error")]
10990

11091
eg = ExceptionGroup("Multiple errors", exceptions)
11192

@@ -134,13 +115,9 @@ def partial_handling():
134115

135116
try:
136117
try:
137-
exceptions = [
138-
ValueError("Value error"),
139-
TypeError("Type error"),
140-
KeyError("Key error")
141-
]
118+
exceptions = [ValueError("Value error"), TypeError("Type error"), KeyError("Key error")]
142119
raise ExceptionGroup("Mixed errors", exceptions)
143-
except* ValueError as eg:
120+
except* ValueError:
144121
# Only handle ValueError, leaving TypeError and KeyError
145122
results.append("Handled ValueError")
146123
# The other exceptions are automatically re-raised
@@ -160,12 +137,7 @@ def filtering_exceptions():
160137
161138
The subgroup() method allows you to filter exceptions by type.
162139
"""
163-
exceptions = [
164-
ValueError("Value 1"),
165-
TypeError("Type 1"),
166-
ValueError("Value 2"),
167-
RuntimeError("Runtime 1")
168-
]
140+
exceptions = [ValueError("Value 1"), TypeError("Type 1"), ValueError("Value 2"), RuntimeError("Runtime 1")]
169141

170142
eg = ExceptionGroup("All errors", exceptions)
171143

@@ -192,6 +164,7 @@ def practical_concurrent_example():
192164
This demonstrates a common use case: running multiple tasks
193165
where each can fail independently.
194166
"""
167+
195168
def process_user(user_id):
196169
"""Simulate processing a user."""
197170
if user_id == 1:
@@ -303,11 +276,7 @@ def split_by_type(eg):
303276
type_errors = eg.subgroup(TypeError)
304277
return value_errors, type_errors
305278

306-
exceptions = [
307-
ValueError("Value 1"),
308-
TypeError("Type 1"),
309-
ValueError("Value 2")
310-
]
279+
exceptions = [ValueError("Value 1"), TypeError("Type 1"), ValueError("Value 2")]
311280
eg = ExceptionGroup("Mixed", exceptions)
312281
values, types = split_by_type(eg)
313282

@@ -325,11 +294,7 @@ def split_by_type(eg):
325294
def simulate_async_failures():
326295
"""Simulate collecting errors from multiple async operations."""
327296
# In real async code, you might use asyncio.gather with return_exceptions=True
328-
task_errors = [
329-
TimeoutError("Task 1 timed out"),
330-
ValueError("Task 2 invalid input"),
331-
ConnectionError("Task 3 connection failed")
332-
]
297+
task_errors = [TimeoutError("Task 1 timed out"), ValueError("Task 2 invalid input"), ConnectionError("Task 3 connection failed")]
333298

334299
results = {"timeout": 0, "value": 0, "connection": 0}
335300

ultimatepython/data_structures/dict_union.py

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -98,36 +98,22 @@ def main() -> None:
9898
dict20 = {"city": "NYC", "scores": [85, 90, 95]}
9999
dict21 = {"active": True}
100100
person = dict19 | dict20 | dict21
101-
assert person == {
102-
"name": "Alice",
103-
"age": 30,
104-
"city": "NYC",
105-
"scores": [85, 90, 95],
106-
"active": True
107-
}
101+
assert person == {"name": "Alice", "age": 30, "city": "NYC", "scores": [85, 90, 95], "active": True}
108102

109103
# Practical use case: Configuration merging
110104
# Start with default configuration
111-
default_config = {
112-
"timeout": 30,
113-
"retries": 3,
114-
"debug": False,
115-
"log_level": "INFO"
116-
}
105+
default_config = {"timeout": 30, "retries": 3, "debug": False, "log_level": "INFO"}
117106

118107
# User provides custom configuration (partial)
119-
user_config = {
120-
"timeout": 60,
121-
"debug": True
122-
}
108+
user_config = {"timeout": 60, "debug": True}
123109

124110
# Merge configurations, user settings override defaults
125111
final_config = default_config | user_config
126112
assert final_config == {
127113
"timeout": 60, # Overridden by user
128-
"retries": 3, # From default
114+
"retries": 3, # From default
129115
"debug": True, # Overridden by user
130-
"log_level": "INFO" # From default
116+
"log_level": "INFO", # From default
131117
}
132118

133119
# Practical use case: Building objects incrementally
@@ -140,34 +126,16 @@ def main() -> None:
140126
# Add profile info
141127
with_profile = with_auth | {"bio": "Developer", "location": "USA"}
142128

143-
assert with_profile == {
144-
"id": 1,
145-
"type": "user",
146-
"username": "john",
147-
"email": "john@example.com",
148-
"bio": "Developer",
149-
"location": "USA"
150-
}
129+
assert with_profile == {"id": 1, "type": "user", "username": "john", "email": "john@example.com", "bio": "Developer", "location": "USA"}
151130

152131
# Practical use case: Updating records with |=
153-
user_record = {
154-
"id": 100,
155-
"name": "Jane",
156-
"status": "active",
157-
"login_count": 5
158-
}
132+
user_record = {"id": 100, "name": "Jane", "status": "active", "login_count": 5}
159133

160134
# Apply update from an external source
161135
update = {"status": "inactive", "login_count": 6, "last_login": "2024-01-15"}
162136
user_record |= update
163137

164-
assert user_record == {
165-
"id": 100,
166-
"name": "Jane",
167-
"status": "inactive",
168-
"login_count": 6,
169-
"last_login": "2024-01-15"
170-
}
138+
assert user_record == {"id": 100, "name": "Jane", "status": "inactive", "login_count": 6, "last_login": "2024-01-15"}
171139

172140
# The union operators only work with dictionaries
173141
# Attempting to use them with non-dict types raises TypeError

ultimatepython/syntax/arg_enforcement.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def keyword_with_defaults(*, x=5, y=3):
6969
7070
When providing arguments, you must use the keyword names.
7171
"""
72-
return x ** y
72+
return x**y
7373

7474

7575
def complex_signature(a, b, /, c, d=10, *, e, f=20):
@@ -230,12 +230,7 @@ def create_user(username, *, admin=False, active=True, send_email=False):
230230
callers must specify exactly what they're setting, improving
231231
readability and preventing accidental mistakes.
232232
"""
233-
return {
234-
"username": username,
235-
"admin": admin,
236-
"active": active,
237-
"send_email": send_email
238-
}
233+
return {"username": username, "admin": admin, "active": active, "send_email": send_email}
239234

240235
# Clear intent at call site
241236
user = create_user("john_doe", admin=True, send_email=True)

0 commit comments

Comments
 (0)