-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
TYP: Type hints & assert statements #42044
Conversation
Hello @rgkimball! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2021-06-25 02:35:14 UTC |
Thanks for the pr @rgkimball! I remember there being an issue where use of asserts was discussed at length, but can't find it now. I think the basic philosophy behind most Also, this pr is pretty complicated since it touches a lot of stuff - review would be a lot faster if you could break it up (for example, into typing addition for merge, typing for ..., change of asserts...) |
pandas/_typing.py
Outdated
MergeTypes = Literal['inner', 'outer', 'left', 'right', 'cross'] | ||
ConcatTypes = Literal['inner', 'outer'] |
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.
master currently supports python3.7 so Literal needs to be inside a TYPE_CHECKING block.
This will be changed shortly #41989, so maybe could wait for that for these changes
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.
Thanks for the comment; either works for me - will all other TYPE_CHECKING blocks be removed at that point? If not, it may be more consistent to just add it here as well for compatibility.
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.
After #41989 we can start the cleanup of the typing workarounds for Python3.7 (which we use since we don't have typing_extensions as a required dependency) so we hopefully can remove the TYPE_CHECKING that were specifically added to use Literal. (and others than were not available in the Python typing module until Python 3.8)
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 see - agreed then it makes sense to just wait until then for most of these additions. Based on Jeff's comment about waiting until 1.3, would ~2-3 weeks be a good estimate?
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.
yep. we've only just released the rc, so a couple of weeks at least.
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.
#41989 now merged.
Ah, understood! The distinction makes sense, in that case there are probably a few instances here where it should revert to the assert for protected library functions - I'll add test cases for the others.
No problem! I wasn't sure if it would be easier by feature or by nature of the change - would you prefer to see those as separate commits on this PR or separate PR's altogether? The latter seems like it could clutter the queue a bit but happy to accommodate what's easiest for you & the team. |
It feels like it gets discussed regularly in different PRs. we did have #32785 to try and keep those discussions in one place. |
@@ -9148,7 +9148,7 @@ def merge( | |||
sort: bool = False, | |||
suffixes: Suffixes = ("_x", "_y"), | |||
copy: bool = True, | |||
indicator: bool = False, | |||
indicator: bool | str = False, |
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.
sure about this?
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.
Per _MergeOperation
you can optionally supply a string as the column name, otherwise the indicator is given a default name:
pandas/pandas/core/reshape/merge.py
Lines 655 to 658 in f68092a
if isinstance(self.indicator, str): | |
self.indicator_name = self.indicator | |
elif isinstance(self.indicator, bool): | |
self.indicator_name = "_merge" if self.indicator else None |
Maybe I missed this, but is there a "valid column name" definition that would be more specific than str
?
Note failing tests are due to importing Literal in <3.8 |
@rgkimball can you merge master (and maybe break this PR up, e.g. maybe keep type annotation additions separate from code changes) |
@@ -5691,7 +5691,9 @@ def _validate_indexer(self, form: str_t, key, kind: str_t): | |||
if key is not None and not is_integer(key): | |||
raise self._invalid_indexer(form, key) | |||
|
|||
def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default): | |||
def _maybe_cast_slice_bound( | |||
self, label, side: str_t, kind: Literal["loc", "getitem"] = no_default |
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 needs to be amended to include no_default?
i like the annotations added, dont think its worth futzing with the assertions for fully internal code |
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. |
Thanks for the PR, but it appears to have gone stale. Let us know if you're still interested in working on this and we can reopen. Closing. |
This PR addresses two concerns of best practices:
DataFrame.merge
for which the correct use of strings in theindicator
argument did not match the boolean hintThanks for your time, happy to discuss or revise any of these as you see fit if they aren't consistent with the contribution guidelines.