Skip to content

tzinfo vs. timestamp type for timezone.utc #567

@dlinnemeyer

Description

@dlinnemeyer

I was running mypy on an existing project, and the following code (recommended by the python datetime docs) ran into an error in mypy, which seems to stem from typeshed.

datetime.fromtimestamp(timestamp, timezone.utc)

Error from mypy is Argument 2 to "fromtimestamp" of "datetime" has incompatible type "tzinfo"; expected "timezone"

It looks like fromtimestamp expects an instance of timezone, but timezone.utc is stubbed as a tzinfo class, which looks like a parent of timezone?

Relevant sections in /stdlib/3/datetime.pyi are lines 16-19

class timezone(tzinfo):
    utc = ...  # type: tzinfo
    min = ...  # type: tzinfo
    max = ...  # type: tzinfo

and 175-ish, under class datetime:

    @classmethod
    def fromtimestamp(cls, t: float, tz: timezone = ...) -> datetime: ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions