Skip to content

Stub for time.gmtime() looks incorrect #3286

Description

@jolaf

The following code:

from time import gmtime
(a, b, c, d, e, f, g, h, i) = gmtime()
print(a, b, c, d, e, f, g, h, i)

produces the following output:

$ python3 Test.py 
2019 9 30 16 29 49 0 273 0
$ mypy Test.py
Test.py:3: error: Too many values to unpack (9 expected, 11 provided)

$ mypy --version
mypy 0.730
$ python3 --version
Python 3.6.8

reveal_type(gmtime) produces the following output:
Revealed type is 'def (secs: Union[builtins.float, None] =) -> Tuple[builtins.int, builtins.int, builtins.int, builtins.int, builtins.int, builtins.int, builtins.int, builtins.int, builtins.int, builtins.str, builtins.int, fallback=time.struct_time]

Activity

  1. jolaf commented on Sep 30, 2019

    @jolaf
    ContributorAuthor

    First reported as python/mypy#7588.

  2. gvanrossum commented on Oct 1, 2019

    @gvanrossum
    Member

    This is painful.

    The implementation in Python produces a magical object that unpacks as a 9-tuple but has two additional fields (tm_zone, tm_gmtoff) that are only accessible by name. I'm not even sure how to express that in a typing stub.

    The best I can think of is to make a class that inherits from a NamedTuple with 9 fields and then adds the two additional fields. Something like

    class N(NamedTuple):
        tm_year: int
        # etc
    class struct_tm(N):
        tm_zone: str
        tm_gmtoff: int

    But pick a better name than N and handle the version-specific code in time.pyi.

    PR welcome!

  3. JelleZijlstra commented on Oct 1, 2019

    @JelleZijlstra
    Member

    I think we can add the additional fields with @property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions