Skip to content

Commit

Permalink
feat!: change codes from TC to TRY
Browse files Browse the repository at this point in the history
Originally implemented by @exister on PR #55
  • Loading branch information
guilatrova committed Apr 29, 2023
1 parent 45eb916 commit 4998fd2
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
.idea
*.egg-info
*.pyc
*.log
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ You can enable experimental analyzers by running:
tryceratops --experimental [filename or dir...]
```

You can ignore specific violations by using: `--ignore TCXXX` repeatedly:
You can ignore specific violations by using: `--ignore TRYXXX` repeatedly:

```
tryceratops --ignore TC201 --ignore TC202 [filename or dir...]
tryceratops --ignore TRY201 --ignore TRY202 [filename or dir...]
```

You can exclude dirs by using: `--exclude dir/path` repeatedly:
Expand All @@ -90,10 +90,10 @@ tryceratops --autofix [filename or dir...]
🦖 Tryceratops is also a plugin for `flake8`, so you can:

```
❯ flake8 --select TC src/tests/samples/violations/call_raise_vanilla.py
src/tests/samples/violations/call_raise_vanilla.py:13:9: TC002 Create your own exception
src/tests/samples/violations/call_raise_vanilla.py:13:9: TC003 Avoid specifying long messages outside the exception class
src/tests/samples/violations/call_raise_vanilla.py:21:9: TC201 Simply use 'raise' without specifying exception object again
❯ flake8 --select TRY src/tests/samples/violations/call_raise_vanilla.py
src/tests/samples/violations/call_raise_vanilla.py:13:9: TRY002 Create your own exception
src/tests/samples/violations/call_raise_vanilla.py:13:9: TRY003 Avoid specifying long messages outside the exception class
src/tests/samples/violations/call_raise_vanilla.py:21:9: TRY201 Simply use 'raise' without specifying exception object again
```

## Violations
Expand All @@ -102,7 +102,7 @@ All violations and its descriptions can be found in [docs](https://github.com/gu

### Autofix support

So far, autofix only supports violations: [TC200](docs/violations/TC200.md), [TC201](docs/violations/TC201.md), and [TC400](docs/violations/TC400.md).
So far, autofix only supports violations: [TRY200](docs/violations/TRY200.md), [TRY201](docs/violations/TRY201.md), and [TRY400](docs/violations/TRY400.md).

### Ignoring violations

Expand All @@ -119,7 +119,7 @@ def verbose_reraise_1():
try:
a = 1
except Exception as ex:
raise ex # noqa: TC202
raise ex # noqa: TRY202
```

### Configuration
Expand All @@ -132,7 +132,7 @@ Example:
```toml
[tool.tryceratops]
exclude = ["samples"]
ignore = ["TC002", "TC200", "TC300"]
ignore = ["TRY002", "TRY200", "TRY300"]
experimental = true
```

Expand Down
14 changes: 7 additions & 7 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ or test it against some violation files we have in place:

```sh
❯ poetry run tryceratops src/tests/samples/violations/call_too_many_try.py
[TC101] Too many try blocks in your function - src/tests/samples/violations/call_too_many_try.py:15:4
[TC101] Too many try blocks in your function - src/tests/samples/violations/call_too_many_try.py:27:4
[TC101] Too many try blocks in your function - src/tests/samples/violations/call_too_many_try.py:32:4
[TRY101] Too many try blocks in your function - src/tests/samples/violations/call_too_many_try.py:15:4
[TRY101] Too many try blocks in your function - src/tests/samples/violations/call_too_many_try.py:27:4
[TRY101] Too many try blocks in your function - src/tests/samples/violations/call_too_many_try.py:32:4
Done processing! 🦖✨
Processed 1 files
Found 3 violations
Expand All @@ -61,10 +61,10 @@ Found 3 violations
You can try it with flake8 if preferred:

```sh
❯ poetry run flake8 --select TC src/tests/samples/violations/call_too_many_try.py
src/tests/samples/violations/call_too_many_try.py:15:5: TC101 Too many try blocks in your function
src/tests/samples/violations/call_too_many_try.py:27:5: TC101 Too many try blocks in your function
src/tests/samples/violations/call_too_many_try.py:32:5: TC101 Too many try blocks in your function
❯ poetry run flake8 --select TRY src/tests/samples/violations/call_too_many_try.py
src/tests/samples/violations/call_too_many_try.py:15:5: TRY101 Too many try blocks in your function
src/tests/samples/violations/call_too_many_try.py:27:5: TRY101 Too many try blocks in your function
src/tests/samples/violations/call_too_many_try.py:32:5: TRY101 Too many try blocks in your function
```

## Linting
Expand Down
34 changes: 17 additions & 17 deletions docs/violations/README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# Violations

## `TC0xx` - Exception Classes
## `TRY0xx` - Exception Classes


| Code | Description |
| ----------------- | ---------------------------------------------------------- |
| [TC002](TC002.md) | Create your own exception |
| [TC003](TC003.md) | Avoid specifying long messages outside the exception class |
| [TC004](TC004.md) | Prefer `TypeError` exception for invalid type |
| [TRY002](TRY002.md) | Create your own exception |
| [TRY003](TRY003.md) | Avoid specifying long messages outside the exception class |
| [TRY004](TRY004.md) | Prefer `TypeError` exception for invalid type |

## `TC1xx` - General
## `TRY1xx` - General


| Code | Description |
| ------------------------------------ | --------------------- |
| [TC100](TC100.md) (**EXPERIMENTAL**) | Check to continue |
| [TC101](TC101.md) | Too many `try` blocks |
| [TRY100](TRY100.md) (**EXPERIMENTAL**) | Check to continue |
| [TRY101](TRY101.md) | Too many `try` blocks |

## `TC2xx` - Except blocks
## `TRY2xx` - Except blocks


| Code | Description |
| ----------------- | --------------------------------------------------- |
| [TC200](TC200.md) | Use `raise Exception from` |
| [TC201](TC201.md) | Simply use `raise` |
| [TC202](TC202.md) | Don't ignore a broad exception without even logging |
| [TRY200](TRY200.md) | Use `raise Exception from` |
| [TRY201](TRY201.md) | Simply use `raise` |
| [TRY202](TRY202.md) | Don't ignore a broad exception without even logging |

## `TC3xx` - Try blocks
## `TRY3xx` - Try blocks


| Code | Description |
| ----------------- | --------------------------------- |
| [TC300](TC300.md) | Consider adding an `else` block |
| [TC301](TC301.md) | Avoid direct raises in `try` body |
| [TRY300](TRY300.md) | Consider adding an `else` block |
| [TRY301](TRY301.md) | Avoid direct raises in `try` body |

## `TC4xx` - Logging usage
## `TRY4xx` - Logging usage


| Code | Description |
| ----------------- | -------------------------------------------- |
| [TC400](TC400.md) | Use logging `.exception` instead of `.error` |
| [TC401](TC401.md) | Do not log the exception object |
| [TRY400](TRY400.md) | Use logging `.exception` instead of `.error` |
| [TRY401](TRY401.md) | Do not log the exception object |
2 changes: 1 addition & 1 deletion docs/violations/TC002.md → docs/violations/TRY002.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC002` - Create your own exception
# `TRY002` - Create your own exception

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC003.md → docs/violations/TRY003.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC003` - Avoid specifying messages outside exception class
# `TRY003` - Avoid specifying messages outside exception class

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC004.md → docs/violations/TRY004.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC004` - Prefer `TypeError` exception for invalid type
# `TRY004` - Prefer `TypeError` exception for invalid type

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC100.md → docs/violations/TRY100.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC100` - Don't check to continue, raise an exception instead
# `TRY100` - Don't check to continue, raise an exception instead

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC101.md → docs/violations/TRY101.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC101` - Too many try blocks in your function
# `TRY101` - Too many try blocks in your function

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC200.md → docs/violations/TRY200.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC200` - Use 'raise from' to specify cause
# `TRY200` - Use 'raise from' to specify cause

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC201.md → docs/violations/TRY201.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC201` - Verbose reraise
# `TRY201` - Verbose reraise

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC202.md → docs/violations/TRY202.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC202` - Verbose reraise
# `TRY202` - Verbose reraise

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC300.md → docs/violations/TRY300.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC300` - Consider `else` block
# `TRY300` - Consider `else` block

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC301.md → docs/violations/TRY301.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC301` - `raise` within `try`
# `TRY301` - `raise` within `try`

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC400.md → docs/violations/TRY400.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC400` - Use logging '.exception' instead of 'error'
# `TRY400` - Use logging '.exception' instead of 'error'

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion docs/violations/TC401.md → docs/violations/TRY401.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `TC401` - Do not log the exception object
# `TRY401` - Do not log the exception object

## Why is it bad

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ packages = [
tryceratops = 'tryceratops.__main__:main'

[tool.poetry.plugins."flake8.extension"]
TC = "tryceratops.flake_plugin:TryceratopsAdapterPlugin"
TRY = "tryceratops.flake_plugin:TryceratopsAdapterPlugin"

[tool.poetry.urls]
"Changelog" = "https://github.com/guilatrova/tryceratops/blob/main/CHANGELOG.md"
Expand Down
2 changes: 1 addition & 1 deletion src/tests/analyzers_try_block_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_finally_dont_consider_else():
assert len(violations) == 1
violation = violations[0]

assert_consider(31, 8, violation)
assert_consider(30, 8, violation)


def test_try_inner_raise():
Expand Down
60 changes: 30 additions & 30 deletions src/tests/files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def test_parse_specific_code_line():

first, second, third = ignore_lines
assert first.line == 13
assert first.code == ["TC202"]
assert first.code == ["TRY202"]

assert second.line == 21
assert second.code == ["TC202", "TC200", "TC201"]
assert second.code == ["TRY202", "TRY200", "TRY201"]

assert third.line == 30
assert third.code == ["TC101"]
assert third.code == ["TRY101"]


def test_parse_ignore_file():
Expand All @@ -61,56 +61,56 @@ def test_parse_ignore_file():
def test_entity_ignores_all():
ignore = IgnoreViolation(10)

assert ignore.is_ignoring(10, "TC200") is True
assert ignore.is_ignoring(10, "TC100") is True
assert ignore.is_ignoring(10, "TC300") is True
assert ignore.is_ignoring(10, "TRY200") is True
assert ignore.is_ignoring(10, "TRY100") is True
assert ignore.is_ignoring(10, "TRY300") is True
assert ignore.is_ignoring(10, "anything") is True

assert ignore.is_ignoring(12, "TC200") is False
assert ignore.is_ignoring(12, "TC100") is False
assert ignore.is_ignoring(12, "TC300") is False
assert ignore.is_ignoring(12, "TRY200") is False
assert ignore.is_ignoring(12, "TRY100") is False
assert ignore.is_ignoring(12, "TRY300") is False
assert ignore.is_ignoring(12, "anything") is False


def test_entity_ignores_specific():
ignore = IgnoreViolation(10, ["TC200", "TC101"])
ignore = IgnoreViolation(10, ["TRY200", "TRY101"])

assert ignore.is_ignoring(10, "TC200") is True
assert ignore.is_ignoring(10, "TC101") is True
assert ignore.is_ignoring(10, "TC100") is False
assert ignore.is_ignoring(10, "TC300") is False
assert ignore.is_ignoring(10, "TRY200") is True
assert ignore.is_ignoring(10, "TRY101") is True
assert ignore.is_ignoring(10, "TRY100") is False
assert ignore.is_ignoring(10, "TRY300") is False
assert ignore.is_ignoring(10, "anything") is False


def test_entity_ignore_all_whole_file():
ignore = IgnoreViolation(1)

assert ignore.is_ignoring(10, "TC200") is True
assert ignore.is_ignoring(10, "TC100") is True
assert ignore.is_ignoring(10, "TC300") is True
assert ignore.is_ignoring(10, "TRY200") is True
assert ignore.is_ignoring(10, "TRY100") is True
assert ignore.is_ignoring(10, "TRY300") is True
assert ignore.is_ignoring(10, "anything") is True

# Still true
assert ignore.is_ignoring(12, "TC200") is True
assert ignore.is_ignoring(12, "TC100") is True
assert ignore.is_ignoring(12, "TC300") is True
assert ignore.is_ignoring(12, "TRY200") is True
assert ignore.is_ignoring(12, "TRY100") is True
assert ignore.is_ignoring(12, "TRY300") is True
assert ignore.is_ignoring(12, "anything") is True


def test_entity_ignore_specific_whole_file():
ignore = IgnoreViolation(1, ["TC200", "TC101"])
ignore = IgnoreViolation(1, ["TRY200", "TRY101"])

# Any line
assert ignore.is_ignoring(10, "TC200") is True
assert ignore.is_ignoring(10, "TC101") is True
assert ignore.is_ignoring(20, "TC200") is True
assert ignore.is_ignoring(20, "TC101") is True
assert ignore.is_ignoring(30, "TC200") is True
assert ignore.is_ignoring(30, "TC101") is True
assert ignore.is_ignoring(10, "TRY200") is True
assert ignore.is_ignoring(10, "TRY101") is True
assert ignore.is_ignoring(20, "TRY200") is True
assert ignore.is_ignoring(20, "TRY101") is True
assert ignore.is_ignoring(30, "TRY200") is True
assert ignore.is_ignoring(30, "TRY101") is True

# Any other violation
assert ignore.is_ignoring(10, "TC300") is False
assert ignore.is_ignoring(10, "TRY300") is False
assert ignore.is_ignoring(10, "anything") is False
assert ignore.is_ignoring(20, "TC002") is False
assert ignore.is_ignoring(30, "TC301") is False
assert ignore.is_ignoring(20, "TRY002") is False
assert ignore.is_ignoring(30, "TRY301") is False
assert ignore.is_ignoring(30, "anything") is False
6 changes: 3 additions & 3 deletions src/tests/samples/ignore_comments/ignore_line_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def verbose_reraise_1():
except (NotImplementedError, ValueError) as ex:
raise
except Exception as ex:
raise ex # noqa: TC202
raise ex # noqa: TRY202


def verbose_reraise_2():
try:
a = 1
except Exception as ex:
if a == 1:
raise ex # noqa: TC202, TC200, TC201 I want to skip
raise ex # noqa: TRY202, TRY200, TRY201 I want to skip


def too_many_try():
Expand All @@ -27,7 +27,7 @@ def too_many_try():
except Exception:
raise

try: # noqa:TC101 this is not a big deal
try: # noqa:TRY101 this is not a big deal
b = 2
except Exception:
raise
2 changes: 1 addition & 1 deletion src/tryceratops/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
IGNORE_OPTION = dict(
multiple=True,
help="A violation to be ignored. e.g. -i TC200 -i TC201",
help="A violation to be ignored. e.g. -i TRY200 -i TRY201",
type=click.Choice(CODE_CHOICES),
)
EXCLUDE_OPTION = dict(multiple=True, help="A dir to be excluded. e.g. -x tests/ -x fixtures/")
Expand Down
2 changes: 1 addition & 1 deletion src/tryceratops/files/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tryceratops.filters import FileFilter, IgnoreViolation

IGNORE_TRYCERATOPS_TOKEN = "noqa"
IGNORE_TOKEN_PATT = r"noqa(: ?((TC\d{3},? ?)+))?"
IGNORE_TOKEN_PATT = r"noqa(: ?((TRY\d{3},? ?)+))?"


def _build_ignore_line(match: re.Match, location: Tuple[int, int]) -> IgnoreViolation:
Expand Down
2 changes: 1 addition & 1 deletion src/tryceratops/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PyprojectConfig(TypedDict):
"""
Represents the expected pyproject config to be loaded
exclude: a list of path patterns to be excluded e.g. [/tests, /fixtures]
ignore: a list of violations to be completely ignored e.g. [TC002, TC300]
ignore: a list of violations to be completely ignored e.g. [TRY002, TRY300]
experimental: whether to enable experimental analyzers
"""

Expand Down
Loading

0 comments on commit 4998fd2

Please sign in to comment.