Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-38870: refactor delimiting with context managers #17612

Open
wants to merge 4 commits into
base: master
from

Conversation

@isidentical
Copy link
Contributor

isidentical commented Dec 15, 2019

Lib/ast.py Outdated
return self._NoDelimit()

def delimit(self, delimiter = "()"):
return self._Delimit(self, delimiter)

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 15, 2019

Member

IHMO a contextlib.contextmanager would be simpler. Something like:

@contextlib.contextmanager
def delim_if(self, condition, delimiter="()"):
    if condition:
        self.write(self.delimiter[0]) 
    try:
        yield
    finally:
        if condition:
            self.write(self.delimiter[-1])

# I'm not sure that this one works as expected :-)
def delim(self, delimiter):
    return self.delim_if(True, delimiter)

This comment has been minimized.

Copy link
@pablogsal

pablogsal Dec 15, 2019

Member

We started with this, but it introduced a regression in the import time and I reverted it.

This comment has been minimized.

Copy link
@pablogsal

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 15, 2019

Member

You wouldn't have such issue if ast.unparse would be a submodule....

This comment has been minimized.

Copy link
@pablogsal

pablogsal Dec 15, 2019

Member

I dislike the submodule idea more:

#17377 (comment)

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 16, 2019

Member

This discussion is splitted between 3 comment threads in 2 PRs. I created https://bugs.python.org/issue39069 to summarize the discussion and discuss the idea there.

Lib/ast.py Outdated Show resolved Hide resolved
Copy link
Member

vstinner left a comment

Apart open questions to related to https://bugs.python.org/issue39069, this PR looks good to me.

@isidentical: Are you interested to measure the overhead of functools/enum imports/code? See https://bugs.python.org/issue39069#msg358498

@isidentical

This comment has been minimized.

Copy link
Contributor Author

isidentical commented Dec 16, 2019

@isidentical

This comment has been minimized.

Copy link
Contributor Author

isidentical commented Dec 16, 2019

Oh, looks like @pablogsal already assigned.

@vstinner

This comment has been minimized.

Copy link
Member

vstinner commented Dec 16, 2019

Yes but currently I can only reply through mail because bpo logins with google doesnt work.

Oh, I didn't know: I reported this issue at python/bugs.python.org#41

Copy link
Member

vstinner left a comment

I would prefer to only not delimit() if the delimiter is always written, like in a function call or item[index].

Lib/ast.py Outdated Show resolved Hide resolved
Lib/ast.py Show resolved Hide resolved
self.fill(def_str)
self.traverse(node.args)
self.write(")")
with self.delimit("()"):

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 16, 2019

Member

Wait. I'm not sure about this one. Are you going to omit parenthesis here sometimes?

The change is correct, but direct write() calls are maybe better.

lambda: self.write(", "), write_item, zip(node.keys, node.values)
)
self.write("}")
with self.delimit("{}"):

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 16, 2019

Member

Same here, I'm not convince thta delimit() makes the code more readable.

else:
self.interleave(lambda: self.write(", "), self.traverse, node.elts)
self.write(")")
with self.delimit("()"):

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 16, 2019

Member

Is delim() useful here?

comma = True
self.traverse(e)
self.write(")")
with self.delimit("()"):

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 16, 2019

Member

Same question here.

self.write("[")
self.traverse(node.slice)
self.write("]")
with self.delimit("[]"):

This comment has been minimized.

Copy link
@vstinner

vstinner Dec 16, 2019

Member

and here

Co-Authored-By: Victor Stinner <vstinner@python.org>
@isidentical

This comment has been minimized.

Copy link
Contributor Author

isidentical commented Dec 17, 2019

@vstinner a general answer, I used delim in everywhere we are using brackets for the sake of consistency. If it looks better without them, I can just revert back in few places.

Copy link
Member

vstinner left a comment

@pablogsal: Do you prefer to only use delim() when parenthesis can be optional, or are you fine with replacing all write(x) ... write(y) with "with delim(xy): ..."?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.