Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Deprecate globals using module-level__getattr__.#20733

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tacaswell merged 1 commit intomatplotlib:masterfromanntzer:dg
Aug 6, 2021

Conversation

anntzer
Copy link
Contributor

This PR is mostly just to propose a pattern for defining module-level
__getattr__s for deprecating globals.

Relying on lru_cache rather than defining variables asglobal (see
change in__init__.py) avoids having to repeattwice the variable
name, and allows immediately returning its value without assigning it.
It means later accesses will be very slightly slower (because they'll
still go through the lru_cache layer), but that should hopefully be
negligible, and will mostly concern deprecated variables anyways.

Onecould consider adding a helper API in_api.deprecation that just
provides the decoration with@lru_cache and generates the
AttributeError message when needed, but that seems rather overkill.
(See#20620 (comment), in response to which this was written:
I don't have any "magic" idea this time :-), I now think the#10735
isn't really worth it.)

The change in deprecation.py allows one to skipobj_name and get a
message like "foo is deprecated..." rather than "The foo is
deprecated...".

PR Summary

PR Checklist

  • Has pytest style unit tests (andpytest passes).
  • IsFlake 8 compliant (runflake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs shouldbuild without error).
  • Conforms to Matplotlib style conventions (installflake8-docstrings and runflake8 --docstring-convention=all).
  • New features have an entry indoc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented indoc/api/next_api_changes/ (follow instructions in README.rst there).

@anntzeranntzer added this to thev3.5.0 milestoneJul 24, 2021
except TypeError as exc:
cursord = {} # deprecated in Matplotlib 3.5.

@functools.lru_cache(None)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@functools.lru_cache(None)
# module-level depredations
@functools.lru_cache(None)

Since we don't make a dedicated function which could give the code segment a name, let's at least use a comment as a standard convention for explaining what this is for. - Also for the other uses of this pattern.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

How would you want that to be formatplotlib/__init__.py, which also uses__getattr__ for__version__?

Copy link
Member

Choose a reason for hiding this comment

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

For simplicity, you can leave that without a comment. Part of the intent of the comment is to easily see that the whole construct serves the deprecation and can be removed when expiring. That's not the case for thematplotlib.__init__.py__getattr__.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Actually, I just put the comment in theif block.

This PR is mostly just to propose a pattern for defining module-level`__getattr__`s for deprecating globals.Relying on lru_cache rather than defining variables as `global` (seechange in `__init__.py`) avoids having to repeat *twice* the variablename, and allows immediately returning its value without assigning it.It means later accesses will be very slightly slower (because they'llstill go through the lru_cache layer), but that should hopefully benegligible, and will mostly concern deprecated variables anyways.One *could* consider adding a helper API in `_api.deprecation` that justprovides the decoration with `@lru_cache` and generates theAttributeError message when needed, but that seems rather overkill.The change in deprecation.py allows one to skip `obj_name` and get amessage like "foo is deprecated..." rather than "The foo  isdeprecated...".
@tacaswelltacaswell merged commit3e32cc7 intomatplotlib:masterAug 6, 2021
@anntzeranntzer deleted the dg branchAugust 6, 2021 16:51
lpsinger added a commit to lpsinger/matplotlib that referenced this pull requestAug 10, 2021
PRmatplotlib#20733 added module-level `__getattr__` functions to severalmodules. All of the functions with the exception of the one in`matplotlib.style.core` had a terminal `raise AttributeError` tohandle unmatched attributes.The omission in `matplotlib.style.core` was probablyunintentional; it results in confusing and buggy behaviorsuch as:```pycon>>> import matplotlib.style.core>>> if hasattr(matplotlib.style.core, '__warningregistry__'):...     del matplotlib.style.core.__warningregistry__...Traceback (most recent call last):  File "<stdin>", line 2, in <module>AttributeError: __warningregistry__```This causes problems in the unit tests for astropy affiliatedpackages. Seeastropy/astropy#12038.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@timhoffmtimhoffmtimhoffm approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
v3.5.0
Development

Successfully merging this pull request may close these issues.

3 participants
@anntzer@timhoffm@tacaswell

[8]ページ先頭

©2009-2025 Movatter.jp