Skip to content

gh-141778: add missing validation in ast.literal_eval() for non-string input#142969

Draft
skirpichev wants to merge 7 commits into
python:mainfrom
skirpichev:validation-of-Constants/141778
Draft

gh-141778: add missing validation in ast.literal_eval() for non-string input#142969
skirpichev wants to merge 7 commits into
python:mainfrom
skirpichev:validation-of-Constants/141778

Conversation

@skirpichev
Copy link
Copy Markdown
Member

@skirpichev skirpichev commented Dec 19, 2025

This also changes parsing of the private __text_signature__ attribute by inspect.signature(). Now we accept here only types, valid for ast.Constant().

…-string input

This also changes parsing of the private `__text_signature__` attribute
by inspect.signature().  Now we accept here only types, valid for
ast.Constant().
Comment thread Lib/ast.py
_type_Ellipsis = type(...)


def _convert_literal(node, omit_validation=False):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this change? It may make life harder for some users.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you be more precise, any example?

We need this, because literal_eval() accepts invalid AST for non-string input. Per documentation:

The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis.

@skirpichev skirpichev closed this Dec 25, 2025
@skirpichev skirpichev deleted the validation-of-Constants/141778 branch December 25, 2025 21:29
@skirpichev skirpichev restored the validation-of-Constants/141778 branch May 23, 2026 08:18
@skirpichev skirpichev reopened this May 23, 2026
@skirpichev skirpichev marked this pull request as draft May 23, 2026 08:18
@skirpichev skirpichev self-assigned this May 23, 2026
@read-the-docs-community
Copy link
Copy Markdown

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