pganssle / zoneinfo

Reference implementation for the proposed standard library module zoneinfo
Other
52 stars 20 forks source link

Incorrect stub file causes type checking error #125

Open stinodego opened 1 year ago

stinodego commented 1 year ago

The new release of mypy 1.0.0 exposed a problem in the typing of this module.

The issue

Consider the following code:

from backports.zoneinfo import ZoneInfo

tzinfo = ZoneInfo("Pacific/Rarotonga")  # Type error

Type-checking this with mypy will result in the following error:

repro.py:3: error: Cannot instantiate abstract class "ZoneInfo" with abstract attributes "dst", "tzname" and "utcoffset"  [abstract]

The solution

After some investigation, I noticed that the type stub file of backports.zoneinfo does not contain type information for the abstract methods of ZoneInfo. The solution would be to add types for these methods.

I don't mind making a PR for this if you want to make a new release including this fix. Let me know.

The workaround

A workaround for dealing with this issue is to import ZoneInfo from the submodule directly:

from backports.zoneinfo._zoneinfo import ZoneInfo

tzinfo = ZoneInfo("Pacific/Rarotonga")  # OK