Skip to content

Commit 5bb730c

Browse files
committed
Upgrade code with pyupgrade
1 parent 8839cff commit 5bb730c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+160
-379
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ repos:
1616
additional_dependencies: [toml]
1717
exclude: ^.*/?setup\.py$
1818

19+
- repo: https://github.com/asottile/pyupgrade
20+
rev: v2.29.0
21+
hooks:
22+
- id: pyupgrade
23+
1924
- repo: https://github.com/pre-commit/pre-commit-hooks
2025
rev: v4.0.1
2126
hooks:

clock

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
from __future__ import unicode_literals
32

43
import glob
54
import json
@@ -41,7 +40,7 @@ class _LambdaCompiler(_GettextCompiler):
4140
code = code.replace("||", "or")
4241
if method == "in":
4342
expr = self.compile(expr)
44-
code = "(%s == %s and %s)" % (expr, expr, code)
43+
code = f"({expr} == {expr} and {code})"
4544
return code
4645

4746

@@ -52,10 +51,7 @@ class LocaleCreate(Command):
5251

5352
arguments = [argument("locales", "Locales to dump.", optional=False, multiple=True)]
5453

55-
TEMPLATE = """# -*- coding: utf-8 -*-
56-
from __future__ import unicode_literals
57-
58-
from .custom import translations as custom_translations
54+
TEMPLATE = """from .custom import translations as custom_translations
5955
6056
6157
\"\"\"
@@ -73,11 +69,7 @@ locale = {{
7369
}}
7470
"""
7571

76-
CUSTOM_TEMPLATE = """# -*- coding: utf-8 -*-
77-
from __future__ import unicode_literals
78-
79-
80-
\"\"\"
72+
CUSTOM_TEMPLATE = """\"\"\"
8173
{locale} custom locale file.
8274
\"\"\"
8375
@@ -99,10 +91,10 @@ translations = {{}}
9991

10092
normalized = normalize_locale(locale.replace("-", "_"))
10193
if not normalized:
102-
self.line("<error>Locale [{}] does not exist.</error>".format(locale))
94+
self.line(f"<error>Locale [{locale}] does not exist.</error>")
10395
continue
10496

105-
self.line("<info>Generating <comment>{}</> locale.</>".format(locale))
97+
self.line(f"<info>Generating <comment>{locale}</> locale.</>")
10698

10799
content = LocaleDataDict(load(normalized))
108100

@@ -140,7 +132,7 @@ translations = {{}}
140132
]
141133
data["units"] = {}
142134
for unit in units:
143-
pattern = patterns["duration-{}".format(unit)]["long"]
135+
pattern = patterns[f"duration-{unit}"]["long"]
144136
if "per" in pattern:
145137
del pattern["per"]
146138

@@ -199,7 +191,7 @@ translations = {{}}
199191
else:
200192
v = repr(v)
201193

202-
s.append("%s%r: %s,\n" % (" " * tab, k, v))
194+
s.append("{}{!r}: {},\n".format(" " * tab, k, v))
203195
s.append("%s}" % (" " * (tab - 1)))
204196

205197
return "".join(s)
@@ -208,7 +200,7 @@ translations = {{}}
208200
to_py = _LambdaCompiler().compile
209201
result = ["lambda n: "]
210202
for tag, ast in PluralRule.parse(rule).abstract:
211-
result.append("'%s' if %s else " % (tag, to_py(ast)))
203+
result.append("'{}' if {} else ".format(tag, to_py(ast)))
212204
result.append("'other'")
213205
return "".join(result)
214206

pendulum/date.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
41
import calendar
52
import math
63

@@ -399,9 +396,9 @@ def start_of(self, unit):
399396
:rtype: Date
400397
"""
401398
if unit not in self._MODIFIERS_VALID_UNITS:
402-
raise ValueError('Invalid unit "{}" for start_of()'.format(unit))
399+
raise ValueError(f'Invalid unit "{unit}" for start_of()')
403400

404-
return getattr(self, "_start_of_{}".format(unit))()
401+
return getattr(self, f"_start_of_{unit}")()
405402

406403
def end_of(self, unit):
407404
"""
@@ -603,9 +600,9 @@ def first_of(self, unit, day_of_week=None):
603600
:rtype: Date
604601
"""
605602
if unit not in ["month", "quarter", "year"]:
606-
raise ValueError('Invalid unit "{}" for first_of()'.format(unit))
603+
raise ValueError(f'Invalid unit "{unit}" for first_of()')
607604

608-
return getattr(self, "_first_of_{}".format(unit))(day_of_week)
605+
return getattr(self, f"_first_of_{unit}")(day_of_week)
609606

610607
def last_of(self, unit, day_of_week=None):
611608
"""
@@ -624,9 +621,9 @@ def last_of(self, unit, day_of_week=None):
624621
:rtype: Date
625622
"""
626623
if unit not in ["month", "quarter", "year"]:
627-
raise ValueError('Invalid unit "{}" for first_of()'.format(unit))
624+
raise ValueError(f'Invalid unit "{unit}" for first_of()')
628625

629-
return getattr(self, "_last_of_{}".format(unit))(day_of_week)
626+
return getattr(self, f"_last_of_{unit}")(day_of_week)
630627

631628
def nth_of(self, unit, nth, day_of_week):
632629
"""
@@ -648,9 +645,9 @@ def nth_of(self, unit, nth, day_of_week):
648645
:rtype: Date
649646
"""
650647
if unit not in ["month", "quarter", "year"]:
651-
raise ValueError('Invalid unit "{}" for first_of()'.format(unit))
648+
raise ValueError(f'Invalid unit "{unit}" for first_of()')
652649

653-
dt = getattr(self, "_nth_of_{}".format(unit))(nth, day_of_week)
650+
dt = getattr(self, f"_nth_of_{unit}")(nth, day_of_week)
654651
if dt is False:
655652
raise PendulumException(
656653
"Unable to find occurence {} of {} in {}".format(
@@ -873,13 +870,13 @@ def today(cls):
873870

874871
@classmethod
875872
def fromtimestamp(cls, t):
876-
dt = super(Date, cls).fromtimestamp(t)
873+
dt = super().fromtimestamp(t)
877874

878875
return cls(dt.year, dt.month, dt.day)
879876

880877
@classmethod
881878
def fromordinal(cls, n):
882-
dt = super(Date, cls).fromordinal(n)
879+
dt = super().fromordinal(n)
883880

884881
return cls(dt.year, dt.month, dt.day)
885882

pendulum/datetime.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
from __future__ import absolute_import
3-
from __future__ import division
4-
51
import calendar
62
import datetime
73

@@ -350,7 +346,7 @@ def _to_string(self, fmt: str, locale: Optional[str] = None) -> str:
350346
Format the instance to a common string format.
351347
"""
352348
if fmt not in self._FORMATS:
353-
raise ValueError("Format [{}] is not supported".format(fmt))
349+
raise ValueError(f"Format [{fmt}] is not supported")
354350

355351
fmt = self._FORMATS[fmt]
356352
if callable(fmt):
@@ -364,7 +360,7 @@ def __str__(self) -> str:
364360
def __repr__(self) -> str:
365361
us = ""
366362
if self.microsecond:
367-
us = ", {}".format(self.microsecond)
363+
us = f", {self.microsecond}"
368364

369365
repr_ = "{klass}(" "{year}, {month}, {day}, " "{hour}, {minute}, {second}{us}"
370366

@@ -665,9 +661,9 @@ def start_of(self, unit: str) -> "DateTime":
665661
* century: date to first day of century and time to 00:00:00
666662
"""
667663
if unit not in self._MODIFIERS_VALID_UNITS:
668-
raise ValueError('Invalid unit "{}" for start_of()'.format(unit))
664+
raise ValueError(f'Invalid unit "{unit}" for start_of()')
669665

670-
return getattr(self, "_start_of_{}".format(unit))()
666+
return getattr(self, f"_start_of_{unit}")()
671667

672668
def end_of(self, unit: str) -> "DateTime":
673669
"""
@@ -884,9 +880,9 @@ def first_of(self, unit: str, day_of_week: Optional[int] = None) -> "DateTime":
884880
Supported units are month, quarter and year.
885881
"""
886882
if unit not in ["month", "quarter", "year"]:
887-
raise ValueError('Invalid unit "{}" for first_of()'.format(unit))
883+
raise ValueError(f'Invalid unit "{unit}" for first_of()')
888884

889-
return getattr(self, "_first_of_{}".format(unit))(day_of_week)
885+
return getattr(self, f"_first_of_{unit}")(day_of_week)
890886

891887
def last_of(self, unit: str, day_of_week: Optional[int] = None) -> "DateTime":
892888
"""
@@ -898,9 +894,9 @@ def last_of(self, unit: str, day_of_week: Optional[int] = None) -> "DateTime":
898894
Supported units are month, quarter and year.
899895
"""
900896
if unit not in ["month", "quarter", "year"]:
901-
raise ValueError('Invalid unit "{}" for first_of()'.format(unit))
897+
raise ValueError(f'Invalid unit "{unit}" for first_of()')
902898

903-
return getattr(self, "_last_of_{}".format(unit))(day_of_week)
899+
return getattr(self, f"_last_of_{unit}")(day_of_week)
904900

905901
def nth_of(self, unit: str, nth: int, day_of_week: int) -> "DateTime":
906902
"""
@@ -913,9 +909,9 @@ def nth_of(self, unit: str, nth: int, day_of_week: int) -> "DateTime":
913909
Supported units are month, quarter and year.
914910
"""
915911
if unit not in ["month", "quarter", "year"]:
916-
raise ValueError('Invalid unit "{}" for first_of()'.format(unit))
912+
raise ValueError(f'Invalid unit "{unit}" for first_of()')
917913

918-
dt = getattr(self, "_nth_of_{}".format(unit))(nth, day_of_week)
914+
dt = getattr(self, f"_nth_of_{unit}")(nth, day_of_week)
919915
if dt is False:
920916
raise PendulumException(
921917
"Unable to find occurence {} of {} in {}".format(

pendulum/duration.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
41
from datetime import timedelta
52

63
import pendulum
@@ -280,31 +277,31 @@ def __str__(self):
280277
return self.in_words()
281278

282279
def __repr__(self):
283-
rep = "{}(".format(self.__class__.__name__)
280+
rep = f"{self.__class__.__name__}("
284281

285282
if self._years:
286-
rep += "years={}, ".format(self._years)
283+
rep += f"years={self._years}, "
287284

288285
if self._months:
289-
rep += "months={}, ".format(self._months)
286+
rep += f"months={self._months}, "
290287

291288
if self._weeks:
292-
rep += "weeks={}, ".format(self._weeks)
289+
rep += f"weeks={self._weeks}, "
293290

294291
if self._days:
295-
rep += "days={}, ".format(self._remaining_days)
292+
rep += f"days={self._remaining_days}, "
296293

297294
if self.hours:
298-
rep += "hours={}, ".format(self.hours)
295+
rep += f"hours={self.hours}, "
299296

300297
if self.minutes:
301-
rep += "minutes={}, ".format(self.minutes)
298+
rep += f"minutes={self.minutes}, "
302299

303300
if self.remaining_seconds:
304-
rep += "seconds={}, ".format(self.remaining_seconds)
301+
rep += f"seconds={self.remaining_seconds}, "
305302

306303
if self.microseconds:
307-
rep += "microseconds={}, ".format(self.microseconds)
304+
rep += f"microseconds={self.microseconds}, "
308305

309306
rep += ")"
310307

pendulum/formatting/difference_formatter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..locales.locale import Locale
66

77

8-
class DifferenceFormatter(object):
8+
class DifferenceFormatter:
99
"""
1010
Handles formatting differences in text.
1111
"""
@@ -105,14 +105,14 @@ def format(
105105
count = 1
106106

107107
if absolute:
108-
key = "translations.units.{}".format(unit)
108+
key = f"translations.units.{unit}"
109109
else:
110110
is_future = diff.invert
111111

112112
if is_now:
113113
# Relative to now, so we can use
114114
# the CLDR data
115-
key = "translations.relative.{}".format(unit)
115+
key = f"translations.relative.{unit}"
116116

117117
if is_future:
118118
key += ".future"
@@ -125,9 +125,9 @@ def format(
125125
# Checking for special pluralization rules
126126
key = "custom.units_relative"
127127
if is_future:
128-
key += ".{}.future".format(unit)
128+
key += f".{unit}.future"
129129
else:
130-
key += ".{}.past".format(unit)
130+
key += f".{unit}.past"
131131

132132
trans = locale.get(key)
133133
if not trans:

0 commit comments

Comments
 (0)