Skip to content

Conversation

asottile
Copy link
Contributor

@asottile asottile commented Oct 21, 2018

I revived the patch listed in the linked bpo issue.

https://bugs.python.org/issue16806

@asottile
Copy link
Contributor Author

asottile commented Oct 21, 2018

still need to fix the fstring test -- might need some help from @ambv given #1800 this should be ready for review now!

@asottile asottile force-pushed the bpo-16806 branch 2 times, most recently from 0284c2a to 33f03d9 Compare October 21, 2018 19:22
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suspect this was incorrect before (being adjusted by -1 due to the multiline string).

For instance, here's the ast dump of (using astpretty)

x = 1 + 1
$ python ~/workspace/astpretty/astpretty.py t.py Module( body=[ Assign( lineno=1, col_offset=0, targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())], value=BinOp( lineno=1, col_offset=4, left=Num(lineno=1, col_offset=4, n=1), op=Add(), right=Num(lineno=1, col_offset=8, n=1), ), ), ], ) 

You'll notice that the BinOp and its left (Num) have the same col_offset

Copy link
Member

Choose a reason for hiding this comment

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

Just a question: after fixing this, when a < line_start possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would think it's not possible now, though I don't know of all the cases for every other token. I think tqs are the only multiline token (since iirc escaped newlines are not a token)

Python/ast.c Outdated
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this function well.

 int lines = LINENO(parent) - 1; int cols = parent->n_col_offset; 

These value are get from parent, before this loop:

 /* Find the full fstring to fix location information in `n`. */ while (parent && parent->n_type != STRING) parent = parent->n_child; 

Is this OK?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I think so, what's happening here (and the variable names aren't super great):

  1. find the original ast offset of the parent node
  2. find the position in the string that the fstring expression appears at
  3. adjust the line/col based on that

From reading #1800 (where this function was introduced) it was discussed briefly but I didn't pick up the rationale for that loop there. When I removed / adjusted it during testing it broke all of the f-string column / offset tests so I think it's necessary.

@asottile
Copy link
Contributor Author

(I've rebased to regen importlib to resolve the conflict)

@asottile
Copy link
Contributor Author

(I've rebased to regen importlib to resolve the conflict)

@methane
Copy link
Member

methane commented Oct 28, 2018

Diff and tests looks good to me.
But I don't understand about parser and tokenizer well.

@ericvsmith would you review this? You seems reviewed #1800.

@ericvsmith
Copy link
Member

ericvsmith commented Oct 28, 2018

I'll take a look, but it might take me a while to get to it. Unfortunately I have a project that's due at the end of the year that's taking up all of my time. So while I'll try, you might not want to wait for me.

I'm working on a fix to bpo-34364 that will completely back out the changes implemented in #1800. I'm not sure if that will interact with this change, though.

@asottile
Copy link
Contributor Author

maybe @ambv can take a look too? (authored #1800)

@asottile
Copy link
Contributor Author

asottile commented Nov 2, 2018

(I've rebased to regen importlib to resolve the conflict)

Commands for me for next time since I expect this to not be the last:

git fetch --all git checkout [bpo-16806](https://bugs.python.org/issue16806) git reset --hard asottile/[bpo-16806](https://bugs.python.org/issue16806) git pull --rebase origin master git checkout origin/master -- Python/importlib* # when rebase fails make regen-importlib git add Python git rebase --continue git push asottile HEAD -f
@asottile asottile closed this Nov 3, 2018
@asottile asottile reopened this Nov 3, 2018
@asottile
Copy link
Contributor Author

urgh, the bot edited my comment and broke my code block :(

@asottile
Copy link
Contributor Author

(I've rebased to regen importlib to resolve the conflict)

Please take a look, this is tedious -- I'd like to be done with this branch!

@asottile
Copy link
Contributor Author

(I've rebased to regen importlib to resolve the conflict)

Please take a look, this is tedious -- I'd like to be done with this branch!

@methane @ericvsmith @ambv <3

@asottile
Copy link
Contributor Author

happy new year! friendly ping :)

@YannickJadoul
Copy link
Contributor

YannickJadoul commented Apr 1, 2020

Is there a reason why this wasn't backported to 3.6 and 3.7, just like #1800/bpo-30465?
Trying to get PyPy to pass related tests, but there seems to be a discrepancy in behavior between 3.6/3.7 and 3.8.

@asottile
Copy link
Contributor Author

asottile commented Apr 1, 2020

changes to the ast generally shouldn't be backported because they break linters / code formatters which make assumptions about how minor versions of python work

in this particular case, this was discussed on bpo if you want more information

@YannickJadoul
Copy link
Contributor

@asottile Sorry, should've read that better! Thanks!

domdfcoding added a commit to domdfcoding/typed_ast that referenced this pull request Mar 7, 2021
domdfcoding added a commit to domdfcoding/typed_ast that referenced this pull request May 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants