-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYP: Annotations in pandas/core/{accessor, sorting}.py #30079
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: Annotations in pandas/core/{accessor, sorting}.py #30079
Conversation
pandas/core/accessor.py
Outdated
@@ -107,7 +109,9 @@ def f(self, *args, **kwargs): | |||
setattr(cls, name, f) | |||
|
|||
|
|||
def delegate_names(delegate, accessors, typ, overwrite=False): | |||
def delegate_names( | |||
delegate, accessors: Union[List[str], Set[str]], typ: str, overwrite: bool = 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.
@simonjayhawkins is there some container-ish thing we would use for Union[List[foo], Set[foo]]
? Also do we have a convention about annotating return from __init__
?
Co-Authored-By: Simon Hawkins <[email protected]>
pandas/core/accessor.py
Outdated
@@ -58,7 +58,9 @@ def _delegate_method(self, name, *args, **kwargs): | |||
raise TypeError("You cannot call method {name}".format(name=name)) | |||
|
|||
@classmethod | |||
def _add_delegate_accessors(cls, delegate, accessors, typ, overwrite=False): | |||
def _add_delegate_accessors( | |||
cls, delegate, accessors: List[str], typ: str, overwrite: bool = 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.
the type annotation of accessors
here needs to be compatible with delegate_names
below.
_add_delegate_accessors
is used in the inner function of delegate_names
. This inner function add_delegate_accessors
does not have type annotations and is therefore not checked by mypy. Annotating the inner functions gives pandas\core\accessor.py:143: error: Argument 2 to "_add_delegate_accessors" of "PandasDelegate" has incompatible type "Union[List[str], Set[str]]"; expected "List[str]"
Probably Sequence[str]
is more appropriate, however beware a string is also a sequence of str.
from typing import Sequence
def foo(bar: Sequence[str]):
pass
foo(3)
foo("hdfjjdhf")
foo([1, 2, 3])
foo(["bar", "baz"])
test.py:8: error: Argument 1 to "foo" has incompatible type "int"; expected "Sequence[str]"
test.py:10: error: List item 0 has incompatible type "int"; expected "str"
test.py:10: error: List item 1 has incompatible type "int"; expected "str"
test.py:10: error: List item 2 has incompatible type "int"; expected "str"
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.
Every combination I tried I got an error. I think I will drop the annotations for accessors
in this PR.
pending word from @simonjayhawkins on whetheer we bother annotating |
it's an undocumented convention that we're not annotating the return type of |
Thanks @MomIsBestFriend |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff