Skip to content
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

Crash on functions with certain typehinting #129

Open
kmantel opened this issue Jul 8, 2020 · 7 comments
Open

Crash on functions with certain typehinting #129

kmantel opened this issue Jul 8, 2020 · 7 comments
Labels
good first issue Good for newcomers

Comments

@kmantel
Copy link

kmantel commented Jul 8, 2020

Simple typehints seem to be supported, but others cause bowler to crash. For example,

foo.py:

import typing


def foo(bar: typing.List[str]):
    pass

Running

bowler.Query('foo.py').select_function('foo').add_argument('baz', 1).diff()

Produces

Skipping foo.py: failed to transform because 'Node' object has no attribute 'value'
Traceback (most recent call last):
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/bowler/tool.py", line 209, in refactor_queue
    hunks = self.refactor_file(filename)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/bowler/tool.py", line 171, in refactor_file
    tree = self.refactor_string(input, filename)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/fissix/refactor.py", line 377, in refactor_string
    self.refactor_tree(tree, name)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/fissix/refactor.py", line 417, in refactor_tree
    self.traverse_by(self.bmi_post_order_heads, tree.post_order())
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/fissix/refactor.py", line 493, in traverse_by
    new = fixer.transform(node, results)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/bowler/query.py", line 962, in transform
    returned_node = callback(node, capture, filename)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/bowler/query.py", line 746, in add_argument_transform
    spec = FunctionSpec.build(node, capture)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/bowler/imr.py", line 214, in build
    arguments = FunctionArgument.build_list(args, is_def)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/bowler/imr.py", line 89, in build_list
    arg = cls.build(leaf, is_def)
File "~/.pyenv/versions/pnl368/lib/python3.6/site-packages/bowler/imr.py", line 50, in build
    kwargs["annotation"] = leaf.children[-1].value
AttributeError: 'Node' object has no attribute 'value'
AttributeError: 'Node' object has no attribute 'value'
<bowler.query.Query object at 0x7fe8de0d8860>

While replacing typing.List[str] with just str produces the expected result

@turbaszek
Copy link

With @olchas we encountered the same issue. How can we help to solve this bug?

@thatch
Copy link
Contributor

thatch commented Jul 24, 2020

@jreese is just checking whether leaf.children[-1] is a Leaf (->store .value) or Node (store the whole thing?) enough of a fix here, or are there downstream dependencies on this that would break. I suspect any type checking we might have is disabled by using kwargs.

@thatch thatch added the good first issue Good for newcomers label Oct 5, 2020
@thatch
Copy link
Contributor

thatch commented Oct 5, 2020

  1. Store the entire node in kwargs["annotation"] seems the way to go. There may be corresponding changes where that's pulled back out
  2. Add a test case near https://github.com/facebookincubator/Bowler/blob/master/bowler/tests/query.py#L297 that includes a type hint of typing.List[str] in the first post
  3. Make sure the tests pass
  4. Submit a PR.

Some getting started docs are at https://github.com/facebookincubator/Bowler/blob/master/CONTRIBUTING.md

@MatthewMarroquin
Copy link

MatthewMarroquin commented Oct 20, 2020

@thatch Taking a look at this; first time working with unit tests any documentation on this?

@MdAfzaalkhan
Copy link

@thatch is this still up can i submit a PR for this ?

@StunningShield4504
Copy link

For Python 3.8 or lower : That code is correct
For Python 3.9 or later :
def foo(bar: list[str]):
pass

@thatch
Copy link
Contributor

thatch commented Sep 17, 2024

@MdAfzaalkhan sure! The dotted notation is legal in 3.9+ as well, especially for third-party types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

6 participants