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

Python 3.4 breaks Function Argument Name checking #10

Closed
kdopen opened this issue Mar 25, 2014 · 3 comments
Closed

Python 3.4 breaks Function Argument Name checking #10

kdopen opened this issue Mar 25, 2014 · 3 comments

Comments

@kdopen
Copy link
Contributor

kdopen commented Mar 25, 2014

Looks like something in the AST modules change in python 3.4

  • In python 3.3, node.args.kwargs returns <class 'str'>
  • In python 3.4, node.args.kwargs returns <class '_ast.arg'>

This breaks FunctionArgNamesCheck.visit_functiondef() at line 187 when the return value is passed to LOWERCASE_REGEX.match() which expects a string value.

A simple test file with the following contents:

def _foo(*popenargs, **kwargs):
    pass

Will produce the following exception trace when run with python 3.4

Traceback (most recent call last):
  File "/home/keith/.virtualenvs/play34/bin/flake8", line 9, in <module>
    load_entry_point('flake8==2.1.0', 'console_scripts', 'flake8')()
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/flake8/main.py", line 32, in main
    report = flake8_style.check_files()
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/pep8.py", line 1624, in check_files
    runner(path)
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/flake8/engine.py", line 73, in input_file
    return fchecker.check_all(expected=expected, line_offset=line_offset)
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/pep8.py", line 1366, in check_all
    self.check_ast()
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/pep8.py", line 1346, in check_ast
    for lineno, offset, text, check in checker.run():
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/pep8ext_naming.py", line 78, in visit_tree
    for error in self.visit_tree(child):
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/pep8ext_naming.py", line 74, in visit_tree
    for error in self.visit_node(node):
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/pep8ext_naming.py", line 92, in visit_node
    for error in getattr(visitor, method)(node, self.parents):
  File "/home/keith/.virtualenvs/play34/lib/python3.4/site-packages/pep8ext_naming.py", line 188, in visit_functiondef
    if not self.check(node.args.kwarg):
TypeError: expected string or buffer

System info:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.4 LTS
Release:    12.04
Codename:   precise
$ python --version
Python 3.4.0
$ pip freeze
flake8==2.1.0
ipython==1.2.1
mccabe==0.2.1
pep8==1.4.6
pep8-naming==0.2.1
pyflakes==0.8

Originally seen on OSX 10.9.1

@florentx florentx added bug and removed bug labels Mar 25, 2014
@kdopen
Copy link
Contributor Author

kdopen commented Mar 25, 2014

been playing with it. In ipython I see the following:

In [49]: s=ast.parse('def foo(*args,**kwargs): pass')

In [50]: class V(ast.NodeVisitor):
    def visit_FunctionDef(self,node):
        print (node.__dict__)
        print(node.args.__dict__)
        print(node.args.kwarg.__dict__)

In [51]: V().visit(s.body[0])
{'body': [<_ast.Pass object at 0xb5ee650c>], 'lineno': 1, 'decorator_list': [], 'args': <_ast.arguments object at 0xb5ee64ac>, 'returns': None, 'name': 'foo', 'col_offset': 0}
{'defaults': [], 'kwonlyargs': [], 'args': [], 'vararg': <_ast.arg object at 0xb5ee64cc>, 'kwarg': <_ast.arg object at 0xb5ee64ec>, 'kw_defaults': []}
{'arg': 'kwargs', 'lineno': 1, 'annotation': None, 'col_offset': 16}

So it looks like you need node.args.kwarg.arg to get the name of the argument

@kdopen
Copy link
Contributor Author

kdopen commented Mar 25, 2014

Think you might find the same issue with node.args.args and node.args.vararg looking at this:

In [69]: s=ast.parse('def foo(normal, *args,**kwargs): pass')

In [70]: for n in ast.walk(s): pprint({ 'class':type(n), 'repr':repr(n), 'name':n.__class__.__name__, 'dict':n.__dict__})
{'class': <class '_ast.Module'>,
 'dict': {'body': [<_ast.FunctionDef object at 0xb5ee05ec>]},
 'name': 'Module',
 'repr': '<_ast.Module object at 0xb5ee05cc>'}
{'class': <class '_ast.FunctionDef'>,
 'dict': {'args': <_ast.arguments object at 0xb5ee060c>,
          'body': [<_ast.Pass object at 0xb5ee066c>],
          'col_offset': 0,
          'decorator_list': [],
          'lineno': 1,
          'name': 'foo',
          'returns': None},
 'name': 'FunctionDef',
 'repr': '<_ast.FunctionDef object at 0xb5ee05ec>'}
{'class': <class '_ast.arguments'>,
 'dict': {'args': [<_ast.arg object at 0xb5ee062c>],
          'defaults': [],
          'kw_defaults': [],
          'kwarg': <_ast.arg object at 0xb5ee048c>,
          'kwonlyargs': [],
          'vararg': <_ast.arg object at 0xb5ee064c>},
 'name': 'arguments',
 'repr': '<_ast.arguments object at 0xb5ee060c>'}
{'class': <class '_ast.Pass'>,
 'dict': {'col_offset': 33, 'lineno': 1},
 'name': 'Pass',
 'repr': '<_ast.Pass object at 0xb5ee066c>'}
{'class': <class '_ast.arg'>,
 'dict': {'annotation': None, 'arg': 'normal', 'col_offset': 8, 'lineno': 1},
 'name': 'arg',
 'repr': '<_ast.arg object at 0xb5ee062c>'}
{'class': <class '_ast.arg'>,
 'dict': {'annotation': None, 'arg': 'args', 'col_offset': 17, 'lineno': 1},
 'name': 'arg',
 'repr': '<_ast.arg object at 0xb5ee064c>'}
{'class': <class '_ast.arg'>,
 'dict': {'annotation': None, 'arg': 'kwargs', 'col_offset': 24, 'lineno': 1},
 'name': 'arg',
 'repr': '<_ast.arg object at 0xb5ee048c>'}

In [71]: 

kdopen added a commit to kdopen/pep8-naming that referenced this issue Mar 26, 2014
In py34, a FunctionDef object changed its underlying datastructures.

FunctionDef.args.kwarg.arg changed from a string to an instance of
_ast.arg. The same thing seems to have happened for the *.args and
*.vararg fields.

This commit adds a small version-aware method to the
FunctionArgNamesCheck class which safely extracts the actual
argument name.

This addresses issue PyCQA#10
kdopen added a commit to kdopen/pep8-naming that referenced this issue Mar 26, 2014
In py34, a FunctionDef object changed its underlying datastructures.

FunctionDef.args.kwarg.arg changed from a string to an instance of
_ast.arg. The same thing seems to have happened for the *.args and
*.vararg fields.

This commit adds a small version-aware method to the
FunctionArgNamesCheck class which safely extracts the actual
argument name.

This addresses issue PyCQA#10
kdopen added a commit to kdopen/pep8-naming that referenced this issue Mar 27, 2014
In py34, a FunctionDef object changed its underlying datastructures.

FunctionDef.args.kwarg.arg changed from a string to an instance of
_ast.arg. The same thing seems to have happened for the *.args and
*.vararg fields.

This commit adds a small method to the visit_functiondef method of
the FunctionArgNamesCheck class which safely extracts the actual
argument name.

This addresses issue PyCQA#10
kdopen added a commit to kdopen/pep8-naming that referenced this issue Mar 27, 2014
In py34, a FunctionDef object changed its underlying datastructures.

FunctionDef.args.kwarg.arg changed from a string to an instance of
_ast.arg. The same thing seems to have happened for the *.args and
*.vararg fields.

This commit adds a small method to the visit_functiondef method of
the FunctionArgNamesCheck class which safely extracts the actual
argument name.

This addresses issue PyCQA#10
kdopen added a commit that referenced this issue Apr 19, 2014
In py34, a FunctionDef object changed its underlying datastructures.

FunctionDef.args.kwarg.arg changed from a string to an instance of
_ast.arg. The same thing seems to have happened for the *.args and
*.vararg fields.

This commit adds a small method to the visit_functiondef method of
the FunctionArgNamesCheck class which safely extracts the actual
argument name.

This addresses issue #10
@kdopen
Copy link
Contributor Author

kdopen commented Apr 19, 2014

Closed in PR #13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants