[issue39069] Move ast.unparse() function to a different module

STINNER Victor report at bugs.python.org
Mon Dec 16 16:31:24 EST 2019


STINNER Victor <vstinner at python.org> added the comment:

Pablo:
> Victor, are you OK if we close this issue and just use the desired imports directly in the PRs for issue 38870?

Yes.

--

*I* don't care of "import ast" performance, since I don't expect it to be commonly used by command line applications. I don't know the story of the revert, but it seems like you don't bother much anymore.

If tomorrow using "functools" and "enum" becomes a performance bottleneck (for import time), maybe we should try to optimize imports and optimize these modules instead?

I opened this issue because I would like to use these modules in ast. I'm unhappy with the current proposed implementation:
---

    class _NoDelimit:
        def __enter__(self): pass
        def __exit__(self, *args): pass

    class _Delimit:
        """A context manager for preparing the source for expressions. It adds
        start of the delimiter  and enters, after exit it adds delimiter end."""
        def __init__(self, unparser, delimiter):
            self.unparser = unparser
            self.delimiter = delimiter
        def __enter__(self):
            self.unparser.write(self.delimiter[0])
        def __exit__(self, exc_type, exc_value, traceback):
            self.unparser.write(self.delimiter[-1])

    def delimit_if(self, condition, delimiter):
        if condition:
            return self._Delimit(self, delimiter)
        else:
            return self._NoDelimit()
---

Having to define two classes just to call the write() method seems overkill to me.

Same remark for the pseudo enum in PR 17377:
---
    PRECEDENCE_LEVELS = {
        "PR_TUPLE": 0,
        "PR_YIELD": 1,
        "PR_TEST": 2,
        "PR_OR": 3,
        "PR_AND": 4,
        "PR_NOT": 5,
        "PR_CMP": 6,
        "PR_EXPR": 7,
        "PR_BOR": 7,
        "PR_BXOR": 8,
        "PR_BAND": 9,
        "PR_SHIFT": 10,
        "PR_ARITH": 11,
        "PR_TERM": 12,
        "PR_FACTOR": 13,
        "PR_POWER": 14,
        "PR_AWAIT": 15,
        "PR_ATOM": 16,
    }
---

It's too easy to introduce a duplicated constant with such construction. The enum module is designed to define unique constants.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39069>
_______________________________________


More information about the Python-bugs-list mailing list