Skip to content

Commit

Permalink
Merge pull request #85 from gforcada/drop-py2-py36
Browse files Browse the repository at this point in the history
Major cleanup: python versions, versions pinned and github actions
  • Loading branch information
gforcada authored Oct 8, 2022
2 parents 762a3c8 + 16d70c3 commit f0220bc
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 587 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/lint_python.yml

This file was deleted.

61 changes: 61 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Testing
on: [pull_request, push]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
test:
name: Testing on
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9, 3.8, 3.7, pypy-3.9]
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Cache packages
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.python-version }}-
- name: pip version
run: pip --version
- name: Install dependencies
run: python -m pip install -r requirements.txt
# formatters
- name: Run pyupgrade
if: matrix.python-version == '3.9'
run: pyupgrade --py37-plus *.py
- name: Run isort
if: matrix.python-version == '3.9'
run: isort --check-only *.py
- name: Run black
if: matrix.python-version == '3.9'
run: black --check --skip-string-normalization *.py
# linters
- name: Lint with bandit
if: matrix.python-version == '3.9'
run: bandit --skip B101 *.py # B101 is assert statements
- name: Lint with codespell
if: matrix.python-version == '3.9'
run: codespell *.rst *.py
- name: Lint with flake8
if: matrix.python-version == '3.9'
run: flake8 *.py --count --max-complexity=18 --max-line-length=88 --show-source --statistics
- name: Lint with mypy
if: matrix.python-version == '3.9'
run: |
mkdir --parents --verbose .mypy_cache
mypy --ignore-missing-imports --install-types --non-interactive *.py || true
- name: Lint with safety
if: matrix.python-version == '3.9'
run: safety check || true
# tests and coverage
- name: Test
run: pytest run_tests.py --cov --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
Loading

0 comments on commit f0220bc

Please sign in to comment.