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

Fix crash with NamedTuple classmethod #3323

Merged
merged 1 commit into from
May 4, 2017

Conversation

JelleZijlstra
Copy link
Member

Really hacky but this should work for now. I think the bug
was introduced when we added support for NamedTuple
classmethods.

Fixes #3317

Really hacky but this should work for now. I think the bug
was introduced when we added support for NamedTuple
classmethods.

Fixes python#3317
# attempting to type check methods may lead to crashes because NamedTuples
# do not have a fully functional TypeInfo.
# TODO remove this hack and add full support for NamedTuple methods
defn.defs.body = [stmt for stmt in defn.defs.body
Copy link
Member Author

Choose a reason for hiding this comment

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

This is safe because statements within a NamedTuple class fall in three groups (see check_namedtuple_classdef below):

  1. assignments, which we still type check
  2. pass and Ellipsis, which don't do anything and which we ignore here
  3. Other statements, which produce an error on line 841/847 below and now are ignored here.

@JelleZijlstra
Copy link
Member Author

More background: This was almost certainly introduced by #2719, which started type checking the body of a namedtuple class in order to validate default values. Now, classmethods get visited during semantic analysis, but when we try to bind their cls argument, there is no usable type info available because NamedTuples aren't really classes. This PR fixes the problem with a sledgehammer by dropping all invalid statements from the body of the NamedTuple class.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! LGTM

@JukkaL JukkaL merged commit 8ea1aa5 into python:master May 4, 2017
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