Skip to content

TYP: Add annotation to assert_frame_equal and assert_series_equal (GH26302) #39504

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

Conversation

avinashpancham
Copy link
Contributor

Incremental typing PR for #26302

  • closes #xxxx
  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry

@avinashpancham
Copy link
Contributor Author

avinashpancham commented Jan 31, 2021

@WillAyd I looked into NoReturn since Python should be >= 3.7 for v1.2.0, but it gave me the mypy error shown below. So I used None as I normally do. If you have any hints on how to make NoReturn work then LMK

Mypy error when using NoReturn: Implicit return in function which does not return [misc]

check_dtype: Union[bool, str] = True,
check_index_type: Union[bool, str] = "equiv",
check_series_type: bool = True,
check_less_precise: Union[bool, int] = no_default,
Copy link
Contributor

Choose a reason for hiding this comment

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

@simonjayhawkins how shall we type this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

revealed type is Any, so not yet an issue. Lets defer handling of this till it becomes an issue. i.e. when we merge the type stub from pyright for lib.pyx

if check_freq and isinstance(left.index, (DatetimeIndex, TimedeltaIndex)):
if (
check_freq
and isinstance(left.index, (DatetimeIndex, TimedeltaIndex))
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, what hits this?

Copy link
Contributor Author

@avinashpancham avinashpancham Feb 5, 2021

Choose a reason for hiding this comment

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

On L959 we use ridx.freq. Initially we didn't check if right.index was also of type DatetimeIndex or TimedeltaIndex and therefore mypy wasnt sure if the index had the freq attribute. This threw the following error

error: "Index" has no attribute "freq"  [attr-defined]

):
left: DataFrame,
right: DataFrame,
check_dtype: Union[bool, str] = True,
Copy link
Contributor

Choose a reason for hiding this comment

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

just bool

Copy link
Contributor Author

Choose a reason for hiding this comment

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

on L128 we call assert_frame_equal with check_dtype=check_dtype and the default value of check_dtype in that context is "equiv". If we don't type it is as Union[bool, str] we get the following error:

error: Argument "check_dtype" to "assert_frame_equal" has incompatible type "Union[bool, str]"; expected "bool"  [arg-type]

Copy link
Member

Choose a reason for hiding this comment

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

could use Literal["equiv"] here instead of str for clarity.

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Feb 2, 2021
@avinashpancham
Copy link
Contributor Author

@jreback could you have a look at my comments? Thanks!

@jreback
Copy link
Contributor

jreback commented Feb 19, 2021

cc @simonjayhawkins if you can have a look.

Comment on lines +820 to +821
left: Series,
right: Series,
Copy link
Member

Choose a reason for hiding this comment

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

see #29364 for a prior discussion on this. IIRC These asserters are intended to raise AssertionError, not TypeError if left of right is not of the correct type.

assert_series_equal is part of the public api and therefore types added here will be used when checking user code. This may lead to false positives, depending on how users are using this function.

probably best to leave left and right untyped for now.

@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Apr 14, 2021
@simonjayhawkins
Copy link
Member

closing as stale. @avinashpancham please ping if you want to pick this up again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants