pax_global_header00006660000000000000000000000064130772632600014520gustar00rootroot0000000000000052 comment=83bf389515e328be144e9a620d85f2d589b985e4 ebeweber-flake8-mutable-83bf389/000077500000000000000000000000001307726326000164475ustar00rootroot00000000000000ebeweber-flake8-mutable-83bf389/.gitignore000066400000000000000000000000411307726326000204320ustar00rootroot00000000000000*.egg-info *.pyc dist build venv ebeweber-flake8-mutable-83bf389/.travis.yml000066400000000000000000000002261307726326000205600ustar00rootroot00000000000000language: python python: - "2.7" - "3.5" - "3.6" install: "pip install -r dev-requirements.txt" before_script: flake8 . script: pytest tests/ ebeweber-flake8-mutable-83bf389/LICENSE000066400000000000000000000020731307726326000174560ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Matthew Ebeweber Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ebeweber-flake8-mutable-83bf389/MANIFEST.in000066400000000000000000000000421307726326000202010ustar00rootroot00000000000000include LICENSE include README.md ebeweber-flake8-mutable-83bf389/README.md000066400000000000000000000014431307726326000177300ustar00rootroot00000000000000flake8-mutable -------------- [![PyPI version](https://img.shields.io/pypi/v/flake8-mutable.svg)](https://pypi.python.org/pypi/flake8-mutable) #### Motivation Python's default arguments are evaluated at definition as opposed to when the function is invoked. This leads to unexpected behavior, as mutations persist between calls. For a more detailed explanation, see [The Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments). #### Example ``` def fnc(a, b={}): pass foo.py:2:14: M511 - mutable default arg of type Dict ``` #### Installation ``` pip install flake8-mutable ``` #### Changes ##### [1.1.0] 2016-11-26 - Callables ##### [1.0.6] 2016-11-26 - added MANIFEST.in #### License [MIT](https://opensource.org/licenses/MIT) ebeweber-flake8-mutable-83bf389/dev-requirements.txt000066400000000000000000000000421307726326000225030ustar00rootroot00000000000000-r requirements.txt pytest==3.0.3 ebeweber-flake8-mutable-83bf389/mutable_defaults.py000066400000000000000000000021361307726326000223430ustar00rootroot00000000000000# -*- coding: utf-8 -*- import ast __version__ = '1.2.0' mutable_types = [ ast.Call, ast.Dict, ast.List, ast.Set, ] class MutableDefaultChecker(object): """Mutable default argument checker. Flake8 extension that alerts when a mutable type is used as an argument's default value. """ name = 'flake-mutable' version = __version__ _error_tmpl = '{} - mutable default arg of type {}' _code = 'M511' def __init__(self, tree, filename): self.tree = tree def run(self): for node in ast.walk(self.tree): if isinstance(node, ast.FunctionDef): for default in node.args.defaults: if any([ isinstance(default, mutable_type) for mutable_type in mutable_types ]): error_msg = self._error_tmpl.format( self._code, type(default).__name__ ) yield (default.lineno, default.col_offset, error_msg, type(self)) ebeweber-flake8-mutable-83bf389/requirements.txt000066400000000000000000000000701307726326000217300ustar00rootroot00000000000000flake8==2.5.4 mccabe==0.4.0 pep8==1.7.0 pyflakes==1.0.0 ebeweber-flake8-mutable-83bf389/setup.cfg000066400000000000000000000001711307726326000202670ustar00rootroot00000000000000[aliases] test=pytest [tool:pytest] addopts = --verbose python_files = tests/*.py norecursedirs = .svn _build tmp* venv ebeweber-flake8-mutable-83bf389/setup.py000066400000000000000000000022701307726326000201620ustar00rootroot00000000000000# -*- coding: utf-8 -*- import setuptools setuptools.setup( version='1.2.0', name='flake8-mutable', description='mutable defaults flake8 extension', keywords='flake8 mutable arg kwarg', author='Matthew Ebeweber', author_email='mebeweber@gmail.com', url='https://github.com/ebeweber/flake8-mutable', license='MIT', py_modules=['mutable_defaults'], zip_safe=False, install_requires=[ 'flake8', ], entry_points={ 'flake8.extension': [ 'M90 = mutable_defaults:MutableDefaultChecker', ], }, setup_requires=[ 'pytest-runner', ], tests_require=[ 'pytest', ], classifiers=[ 'Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', 'Topic :: Software Development :: Libraries :: Python Modules', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', ], ) ebeweber-flake8-mutable-83bf389/tests/000077500000000000000000000000001307726326000176115ustar00rootroot00000000000000ebeweber-flake8-mutable-83bf389/tests/__init__.py000066400000000000000000000000001307726326000217100ustar00rootroot00000000000000ebeweber-flake8-mutable-83bf389/tests/mutable_defaults_test.py000066400000000000000000000045501307726326000245460ustar00rootroot00000000000000import ast import pytest from mutable_defaults import MutableDefaultChecker @pytest.mark.parametrize('code,error_count', [ ('def foo(bar={}): pass', 1), ('def foo(bar=[]): pass', 1), ('def foo(): pass', 0), ('def foo(a, b, c=False): pass', 0), ( '\n'.join([ 'def foo():', ' def bar(a={}):', ' pass', ' pass', ]), 1 ), ('def foo(bar=[], baz={}): pass', 2), ('def foo(bar=range(10)): pass', 1), ('def foo(bar=set()): pass', 1), ( '\n'.join([ 'def foo():', ' return True', 'def bar(a=foo()):', ' pass', ]), 1 ), ('', 0), ], ids=( 'dict', 'list', 'empty_arg_list', 'valid_args_and_kwargs', 'nested_function', 'multiple_mutable_defaults', 'builtin_callable_with_value', 'builtin_callable_without_value', 'user_defined_callable', 'empty_tree', )) def test_mutable_defaults(code, error_count): tree = ast.parse(code) assert ( len(list(MutableDefaultChecker(tree, 'filename').run())) == error_count ) @pytest.mark.parametrize('code,location_list', [ ('def foo(bar={}): pass', [(1, 12)]), ('def foo(bar=[]): pass', [(1, 12)]), ( '\n'.join([ 'def foo():', ' def bar(a={}):', ' pass', ' pass', ]), [(2, 14)] ), ('def foo(bar=[], baz={}): pass', [(1, 12), (1, 20)]), ('def foo(bar=range(10)): pass', [(1, 12)]), ('def foo(bar=set()): pass', [(1, 12)]), ( '\n'.join([ 'def foo():', ' return True', 'def bar(a=foo()):', ' pass', ]), [(3, 10)] ), ( '\n'.join([ 'def foo(bar=[],', ' baz={}):', ' pass', ]), [(1, 12), (2, 12)] ), ], ids=( 'dict', 'list', 'nested_function', 'multiple_mutable_defaults', 'builtin_callable_with_value', 'builtin_callable_without_value', 'user_defined_callable', 'multiple_line_function_def', )) def test_mutable_defaults_location(code, location_list): tree = ast.parse(code) assert ( location_list == [(e[0], e[1]) for e in MutableDefaultChecker(tree, 'filename').run()] )