Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Python 3.12. #332

Merged
merged 4 commits into from
Oct 6, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- name: Checkout code
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def find_version(*parts):
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Quality Assurance",
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = cleanup, py{38,310,311}, style # Skip py39 since it chokes on distutils.
envlist = cleanup, py{38,310,311,312}, style # Skip py39 since it chokes on distutils.
skip_missing_interpreters = true

# Erase old coverage results, then accumulate them during this tox run.
Expand All @@ -13,7 +13,7 @@ commands =
deps =
coverage==7.0.5
pint # Use latest version to catch API changes.
pytest==7.2.1
pytest==7.4.2
pytest-cov==4.0.0
commands =
pytest {posargs}
Expand Down
16 changes: 8 additions & 8 deletions vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,11 @@ def visit_BinOp(self, node):
"%(my_var)s" % locals()
"""
if (
isinstance(node.left, ast.Str)
utils.is_ast_string(node.left)
and isinstance(node.op, ast.Mod)
and self._is_locals_call(node.right)
):
self.used_names |= set(re.findall(r"%\((\w+)\)", node.left.s))
self.used_names |= set(re.findall(r"%\((\w+)\)", node.left.value))

def visit_Call(self, node):
# Count getattr/hasattr(x, "some_attr", ...) as usage of some_attr.
Expand All @@ -531,21 +531,21 @@ def visit_Call(self, node):
or (node.func.id == "hasattr" and len(node.args) == 2)
):
attr_name_arg = node.args[1]
if isinstance(attr_name_arg, ast.Str):
self.used_names.add(attr_name_arg.s)
if utils.is_ast_string(attr_name_arg):
self.used_names.add(attr_name_arg.value)

# Parse variable names in new format strings:
# "{my_var}".format(**locals())
if (
isinstance(node.func, ast.Attribute)
and isinstance(node.func.value, ast.Str)
and utils.is_ast_string(node.func.value)
and node.func.attr == "format"
and any(
kw.arg is None and self._is_locals_call(kw.value)
for kw in node.keywords
)
):
self._handle_new_format_string(node.func.value.s)
self._handle_new_format_string(node.func.value.value)

def _handle_new_format_string(self, s):
def is_identifier(name):
Expand Down Expand Up @@ -651,8 +651,8 @@ def visit_Assign(self, node):
if _assigns_special_variable__all__(node):
assert isinstance(node.value, (ast.List, ast.Tuple))
for elt in node.value.elts:
if isinstance(elt, ast.Str):
self.used_names.add(elt.s)
if utils.is_ast_string(elt):
self.used_names.add(elt.value)

def visit_While(self, node):
self._handle_conditional_node(node, "while")
Expand Down
4 changes: 4 additions & 0 deletions vulture/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def condition_is_always_true(condition):
return _safe_eval(condition, False)


def is_ast_string(node):
return isinstance(node, ast.Constant) and isinstance(node.value, str)


def format_path(path):
try:
return path.relative_to(pathlib.Path.cwd())
Expand Down