Giter VIP home page Giter VIP logo

Comments (4)

alexmojaki avatar alexmojaki commented on May 28, 2024 1

The new asttokens.ASTText class now supports nodes inside f-strings for Python 3.9.7+ with the .get_text* methods. But there's still no token information.

from asttokens.

jamescooke avatar jamescooke commented on May 28, 2024

@LiraNuna thanks for keeping this issue open and for the explanations in #4 about what's going on 👍

The original Python issue 29051 was split into two - and the first one https://bugs.python.org/issue30465 "FormattedValue expressions have wrong lineno and col_offset information" is now resolved. Does this mean that this bug can be solved properly now?

For reference, I'm about to wire in a work around to fix this here: jamescooke/flake8-aaa#101 - probably something to prevent the error being raised when the linter tries to access the token information on an f-string.

from asttokens.

LiraNuna avatar LiraNuna commented on May 28, 2024

It's been a while and I have to admit I forgot most of the details.

from asttokens.

alexmojaki avatar alexmojaki commented on May 28, 2024

I'm not really up for a PR right now, but here's an idea for an algorithm:

  1. Get the range of tokens (string literals) representing the JoinedString
  2. For each FormattedValue (iterating through the string tokens in parallel):
    1. Find the starting { in the current string token.
    2. Determine which character is the end of the main expression: }, !, or :, based on the conversion and format specifier values in the node.
    3. For each such character after the starting position:
      1. Try parsing the text between start and end.
      2. If successful, compare the resulting tree to the FormattedValue tree.
      3. If equal, mark tokens on the subtree, then readjust positions based on the starting position of the string.
    4. Repeat for format specifier if present.

from asttokens.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.