Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions _flake8_tergeo/checks/ast_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def check_call(node: ast.Call) -> IssueGenerator:
yield from _check_type_none_in_isinstance(node)
yield from _check_mock_builtins_open(node)
yield from _check_sys_trace_functions(node)
yield from _check_utf_8_encoding(node)


def _check_os_walk(node: ast.Call) -> IssueGenerator:
Expand Down Expand Up @@ -867,3 +868,24 @@ def _check_sys_trace_functions(node: ast.Call) -> IssueGenerator:
issue_number="138",
message=f"Trace function {name} should not be called.",
)


def _check_utf_8_encoding(node: ast.Call) -> IssueGenerator:
if get_python_version() < (3, 15):
return
for keyword in node.keywords:
if (
keyword.arg == "encoding"
and is_constant_node(keyword.value, str)
and cast(str, keyword.value.value).lower() == "utf-8"
):
yield Issue(
line=node.lineno,
column=node.col_offset,
issue_number="140",
message=(
"UTF-8 encoding is the default in Python 3.15 and does not need to be "
"specified. Remove the encoding argument or specify a different encoding if "
"needed."
),
)
5 changes: 5 additions & 0 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ Finds calls of `sys` trace functions like `sys.settrace`.
Checks if `profile` or `cProfile` are imported as with python 3.15 `profiling.tracing` should be used.
The check is only active for python 3.15 and onwards.

## FTP140
Checks if `encoding="UTF-8"` is used in any function call as UTF-8 is the default encoding
starting with python 3.15.
The check is only active for python 3.15 and onwards.

## FTP200
Find calls of `flask.abort` and `werkzeug.exceptions.abort`.
Instead of calling this helper function raise the appropriate exception directly
Expand Down
1 change: 1 addition & 0 deletions news/+e471e45f.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FTP140 which can find unnecessary UTF-8 encodings
21 changes: 21 additions & 0 deletions tests/checks/test_ast_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@
issue_number="FTP138",
message="Trace function {func} should not be called.",
)
FTP140 = partial(
Issue,
issue_number="FTP140",
message=(
"UTF-8 encoding is the default in Python 3.15 and does not need to be specified. "
"Remove the encoding argument or specify a different encoding if needed."
),
)


def FTP073( # pylint:disable=invalid-name
Expand Down Expand Up @@ -1104,3 +1112,16 @@ def test(self, runner: Flake8RunnerFixture, imp: str, func: str) -> None:
filename="ftp138.txt", issue_number="FTP138", imp=imp, func=func
)
assert results == [FTP138(line=9, column=1, func=func)]


@pytest.mark.parametrize(
"python_version,find_by_version", [("3.10.0", False), ("3.15.1", True)]
)
def test_ftp140(
runner: Flake8RunnerFixture, python_version: str, find_by_version: bool
) -> None:
assert runner(
filename="ftp140.txt",
issue_number="FTP140",
args=("--ftp-python-version", python_version),
) == ([FTP140(line=8, column=1)] if find_by_version else [])
8 changes: 8 additions & 0 deletions tests/checks/test_ast_call/ftp140.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# code without issues
x()
x = 1
x(env="utf-8")
foo(encoding="utf-16")

# code with issues
foo(encoding="utf-8")