Skip to content

CI: check for some unused ignore comments #213

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 8 commits into from
Aug 22, 2022
Merged

Conversation

twoertwein
Copy link
Member

@twoertwein twoertwein commented Aug 20, 2022

Enable checks for unused mypy checks. This will at least catch cases that unintentionally widen some arguments.

Cannot enable it for pyright because mypy has no mypy-specific ignore comments and because pyright does not enforce some of mypy's rules microsoft/pyright#3849

To find all cases that should not work but that pass according to mypy: grep -R "TODO" tests

@twoertwein twoertwein requested a review from Dr-Irv August 20, 2022 15:41
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.

For any of the TODO, can we say # TODO: ignore[operator] so we know what was there?

Also, the ones that are surrounded by TYPE_CHECKING with # type: ignore are meant to fail. The idea here is to have tests that check invalid operations. So we want the type checkers to ignore the failure, but report if the ignore was invalid.


if not isinstance(actual, klass):
raise RuntimeError(f"Expected type '{klass}' but got '{type(actual)}'")
if dtype is None:
return None
return actual # type: ignore[return-value]
Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds like a mypy error to me: I think it is of type object because of the instance call where class is just type without a specification which type. Might fallback to object.

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Looking at the TODO comments, maybe we should make them into 3 categories:

  1. TODOmypy - fix needed from mypy
  2. TODO pyright - fix needed from pyright (I don't think we have any of these)
  3. TODO both - investigate why mypy and pyright aren't flagging this

Your thoughts?

@twoertwein
Copy link
Member Author

Your thoughts?

I like the idea to document for which type checker it is (not) working but I'm not sure about it. If one of the two type checkers detects code that doesn't works as invalid, it probably isn't our fault why it isn't working for the other type checker :)

I would be fine with removing TODOs when one type checker detects it as invalid.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Aug 22, 2022

Your thoughts?

I like the idea to document for which type checker it is (not) working but I'm not sure about it. If one of the two type checkers detects code that doesn't works as invalid, it probably isn't our fault why it isn't working for the other type checker :)

I would be fine with removing TODOs when one type checker detects it as invalid.

Part of the issue here is that we can denote when pyright detects it, but there isn't a separate designation for mypy.

And while it's true that it's not our fault, if we track it now, it might get fixed in the future, and we know how to reinstate the check.

So can you change the TODO to be one of TODO mypy, TODO pyright, TODO both with an indication of the error message we want to support?

One other idea that I had is that whenever we are currently doing if TYPE_CHECKING, it is to check if we are trapping invalid usage. So maybe we create a constant called TYPE_CHECKING_INVALID_USAGE that has TYPE_CHECKING_INVALID_USAGE=TYPE_CHECKING, and change if TYPE_CHECKING to if TYPE_CHECKING_INVALID_USAGE, we then make it clear to everyone what we are trying to do with those tests.

@twoertwein
Copy link
Member Author

So can you change the TODO to be one of TODO mypy, TODO pyright, TODO both with an indication of the error message we want to support?

Okay

One other idea that I had is that whenever we are currently doing if TYPE_CHECKING, it is to check if we are trapping invalid usage. So maybe we create a constant called TYPE_CHECKING_INVALID_USAGE that has TYPE_CHECKING_INVALID_USAGE=TYPE_CHECKING, and change if TYPE_CHECKING to if TYPE_CHECKING_INVALID_USAGE, we then make it clear to everyone what we are trying to do with those tests.

I like that! This will also help to distinguish some IO code we don't want to run from negative tests.

@@ -1389,6 +1389,7 @@ def test_set_columns() -> None:
df = pd.DataFrame({"a": [1, 2, 3], "b": [0.0, 1, 1]})
# Next line should work, but it is a mypy bug
# https://github.com/python/mypy/issues/3004
# pyright doesn't need the ignore
Copy link
Member Author

Choose a reason for hiding this comment

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

This is almost the only ignore in the tests that is not needed by pyright (there are many in the stubs that are not "needed").

The other two ignores are in tests/__init__.py.

If pyright had no unnecessary ignore comments in the tests, we could have a second pyright config for the installed stub check (it only checks the tests using the stubs, but not the stubs).

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

@Dr-Irv Dr-Irv merged commit 8fb0a9b into pandas-dev:main Aug 22, 2022
@Dr-Irv Dr-Irv mentioned this pull request Aug 22, 2022
@twoertwein twoertwein deleted the ignore branch September 21, 2022 15:27
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