Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mypy and fix issues #680

Merged
merged 12 commits into from
Jun 10, 2023
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ per-file-ignores =
# F401: Module imported but unused
# TC001: Move import into type checking block
__init__.py:F401, TC001
pendulum/date.py:E800
# F811: Redefinition of unused name from line n
pendulum/tz/timezone.py:F811
pendulum/tz/timezone.py:F811, E800
min_python_version = 3.7.0
ban-relative-imports = true
# flake8-use-fstring: https://github.com/MichaelKim0407/flake8-use-fstring#--percent-greedy-and---format-greedy
Expand Down
11 changes: 9 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ jobs:
if: steps.cache.outputs.cache-hit == 'true' && matrix.os != 'MacOS'
run: timeout 10s poetry run pip --version || rm -rf .venv

- name: Install dependencies
run: poetry install --only main --only test -vvv
- name: Install runtime, testing, and typing dependencies
run: poetry install --only main --only test --only typing -vvv

- name: Run type checking
run: poetry run mypy

- name: Uninstall typing dependencies
# This ensures pendulum runs without typing_extensions installed
run: poetry install --only main --only test --sync --no-root -vvv

- name: Test Pure Python
run: |
Expand Down
11 changes: 0 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,3 @@ repos:
exclude: ^build\.py$
args:
- --py37-plus

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
hooks:
- id: mypy
pass_filenames: false
exclude: ^build\.py$
additional_dependencies:
- pytest>=7.1.2
- types-backports
- types-python-dateutil
33 changes: 31 additions & 2 deletions pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import Union
from typing import cast
from typing import overload

from pendulum.__version__ import __version__
from pendulum.constants import DAYS_PER_WEEK
Expand Down Expand Up @@ -38,10 +39,10 @@
from pendulum.testing.traveller import Traveller
from pendulum.time import Time
from pendulum.tz import UTC
from pendulum.tz import fixed_timezone
from pendulum.tz import local_timezone
from pendulum.tz import set_local_timezone
from pendulum.tz import test_local_timezone
from pendulum.tz import timezone
from pendulum.tz import timezones
from pendulum.tz.timezone import FixedTimezone
from pendulum.tz.timezone import Timezone
Expand All @@ -54,6 +55,34 @@
_formatter = Formatter()


@overload
def timezone(name: int) -> FixedTimezone:
...


@overload
def timezone(name: str) -> Timezone:
...


@overload
def timezone(name: str | int) -> Timezone | FixedTimezone:
...


def timezone(name: str | int) -> Timezone | FixedTimezone:
"""
Return a Timezone instance given its name.
"""
if isinstance(name, int):
return fixed_timezone(name)

if name.lower() == "utc":
return UTC

return Timezone(name)


def _safe_timezone(
obj: str | float | _datetime.tzinfo | Timezone | FixedTimezone | None,
dt: _datetime.datetime | None = None,
Expand All @@ -73,7 +102,7 @@ def _safe_timezone(
elif isinstance(obj, _datetime.tzinfo):
# zoneinfo
if hasattr(obj, "key"):
obj = obj.key # type: ignore
obj = obj.key
# pytz
elif hasattr(obj, "localize"):
obj = obj.zone # type: ignore
Expand Down
9 changes: 2 additions & 7 deletions pendulum/_extensions/_helpers.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from collections import namedtuple
from datetime import date
from datetime import datetime
from typing import NamedTuple

def days_in_year(year: int) -> int: ...
def is_leap(year: int) -> bool: ...
Expand All @@ -11,12 +11,7 @@ def local_time(
unix_time: int, utc_offset: int, microseconds: int
) -> tuple[int, int, int, int, int, int, int]: ...

class PreciseDiff(
namedtuple(
"PreciseDiff",
"years months days " "hours minutes seconds microseconds " "total_days",
)
):
class PreciseDiff(NamedTuple):
years: int
months: int
days: int
Expand Down
9 changes: 2 additions & 7 deletions pendulum/_extensions/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime
import math

from collections import namedtuple
from typing import NamedTuple
from typing import cast

from pendulum.constants import DAY_OF_WEEK_TABLE
Expand All @@ -25,12 +25,7 @@
from pendulum.utils._compat import zoneinfo


class PreciseDiff(
namedtuple(
"PreciseDiff",
"years months days " "hours minutes seconds microseconds " "total_days",
)
):
class PreciseDiff(NamedTuple):
years: int
months: int
days: int
Expand Down
Loading