Skip to content

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

Merged
merged 6 commits into from
Dec 6, 2019

Conversation

ShaharNaveh
Copy link
Member

@ShaharNaveh ShaharNaveh commented Dec 5, 2019

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

@@ -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
Copy link
Member

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__?

@jreback jreback added the Typing type annotations, mypy/pyright type checking label Dec 5, 2019
@jreback jreback added this to the 1.0 milestone Dec 5, 2019
Co-Authored-By: Simon Hawkins <[email protected]>
@@ -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
Copy link
Member

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"

Copy link
Member Author

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.

@jbrockmendel
Copy link
Member

pending word from @simonjayhawkins on whetheer we bother annotating -> None for __init__, LGTM

@simonjayhawkins
Copy link
Member

pending word from @simonjayhawkins on whetheer we bother annotating -> None for __init__, LGTM

it's an undocumented convention that we're not annotating the return type of __init__

@WillAyd WillAyd merged commit 5db4097 into pandas-dev:master Dec 6, 2019
@WillAyd
Copy link
Member

WillAyd commented Dec 6, 2019

Thanks @MomIsBestFriend

@ShaharNaveh ShaharNaveh deleted the TYP-core/accessor__sorting branch December 7, 2019 09:08
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants