Conversation
| print("\033[31m2. --- 校验失败处理 ---\033[0m") | ||
| try: | ||
| User(id=1, signup_ts=datetime.today(), friends=[1, 2, "not number"]) | ||
| User(id=1, signup_ts=datetime.now(), friends=[1, 2, "not number"]) |
There was a problem hiding this comment.
Lines 49-49 refactored with the following changes:
- Replace datetime.datetime.today() with datetime.datetime.now() (
use-datetime-now-not-today)
| def create_city(city: schemas.CreateCity, db: Session = Depends(get_db)): | ||
| db_city = crud.get_city_by_name(db, name=city.province) | ||
| if db_city: | ||
| if db_city := crud.get_city_by_name(db, name=city.province): |
There was a problem hiding this comment.
Function create_city refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| cities = crud.get_cities(db, skip=skip, limit=limit) | ||
| return cities | ||
| return crud.get_cities(db, skip=skip, limit=limit) |
There was a problem hiding this comment.
Function get_cities refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| data = crud.get_data(db, city=city, skip=skip, limit=limit) | ||
| return data | ||
| return crud.get_data(db, city=city, skip=skip, limit=limit) |
There was a problem hiding this comment.
Function get_data refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if 200 == city_data.status_code: | ||
| if city_data.status_code == 200: |
There was a problem hiding this comment.
Function bg_task refactored with the following changes:
- Ensure constant in comparison is on the right [×2] (
flip-comparison)
| user = UserInDB(**user_dict) | ||
| hashed_password = fake_hash_password(form_data.password) | ||
| if not hashed_password == user.hashed_password: | ||
| if hashed_password != user.hashed_password: |
There was a problem hiding this comment.
Function login refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| def fake_decode_token(token: str): | ||
| user = get_user(fake_users_db, token) | ||
| return user | ||
| return get_user(fake_users_db, token) |
There was a problem hiding this comment.
Function fake_decode_token refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| user = fake_decode_token(token) | ||
| if not user: | ||
| if user := fake_decode_token(token): | ||
| return user | ||
| else: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||
| detail="Invalid authentication credentials", | ||
| headers={"WWW-Authenticate": "Bearer"}, # OAuth2的规范,如果认证失败,请求头中返回“WWW-Authenticate” | ||
| ) | ||
| return user |
There was a problem hiding this comment.
Function get_current_user refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches)
| user = jwt_get_user(db=db, username=username) | ||
| if not user: | ||
| return False | ||
| if not verity_password(plain_password=password, hashed_password=user.hashed_password): | ||
| if user := jwt_get_user(db=db, username=username): | ||
| return ( | ||
| False | ||
| if not verity_password( | ||
| plain_password=password, hashed_password=user.hashed_password | ||
| ) | ||
| else user | ||
| ) | ||
| else: | ||
| return False | ||
| return user |
There was a problem hiding this comment.
Function jwt_authenticate_user refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Lift code into else after jump in control flow [×2] (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches) - Replace if statement with if expression (
assign-if-exp)
| to_encode.update({"exp": expire}) | ||
| encoded_jwt = jwt.encode(claims=to_encode, key=SECRET_KEY, algorithm=ALGORITHM) | ||
| return encoded_jwt | ||
| to_encode["exp"] = expire | ||
| return jwt.encode(claims=to_encode, key=SECRET_KEY, algorithm=ALGORITHM) |
There was a problem hiding this comment.
Function create_access_token refactored with the following changes:
- Add single value to dictionary directly rather than using update() (
simplify-dictionary-update) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
Branch
masterrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run:Help us improve this pull request!