Skip to content

Commit e014199

Browse files
committed
Modernize with pyupgrade and com2ann
1 parent 9eea231 commit e014199

File tree

19 files changed

+203
-204
lines changed

19 files changed

+203
-204
lines changed

clock

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ translations = {{}}
193193
else:
194194
v = repr(v)
195195

196-
s.append("{}{!r}: {},\n".format(" " * tab, k, v))
197-
s.append("%s}" % (" " * (tab - 1)))
196+
s.append(f"{' ' * tab}{k!r}: {v},\n")
197+
s.append(f'{" " * (tab - 1)}}}')
198198

199199
return "".join(s)
200200

201201
def plural_rule_to_lambda(self, rule):
202202
to_py = _LambdaCompiler().compile
203203
result = ["lambda n: "]
204204
for tag, ast in PluralRule.parse(rule).abstract:
205-
result.append("'{}' if {} else ".format(tag, to_py(ast)))
205+
result.append(f"'{tag}' if {to_py(ast)} else ")
206206
result.append("'other'")
207207
return "".join(result)
208208

@@ -217,13 +217,13 @@ translations = {{}}
217217
limit = PATTERN_CHARS[fieldchar]
218218
if limit and fieldnum not in limit:
219219
raise ValueError(
220-
"Invalid length for field: %r" % (fieldchar * fieldnum)
220+
f"Invalid length for field: {(fieldchar * fieldnum)!r}"
221221
)
222222
result.append(
223223
self.TOKENS_MAP.get(fieldchar * fieldnum, fieldchar * fieldnum)
224224
)
225225
else:
226-
raise NotImplementedError("Unknown token type: %s" % tok_type)
226+
raise NotImplementedError(f"Unknown token type: {tok_type}")
227227

228228
return "".join(result)
229229

@@ -258,11 +258,10 @@ class WindowsTzDump(Command):
258258
for name in sorted_names:
259259
tznames[name] = raw_tznames[name]
260260

261-
mapping = json.dumps(tznames, indent=4)
262-
mapping = "windows_timezones = " + mapping.replace('"', "'") + "\n"
261+
mapping = json.dumps(tznames, indent=4).replace('"', "'")
263262

264263
with open(os.path.join(self.MAPPING_DIR, "windows.py"), "w") as f:
265-
f.write(mapping)
264+
f.write(f"windows_timezones = {mapping}\n")
266265

267266

268267
app = Application("clock", __version__)

pendulum/__init__.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
from .tz.timezone import Timezone
5454

5555

56-
_TEST_NOW = None # type: Optional[DateTime]
56+
_TEST_NOW: DateTime | None = None
5757
_LOCALE = "en"
5858
_WEEK_STARTS_AT = MONDAY
5959
_WEEK_ENDS_AT = SUNDAY
@@ -62,7 +62,7 @@
6262

6363

6464
def _safe_timezone(
65-
obj: Optional[Union[str, float, _datetime.tzinfo, Timezone]]
65+
obj: str | float | _datetime.tzinfo | Timezone | None
6666
) -> Timezone:
6767
"""
6868
Creates a timezone instance
@@ -102,8 +102,8 @@ def datetime(
102102
minute: int = 0,
103103
second: int = 0,
104104
microsecond: int = 0,
105-
tz: Optional[Union[str, float, Timezone]] = UTC,
106-
fold: Optional[int] = 1,
105+
tz: str | float | Timezone | None = UTC,
106+
fold: int | None = 1,
107107
raise_on_unknown_times: bool = False,
108108
) -> DateTime:
109109
"""
@@ -148,7 +148,7 @@ def naive(
148148
minute: int = 0,
149149
second: int = 0,
150150
microsecond: int = 0,
151-
fold: Optional[int] = 1,
151+
fold: int | None = 1,
152152
) -> DateTime:
153153
"""
154154
Return a naive DateTime.
@@ -171,7 +171,7 @@ def time(hour: int, minute: int = 0, second: int = 0, microsecond: int = 0) -> T
171171

172172

173173
def instance(
174-
dt: _datetime.datetime, tz: Optional[Union[str, Timezone]] = UTC
174+
dt: _datetime.datetime, tz: str | Timezone | None = UTC
175175
) -> DateTime:
176176
"""
177177
Create a DateTime instance from a datetime one.
@@ -200,28 +200,28 @@ def instance(
200200
)
201201

202202

203-
def now(tz: Optional[Union[str, Timezone]] = None) -> DateTime:
203+
def now(tz: str | Timezone | None = None) -> DateTime:
204204
"""
205205
Get a DateTime instance for the current date and time.
206206
"""
207207
return DateTime.now(tz)
208208

209209

210-
def today(tz: Union[str, Timezone] = "local") -> DateTime:
210+
def today(tz: str | Timezone = "local") -> DateTime:
211211
"""
212212
Create a DateTime instance for today.
213213
"""
214214
return now(tz).start_of("day")
215215

216216

217-
def tomorrow(tz: Union[str, Timezone] = "local") -> DateTime:
217+
def tomorrow(tz: str | Timezone = "local") -> DateTime:
218218
"""
219219
Create a DateTime instance for today.
220220
"""
221221
return today(tz).add(days=1)
222222

223223

224-
def yesterday(tz: Union[str, Timezone] = "local") -> DateTime:
224+
def yesterday(tz: str | Timezone = "local") -> DateTime:
225225
"""
226226
Create a DateTime instance for today.
227227
"""
@@ -231,8 +231,8 @@ def yesterday(tz: Union[str, Timezone] = "local") -> DateTime:
231231
def from_format(
232232
string: str,
233233
fmt: str,
234-
tz: Union[str, Timezone] = UTC,
235-
locale: Optional[str] = None, # noqa
234+
tz: str | Timezone = UTC,
235+
locale: str | None = None, # noqa
236236
) -> DateTime:
237237
"""
238238
Creates a DateTime instance from a specific format.
@@ -245,7 +245,7 @@ def from_format(
245245

246246

247247
def from_timestamp(
248-
timestamp: Union[int, float], tz: Union[str, Timezone] = UTC
248+
timestamp: int | float, tz: str | Timezone = UTC
249249
) -> DateTime:
250250
"""
251251
Create a DateTime instance from a timestamp.

pendulum/_extensions/helpers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ def __repr__(self):
4949
)
5050

5151

52-
def is_leap(year): # type: (int) -> bool
52+
def is_leap(year: int) -> bool:
5353
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
5454

5555

56-
def is_long_year(year): # type: (int) -> bool
56+
def is_long_year(year: int) -> bool:
5757
def p(y):
5858
return y + y // 4 - y // 100 + y // 400
5959

6060
return p(year) % 7 == 4 or p(year - 1) % 7 == 3
6161

6262

63-
def week_day(year, month, day): # type: (int, int, int) -> int
63+
def week_day(year: int, month: int, day: int) -> int:
6464
if month < 3:
6565
year -= 1
6666

@@ -79,14 +79,14 @@ def week_day(year, month, day): # type: (int, int, int) -> int
7979
return w
8080

8181

82-
def days_in_year(year): # type: (int) -> int
82+
def days_in_year(year: int) -> int:
8383
if is_leap(year):
8484
return DAYS_PER_L_YEAR
8585

8686
return DAYS_PER_N_YEAR
8787

8888

89-
def timestamp(dt): # type: (datetime.datetime) -> int
89+
def timestamp(dt: datetime.datetime) -> int:
9090
year = dt.year
9191

9292
result = (year - 1970) * 365 + MONTHS_OFFSETS[0][dt.month]
@@ -109,8 +109,8 @@ def timestamp(dt): # type: (datetime.datetime) -> int
109109

110110

111111
def local_time(
112-
unix_time, utc_offset, microseconds
113-
): # type: (int, int, int) -> typing.Tuple[int, int, int, int, int, int, int]
112+
unix_time: int, utc_offset: int, microseconds: int
113+
) -> tuple[int, int, int, int, int, int, int]:
114114
"""
115115
Returns a UNIX time as a broken down time
116116
for a particular transition type.
@@ -186,8 +186,8 @@ def local_time(
186186

187187

188188
def precise_diff(
189-
d1, d2
190-
): # type: (typing.Union[datetime.datetime, datetime.date], typing.Union[datetime.datetime, datetime.date]) -> PreciseDiff
189+
d1: datetime.datetime | datetime.date, d2: datetime.datetime | datetime.date
190+
) -> PreciseDiff:
191191
"""
192192
Calculate a precise difference between two datetimes.
193193
@@ -351,7 +351,7 @@ def precise_diff(
351351
)
352352

353353

354-
def _day_number(year, month, day): # type: (int, int, int) -> int
354+
def _day_number(year: int, month: int, day: int) -> int:
355355
month = (month + 9) % 12
356356
year = year - month // 10
357357

pendulum/date.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ def end_of(self, unit):
419419
:rtype: Date
420420
"""
421421
if unit not in self._MODIFIERS_VALID_UNITS:
422-
raise ValueError('Invalid unit "%s" for end_of()' % unit)
422+
raise ValueError(f'Invalid unit "{unit}" for end_of()')
423423

424-
return getattr(self, "_end_of_%s" % unit)()
424+
return getattr(self, f"_end_of_{unit}")()
425425

426426
def _start_of_day(self):
427427
"""

0 commit comments

Comments
 (0)