pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

parse return value fails mypy check

Open natflow opened this issue 5 years ago • 1 comments

  • [x] I am on the latest Pendulum version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: Darwin 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan 9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64 i386
  • Pendulum version: 2.1.0
  • Mypy version: v0.770
  • Python version: 3.7.6

Issue

$ python3 -m virtualenv venv
Using base prefix '/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/___/devel/repro-pendulum-typing/venv/bin/python3.7
Also creating executable in /Users/___/devel/repro-pendulum-typing/venv/bin/python
Installing setuptools, pip, wheel...
done.
$ source venv/bin/activate
$ env/bin/pip install pendulum==2.1.0 mypy==v0.770
...
$ cat repro.py
import pendulum
d = pendulum.parse('P35D')
print(d.in_seconds())
$ env/bin/mypy --python-executable venv/bin/python repro.py
repro.py:3: error: "str" has no attribute "in_seconds"

So, mypy thinks pendulum.parse returns a string, when in fact it returns a duration (in this case). The str return type was just added to parse: https://github.com/sdispater/pendulum/pull/320/files#diff-d79c6f58aeecd540ddd61603df9184aaR17.

I think the right type would be a Union of pendulum.duration.Duration and other types.

natflow avatar Mar 11 '20 20:03 natflow

python = "^3.9" pendulum = "^2.1.2" mypy = "^0.931"

For below code mypy will show the issue error: Item "Duration" of "Union[Date, Time, DateTime, Duration]" has no attribute "format" While you can see format is working fine and returning the desired results.

Code to reproduce the error

"""Pendulum mypy issue""" import pendulum COB = "20220224" cob_date_time = pendulum.parse(COB, strict=False) print(type(cob_date_time)) formatted_cob = cob_date_time.format("DDMMYYYY") print(formatted_cob) print(type(formatted_cob))

sanjayaggi avatar Feb 24 '22 16:02 sanjayaggi