Skip to content

Incompatible type reported by mypy when using pendulum.parse #620

Open
@yoanisgil

Description

@yoanisgil
  • I am on the latest Pendulum version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: Darwin 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000 arm64
  • Pendulum version: 2.1.2

Issue

Given the following code:

import dataclasses
import pendulum
from pendulum import parse
from pendulum.datetime import DateTime

@dataclasses.dataclass
class A:
    creation_timestamp: DateTime

if __name__ == "__main__":
    a = A(creation_timestamp=pendulum.parse("2022-05-10T04:19:26Z"))
    print(a.creation_timestamp)

when I run type checking with mypy (version 0.950). I get the following error:

test_pendulum.py:11: error: Argument "creation_timestamp" to "A" has incompatible type "Union[Date, Time, Duration]"; expected "DateTime"

which is kind of surprising as one would say that the type hints here are correct:

def parse(
    text, **options
):  # type: (str, **typing.Any) -> typing.Union[Date, Time, DateTime, Duration]

Apparently mypy thinks pendulum.parse returns Union[Date, Time, Duration] (instead of Union[Date, Time, DateTime, Duration]). Any ideas why?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions