fix: avoid uncaught TokenError when computing node positions#3075
Open
Pierre-Sassoulas wants to merge 2 commits into
Open
fix: avoid uncaught TokenError when computing node positions#3075Pierre-Sassoulas wants to merge 2 commits into
Pierre-Sassoulas wants to merge 2 commits into
Conversation
``RebuildVisitor._get_position_info`` tokenizes a slice of the source to locate the keyword/name span of a class or function. On Python < 3.12 ``tokenize.generate_tokens`` can raise ``TokenError`` for malformed input (e.g. an unterminated bracket), which escaped as an uncaught non-Astroid exception. Such a node now simply has no position information. Closes #2527
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3075 +/- ##
==========================================
+ Coverage 93.52% 93.54% +0.01%
==========================================
Files 92 92
Lines 11329 11332 +3
==========================================
+ Hits 10596 10600 +4
+ Misses 733 732 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Adds a test for a class whose name needs NFKC normalization (the ``fi`` ligature): ``node.name`` differs from the raw token spelling, so the name token is never matched and ``_get_position_info`` returns via its ``for``/``else`` branch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of Changes
Description
RebuildVisitor._get_position_infotokenizes a slice of the source to locate the keyword/name span of a class or function definition. On Python < 3.12,tokenize.generate_tokenscan raiseTokenErrorfor malformed input (e.g. an unterminated bracket), which escaped as an uncaught non-AstroidErrorexception during the build.Position computation now catches
TokenErrorand treats such a node as having no position information, instead of crashing.Closes #2527