Skip to content

Commit ac2bcd2

Browse files
committed
Update code to use flake8-helper and ast NodeVisitor.
1 parent 24de8cd commit ac2bcd2

13 files changed

Lines changed: 160 additions & 1083 deletions

File tree

README.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
=======================
2+
flake8-unused-fstrings
3+
=======================
4+
5+
.. start short_desc
6+
7+
**Flake8 plugin to catch f-strings with no fields.**
8+
9+
.. end short_desc
10+
11+
12+
.. start shields
13+
14+
.. list-table::
15+
:stub-columns: 1
16+
:widths: 10 90
17+
18+
* - Tests
19+
- |actions_linux| |actions_windows| |actions_macos| |coveralls|
20+
* - Activity
21+
- |commits-latest| |commits-since| |maintained|
22+
* - QA
23+
- |codefactor| |actions_flake8| |actions_mypy|
24+
* - Other
25+
- |license| |language| |requires|
26+
27+
.. |actions_linux| image:: https://github.com/python-formate/flake8-unused-fstrings/workflows/Linux/badge.svg
28+
:target: https://github.com/python-formate/flake8-unused-fstrings/actions?query=workflow%3A%22Linux%22
29+
:alt: Linux Test Status
30+
31+
.. |actions_windows| image:: https://github.com/python-formate/flake8-unused-fstrings/workflows/Windows/badge.svg
32+
:target: https://github.com/python-formate/flake8-unused-fstrings/actions?query=workflow%3A%22Windows%22
33+
:alt: Windows Test Status
34+
35+
.. |actions_macos| image:: https://github.com/python-formate/flake8-unused-fstrings/workflows/macOS/badge.svg
36+
:target: https://github.com/python-formate/flake8-unused-fstrings/actions?query=workflow%3A%22macOS%22
37+
:alt: macOS Test Status
38+
39+
.. |actions_flake8| image:: https://github.com/python-formate/flake8-unused-fstrings/workflows/Flake8/badge.svg
40+
:target: https://github.com/python-formate/flake8-unused-fstrings/actions?query=workflow%3A%22Flake8%22
41+
:alt: Flake8 Status
42+
43+
.. |actions_mypy| image:: https://github.com/python-formate/flake8-unused-fstrings/workflows/mypy/badge.svg
44+
:target: https://github.com/python-formate/flake8-unused-fstrings/actions?query=workflow%3A%22mypy%22
45+
:alt: mypy status
46+
47+
.. |requires| image:: https://dependency-dash.repo-helper.uk/github/python-formate/flake8-unused-fstrings/badge.svg
48+
:target: https://dependency-dash.repo-helper.uk/github/python-formate/flake8-unused-fstrings/
49+
:alt: Requirements Status
50+
51+
.. |coveralls| image:: https://img.shields.io/coveralls/github/python-formate/flake8-unused-fstrings/master?logo=coveralls
52+
:target: https://coveralls.io/github/python-formate/flake8-unused-fstrings?branch=master
53+
:alt: Coverage
54+
55+
.. |codefactor| image:: https://img.shields.io/codefactor/grade/github/python-formate/flake8-unused-fstrings?logo=codefactor
56+
:target: https://www.codefactor.io/repository/github/python-formate/flake8-unused-fstrings
57+
:alt: CodeFactor Grade
58+
59+
.. |license| image:: https://img.shields.io/github/license/python-formate/flake8-unused-fstrings
60+
:target: https://github.com/python-formate/flake8-unused-fstrings/blob/master/LICENSE
61+
:alt: License
62+
63+
.. |language| image:: https://img.shields.io/github/languages/top/python-formate/flake8-unused-fstrings
64+
:alt: GitHub top language
65+
66+
.. |commits-since| image:: https://img.shields.io/github/commits-since/python-formate/flake8-unused-fstrings/v1.0.1
67+
:target: https://github.com/python-formate/flake8-unused-fstrings/pulse
68+
:alt: GitHub commits since tagged version
69+
70+
.. |commits-latest| image:: https://img.shields.io/github/last-commit/python-formate/flake8-unused-fstrings
71+
:target: https://github.com/python-formate/flake8-unused-fstrings/commit/master
72+
:alt: GitHub last commit
73+
74+
.. |maintained| image:: https://img.shields.io/maintenance/yes/2026
75+
:alt: Maintenance
76+
77+
.. end shields
78+
79+
Installation
80+
--------------
81+
82+
.. start installation
83+
84+
``flake8-unused-fstrings`` can be installed from GitHub.
85+
86+
To install with ``pip``:
87+
88+
.. code-block:: bash
89+
90+
$ python -m pip install git+https://github.com/python-formate/flake8-unused-fstrings
91+
92+
.. end installation
93+
94+
95+
Rules
96+
------
97+
98+
* **NUF001**: f-string without interpolation.

flake8_unused_fstrings/__init__.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
#
3+
# __init__.py
4+
"""
5+
Flake8 plugin to catch f-strings with no fields.
6+
"""
7+
#
8+
# Copyright © 2019-2020 adam hitchcock <adam+python@northisup.com>
9+
# License: ISC License (ISCL)
10+
# See the LICENSE file for details.
11+
#
12+
13+
# stdlib
14+
import ast
15+
16+
# 3rd party
17+
import flake8_helper
18+
19+
__author__: str = "adam hitchcock"
20+
__copyright__: str = "2019-2020 adam hitchcock"
21+
__license__: str = "ISC License (ISCL)"
22+
__version__: str = "1.0.1"
23+
__email__: str = "adam+python@northisup.com"
24+
25+
__all__ = ["Plugin", "Visitor"]
26+
27+
NUF001 = "NUF001 f-string without interpolation."
28+
29+
30+
class Visitor(flake8_helper.Visitor):
31+
"""
32+
AST node visitor for identifying f-strings with no fields.
33+
"""
34+
35+
def check_joinedstring_has_formatted_value(self, node: ast.JoinedStr) -> bool:
36+
return any(isinstance(value, ast.FormattedValue) for value in ast.walk(node))
37+
38+
def visit_JoinedStr(self, node: ast.JoinedStr) -> None: # noqa: D102
39+
is_good = self.check_joinedstring_has_formatted_value(node)
40+
if not is_good:
41+
self.errors.append((
42+
node.lineno,
43+
node.col_offset,
44+
NUF001,
45+
))
46+
47+
48+
class Plugin(flake8_helper.Plugin[Visitor]):
49+
"""
50+
A Flake8 plugin which checks for f-strings with no fields.
51+
52+
:param tree: The abstract syntax tree (AST) to check.
53+
"""
54+
55+
name: str = __name__
56+
57+
#: The plugin version
58+
version: str = __version__
59+
60+
visitor_class = Visitor

flake8_unused_fstrings/py.typed

Whitespace-only changes.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
flake8>=4
1+
flake8-helper>=0.1.1

requirements/compile.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)