Skip to content

STYLE disallow Series | AnyArrayLike #42359

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
MarcoGorelli opened this issue Jul 3, 2021 · 9 comments · Fixed by #46529
Closed

STYLE disallow Series | AnyArrayLike #42359

MarcoGorelli opened this issue Jul 3, 2021 · 9 comments · Fixed by #46529
Assignees
Labels
Code Style Code style, linting, code_checks good first issue
Milestone

Comments

@MarcoGorelli
Copy link
Member

PR should be made to https://github.com/pandas-dev/pandas-dev-flaker

idea is to disallow foo: Series | AnyArrayLike, as Series is array-like already

This came out of this review: #41955 (comment)

@jramirez857
Copy link

Hello, I'd like to try taking this on!

@MarcoGorelli
Copy link
Member Author

Awesome @jramirez857 - a PR should probably go to https://github.com/pandas-dev/pandas-dev-flaker , let me know if you want / need any help

@jramirez857
Copy link

jramirez857 commented Dec 1, 2021

Thank you, @MarcoGorelli for offering help. Yes, this is my first contribution and if you could point me in the right direction that would be very helpful. Here is what I currently have.

import ast
from typing import Iterator, Tuple

from pandas_dev_flaker._data_tree import State, register

MSG = "PDF026 found union between State and AnyArrayLike in type hint"

@register(ast.FunctionDef)
def visit_arg(
    state: State,
    node: ast.FunctionDef,
    parent: ast.AST,
) -> Iterator[Tuple[int, int, str]]:

    arguments = node.args
    for arg in arguments:
        if (isinstance(arg.annotation, ast.BinOp)):
            yield node.lineno, node.col_offset, MSG

I am currently trying out using registering ast.FunctionDef using the decorator and I think I will need to traverse a binary tree of BinOp of argument nodes. I have written a test case shown below:

@pytest.mark.parametrize(
   "source, expected",
   (
       pytest.param(
           "def dot(foo, other: Series | DataFrame | AnyArrayLike): pass",
           "1:0: PDF026 Both State and AnyArrayLike in type hint",
           id="Series DataFrame AnyArrayLike type hint",
       ),
   ),
)
def test_violation(source, expected):
   (result,) = results(source)
   assert result == expected

I am currently figuring out how I can try to step through it in the repl. If you have any tips on how I could step through the tree traversal within the pandas-dev-flake environment it would be a huge help for my debugging purposes.

@MarcoGorelli
Copy link
Member Author

You might want to have a look at pyupgrade, they do some similar things (for example to rewrite Optional[int] as int | None)

@Pydare
Copy link
Contributor

Pydare commented Dec 30, 2021

hello, is it too late to try this out?

@MarcoGorelli
Copy link
Member Author

Hey @Pydare - I don't think anyone's working on this, though @jramirez857 please do speak up if you still are

@jramirez857
Copy link

Feel free to take this @Pydare. I made some progress initially but have not been actively working on this recently.

@Pydare
Copy link
Contributor

Pydare commented Jan 1, 2022

take

@MarcoGorelli
Copy link
Member Author

this is done, thanks to Pydare - all that needs doing next is to make a new release and bump the version - I'll do that when I get a chance, hopefully soon

@MarcoGorelli MarcoGorelli assigned MarcoGorelli and unassigned Pydare Feb 12, 2022
@jreback jreback added this to the 1.5 milestone Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Style Code style, linting, code_checks good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants