Thanks to visit codestin.com
Credit goes to github.com

Skip to content

parse extra keyword args in class definition #1378

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

Merged
merged 3 commits into from
Apr 18, 2016
Merged

parse extra keyword args in class definition #1378

merged 3 commits into from
Apr 18, 2016

Conversation

pkienzle
Copy link
Contributor

mypy cannot parse the classes defined in typing.py, such as:

class Tuple(Final, metaclass=TupleMeta, _root=True): ...

Instead, it stops with a parse error at the comma following metaclass=TupleMeta. The original code assumed that there would be a list of superclasses, possibly followed by metaclass=.... This patch generalizes the parsing to an arbitrary set of keyword arguments, popping the metaclass out of the list when it is complete.

I updated the ClassDef node in nodes.py to hold on to the keyword arguments and serialize to and from json, but I don't know how to trigger the serialization code, so this is not tested.

@@ -670,6 +674,7 @@ def deserialize(self, data: JsonDict) -> 'ClassDef':
Block([]),
[mypy.types.TypeVarDef.deserialize(v) for v in data['type_vars']],
metaclass=data['metaclass'],
kwargs=dict((k, v.deserialize()) for k, v in data['kwargs']),
Copy link
Member

Choose a reason for hiding this comment

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

That's not how you invoke deserialize() -- it is a class method.

@gvanrossum
Copy link
Member

As you can tell from running mypy on itself (which is what the Travis CI test does) there are a few problems with this code, but nothing that can't be fixed!

I also wonder if it's worth fixing this right now, since @ddfisher is working on a complete rewrite of the parser (see #1353). The new parser will be based on Python's ast module so it will automatically support this. (Well, mypy still needs to type-check the keyword args and possibly understand how they affect a class definition, but that's secondary -- first it needs to not reject the syntax.)

However if you want to learn something about mypy I encourage you to persist and finish this PR!

@pkienzle pkienzle closed this Apr 15, 2016
@pkienzle pkienzle reopened this Apr 15, 2016
@pkienzle
Copy link
Contributor Author

I simplified the patch so that it is equivalent to the fastparse handling of class definitions. That is, the ClassDef node includes the metaclass but not any other keyword args and this patch doesn't change that.

@gvanrossum
Copy link
Member

LGTM. Can you please add a test showing that it ignores other keywords?

@pkienzle
Copy link
Contributor Author

Done.

@gvanrossum
Copy link
Member

Thanks!

@gvanrossum gvanrossum merged commit e1a3fde into python:master Apr 18, 2016
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

Successfully merging this pull request may close these issues.

2 participants