Skip to content

python 3.6 variable annotation #500

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

Open
kamahen opened this issue Jan 8, 2018 · 7 comments
Open

python 3.6 variable annotation #500

kamahen opened this issue Jan 8, 2018 · 7 comments

Comments

@kamahen
Copy link
Contributor

kamahen commented Jan 8, 2018

Python 3.6 allows variable annotation, but yapf doesn't support it. e.g.:

self.anchors: Dict[Tuple[Int, Int], Vname] = {}
@bwendling
Copy link
Member

We should support it. But you might need to run yapf with Python 3.6+ to enable it. I looked at the link. Are there specific examples that were being messed up?

@kamahen
Copy link
Contributor Author

kamahen commented Feb 26, 2018

Right now, I need to write something like this snippet taken from my code:

        anchors = []  # type: List[kythe.Anchor]
        cooked_nodes.anchors(
            ast_cooked.FqnCtx(
                fqn_dot=file_to_module(src) + '.',
                bindings=collections.ChainMap(collections.OrderedDict()),
                python_version=args.python_version), anchors)

If Python 3.6 variable annotations were supported, I could write this:

        anchors: List[kythe.Anchor] = []
        cooked_nodes.anchors(
            ast_cooked.FqnCtx(
                fqn_dot=file_to_module(src) + '.',
                bindings=collections.ChainMap(collections.OrderedDict()),
                python_version=args.python_version), 
           anchors,
        )

The variable annotation is needed for mypy, which can't infer the type of an empty list.

Another example is this (which is a workaround for mypy issue 4547:

    __slots__ = ()  # type: Sequence[str]

which I'd like to write:

    __slots__: Sequence[str] = ()

@kamahen
Copy link
Contributor Author

kamahen commented Apr 2, 2018

@gwelymernans Is this the kind of thing that I could fix by myself, if you point me at the appropriate place(s) in yapf?

@kamahen
Copy link
Contributor Author

kamahen commented May 24, 2018

Python 3.7 dataclasses require the use of variable annotations, so it would be really nice if variable annotatios could be supported by yapf.

https://www.python.org/dev/peps/pep-0557/

@bwendling
Copy link
Member

I'm not familiar with this new syntax, but from briefly looking at the grammar we would need to add Visit_annassign() methods to the various visitor modules (at least where needed). After that, we just need to make sure that the format_decision_state.py module is enforcing the proper splits and penalties. In truth, it should support most of the type annotations, since they don't violate the original Python syntax too much. There would only be issues like "there should be a space here" or "a split here would be keen" to contend with...

@kamahen
Copy link
Contributor Author

kamahen commented Sep 7, 2018

The new syntax is essentially the same as the parameter:type=default syntax in "def" statements.

Probably it can be processed the same way.

lib2to3/Grammar.txt has some changes between 3.6 and 3.7 (besides the one change you've already handled), so I'm surprised that yapf even works without handling the other grammar changes. If it helps, I can go back through some of my changelists to find out what changes I've needed to handle the changes between 3.6 and 3.7.

@Yoshi325
Copy link

I would like to help with this Issue, but I am unfamiliar with the codebase. Is there any way I may contribute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants