Skip to content

datetime.tzinfo is abtract #8908

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

Merged
merged 9 commits into from
Oct 16, 2022
Merged

Conversation

sobolevn
Copy link
Member

This class is not marked as abstract, but it is documented:

Abstract base class for time zone info classes.
Subclasses must override the name(), utcoffset() and dst() methods.

Source: https://github.com/python/cpython/blob/f6b1e4048dc353aecfbfbae07de8212900632098/Lib/datetime.py#L1222-L1243

@github-actions

This comment has been minimized.

@sobolevn
Copy link
Member Author

I was following return types from https://docs.python.org/3/library/datetime.html#datetime.timezone

@sobolevn
Copy link
Member Author

@sobolevn
Copy link
Member Author

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/builders/gettext.py:191:7: error: Cannot instantiate abstract class "LocalTimeZone" with abstract attribute "tzname"  [abstract]

@sobolevn
Copy link
Member Author

Example in Sphinx is technically correct:

class LocalTimeZone(tzinfo):
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.tzdelta = tzdelta

    def utcoffset(self, dt: Optional[datetime]) -> timedelta:
        return self.tzdelta

    def dst(self, dt: Optional[datetime]) -> timedelta:
        return timedelta(0)


ltz = LocalTimeZone()

LocalTimeZone misses tzname method definition.
Link: https://github.com/sphinx-doc/sphinx/blob/c1f2a52d5d97f874d84e4d5421c99a566242af6b/sphinx/builders/gettext.py#L191

I will send a PR.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Do we need to add the metaclass, since runtime doesn't have it? mypy allows for checking abstractmethod without metaclass

@JelleZijlstra JelleZijlstra merged commit 9c5a588 into python:master Oct 16, 2022
@sobolevn
Copy link
Member Author

Do we need to add the metaclass, since runtime doesn't have it? mypy allows for checking abstractmethod without metaclass

Yes, we sometimes need this.

Please, take a look at this error:

error: not checking stubs due to mypy build errors:
stdlib/zoneinfo/__init__.pyi:12: error: Class zoneinfo.ZoneInfo has abstract attributes "dst", "tzname", "utcoffset"
stdlib/zoneinfo/__init__.pyi:12: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass

@sobolevn
Copy link
Member Author

Basically, ABCMeta metaclass is an explicit marker that some class is indeed abstract. That it is not just a missing method by accident.

@hauntsaninja
Copy link
Collaborator

Oh interesting, didn't realise there was this stub-only check: https://github.com/python/mypy/blob/5e1e26eba15b43449bdce4010bf16d160d3ec505/mypy/semanal_classprop.py#L97

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 17, 2022

We need the metaclass change in dateutil, but I don't think we need the metaclass change for datetime.tzinfo itself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants