summaryrefslogtreecommitdiffstats
path: root/Lib/ast.py
Commit message (Collapse)AuthorAgeFilesLines
* gh-91818: Use default program name in the CLI of many modules (GH-124867)Serhiy Storchaka2024-10-091-1/+1
| | | | | | As argparse now detects by default when the code was run as a module. This leads to using the actual executable name instead of simply "python" to display in the usage message ("usage: python -m ...").
* gh-121210: handle nodes with missing attributes/fields in `ast.compare` ↵Bénédikt Tran2024-07-021-4/+15
| | | | (#121211)
* gh-117865: Defer import of re in ast (#119546)Jelle Zijlstra2024-05-281-2/+7
| | | | | This is used only by ast.get_source_segment(), so it seems sensible to avoid importing it. Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
* gh-119562: Remove unused private string constants from `ast.py` (#119576)Alex Waygood2024-05-261-9/+0
|
* gh-119562: Remove AST nodes deprecated since Python 3.8 (#119563)Alex Waygood2024-05-261-173/+1
|
* gh-60191: Implement ast.compare (#19211)Batuhan Taskaya2024-05-221-0/+71
| | | | | | | | | | * bpo-15987: Implement ast.compare Add a compare() function that compares two ASTs for structural equality. There are two set of attributes on AST node objects, fields and attributes. The fields are always compared, since they represent the actual structure of the code. The attributes can be optionally be included in the comparison. Attributes capture things like line numbers of column offsets, so comparing them involves test whether the layout of the program text is the same. Since whitespace seems inessential for comparing ASTs, the default is to compare fields but not attributes. ASTs are just Python objects that can be modified in arbitrary ways. The API for ASTs is under-specified in the presence of user modifications to objects. The comparison respects modifications to fields and attributes, and to _fields and _attributes attributes. A user could create obviously malformed objects, and the code will probably fail with an AttributeError when that happens. (For example, adding "spam" to _fields but not adding a "spam" attribute to the object.) Co-authored-by: Jeremy Hylton <jeremy@alum.mit.edu>
* gh-116126: Implement PEP 696 (#116129)Jelle Zijlstra2024-05-031-0/+9
| | | | | Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
* gh-116023: Add `show_empty=False` to `ast.dump` (#116037)Nikita Sobolev2024-04-241-1/+20
| | | Co-authored-by: Carl Meyer <carl@oddbird.net>
* gh-112364: Correct unparsing of backslashes and quotes in ast.unparse (#115696)Frank Hoffmann2024-02-211-7/+8
|
* Clean up backslash avoiding code in ast, fix typo (#113605)Shantanu2024-01-161-8/+5
| | | | | | As of #108553, the `_avoid_backslashes` code path is dead `scape_newlines` was introduced in #110271. Happy to drop the typo fix if we don't want it
* gh-58032: Do not use argparse.FileType in module CLIs and scripts (GH-113649)Serhiy Storchaka2024-01-101-5/+9
| | | | | Open and close files manually. It prevents from leaking files, preliminary creation of output files, and accidental closing of stdin and stdout.
* gh-110259: Fix f-strings with multiline expressions and format specs (#110271)Pablo Galindo Salgado2023-10-051-3/+8
| | | Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
* gh-108843: fix ast.unparse for f-string with many quotes (#108981)Shantanu2023-09-181-1/+20
|
* gh-108469: Update ast.unparse for unescaped quote support from PEP701 [3.12] ↵Anthony Shaw2023-09-051-21/+10
| | | | | | (#108553) Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
* gh-108113: Make it possible to create an optimized AST (#108154)Irit Katriel2023-08-211-2/+4
|
* gh-104799: PEP 695 backward compatibility for ast.unparse (#105846)Jelle Zijlstra2023-06-161-2/+4
|
* gh-104656: Rename typeparams AST node to type_params (#104657)Jelle Zijlstra2023-05-221-6/+6
|
* gh-103763: Implement PEP 695 (#103764)Jelle Zijlstra2023-05-161-0/+26
| | | | | | | | | | | | | | This implements PEP 695, Type Parameter Syntax. It adds support for: - Generic functions (def func[T](): ...) - Generic classes (class X[T](): ...) - Type aliases (type X = ...) - New scoping when the new syntax is used within a class body - Compiler and interpreter changes to support the new syntax and scoping rules Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Eric Traut <eric@traut.com> Co-authored-by: Larry Hastings <larry@hastings.org> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
* gh-90953: Emit deprecation warnings for `ast` features deprecated in Python ↵Alex Waygood2023-05-061-8/+74
| | | | | | | 3.8 (#104199) `ast.Num`, `ast.Str`, `ast.Bytes`, `ast.Ellipsis` and `ast.NameConstant` now all emit deprecation warnings on import, access, instantation or `isinstance()` checks. Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
* gh-103285: Rewrite _splitlines_no_ff to improve performance (#103307)Tian Gao2023-04-241-18/+8
|
* gh-99341: Cover type ignore nodes when incrementing line numbers (GH-99422)Batuhan Taskaya2022-11-221-0/+6
|
* gh-95588: Drop the safety claim from `ast.literal_eval` docs. (#95919)Gregory P. Smith2022-10-021-1/+3
| | | It was never really safe and this claim conflicts directly with the big warning in the docs about it being able to crash the interpreter.
* gh-92986: Fix ast.unparse when ImportFrom.level is None (#92992)Shantanu2022-09-051-1/+1
| | | | | This doesn't happen naturally, but is allowed by the ASDL and compiler. We don't want to change ASDL for backward compatibility reasons (#57645, #92987)
* ast.parse: check `feature_version` common case first (GH-94640)Anthony Sottile2022-08-291-3/+3
|
* gh-95066: ast: Replace assert with ValueError (GH-95072)Shantanu2022-07-261-1/+2
| | | Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
* gh-92671: Don't omit parentheses when unparsing empty tuples (GH-92673)Batuhan Taskaya2022-05-161-1/+5
|
* bpo-43224: Implement PEP 646 grammar changes (GH-31018)Matthew Rahtz2022-03-261-6/+3
| | | Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
* bpo-45292: [PEP-654] add except* (GH-29581)Irit Katriel2021-12-141-2/+19
|
* Fix typos in multiple files (GH-26689)Binbin2021-06-131-1/+1
| | | Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
* bpo-44142: drop redundant parantheses when unparsing tuples as assignment ↵Batuhan Taskaya2021-05-161-3/+6
| | | | targets (GH-26156)
* bpo-44081: improve ast.unparse() for lambdas with no parameters (GH-26000)Batuhan Taskaya2021-05-151-5/+8
|
* bpo-43417: Better buffer handling for ast.unparse (GH-24772)Batuhan Taskaya2021-05-081-57/+59
|
* Fix typo in ast.py (GH-25740)Ikko Ashimine2021-05-041-2/+2
| | | parantheses -> parentheses
* bpo-43892: Make match patterns explicit in the AST (GH-25585)Nick Coghlan2021-04-291-4/+75
| | | Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
* bpo-38659: [Enum] add _simple_enum decorator (GH-25497)Ethan Furman2021-04-211-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | add: * `_simple_enum` decorator to transform a normal class into an enum * `_test_simple_enum` function to compare * `_old_convert_` to enable checking `_convert_` generated enums `_simple_enum` takes a normal class and converts it into an enum: @simple_enum(Enum) class Color: RED = 1 GREEN = 2 BLUE = 3 `_old_convert_` works much like` _convert_` does, using the original logic: # in a test file import socket, enum CheckedAddressFamily = enum._old_convert_( enum.IntEnum, 'AddressFamily', 'socket', lambda C: C.isupper() and C.startswith('AF_'), source=_socket, ) `_test_simple_enum` takes a traditional enum and a simple enum and compares the two: # in the REPL or the same module as Color class CheckedColor(Enum): RED = 1 GREEN = 2 BLUE = 3 _test_simple_enum(CheckedColor, Color) _test_simple_enum(CheckedAddressFamily, socket.AddressFamily) Any important differences will raise a TypeError
* Revert "bpo-38659: [Enum] add _simple_enum decorator (GH-25285)" (GH-25476)Ethan Furman2021-04-201-3/+2
| | | This reverts commit dbac8f40e81eb0a29dc833e6409a1abf47467da6.
* bpo-38659: [Enum] add _simple_enum decorator (GH-25285)Ethan Furman2021-04-201-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | add: _simple_enum decorator to transform a normal class into an enum _test_simple_enum function to compare _old_convert_ to enable checking _convert_ generated enums _simple_enum takes a normal class and converts it into an enum: @simple_enum(Enum) class Color: RED = 1 GREEN = 2 BLUE = 3 _old_convert_ works much like _convert_ does, using the original logic: # in a test file import socket, enum CheckedAddressFamily = enum._old_convert_( enum.IntEnum, 'AddressFamily', 'socket', lambda C: C.isupper() and C.startswith('AF_'), source=_socket, ) test_simple_enum takes a traditional enum and a simple enum and compares the two: # in the REPL or the same module as Color class CheckedColor(Enum): RED = 1 GREEN = 2 BLUE = 3 _test_simple_enum(CheckedColor, Color) _test_simple_enum(CheckedAddressFamily, socket.AddressFamily) Any important differences will raise a TypeError
* bpo-43521: Allow ast.unparse with empty sets and NaN (GH-24897)Kodi Arfer2021-03-181-6/+14
| | | Automerge-Triggered-By: GH:pablogsal
* bpo-42128: Structural Pattern Matching (PEP 634) (GH-22917)Brandt Bucher2021-02-261-0/+27
| | | | | Co-authored-by: Guido van Rossum <guido@python.org> Co-authored-by: Talin <viridia@gmail.com> Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
* bpo-28964: add line number of node (if available) to ast.literal_eval error ↵Irit Katriel2020-12-251-1/+4
| | | | messages (GH-23677)
* bpo-28002: Roundtrip f-strings with ast.unparse better (#19612)Shantanu2020-11-201-24/+86
| | | | | | | | | By attempting to avoid backslashes in f-string expressions. We also now proactively raise errors for some backslashes we can't avoid while unparsing FormattedValues Co-authored-by: hauntsaninja <> Co-authored-by: Shantanu <hauntsaninja@users.noreply.github.com> Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
* bpo-41887: omit leading spaces/tabs on ast.literal_eval (#22469)Batuhan Taskaya2020-10-041-1/+1
| | | Also document that eval() does this (the same way).
* bpo-41631: _ast module uses again a global state (#21961)Victor Stinner2020-09-151-15/+22
| | | | | | | | | | | | | | | | | Partially revert commit ac46eb4ad6662cf6d771b20d8963658b2186c48c: "bpo-38113: Update the Python-ast.c generator to PEP384 (gh-15957)". Using a module state per module instance is causing subtle practical problems. For example, the Mercurial project replaces the __import__() function to implement lazy import, whereas Python expected that "import _ast" always return a fully initialized _ast module. Add _PyAST_Fini() to clear the state at exit. The _ast module has no state (set _astmodule.m_size to 0). Remove astmodule_traverse(), astmodule_clear() and astmodule_free() functions.
* bpo-40726: handle uninitalized end_lineno on ast.increment_lineno (GH-20312)Batuhan Taskaya2020-08-051-3/+10
|
* bpo-36290: Fix keytword collision handling in AST node constructors (GH-12382)Rémi Lapeyre2020-05-241-0/+7
|
* bpo-38870: Don't omit parenthesis when unparsing a slice in ast.unparseBatuhan Taskaya2020-05-181-1/+11
| | | | When unparsing a non-empty tuple, the parentheses can be safely omitted if there aren't any elements that explicitly require them (such as starred expressions).
* bpo-38870: correctly escape unprintable characters on ast.unparse (GH-20166)CyberSaxosTiGER2020-05-181-3/+11
| | | | | | | | | Unprintable characters such as `\x00` weren't correctly roundtripped due to not using default string repr when generating docstrings. This patch correctly encodes all unprintable characters (except `\n` and `\t`, which are commonly used for formatting, and found unescaped). Co-authored-by: Pablo Galindo <Pablogsal@gmail.com> Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
* bpo-40662: Fixed ast.get_source_segment for ast nodes that have incomplete ↵Irit Katriel2020-05-181-0/+2
| | | | | location information (GH-20157) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
* bpo-38870: Implement round tripping support for typed AST in ast.unparse ↵Batuhan Taskaya2020-05-161-6/+23
| | | | (GH-17797)
* bpo-38870: Correctly handle empty docstrings in ast.unparse (GH-18768)Batuhan Taskaya2020-05-161-5/+8
| | | Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>