Skip to content

Commit

Permalink
Merge 8e22676 into 762a3c8
Browse files Browse the repository at this point in the history
  • Loading branch information
gforcada committed Oct 8, 2022
2 parents 762a3c8 + 8e22676 commit 8af009f
Show file tree
Hide file tree
Showing 10 changed files with 530 additions and 583 deletions.
80 changes: 61 additions & 19 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@
name: lint_python
name: ci
on: [pull_request, push]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
lint_python:
test:
strategy:
matrix:
config:
- "3.7"
- "3.8"
- "3.9"
- "pypy-3.7"
runs-on: ubuntu-latest
name: ${{matrix[1]}}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: pip install --upgrade pip wheel
- run: pip install bandit black codespell flake8 flake8-bugbear isort
mypy pytest pyupgrade safety # flake8-comprehensions
- run: bandit --recursive --skip B101 . # B101 is assert statements
- run: black --check --skip-string-normalization .
- run: codespell
- run: flake8 . --count --max-complexity=18 --max-line-length=88 --show-source --statistics
- run: isort --check-only --profile black .
- run: pip install -r requirements.txt
- run: mkdir --parents --verbose .mypy_cache
- run: mypy --ignore-missing-imports --install-types --non-interactive . || true
- run: pytest run_tests.py --cov flake8_builtins --cov-report term-missing
- run: coveralls || true
- run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true
- run: safety check || true
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.config }}
- name: Cache packages
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ matrix.config }}-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.config }}-
- name: pip version
run: pip --version
- name: Install dependencies
run: python -m pip install -r requirements.txt
# formatters
- name: Run pyupgrade
if: matrix.config == '3.9'
run: pyupgrade --py37-plus *.py
- name: Run isort
if: matrix.config == '3.9'
run: isort --check-only *.py
- name: Run black
if: matrix.config == '3.9'
run: black --check --skip-string-normalization *.py
# linters
- name: Lint with bandit
if: matrix.config == '3.9'
run: bandit --skip B101 *.py # B101 is assert statements
- name: Lint with codespell
if: matrix.config == '3.9'
run: codespell *.rst *.py
- name: Lint with flake8
if: matrix.config == '3.9'
run: flake8 *.py --count --max-complexity=18 --max-line-length=88 --show-source --statistics
- name: Lint with mypy
if: matrix.config == '3.9'
run: |
mkdir --parents --verbose .mypy_cache
mypy --ignore-missing-imports --install-types --non-interactive *.py || true
- name: Lint with safety
if: matrix.config == '3.9'
run: safety check || true
# tests and coverage
- name: Test
run: pytest run_tests.py --cov flake8_builtins --cov-report term-missing
- name: Coverage
run: coveralls --service=github
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.cache
.coverage
.installed.cfg
.tox
.hypothesis
bin
develop-eggs
include
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Changelog

- Make black and isort mandatory. [cclauss]

- Drop python 2.7 and 3.6. [gforcada]

- Overhaul GitHub actions to test on actual supported python versions. [gforcada]

1.5.3 (2020-05-14)
------------------

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Install with pip::

Requirements
------------
- Python 2.7, 3.6, 3.7, 3.8, 3.9
- Python 3.7, 3.8, 3.9
- flake8

License
Expand Down
106 changes: 34 additions & 72 deletions flake8_builtins.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# -*- coding: utf-8 -*-
import ast
import builtins
import inspect
import sys


try:
from flake8.engine import pep8 as stdin_utils
except ImportError:
from flake8 import utils as stdin_utils

from flake8 import utils as stdin_utils

WHITE_LIST = {
'__name__',
Expand All @@ -18,29 +13,15 @@
}


if sys.version_info >= (3, 0):
import builtins

BUILTINS = [a[0] for a in inspect.getmembers(builtins) if a[0] not in WHITE_LIST]
PY3 = True
else:
import __builtin__

BUILTINS = [a[0] for a in inspect.getmembers(__builtin__) if a[0] not in WHITE_LIST]
PY3 = False

if sys.version_info >= (3, 6):
AnnAssign = ast.AnnAssign
else: # There was no `AnnAssign` before python3.6
AnnAssign = type('AnnAssign', (ast.AST,), {})
BUILTINS = [a[0] for a in inspect.getmembers(builtins) if a[0] not in WHITE_LIST]

if sys.version_info >= (3, 8):
NamedExpr = ast.NamedExpr
else: # There was no walrus operator before python3.8
NamedExpr = type('NamedExpr', (ast.AST,), {})


class BuiltinsChecker(object):
class BuiltinsChecker:
name = 'flake8_builtins'
version = '1.5.2'
assign_msg = 'A001 variable "{0}" is shadowing a python builtin'
Expand Down Expand Up @@ -86,7 +67,7 @@ def run(self):

value = None
for statement in ast.walk(tree):
if isinstance(statement, (ast.Assign, AnnAssign, NamedExpr)):
if isinstance(statement, (ast.Assign, ast.AnnAssign, NamedExpr)):
value = self.check_assignment(statement)

elif isinstance(statement, function_nodes):
Expand All @@ -111,8 +92,7 @@ def run(self):
value = self.check_class(statement)

if value:
for line, offset, msg, rtype in value:
yield line, offset, msg, rtype
yield from value

def check_assignment(self, statement):
msg = self.assign_msg
Expand All @@ -130,7 +110,7 @@ def check_assignment(self, statement):
stack.extend(list(item.elts))
elif isinstance(item, ast.Name) and item.id in BUILTINS:
yield self.error(item, message=msg, variable=item.id)
elif PY3 and isinstance(item, ast.Starred):
elif isinstance(item, ast.Starred):
if hasattr(item.value, 'id') and item.value.id in BUILTINS:
yield self.error(
statement,
Expand All @@ -148,23 +128,18 @@ def check_function_definition(self, statement):

yield self.error(statement, message=msg, variable=statement.name)

if PY3:
all_arguments = []
all_arguments.extend(statement.args.args)
all_arguments.extend(getattr(statement.args, 'kwonlyargs', []))
all_arguments.extend(getattr(statement.args, 'posonlyargs', []))
all_arguments = []
all_arguments.extend(statement.args.args)
all_arguments.extend(getattr(statement.args, 'kwonlyargs', []))
all_arguments.extend(getattr(statement.args, 'posonlyargs', []))

for arg in all_arguments:
if isinstance(arg, ast.arg) and arg.arg in BUILTINS:
yield self.error(
arg,
message=self.argument_msg,
variable=arg.arg,
)
else:
for arg in statement.args.args:
if isinstance(arg, ast.Name) and arg.id in BUILTINS:
yield self.error(arg, message=self.argument_msg)
for arg in all_arguments:
if isinstance(arg, ast.arg) and arg.arg in BUILTINS:
yield self.error(
arg,
message=self.argument_msg,
variable=arg.arg,
)

def check_for_loop(self, statement):
stack = [statement.target]
Expand All @@ -174,7 +149,7 @@ def check_for_loop(self, statement):
stack.extend(list(item.elts))
elif isinstance(item, ast.Name) and item.id in BUILTINS:
yield self.error(statement, variable=item.id)
elif PY3 and isinstance(item, ast.Starred):
elif isinstance(item, ast.Starred):
if hasattr(item.value, 'id') and item.value.id in BUILTINS:
yield self.error(
statement,
Expand All @@ -184,44 +159,31 @@ def check_for_loop(self, statement):
stack.extend(list(item.value.elts))

def check_with(self, statement):
if not PY3:
var = statement.optional_vars
for item in statement.items:
var = item.optional_vars
if isinstance(var, (ast.Tuple, ast.List)):
for element in var.elts:
if isinstance(element, ast.Name) and element.id in BUILTINS:
yield self.error(statement, variable=element.id)
elif (
isinstance(element, ast.Starred)
and element.value.id in BUILTINS
):
yield self.error(
element,
variable=element.value.id,
)

elif isinstance(var, ast.Name) and var.id in BUILTINS:
yield self.error(statement, variable=var.id)
else:
for item in statement.items:
var = item.optional_vars
if isinstance(var, (ast.Tuple, ast.List)):
for element in var.elts:
if isinstance(element, ast.Name) and element.id in BUILTINS:
yield self.error(statement, variable=element.id)
elif (
isinstance(element, ast.Starred)
and element.value.id in BUILTINS
):
yield self.error(
element,
variable=element.value.id,
)

elif isinstance(var, ast.Name) and var.id in BUILTINS:
yield self.error(statement, variable=var.id)

def check_exception(self, statement):
exception_name = statement.name
value = ''
if isinstance(exception_name, ast.Name):
value = exception_name.id
elif isinstance(exception_name, str): # Python +3.x
value = exception_name

if value in BUILTINS:
yield self.error(statement, variable=value)
if exception_name is None:
return

if exception_name in BUILTINS:
yield self.error(statement, variable=exception_name)

def check_comprehension(self, statement):
for generator in statement.generators:
Expand Down
25 changes: 13 additions & 12 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
bandit
black
codespell
coveralls
flake8-blind-except
flake8-coding
flake8-commas
flake8-bugbear
flake8-comprehensions
flake8-debugger
flake8-deprecated
flake8-isort
flake8-pep3101
flake8-polyfill
flake8-print
flake8-quotes
flake8-string-format
flake8-todo
futures; python_version < '3.0'
hypothesis; python_version >= '3.6'
hypothesmith; python_version >= '3.6'
mock ; python_version < '3.0'
pytest<5; python_version < '3.0'
pytest>5; python_version >= '3.0'
importlib-metadata; python_version < '3.8'
isort
mypy
pytest
pytest-cov
more-itertools==5.0.0
zipp ; python_version >= '3.0'
pyupgrade
safety
typed-ast; python_version < '3.8' # dependency of black and mypy
zipp; python_version < '3.8' # dependency of importlib-metadata

0 comments on commit 8af009f

Please sign in to comment.