Skip to content

fix freqstr in BaseOffset being a property #430

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

Merged
merged 2 commits into from
Nov 17, 2022
Merged

fix freqstr in BaseOffset being a property #430

merged 2 commits into from
Nov 17, 2022

Conversation

jkittner
Copy link
Contributor

@jkittner jkittner commented Nov 17, 2022

  • Closes #xxxx (Replace xxxx with the Github issue number) [does not apply]
  • Tests added: Please use assert_type() to assert the type of any return value

The freqstr is supposed to be a property, not a method returning a str

with this examples

from pandas import DateOffset

offset = DateOffset(minutes=10)
reveal_type(offset.freqstr)

and running mypy on this results in:

tt.py:4: note: Revealed type is "def () -> builtins.str"
Success: no issues found in 1 source file

with this patch it is properly captured as str:

tt.py:4: note: Revealed type is "builtins.str"
Success: no issues found in 1 source file

I ran into to this while doing this:

import pandas

delta = pandas.Timedelta(minutes=10)
offset = pandas.tseries.frequencies.to_offset(delta)

freq = None
if offset is not None:
    freq = offset.freqstr
    full_idx = pandas.date_range('2021', '2022', freq=freq)
tt.py:9: error: Argument "freq" to "date_range" has incompatible type "Callable[[], str]"; expected "Union[str, timedelta, Timedelta, BaseOffset]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

I'd be happy to add a test for this, but I'm not sure how - maybe you can give me a hint. I tried this, but it passed both times, so does not capture the error where Offset becomes a Callable[[], str].

def test_freqstr() -> None:
    offset = DateOffset(minutes=10)
    check(assert_type(offset.freqstr, str), str)

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 17, 2022

I'd be happy to add a test for this, but I'm not sure how - maybe you can give me a hint. I tried this, but it passed both times, so does not capture the error where Offset becomes a Callable[[], str].

def test_freqstr() -> None:
    offset = DateOffset(minutes=10)
    check(assert_type(offset.freqstr, str), str)

The above test should work. If you remove your change, at least pyright picks up that assert_type will fail. mypy should do that as well, although I didn't test that.

Can you add the test, then I'll enable the CI test.

@jkittner
Copy link
Contributor Author

thanks for looking into this! I added the test, but it passes also on main, so not verifying my fix.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 17, 2022

Can you indicate how you tested it on main?

I did poetry run poe mypy and got this:

tests\test_timefuncs.py:1036: error: Expression is of type "Callable[[], str]", not "str"  [assert-type]

@jkittner
Copy link
Contributor Author

oh I was just using pytest.

When using poetry run poe mypy I get an error on main but it's passing on freqstr-property so exactly what I want!

But with poetry run poe pytest everything is passing (like when I was just using pytest)
I was not aware how the tests are supposed to be run - sorry.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 17, 2022

oh I was just using pytest.

When using poetry run poe mypy I get an error on main but it's passing on freqstr-property so exactly what I want!

But with poetry run poe pytest everything is passing (like when I was just using pytest) I was not aware how the tests are supposed to be run - sorry.

If you do poetry run poe test, it will run mypy, pyright and pytest. The first two do the type checking, which is what you are fixing. The last one checks that what we expect in the types is what happens at runtime. At runtime, assert_type(x, a) just returns x which is why it worked with pytest on main

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @theendlessriver13

@Dr-Irv Dr-Irv merged commit 44f804c into pandas-dev:main Nov 17, 2022
@jkittner jkittner deleted the freqstr-property branch November 17, 2022 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants