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

Stub methods for abstract tzinfo to allow usage with mypy 1.0.0 #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/backports/zoneinfo/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import typing
from datetime import datetime, tzinfo
from datetime import datetime, timedelta, tzinfo
from typing import (
Any,
Iterable,
Expand Down Expand Up @@ -30,6 +30,9 @@ class ZoneInfo(tzinfo):
) -> _T: ...
@classmethod
def clear_cache(cls, *, only_keys: Iterable[str] = ...) -> None: ...
def tzname(self, __dt: datetime | None) -> str | None: ...
andersk marked this conversation as resolved.
Show resolved Hide resolved
def utcoffset(self, __dt: datetime | None) -> timedelta | None: ...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not too concerned with this since it's maybe better taken up with typeshed, but I am not sure that these are actually the right type signatures for these functions.

They can take a time as well, and they never return None if you pass them a datetime, so the correct signatures are probably:

@overload
def utcoffset(self, __dt: datetime) -> timedelta: ...

@overload
def utcoffset(self, __dt: Union[time, None]) -> Union[timedelta, None]: ...

I created a PR upstream in typeshed for discussion: python/typeshed#9862

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I continue to request that this be merged as-is, because the purpose of a backport is to optimize compatibility with the released upstream version, not to pursue incompatible improvements. Obviously you’re welcome to continue pursuing those improvements upstream, and then we can backport them here in the future. But there’s no reason to block this current backport on that.

def dst(self, __dt: datetime | None) -> timedelta | None: ...

# Note: Both here and in clear_cache, the types allow the use of `str` where
# a sequence of strings is required. This should be remedied if a solution
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ commands =
description = Run mypy on the testing example
basepython = 3.8
deps =
mypy < 1
mypy
commands =
mypy src/backports/zoneinfo/__init__.pyi
mypy tests/typing_example.py
Expand Down