Skip to content

Conversation

@timhoffm
Copy link
Member

@timhoffm timhoffm commented Oct 22, 2024

Historically, pyplot dynamically added a number attribute to figure. However, this number must stay in sync with the figure manger. AFAICS overwriting the number attribute does not have the desired effect for pyplot. But there are some repos in GitHub that do change number. So let's take it slow and properly migrate away from writing.

Making the dynamic attribute private and wrapping it in a property allows to maintain current behavior and deprecate write-access.

When the deprecation expires, there's no need for duplicate state anymore and the private _number attribute can be replaced by return self.canvas.manager.num if that exists and None otherwise.

Also closes #28994 (dynamic number attribute is not handled by type checkers).


@ksunden it would be nice to get this in for 3.10, but if that's too tight, feel free to move to 3.11.

@tacaswell
Copy link
Member

👍🏻 in principle, but we need tests.

Do we want to leave public API for setting in the first time?

@jklymak
Copy link
Member

jklymak commented Oct 22, 2024

Why are people overwriting the attribute if it "doesn't have the desired effect"?

@tacaswell
Copy link
Member

Why are people overwriting the attribute if it "doesn't have the desired effect"?

Either their own reasons or wishful API usage ;)


@number.setter
def number(self, num):
_api.warn_deprecated("3.10", message="Changing 'Figure.number' is deprecated")
Copy link
Member

Choose a reason for hiding this comment

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

message needs %(since)s and %(removal)s.

@jklymak
Copy link
Member

jklymak commented Oct 23, 2024

If number is not to be used externally, why make it public at all?

@timhoffm
Copy link
Member Author

Digging into, there are only a few cases that are setting figure numbers:

I'm not clear why they do it, but we should not break them without prior warning.


If number is not to be used externally, why make it public at all?

Accessing number is not super-useful. Either you work with the number as a standalone label

plt.figure(2)
...
plt.close(2)

or you keep a reference to the figure, in which case you can use the figure instead of the number:

fig = plt.figure(2)
...
plt.close(fig)

but since it is used as a core identifier, it feels right to be able to get the information fig.number is you really want to.

@timhoffm timhoffm force-pushed the figure-num branch 3 times, most recently from a3bd0ab to ea5b222 Compare October 23, 2024 14:47
Historically, pyplot dynamically added a number attribute to figure.
However, this number must stay in sync with the figure manger.
AFAICS overwriting the number attribute does not have the desired
effect for pyplot. But there are some repos in GitHub that do change
number. So let's take it slow and properly migrate away from writing.

Making the dynamic attribute private and wrapping it in a property
allows to maintain current behavior and deprecate write-access.

When the deprecation expires, there's no need for duplicate state
anymore and the private _number attribute can be replaced by
`self.canvas.manager.num` if that exists and None otherwise.

Also closes matplotlib#28994.
@greglucas greglucas merged commit e2cbe8b into matplotlib:main Oct 23, 2024
42 of 43 checks passed
@timhoffm timhoffm deleted the figure-num branch November 8, 2024 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Figure Number Gives Type Error

5 participants