Skip to content

Commit 1cb2014

Browse files
standardize on is False for boolean flags
1 parent 9c9ed24 commit 1cb2014

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

docs/user-guide/api/endpoints.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async def get_user(
2424
):
2525
"""Get a user by ID."""
2626
user = await crud_users.get(db=db, id=user_id, schema_to_select=UserRead)
27-
if not user:
27+
if user is None:
2828
raise HTTPException(status_code=404, detail="User not found")
2929
return user
3030
```
@@ -42,7 +42,7 @@ async def get_user(
4242
db: Annotated[AsyncSession, Depends(async_get_db)]
4343
):
4444
user = await crud_users.get(db=db, id=user_id, schema_to_select=UserRead)
45-
if not user:
45+
if user is None:
4646
raise HTTPException(status_code=404, detail="User not found")
4747
return user
4848
```
@@ -82,7 +82,7 @@ async def create_user(
8282
db: Annotated[AsyncSession, Depends(async_get_db)]
8383
):
8484
# Check if user already exists
85-
if await crud_users.exists(db=db, email=user_data.email):
85+
if await crud_users.exists(db=db, email=user_data.email) is True:
8686
raise HTTPException(status_code=409, detail="Email already exists")
8787

8888
# Create user
@@ -100,7 +100,7 @@ async def update_user(
100100
db: Annotated[AsyncSession, Depends(async_get_db)]
101101
):
102102
# Check if user exists
103-
if not await crud_users.exists(db=db, id=user_id):
103+
if await crud_users.exists(db=db, id=user_id) is None:
104104
raise HTTPException(status_code=404, detail="User not found")
105105

106106
# Update user
@@ -116,7 +116,7 @@ async def delete_user(
116116
user_id: int,
117117
db: Annotated[AsyncSession, Depends(async_get_db)]
118118
):
119-
if not await crud_users.exists(db=db, id=user_id):
119+
if await crud_users.exists(db=db, id=user_id) is None:
120120
raise HTTPException(status_code=404, detail="User not found")
121121

122122
await crud_users.delete(db=db, id=user_id)
@@ -176,9 +176,9 @@ async def search_users(
176176
db: Annotated[AsyncSession, Depends(async_get_db)]
177177
):
178178
filters = {"is_active": is_active}
179-
if name:
179+
if name is True:
180180
filters["name"] = name
181-
if age:
181+
if age is True:
182182
filters["age"] = age
183183

184184
users = await crud_users.get_multi(db=db, **filters)
@@ -220,13 +220,13 @@ from app.core.exceptions.http_exceptions import (
220220
@router.get("/{user_id}")
221221
async def get_user(user_id: int, db: AsyncSession):
222222
user = await crud_users.get(db=db, id=user_id)
223-
if not user:
223+
if user is None:
224224
raise NotFoundException("User not found") # Returns 404
225225
return user
226226

227227
@router.post("/")
228228
async def create_user(user_data: UserCreate, db: AsyncSession):
229-
if await crud_users.exists(db=db, email=user_data.email):
229+
if await crud_users.exists(db=db, email=user_data.email) is True:
230230
raise DuplicateValueException("Email already exists") # Returns 409
231231

232232
return await crud_users.create(db=db, object=user_data)
@@ -245,7 +245,7 @@ async def upload_avatar(
245245
db: Annotated[AsyncSession, Depends(async_get_db)]
246246
):
247247
# Check file type
248-
if not file.content_type.startswith('image/'):
248+
if file.content_type is None or file.content_type.startswith("image/") is False:
249249
raise HTTPException(status_code=400, detail="File must be an image")
250250

251251
# Save file and update user

docs/user-guide/api/exceptions.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from app.core.exceptions.http_exceptions import NotFoundException
1212
@router.get("/{user_id}")
1313
async def get_user(user_id: int, db: AsyncSession):
1414
user = await crud_users.get(db=db, id=user_id)
15-
if not user:
15+
if user is None:
1616
raise NotFoundException("User not found") # Returns 404
1717
return user
1818
```
@@ -30,7 +30,7 @@ from app.core.exceptions.http_exceptions import NotFoundException
3030
@router.get("/{user_id}")
3131
async def get_user(user_id: int):
3232
user = await crud_users.get(db=db, id=user_id)
33-
if not user:
33+
if user is None:
3434
raise NotFoundException("User not found")
3535
return user
3636

@@ -45,7 +45,7 @@ from app.core.exceptions.http_exceptions import DuplicateValueException
4545

4646
@router.post("/")
4747
async def create_user(user_data: UserCreate):
48-
if await crud_users.exists(db=db, email=user_data.email):
48+
if await crud_users.exists(db=db, email=user_data.email) is True:
4949
raise DuplicateValueException("Email already exists")
5050

5151
return await crud_users.create(db=db, object=user_data)
@@ -64,7 +64,7 @@ async def delete_user(
6464
user_id: int,
6565
current_user: Annotated[dict, Depends(get_current_user)]
6666
):
67-
if current_user["id"] != user_id and not current_user["is_superuser"]:
67+
if current_user["id"] != user_id and current_user["is_superuser"] is False:
6868
raise ForbiddenException("You can only delete your own account")
6969

7070
await crud_users.delete(db=db, id=user_id)
@@ -83,7 +83,7 @@ from app.core.exceptions.http_exceptions import UnauthorizedException
8383
@router.get("/admin-only")
8484
async def admin_endpoint():
8585
# Some validation logic
86-
if not user_is_admin:
86+
if user_is_admin is False:
8787
raise UnauthorizedException("Admin access required")
8888

8989
return {"data": "secret admin data"}
@@ -100,11 +100,11 @@ async def admin_endpoint():
100100
@router.post("/", response_model=UserRead)
101101
async def create_user(user_data: UserCreate, db: AsyncSession):
102102
# Check email
103-
if await crud_users.exists(db=db, email=user_data.email):
103+
if await crud_users.exists(db=db, email=user_data.email) is True:
104104
raise DuplicateValueException("Email already exists")
105105

106106
# Check username
107-
if await crud_users.exists(db=db, username=user_data.username):
107+
if await crud_users.exists(db=db, username=user_data.username) is True:
108108
raise DuplicateValueException("Username already taken")
109109

110110
# Create user
@@ -123,13 +123,13 @@ async def update_user(
123123
db: AsyncSession
124124
):
125125
# Check if user exists
126-
if not await crud_users.exists(db=db, id=user_id):
126+
if await crud_users.exists(db=db, id=user_id) is None:
127127
raise NotFoundException("User not found")
128128

129129
# Check for email conflicts (if email is being updated)
130-
if user_data.email:
130+
if user_data.email is True:
131131
existing = await crud_users.get(db=db, email=user_data.email)
132-
if existing and existing.id != user_id:
132+
if existing is True and existing.id != user_id:
133133
raise DuplicateValueException("Email already taken")
134134

135135
# Update user
@@ -145,11 +145,11 @@ async def get_post(
145145
db: AsyncSession
146146
):
147147
post = await crud_posts.get(db=db, id=post_id)
148-
if not post:
148+
if post is None:
149149
raise NotFoundException("Post not found")
150150

151151
# Check if user owns the post or is admin
152-
if post.author_id != current_user["id"] and not current_user["is_superuser"]:
152+
if post.author_id != current_user["id"] and current_user["is_superuser"] is False:
153153
raise ForbiddenException("You can only view your own posts")
154154

155155
return post
@@ -187,7 +187,7 @@ from fastapi import HTTPException
187187
# Bad Request (400)
188188
@router.post("/")
189189
async def create_something(data: dict):
190-
if not data.get("required_field"):
190+
if data.get("required_field") is None:
191191
raise HTTPException(
192192
status_code=400,
193193
detail="required_field is missing"
@@ -196,7 +196,7 @@ async def create_something(data: dict):
196196
# Too Many Requests (429)
197197
@router.post("/")
198198
async def rate_limited_endpoint():
199-
if rate_limit_exceeded():
199+
if rate_limit_exceeded() is True:
200200
raise HTTPException(
201201
status_code=429,
202202
detail="Rate limit exceeded. Try again later."
@@ -315,13 +315,13 @@ async def login(credentials: LoginCredentials):
315315
user = await crud_users.get(db=db, username=credentials.username)
316316

317317
# Don't do this - reveals if username exists
318-
# if not user:
318+
# if user is None:
319319
# raise NotFoundException("User not found")
320-
# if not verify_password(credentials.password, user.hashed_password):
320+
# if verify_password(credentials.password, user.hashed_password) is False:
321321
# raise UnauthorizedException("Invalid password")
322322

323323
# Do this - generic message for all auth failures
324-
if not user or not verify_password(credentials.password, user.hashed_password):
324+
if user is None or verify_password(credentials.password, user.hashed_password) is False:
325325
raise UnauthorizedException("Invalid username or password")
326326

327327
return create_access_token(user.id)
@@ -332,11 +332,11 @@ async def forgot_password(email: str):
332332
user = await crud_users.get(db=db, email=email)
333333

334334
# Don't do this - reveals if email exists
335-
# if not user:
335+
# if user is None:
336336
# raise NotFoundException("Email not found")
337337

338338
# Do this - always return success message
339-
if user:
339+
if user is True:
340340
await send_password_reset_email(user.email)
341341

342342
# Always return the same message
@@ -355,7 +355,7 @@ async def get_post(
355355
current_user: Annotated[dict, Depends(get_current_user)]
356356
):
357357
post = await crud_posts.get(db=db, id=post_id)
358-
if not post:
358+
if post is None:
359359
raise NotFoundException("Post not found") # Safe to be specific
360360

361361
if post.author_id != current_user["id"]:
@@ -370,7 +370,7 @@ async def get_post(
370370
### 1. Use Specific Exceptions (When Safe)
371371
```python
372372
# Good for non-sensitive operations
373-
if not user:
373+
if user is None:
374374
raise NotFoundException("User not found")
375375

376376
# Good for validation errors
@@ -398,7 +398,7 @@ async def delete_user(
398398
raise ForbiddenException("Cannot delete other users")
399399

400400
# Then check if user exists
401-
if not await crud_users.exists(db=db, id=user_id):
401+
if await crud_users.exists(db=db, id=user_id) is False:
402402
raise NotFoundException("User not found")
403403

404404
await crud_users.delete(db=db, id=user_id)

docs/user-guide/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ from app.core.exceptions.http_exceptions import NotFoundException
7373
@router.get("/{user_id}")
7474
async def get_user(user_id: int):
7575
user = await crud_users.get(id=user_id)
76-
if not user:
76+
if user is None:
7777
raise NotFoundException("User not found") # Returns proper 404
7878
return user
7979
```

docs/user-guide/api/pagination.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async def get_users(
7373
):
7474
# Build filters
7575
filters = {}
76-
if search:
76+
if search is True:
7777
filters["name__icontains"] = search # Search by name
7878
if is_active is not None:
7979
filters["is_active"] = is_active
@@ -185,12 +185,12 @@ async def get_users(
185185

186186
if is_active is not None:
187187
filters["is_active"] = is_active
188-
if tier_id:
188+
if tier_id is True:
189189
filters["tier_id"] = tier_id
190190

191191
# Handle search
192192
search_criteria = []
193-
if search:
193+
if search is True:
194194
from sqlalchemy import or_, func
195195
search_criteria = [
196196
or_(
@@ -280,7 +280,7 @@ async def get_all_users_admin(
280280
db: Annotated[AsyncSession, Depends(async_get_db)]
281281
):
282282
filters = {}
283-
if not include_deleted:
283+
if include_deleted is False:
284284
filters["is_deleted"] = False
285285

286286
users = await crud_users.get_multi(db=db, **filters)

docs/user-guide/api/versioning.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ import logging
238238
logger = logging.getLogger(__name__)
239239

240240
async def version_tracking_middleware(request: Request, call_next):
241-
if request.url.path.startswith("/api/v1/"):
241+
if request.url.path.startswith("/api/v1/") is True:
242242
logger.info(f"v1 usage: {request.method} {request.url.path}")
243-
elif request.url.path.startswith("/api/v2/"):
243+
elif request.url.path.startswith("/api/v2/") is True:
244244
logger.info(f"v2 usage: {request.method} {request.url.path}")
245245

246246
response = await call_next(request)

0 commit comments

Comments
 (0)