refactor: Replace all __unicode__ method with __str__.

Close #6627.
This commit is contained in:
rht 2017-10-27 09:06:40 +02:00 committed by showell
parent e51d98cd96
commit dcc831f767
9 changed files with 54 additions and 54 deletions

View File

@ -46,9 +46,9 @@ class CountStat(object):
else: # frequency == CountStat.DAY
self.interval = timedelta(days=1)
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<CountStat: %s>" % (self.property,)
return "<CountStat: %s>" % (self.property,)
class LoggingCountStat(CountStat):
def __init__(self, property, output_table, frequency):

View File

@ -18,9 +18,9 @@ class FillState(models.Model):
last_modified = models.DateTimeField(auto_now=True) # type: datetime.datetime
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<FillState: %s %s %s>" % (self.property, self.end_time, self.state)
return "<FillState: %s %s %s>" % (self.property, self.end_time, self.state)
# The earliest/starting end_time in FillState
# We assume there is at least one realm
@ -42,9 +42,9 @@ def last_successful_fill(property):
class Anomaly(models.Model):
info = models.CharField(max_length=1000) # type: Text
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<Anomaly: %s... %s>" % (self.info, self.id)
return "<Anomaly: %s... %s>" % (self.info, self.id)
class BaseCount(models.Model):
# Note: When inheriting from BaseCount, you may want to rearrange
@ -64,9 +64,9 @@ class InstallationCount(BaseCount):
class Meta(object):
unique_together = ("property", "subgroup", "end_time")
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<InstallationCount: %s %s %s>" % (self.property, self.subgroup, self.value)
return "<InstallationCount: %s %s %s>" % (self.property, self.subgroup, self.value)
class RealmCount(BaseCount):
realm = models.ForeignKey(Realm)
@ -75,9 +75,9 @@ class RealmCount(BaseCount):
unique_together = ("realm", "property", "subgroup", "end_time")
index_together = ["property", "end_time"]
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<RealmCount: %s %s %s %s>" % (self.realm, self.property, self.subgroup, self.value)
return "<RealmCount: %s %s %s %s>" % (self.realm, self.property, self.subgroup, self.value)
class UserCount(BaseCount):
user = models.ForeignKey(UserProfile)
@ -89,9 +89,9 @@ class UserCount(BaseCount):
# aggregating from users to realms
index_together = ["property", "realm", "end_time"]
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<UserCount: %s %s %s %s>" % (self.user, self.property, self.subgroup, self.value)
return "<UserCount: %s %s %s %s>" % (self.user, self.property, self.subgroup, self.value)
class StreamCount(BaseCount):
stream = models.ForeignKey(Stream)
@ -103,6 +103,6 @@ class StreamCount(BaseCount):
# aggregating from streams to realms
index_together = ["property", "realm", "end_time"]
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<StreamCount: %s %s %s %s %s>" % (self.stream, self.property, self.subgroup, self.value, self.id)
return "<StreamCount: %s %s %s %s %s>" % (self.stream, self.property, self.subgroup, self.value, self.id)

View File

@ -98,7 +98,7 @@ class Confirmation(models.Model):
MULTIUSE_INVITE = 6
type = models.PositiveSmallIntegerField() # type: int
def __unicode__(self):
def __str__(self):
# type: () -> Text
return '<Confirmation: %s>' % (self.content_object,)

View File

@ -9,8 +9,8 @@ exclude_lines =
raise NotImplementedError()
# Don't require coverage for test suite AssertionError -- they're usually for clarity
raise AssertionError
# Don't require coverage for __unicode__ statements just used for printing
def __unicode__[(]self[)]:
# Don't require coverage for __str__ statements just used for printing
def __str__[(]self[)]:
[run]
omit =

View File

@ -222,9 +222,9 @@ class Realm(models.Model):
ret[k] = v
return ret
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<Realm: %s %s>" % (self.string_id, self.id)
return "<Realm: %s %s>" % (self.string_id, self.id)
@cache_with_key(get_realm_emoji_cache_key, timeout=3600*24*7)
def get_emoji(self):
@ -383,9 +383,9 @@ class RealmEmoji(models.Model):
class Meta(object):
unique_together = ("realm", "name")
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<RealmEmoji(%s): %s %s>" % (self.realm.string_id, self.name, self.file_name)
return "<RealmEmoji(%s): %s %s>" % (self.realm.string_id, self.name, self.file_name)
def get_realm_emoji_uncached(realm):
# type: (Realm) -> Dict[Text, Dict[str, Any]]
@ -442,9 +442,9 @@ class RealmFilter(models.Model):
class Meta(object):
unique_together = ("realm", "pattern")
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<RealmFilter(%s): %s %s>" % (self.realm.string_id, self.pattern, self.url_format_string)
return "<RealmFilter(%s): %s %s>" % (self.realm.string_id, self.pattern, self.url_format_string)
def get_realm_filters_cache_key(realm_id):
# type: (int) -> Text
@ -703,9 +703,9 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
else:
return False
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<UserProfile: %s %s>" % (self.email, self.realm)
return "<UserProfile: %s %s>" % (self.email, self.realm)
@property
def is_incoming_webhook(self):
@ -890,9 +890,9 @@ class Stream(models.Model):
date_created = models.DateTimeField(default=timezone_now) # type: datetime.datetime
deactivated = models.BooleanField(default=False) # type: bool
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<Stream: %s>" % (self.name,)
return "<Stream: %s>" % (self.name,)
def is_public(self):
# type: () -> bool
@ -942,10 +942,10 @@ class Recipient(models.Model):
# Raises KeyError if invalid
return self._type_names[self.type]
def __unicode__(self):
def __str__(self):
# type: () -> Text
display_recipient = get_display_recipient(self)
return u"<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type)
return "<Recipient: %s (%d, %s)>" % (display_recipient, self.type_id, self.type)
class MutedTopic(models.Model):
user_profile = models.ForeignKey(UserProfile, on_delete=CASCADE)
@ -956,16 +956,16 @@ class MutedTopic(models.Model):
class Meta(object):
unique_together = ('user_profile', 'stream', 'topic_name')
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<MutedTopic: (%s, %s, %s)>" % (self.user_profile.email, self.stream.name, self.topic_name)
return "<MutedTopic: (%s, %s, %s)>" % (self.user_profile.email, self.stream.name, self.topic_name)
class Client(models.Model):
name = models.CharField(max_length=30, db_index=True, unique=True) # type: Text
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<Client: %s>" % (self.name,)
return "<Client: %s>" % (self.name,)
get_client_cache = {} # type: Dict[Text, Client]
def get_client(name):
@ -1123,10 +1123,10 @@ class AbstractMessage(models.Model):
class Meta(object):
abstract = True
def __unicode__(self):
def __str__(self):
# type: () -> Text
display_recipient = get_display_recipient(self.recipient)
return u"<%s: %s / %s / %r>" % (self.__class__.__name__, display_recipient,
return "<%s: %s / %s / %r>" % (self.__class__.__name__, display_recipient,
self.subject, self.sender)
@ -1330,10 +1330,10 @@ class AbstractUserMessage(models.Model):
if flags & (2 ** i)
]
def __unicode__(self):
def __str__(self):
# type: () -> Text
display_recipient = get_display_recipient(self.message.recipient)
return u"<%s: %s / %s (%s)>" % (self.__class__.__name__, display_recipient,
return "<%s: %s / %s (%s)>" % (self.__class__.__name__, display_recipient,
self.user_profile.email, self.flags_list())
@ -1373,9 +1373,9 @@ class AbstractAttachment(models.Model):
class Meta(object):
abstract = True
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<%s: %s>" % (self.__class__.__name__, self.file_name,)
return "<%s: %s>" % (self.__class__.__name__, self.file_name,)
class ArchivedAttachment(AbstractAttachment):
@ -1455,9 +1455,9 @@ class Subscription(models.Model):
class Meta(object):
unique_together = ("user_profile", "recipient")
def __unicode__(self):
def __str__(self):
# type: () -> Text
return u"<Subscription: %r -> %s>" % (self.user_profile, self.recipient)
return "<Subscription: %r -> %s>" % (self.user_profile, self.recipient)
@cache_with_key(user_profile_by_id_cache_key, timeout=3600*24*7)
def get_user_profile_by_id(uid):
@ -1839,7 +1839,7 @@ class ScheduledEmail(AbstractScheduledJob):
def __str__(self):
# type: () -> Text
return u"<ScheduledEmail: %s %s %s>" % (self.type, self.user or self.address,
return "<ScheduledEmail: %s %s %s>" % (self.type, self.user or self.address,
self.scheduled_timestamp)
EMAIL_TYPES = {
@ -1862,12 +1862,12 @@ class RealmAuditLog(models.Model):
backfilled = models.BooleanField(default=False) # type: bool
extra_data = models.TextField(null=True) # type: Optional[Text]
def __unicode__(self):
def __str__(self):
# type: () -> str
if self.modified_user is not None:
return u"<RealmAuditLog: %s %s %s>" % (self.modified_user, self.event_type, self.event_time)
return "<RealmAuditLog: %s %s %s>" % (self.modified_user, self.event_type, self.event_time)
if self.modified_stream is not None:
return u"<RealmAuditLog: %s %s %s>" % (self.modified_stream, self.event_type, self.event_time)
return "<RealmAuditLog: %s %s %s>" % (self.modified_stream, self.event_type, self.event_time)
return "<RealmAuditLog: %s %s %s>" % (self.realm, self.event_type, self.event_time)
class UserHotspot(models.Model):

View File

@ -623,7 +623,7 @@ class BugdownTest(ZulipTestCase):
url_format_string=url_format_string)
realm_filter.save()
self.assertEqual(
realm_filter.__unicode__(),
realm_filter.__str__(),
'<RealmFilter(zulip): #(?P<id>[0-9]{2,8})'
' https://trac.zulip.net/ticket/%(id)s>')

View File

@ -328,12 +328,12 @@ class PersonalMessagesTest(ZulipTestCase):
with mock.patch('zerver.models.get_display_recipient', return_value='recip'):
self.assertEqual(str(message),
u'<Message: recip / / '
'<Message: recip / / '
'<UserProfile: test@zulip.com <Realm: zulip 1>>>')
user_message = most_recent_usermessage(user_profile)
self.assertEqual(str(user_message),
u'<UserMessage: recip / test@zulip.com ([])>'
'<UserMessage: recip / test@zulip.com ([])>'
)
@slow("checks several profiles")

View File

@ -53,10 +53,10 @@ class TestClientModel(ZulipTestCase):
def test_client_stringification(self):
# type: () -> None
'''
This test is designed to cover __unicode__ method for Client.
This test is designed to cover __str__ method for Client.
'''
client = make_client('some_client')
self.assertEqual(str(client), u'<Client: some_client>')
self.assertEqual(str(client), '<Client: some_client>')
class UserPresenceModelTests(ZulipTestCase):
def test_date_logic(self):

View File

@ -2146,7 +2146,7 @@ class SubscriptionAPITest(ZulipTestCase):
self.assert_adding_subscriptions_for_principal(invitee_email, invitee_realm, invite_streams)
subscription = self.get_subscription(user_profile, invite_streams[0])
with mock.patch('zerver.models.Recipient.__unicode__', return_value='recip'):
with mock.patch('zerver.models.Recipient.__str__', return_value='recip'):
self.assertEqual(str(subscription),
u'<Subscription: '
'<UserProfile: %s <Realm: zulip 1>> -> recip>' % (self.example_email('iago'),))