Skip to content

Commit 5bc805c

Browse files
authored
Update mypy and fix issues (#680)
* Update mypy and fix issues * Fix install invocation * Remove importing _winreg * Remove namedtuple usage * Update to mypy 1.0.1 * Fix mypy errors * Only build the project once in CI * Improve more typing * Update mypy and fix errors * Move `timezone()` to main module to work around mypy sometimes mixing up the function and module * Improve `ClassVar` setting for `DateTime` * Fix unused ignore error
1 parent f071ed3 commit 5bc805c

32 files changed

+563
-447
lines changed

.flake8

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ per-file-ignores =
44
# F401: Module imported but unused
55
# TC001: Move import into type checking block
66
__init__.py:F401, TC001
7+
pendulum/date.py:E800
78
# F811: Redefinition of unused name from line n
8-
pendulum/tz/timezone.py:F811
9+
pendulum/tz/timezone.py:F811, E800
910
min_python_version = 3.7.0
1011
ban-relative-imports = true
1112
# flake8-use-fstring: https://github.com/MichaelKim0407/flake8-use-fstring#--percent-greedy-and---format-greedy

.github/workflows/tests.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ jobs:
6464
if: steps.cache.outputs.cache-hit == 'true' && matrix.os != 'MacOS'
6565
run: timeout 10s poetry run pip --version || rm -rf .venv
6666

67-
- name: Install dependencies
68-
run: poetry install --only main --only test -vvv
67+
- name: Install runtime, testing, and typing dependencies
68+
run: poetry install --only main --only test --only typing -vvv
69+
70+
- name: Run type checking
71+
run: poetry run mypy
72+
73+
- name: Uninstall typing dependencies
74+
# This ensures pendulum runs without typing_extensions installed
75+
run: poetry install --only main --only test --sync --no-root -vvv
6976

7077
- name: Test Pure Python
7178
run: |

.pre-commit-config.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,3 @@ repos:
5858
exclude: ^build\.py$
5959
args:
6060
- --py37-plus
61-
62-
- repo: https://github.com/pre-commit/mirrors-mypy
63-
rev: v0.982
64-
hooks:
65-
- id: mypy
66-
pass_filenames: false
67-
exclude: ^build\.py$
68-
additional_dependencies:
69-
- pytest>=7.1.2
70-
- types-backports
71-
- types-python-dateutil

pendulum/__init__.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import Union
66
from typing import cast
7+
from typing import overload
78

89
from pendulum.__version__ import __version__
910
from pendulum.constants import DAYS_PER_WEEK
@@ -38,10 +39,10 @@
3839
from pendulum.testing.traveller import Traveller
3940
from pendulum.time import Time
4041
from pendulum.tz import UTC
42+
from pendulum.tz import fixed_timezone
4143
from pendulum.tz import local_timezone
4244
from pendulum.tz import set_local_timezone
4345
from pendulum.tz import test_local_timezone
44-
from pendulum.tz import timezone
4546
from pendulum.tz import timezones
4647
from pendulum.tz.timezone import FixedTimezone
4748
from pendulum.tz.timezone import Timezone
@@ -54,6 +55,34 @@
5455
_formatter = Formatter()
5556

5657

58+
@overload
59+
def timezone(name: int) -> FixedTimezone:
60+
...
61+
62+
63+
@overload
64+
def timezone(name: str) -> Timezone:
65+
...
66+
67+
68+
@overload
69+
def timezone(name: str | int) -> Timezone | FixedTimezone:
70+
...
71+
72+
73+
def timezone(name: str | int) -> Timezone | FixedTimezone:
74+
"""
75+
Return a Timezone instance given its name.
76+
"""
77+
if isinstance(name, int):
78+
return fixed_timezone(name)
79+
80+
if name.lower() == "utc":
81+
return UTC
82+
83+
return Timezone(name)
84+
85+
5786
def _safe_timezone(
5887
obj: str | float | _datetime.tzinfo | Timezone | FixedTimezone | None,
5988
dt: _datetime.datetime | None = None,
@@ -73,7 +102,7 @@ def _safe_timezone(
73102
elif isinstance(obj, _datetime.tzinfo):
74103
# zoneinfo
75104
if hasattr(obj, "key"):
76-
obj = obj.key # type: ignore
105+
obj = obj.key
77106
# pytz
78107
elif hasattr(obj, "localize"):
79108
obj = obj.zone # type: ignore

pendulum/_extensions/_helpers.pyi

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
22

3-
from collections import namedtuple
43
from datetime import date
54
from datetime import datetime
5+
from typing import NamedTuple
66

77
def days_in_year(year: int) -> int: ...
88
def is_leap(year: int) -> bool: ...
@@ -11,12 +11,7 @@ def local_time(
1111
unix_time: int, utc_offset: int, microseconds: int
1212
) -> tuple[int, int, int, int, int, int, int]: ...
1313

14-
class PreciseDiff(
15-
namedtuple(
16-
"PreciseDiff",
17-
"years months days " "hours minutes seconds microseconds " "total_days",
18-
)
19-
):
14+
class PreciseDiff(NamedTuple):
2015
years: int
2116
months: int
2217
days: int

pendulum/_extensions/helpers.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import datetime
44
import math
55

6-
from collections import namedtuple
6+
from typing import NamedTuple
77
from typing import cast
88

99
from pendulum.constants import DAY_OF_WEEK_TABLE
@@ -25,12 +25,7 @@
2525
from pendulum.utils._compat import zoneinfo
2626

2727

28-
class PreciseDiff(
29-
namedtuple(
30-
"PreciseDiff",
31-
"years months days " "hours minutes seconds microseconds " "total_days",
32-
)
33-
):
28+
class PreciseDiff(NamedTuple):
3429
years: int
3530
months: int
3631
days: int

0 commit comments

Comments
 (0)