Skip to content
This repository was archived by the owner on Feb 19, 2023. It is now read-only.

Commit 939c0ec

Browse files
committed
init
0 parents  commit 939c0ec

14 files changed

+258
-0
lines changed

Diff for: .gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.egg-info
2+
*.pyc
3+
/.coverage
4+
/.mypy_cache
5+
/.pytest_cache
6+
/.tox
7+
/venv*

Diff for: LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 Anthony Sottile
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Diff for: README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.flake8-match?branchName=master)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=69&branchName=master)
2+
[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/69/master.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=69&branchName=master)
3+
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/flake8-match/master.svg)](https://results.pre-commit.ci/latest/github/asottile/flake8-match/master)
4+
5+
flake8-match
6+
============
7+
8+
flake8 plugin which forbids match statements (PEP 634)
9+
10+
## installation
11+
12+
`pip install flake8-match`
13+
14+
## flake8 codes
15+
16+
| Code | Description |
17+
|--------|-----------------------------|
18+
| MAT001 | do not use match statements |
19+
20+
## rationale
21+
22+
lol
23+
24+
## as a pre-commit hook
25+
26+
See [pre-commit](https://github.com/pre-commit/pre-commit) for instructions
27+
28+
Sample `.pre-commit-config.yaml`:
29+
30+
```yaml
31+
- repo: https://gitlab.com/pycqa/flake8
32+
rev: 3.8.4
33+
hooks:
34+
- id: flake8
35+
additional_dependencies: [flake8-match==1.0.0]
36+
```

Diff for: azure-pipelines.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trigger:
2+
branches:
3+
include: [master, test-me-*]
4+
tags:
5+
include: ['*']
6+
7+
resources:
8+
repositories:
9+
- repository: asottile
10+
type: github
11+
endpoint: github
12+
name: asottile/azure-pipeline-templates
13+
ref: refs/tags/v2.1.0
14+
15+
jobs:
16+
- template: job--python-tox.yml@asottile
17+
parameters:
18+
toxenvs: [py310]
19+
os: linux

Diff for: pandas_style_guide/__main__.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import annotations
2+
from pandas_style_guide.check_for_exec import check_for_exec
3+
4+
5+
import ast
6+
import importlib.metadata
7+
from typing import Any, List, Dict, Tuple, Type, Callable
8+
import collections
9+
from typing import Generator
10+
11+
from pandas_style_guide._data import FUNCS, visit
12+
13+
14+
15+
16+
class Plugin:
17+
name = __name__
18+
version = 1#importlib.metadata.version(__name__)
19+
20+
def __init__(self, tree: ast.AST):
21+
self._tree = tree
22+
23+
def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
24+
callbacks = visit(FUNCS, self._tree)
25+
if not callbacks:
26+
return
27+
for line, col, msg in callbacks:
28+
yield line, col, msg, type(self)
29+

Diff for: pandas_style_guide/_data.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import collections
2+
from typing import List, Tuple, Dict
3+
import ast
4+
5+
FUNCS = collections.defaultdict(list)
6+
def register(tp):
7+
def register_decorator(func):
8+
FUNCS[tp].append(func)
9+
return func
10+
return register_decorator
11+
12+
13+
def visit(funcs, tree: ast.Module) -> Dict[int, List[int]]:
14+
"Step through tree, recording when nodes are in annotations."
15+
in_annotation = False
16+
nodes: List[Tuple[bool, ast.AST]] = [(in_annotation, tree)]
17+
18+
while nodes:
19+
in_annotation, node = nodes.pop()
20+
21+
tp = type(node)
22+
for ast_func in funcs[tp]:
23+
yield from ast_func(in_annotation, node)
24+
25+
for name in reversed(node._fields):
26+
value = getattr(node, name)
27+
if name in {"annotation", "returns"}:
28+
next_in_annotation = True
29+
else:
30+
next_in_annotation = in_annotation
31+
if isinstance(value, ast.AST):
32+
nodes.append((next_in_annotation, value))
33+
elif isinstance(value, list):
34+
for value in reversed(value):
35+
if isinstance(value, ast.AST):
36+
nodes.append((next_in_annotation, value))
37+

Diff for: pandas_style_guide/check_for_exec.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import ast
2+
from pandas_style_guide._data import register
3+
4+
MSG = 'PSG001 do not use exec'
5+
@register(ast.Call)
6+
def check_for_exec(self, node):
7+
if node.func.id == 'exec':
8+
yield (node.lineno, node.col_offset, MSG)

Diff for: requirements-dev.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
covdefaults
2+
coverage
3+
pytest

Diff for: setup.cfg

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[metadata]
2+
name = pandas-style-guide
3+
version = 2.11.0
4+
description = A tool to automatically upgrade syntax for newer versions.
5+
long_description = file: README.md
6+
long_description_content_type = text/markdown
7+
url = https://github.com/asottile/pyupgrade
8+
author = Anthony Sottile
9+
author_email = [email protected]
10+
license = MIT
11+
license_file = LICENSE
12+
classifiers =
13+
License :: OSI Approved :: MIT License
14+
Programming Language :: Python :: 3
15+
Programming Language :: Python :: 3 :: Only
16+
Programming Language :: Python :: 3.6
17+
Programming Language :: Python :: 3.7
18+
Programming Language :: Python :: 3.8
19+
Programming Language :: Python :: 3.9
20+
Programming Language :: Python :: Implementation :: CPython
21+
Programming Language :: Python :: Implementation :: PyPy
22+
23+
[options]
24+
py_modules = pandas_style_guide
25+
install_requires =
26+
flake8>=3.8
27+
packages = find:
28+
python_requires = >=3.6.1
29+
30+
[options.packages.find]
31+
exclude =
32+
tests*
33+
testing*
34+
35+
[options.entry_points]
36+
flake8.extension =
37+
PSG=pandas_style_guide.__main__:Plugin
38+
39+
[bdist_wheel]
40+
universal = True
41+
42+
[coverage:run]
43+
plugins = covdefaults
44+
45+
[mypy]
46+
check_untyped_defs = true
47+
disallow_any_generics = true
48+
disallow_incomplete_defs = true
49+
disallow_untyped_defs = true
50+
no_implicit_optional = true
51+
52+
[mypy-testing.*]
53+
disallow_untyped_defs = false
54+
55+
[mypy-tests.*]
56+
disallow_untyped_defs = false

Diff for: setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from setuptools import setup
2+
setup()

Diff for: t.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = exec('3')
2+
3+
4+
exec('43')

Diff for: tests/__init__.py

Whitespace-only changes.

Diff for: tests/flake8_match_test.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import ast
2+
3+
from flake8_match import Plugin
4+
5+
6+
def results(s):
7+
return {'{}:{}: {}'.format(*r) for r in Plugin(ast.parse(s)).run()}
8+
9+
10+
def test_trivial():
11+
assert not results('')
12+
13+
14+
def test_assignment_expression_not_ok():
15+
src = '''\
16+
match 1:
17+
case 1:
18+
print(1)
19+
'''
20+
msg, = results(src)
21+
assert msg == '1:0: MAT001 do not use match statements'

Diff for: tox.ini

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[tox]
2+
envlist = py310,pre-commit
3+
4+
[testenv]
5+
deps = -rrequirements-dev.txt
6+
commands =
7+
coverage erase
8+
coverage run -m pytest {posargs:tests}
9+
coverage report --fail-under 100
10+
11+
[testenv:pre-commit]
12+
skip_install = true
13+
deps = pre-commit
14+
commands = pre-commit run --all-files --show-diff-on-failure
15+
16+
[pep8]
17+
ignore = E265,E501,W504

0 commit comments

Comments
 (0)