Skip to content

Conversation

@bashtage
Copy link
Contributor

@bashtage bashtage commented Jul 7, 2022

Set type to correct type

npt.NDArray[np.int64],
]
cat = pd.Categorical(["a", "b", "a"])
codes = cat.codes
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you do this instead?

  1. import np_ndarray_int as follows at the top:
if TYPE_CHECKING: from pandas._typing import np_ndarray_int
  1. Then you can remove the type of codes in the test, and just do:
assert_type(cat.codes, "np_ndarray_int")
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 10, 2022

@bashtage can you add the assert_type() as discussed above? Then we can get this one done.

Set type to correct type
@bashtage
Copy link
Contributor Author

I had to noqa the import to get it to pass flake8 since the import is only used in the assert type.

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 @bashtage

@Dr-Irv Dr-Irv merged commit 963dd5f into pandas-dev:main Jul 11, 2022
@twoertwein
Copy link
Member

I had to noqa the import to get it to pass flake8 since the import is only used in the assert type.

The second argument of assert_type can not only be of type str but also the type itself.

@bashtage bashtage deleted the issue-111 branch July 11, 2022 15:45
@bashtage
Copy link
Contributor Author

@twoertwein When I remove the quotes it doesn't work in two different ways:

  1. If I leave the if TYPE_CHCKING on the import, pytest fails NameError: name 'np_ndarray_int' is not defined
  2. If I take away the if TYPECHECKING on the import, I get ImportError: cannot import name 'np_ndarray_int' from 'pandas._typing'
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 11, 2022

I got around this issue in test_timefuncs.py by defining np_ndarray_bool there. Could use that pattern here as well, if desired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants