Skip to content

Fixed Error: local variable 'xdata' referenced before assignment" in legend_handler.py - #8478

Merged
phobson merged 1 commit into
matplotlib:masterfrom
kalagau:TeamDoItTomorrowfix-6921
Apr 18, 2017
Merged

phobson merged 1 commit into
matplotlib:masterfrom
kalagau:TeamDoItTomorrowfix-6921

Conversation

@kalagau

@kalagau kalagau commented Apr 13, 2017

Copy link
Copy Markdown
Contributor

This is the PR for #6921

Comment thread lib/matplotlib/legend_handler.py Outdated
numpoints = self.get_numpoints(legend)

if numpoints > 1:
if ((numpoints < 1) or not (isinstance(numpoints, int) or numpoints.is_integer())):

@story645 story645 Apr 13, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's the difference between these two?
isinstance(numpoints, int) or numpoints.is_integer()

@kalagau kalagau Apr 13, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ahh they do the same thing. Which style do you prefer?
Edit: Decided to stick to numpoints.is_integer()

Comment thread lib/matplotlib/legend_handler.py Outdated
numpoints = self.get_numpoints(legend)

if numpoints > 1:
if ((numpoints < 1) or not (numpoints.is_integer())):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should check for integralness first, I think. Also, most of these parentheses are unnecessary.

Comment thread lib/matplotlib/legend_handler.py Outdated
if ((numpoints < 1) or not (numpoints.is_integer())):
raise ValueError("numpoints must be a whole number and greater" +
" than or equal to 1; it was", numpoints)
elif (numpoints == 1):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unnecessary parentheses.

@anntzer

anntzer commented Apr 14, 2017

Copy link
Copy Markdown
Contributor

I would just have a check if > 1: ... else: ... (basically just remove the ==1)
Non-integer values will trigger a warning in linspace ("DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.") which should be enough.
Otherwise, adding typechecks everywhere is just a never ending task.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Apr 14, 2017
@kalagau

kalagau commented Apr 14, 2017

Copy link
Copy Markdown
Contributor Author

@anntzer I was just fixing it up.

@tacaswell

Copy link
Copy Markdown
Member

@kalagau This looks like you have mixed tabs and spaces! Can you replace them all with 4 spaces (most editors will have a tool for this (ex untabify in emacs) or just find and replace)?

@tacaswell tacaswell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Adding a test

def test_silly_numpoints():
    fig, ax = plt.subplots()
    ax.plot(range(5), label='test')
    ax.legend(numpoints=0.5)

would not be bad, but I do not think is essential.

@kalagau

kalagau commented Apr 17, 2017

Copy link
Copy Markdown
Contributor Author

Ill add it in later today

@kalagau

kalagau commented Apr 17, 2017

Copy link
Copy Markdown
Contributor Author

I think this should be good now! all tests are passing!

@phobson

phobson commented Apr 17, 2017

Copy link
Copy Markdown
Member

@kalagau Are you comfortable squashing this down to 1 commit?

Fix Error: local variable 'xdata' referenced before assignment" in legend_handler.py
@kalagau
kalagau force-pushed the TeamDoItTomorrowfix-6921 branch from 477a129 to f3b82c1 Compare April 17, 2017 19:43
@kalagau

kalagau commented Apr 17, 2017

Copy link
Copy Markdown
Contributor Author

Sure @phobson, just did it.

@phobson
phobson merged commit 481e860 into matplotlib:master Apr 18, 2017
@phobson

phobson commented Apr 18, 2017

Copy link
Copy Markdown
Member

thanks @kalagau !

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.

6 participants