Skip to content

Stub for time.gmtime() looks incorrect #3286

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

Closed
jolaf opened this issue Sep 30, 2019 · 3 comments · Fixed by #4256
Closed

Stub for time.gmtime() looks incorrect #3286

jolaf opened this issue Sep 30, 2019 · 3 comments · Fixed by #4256
Labels
stubs: false positive Type checkers report false errors

Comments

@jolaf
Copy link
Contributor

jolaf commented Sep 30, 2019

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]

@jolaf
Copy link
Contributor Author

jolaf commented Sep 30, 2019

First reported as python/mypy#7588.

@gvanrossum
Copy link
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!

@JelleZijlstra
Copy link
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
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants