Skip to content

Commit

Permalink
Remove typing Union & Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
injoonH committed Jul 20, 2023
1 parent 9efd9e7 commit 6778ff2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 34 deletions.
9 changes: 5 additions & 4 deletions apps/core/models/article.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import typing
from enum import Enum
from typing import Dict, Union

import bs4
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.files.storage import default_storage
from django.db import IntegrityError, models, transaction
from django.utils import timezone
Expand All @@ -27,6 +26,8 @@
from .communication_article import SchoolResponseStatus
from .report import Report

User = get_user_model()


class ArticleHiddenReason(str, Enum):
ADULT_CONTENT = "ADULT_CONTENT"
Expand Down Expand Up @@ -261,7 +262,7 @@ def created_by_nickname(self):

# API 상에서 보이는 사용자 (익명일 경우 익명화된 글쓴이, 그 외는 그냥 글쓴이)
@cached_property
def postprocessed_created_by(self) -> Union[settings.AUTH_USER_MODEL, Dict]:
def postprocessed_created_by(self) -> User | dict:
if self.name_type == NameType.REGULAR:
return self.created_by

Expand Down Expand Up @@ -295,7 +296,7 @@ def postprocessed_created_by(self) -> Union[settings.AUTH_USER_MODEL, Dict]:
}

@cache_by_user
def hidden_reasons(self, user: settings.AUTH_USER_MODEL) -> typing.List:
def hidden_reasons(self, user: User) -> list:
reasons = []
if self.is_hidden_by_reported():
reasons.append(ArticleHiddenReason.REPORTED_CONTENT)
Expand Down
11 changes: 6 additions & 5 deletions apps/core/models/comment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import hashlib
import typing
from enum import Enum
from typing import Dict, Union

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.files.storage import default_storage
from django.db import IntegrityError, models, transaction
from django.utils import timezone
Expand All @@ -20,6 +19,8 @@
from .board import NameType
from .report import Report

User = get_user_model()


class CommentHiddenReason(Enum):
REPORTED_CONTENT = "REPORTED_CONTENT"
Expand Down Expand Up @@ -166,7 +167,7 @@ def update_report_count(self):

# API 상에서 보이는 사용자 (익명일 경우 익명화된 글쓴이, 그 외는 그냥 글쓴이)
@cached_property
def postprocessed_created_by(self) -> Union[settings.AUTH_USER_MODEL, Dict]:
def postprocessed_created_by(self) -> User | dict:
if self.name_type == NameType.REGULAR:
return self.created_by

Expand Down Expand Up @@ -221,8 +222,8 @@ def postprocessed_created_by(self) -> Union[settings.AUTH_USER_MODEL, Dict]:
}

@cache_by_user
def hidden_reasons(self, user: settings.AUTH_USER_MODEL) -> typing.List:
reasons: typing.List[CommentHiddenReason] = []
def hidden_reasons(self, user: User) -> list[CommentHiddenReason]:
reasons: list[CommentHiddenReason] = []

if self.is_deleted():
reasons.append(CommentHiddenReason.DELETED_CONTENT)
Expand Down
13 changes: 6 additions & 7 deletions apps/core/serializers/article.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing
from enum import Enum

from django.utils.translation import gettext
Expand Down Expand Up @@ -37,7 +36,7 @@ class Meta:
"migrated_negative_vote_count",
)

def get_my_vote(self, obj) -> typing.Optional[bool]:
def get_my_vote(self, obj) -> bool | None:
request = self.context["request"]
if not obj.vote_set.filter(voted_by=request.user).exists():
return None
Expand All @@ -47,7 +46,7 @@ def get_my_vote(self, obj) -> typing.Optional[bool]:
return my_vote.is_positive

@staticmethod
def get_my_scrap(obj) -> typing.Optional[dict]:
def get_my_scrap(obj) -> dict | None:
from apps.core.serializers.scrap import BaseScrapSerializer

if not obj.scrap_set.exists():
Expand All @@ -57,12 +56,12 @@ def get_my_scrap(obj) -> typing.Optional[dict]:

return BaseScrapSerializer(my_scrap).data

def get_title(self, obj) -> typing.Optional[str]:
def get_title(self, obj) -> str | None:
if self.visible_verdict(obj):
return obj.title
return None

def get_content(self, obj) -> typing.Optional[str]:
def get_content(self, obj) -> str | None:
if self.visible_verdict(obj):
return obj.content
return None
Expand Down Expand Up @@ -99,7 +98,7 @@ def get_read_status(obj) -> str:
return "-"

# TODO: article_current_page property must be cached
def get_article_current_page(self, obj) -> typing.Optional[int]:
def get_article_current_page(self, obj) -> int | None:
view = self.context.get("view")

if view:
Expand Down Expand Up @@ -316,7 +315,7 @@ def get_side_articles_of_recent_article(self, obj, request):
after = None if len(after) == 0 else after[0]
return after, before

def get_attachments(self, obj): # -> typing.Optional[list]:
def get_attachments(self, obj) -> list | None:
if self.visible_verdict(obj):
return obj.attachments.all().values_list("id")
return None
Expand Down
6 changes: 2 additions & 4 deletions apps/core/serializers/comment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import typing

from rest_framework import serializers

from apps.core.models import Block, Comment, CommentHiddenReason
Expand All @@ -22,15 +20,15 @@ class Meta:
exclude = ("attachment",)

@staticmethod
def get_my_vote(obj) -> typing.Optional[bool]:
def get_my_vote(obj) -> bool | None:
if not obj.vote_set.exists():
return None

my_vote = obj.vote_set.all()[0]

return my_vote.is_positive

def get_content(self, obj) -> typing.Optional[str]:
def get_content(self, obj) -> str | None:
if self.visible_verdict(obj):
return obj.content
return None
Expand Down
8 changes: 3 additions & 5 deletions apps/core/serializers/mixins/hidden.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import typing

from rest_framework import serializers


Expand All @@ -12,11 +10,11 @@ def get_is_mine(self, obj) -> bool:
def get_is_hidden(self, obj) -> bool:
return not self.visible_verdict(obj)

def get_why_hidden(self, obj) -> typing.List[str]:
def get_why_hidden(self, obj) -> list[str]:
_, _, reasons = self.hidden_info(obj)
return [reason.value for reason in reasons]

def get_can_override_hidden(self, obj) -> typing.Optional[bool]:
def get_can_override_hidden(self, obj) -> bool | None:
hidden, can_override, _ = self.hidden_info(obj)
if not hidden:
return
Expand All @@ -33,7 +31,7 @@ def requested_override_hidden(self):
and self.context["override_hidden"] is True
)

def hidden_info(self, obj) -> typing.Tuple[bool, bool, typing.List]:
def hidden_info(self, obj) -> tuple[bool, bool, list]:
user = self.context["request"].user
reasons = obj.hidden_reasons(user)
cannot_override_reasons = [
Expand Down
4 changes: 1 addition & 3 deletions apps/core/serializers/notification.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import typing

from rest_framework import serializers

from apps.core.models import Notification
Expand All @@ -11,7 +9,7 @@ class Meta:
model = Notification
fields = "__all__"

def get_is_read(self, obj) -> typing.Optional[bool]:
def get_is_read(self, obj) -> bool | None:
if not obj.notification_read_log_set.exists():
return None

Expand Down
4 changes: 1 addition & 3 deletions apps/user/serializers/user_profile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import typing

from dateutil.relativedelta import relativedelta
from django.utils import timezone
from django.utils.translation import gettext
Expand All @@ -18,7 +16,7 @@ class Meta:
fields = "__all__"

@staticmethod
def get_email(obj) -> typing.Optional[str]:
def get_email(obj) -> str | None:
if obj.email.endswith("@sso.sparcs.org"):
return None
return obj.email
Expand Down
4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Python 3.5 이후로는 pytest-django를 쓸 때 module-scope fixture에서 DB접근이 안되기 때문에 class-scope fixture 사용
https://github.com/pytest-dev/pytest-django/issues/53#issuecomment-407073682
"""
from typing import List

import pytest
from django.contrib.auth import get_user_model
from django.test import TestCase as DjangoTestCase
Expand Down Expand Up @@ -239,5 +237,5 @@ def create_users(
cls,
num: int,
group: UserProfile.UserGroup = UserProfile.UserGroup.KAIST_MEMBER,
) -> List[User]:
) -> list[User]:
return [cls.create_user_with_index(idx, group) for idx in range(num)]

0 comments on commit 6778ff2

Please sign in to comment.