-
-
Notifications
You must be signed in to change notification settings - Fork 145
Fix series.map overloads #960
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
JanEricNitschke
wants to merge
3
commits into
pandas-dev:main
from
JanEricNitschke:fix-map-overloads
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From how we want to test the stubs, this is not a good test, and I think it should be deleted. What we want in the tests is the following:
if TYPE_CHECKING_INVALID_USAGE
and then put theignore
tags on those tests, which also means that the code will not be tested at runtime.In your case,
bad_callable()
has a signature that could be a "good" callable, and the type checker can't tell the difference. So I don't think the 2 tests you have here should be included. The test below is fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the essence of these two tests is important. A callable that does not explicitly allow
pd.NA
as an argument should not be valid to be passed toSeries.map
ifna_action=None
(either explicitly or as a default value). The default value case was not detected before the changes that i made in this PR and caused a runtime error in a project of mine. So i do think it makes sense to explicitly test this case.I do agree that they should be moved into a
TPYE_CHECHKING_INVALID_USAGE
block and have the pytest.raises removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd still have a problem for the people that do not use
pd.NA
yet. So if you hadThen if you had
s = pd.Series([1.1, np.nan, 2.2])
, thens.map(float_callable)
is valid code. Your typing is preventing that.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then i would argue that the hints are just poor and that is purely on the user (author of the function).
Take for example:
While both calls do not cause issues at run time, the second should definitely cause an error from the type checker. Because the type checker only validates the signature, because that is what external users interact with. The implementation could change at any point and cause runtime errors in the future if you are using the function in a way that does not match the signature.
even more explicitly
causes a typechecking failure by itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. But we also make choices in the pandas stubs that cover the more common usage of pandas. Handling the case of
np.nan
versuspd.NA
is a long topic of discussion in the pandas team, and we hope to resolve it some day. But right now, the majority of pandas users usenp.nan
to represent missing values, so we can't have stubs that prevent their code from working. The changes you are proposing cause existing (and valid) pandas code to fail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if you want to separate the cases of
na_action=None
being explicit which should not be allowed, versusna_action=None
being implicit (becauseNone
is the default value), I'd be OK with that. For me, what's important is that the implicit case should pass type checking, because the default behavior (not specifyingna_action
) is most likely what people use today.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that is basically exactly what the status is before this PR actually, because it allows the interpretation of
na_action="ignore"
as a default value which is more forgiving. Then i would just add aTPYE_CHECKING_INVALID_USAGE
fors.map(bad_callable, na_action=None)
which captures the current behavior.Additionally, with
na_action=None
(especially implicitly), the reason why a lot of current incorrect (pure type theory wise) code doesnt cause runtime errors despite not handlingpd.NA
andnp.nan
explicitly, is because they play nicely with most number interactions by just propagating them. However, this is not the case with most other types. So maybe it would make sense to also fail on the implicit case for functions that take most types, but explicitly allow int/float/complex?Ill try to see if i can do it and add it to the PR to show what i mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the changes i proposed above and added some comments on the PR to explain them. Let me know what you think.
I actually also wanted to specifically allow mappings to return none-values
Mapping[S1, S2]
->Mapping[S1, S2 | NAType]
but this seems to mess with mypys overload resolution again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with putting
# type: ignore
statements in the stubs whenmypy
and/orpyright
complains about things like that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but for mypy it also causes issues at the user side where it just infers things as
Any
.