@@ -12,7 +12,7 @@ from app.core.exceptions.http_exceptions import NotFoundException
1212@router.get (" /{user_id} " )
1313async 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} " )
3131async 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 (" /" )
4747async 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" )
8484async 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)
101101async 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 (" /" )
189189async 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 (" /" )
198198async 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)
0 commit comments