Visitar URL original
Lower version guard in check_ast_node to Python 3.12 by rcgray · Pull Request #1355 · PyCQA/bandit · GitHub
Skip to content

Lower version guard in check_ast_node to Python 3.12 - #1355

Merged
ericwb merged 1 commit into
PyCQA:mainfrom
rcgray:fix-deprecation-warning-python-312
Feb 23, 2026
Merged

ericwb merged 1 commit into
PyCQA:mainfrom
rcgray:fix-deprecation-warning-python-312

Conversation

@rcgray

@rcgray rcgray commented Feb 7, 2026

Copy link
Copy Markdown
Contributor

Summary

The version guard in check_ast_node() for deprecated AST node types (Num, Str, Bytes, Ellipsis, NameConstant) currently activates only on Python >= 3.14, where these types are fully removed. However, accessing these types via getattr(ast, name) emits DeprecationWarning on Python 3.12 and 3.13.

This one-line change lowers the guard from (3, 14) to (3, 12) to avoid the deprecation warning on currently-supported Python versions.

Background

PR #1323 introduced the version guard targeting Python 3.14 where these AST types were removed entirely. The guard correctly short-circuits to return the name without calling getattr(ast, name). However, the runtime DeprecationWarning for these types was added in Python 3.12 (see Python 3.12 docs: Pending removal in 3.14, contributed by Serhiy Storchaka in CPython gh-90953), and the getattr call triggers this warning on 3.12 and 3.13.

Four plugins register checks against the "Str" node type, causing the warning to fire during plugin loading:

  • general_bind_all_interfaces.py
  • general_hardcoded_password.py
  • general_hardcoded_tmp.py
  • injection_sql.py

Change

-        if sys.version_info >= (3, 14) and name in (
+        if sys.version_info >= (3, 12) and name in (

The early return value is unchanged: the node name is known-valid and is returned directly. The only difference is that we skip the getattr call that triggers the warning.

Testing

Existing tests pass unchanged. The early-return path produces the same result as the getattr path for these known node names.

The version guard for deprecated AST node types (Num, Str, Bytes,
Ellipsis, NameConstant) currently activates only on Python >= 3.14,
where these types are removed. However, these types emit
DeprecationWarning on Python 3.12 and 3.13 when accessed via getattr.

Lower the guard from (3, 14) to (3, 12) to avoid triggering
DeprecationWarning on Python 3.12-3.13. The early return behavior is
identical: the node name is known-valid and is returned directly,
skipping the unnecessary getattr call.
@ericwb
ericwb merged commit c8c8a55 into PyCQA:main Feb 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants