FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add typing for internal helpers by QuLogic · Pull Request #26367 · matplotlib/matplotlib · GitHub

Repository navigation

Add typing for internal helpers - #26367

Merged
greglucas merged 1 commit into
matplotlib:mainfrom
QuLogic:api-types
Jul 28, 2023
Merged

greglucas merged 1 commit into
matplotlib:mainfrom
QuLogic:api-types

Conversation

QuLogic commented Jul 21, 2023

Copy link
Copy Markdown
Member

PR summary

While we don't normally type private API, these are used all over Matplotlib, and it makes sense to type them for internal checking purposes.

I'm uncertain how to actually type hint deprecate_privatize_attribute because it takes on the type of the thing it's wrapping. I tried to do this in test_api.py, but I'm not sure I was successful.

PR checklist

QuLogic force-pushed the api-types branch 2 times, most recently from defee83 to 648bd55 Compare July 21, 2023 07:22

ksunden commented Jul 21, 2023

Copy link
Copy Markdown
Member

deprecate_privatize_attribute relies on __setname__/setattr behavior which is not conducive to automatically type hinting.

Thus I don't really think its worth fretting over trying to get it to do so. The end effect is that you just do:

.py file:

class Foo:
   attr = deprecate_privatize_attribute("3.8")
   _attr = "bar"

.pyi file:

class Foo:
   attr: str
   _attr: str

(You can also opt to not type hint the private version, and potentially to omit the public version as well and add it to the ci/mypy_stubtest_allowlist.txt)

Essentially you just take the effect of the decorator and represent it in the pyi

This is one of the things that would make inlining more type hints more difficult (if we ever do want to go down that route), but also not necessarily a blocker as I'm not too offended by type checking flagging deprecated things.

Comment thread lib/matplotlib/_api/deprecation.pyi Outdated
Comment thread lib/matplotlib/_api/deprecation.pyi Outdated

QuLogic commented Jul 22, 2023

Copy link
Copy Markdown
Member Author

deprecate_privatize_attribute relies on __setname__/setattr behavior which is not conducive to automatically type hinting.

The main issue is the assignment; if you type the left-hand side, the right-hand side is the deprecate_privatize_attribute instance. It appears now I can have it derive from Any, but that apparently was only added in 3.11. On the other hand, we've generally avoided typing deprecated thing if they're difficult, so maybe we should just leave out the types on this one.

QuLogic force-pushed the api-types branch 6 times, most recently from 969c653 to ba2da1c Compare July 26, 2023 04:07

QuLogic commented Jul 26, 2023 •
edited
Loading

Copy link
Copy Markdown
Member Author

Hmm, I'm confused why dates.py is now failing, as I thought it didn't have any typing, but I think I've fixed it.

Comment thread lib/matplotlib/_api/deprecation.pyi Outdated

@overload
def rename_parameter(
since: str, old: str, new: str, func: Literal[None] = None

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
since: str, old: str, new: str, func: Literal[None] = None
since: str, old: str, new: str, func: None=...

None, being a singleton, has no need to be called out as Literal[None].
Additionally, pyi files call for the actual value of defaults to be omitted (which admittedly makes little difference when the type hint is None...)

Same applies to all other uses of Literal in this file.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It's not wrong just a bit out of place/using tools that are not needed for the task at hand

Comment thread lib/matplotlib/_api/__init__.pyi Outdated
While we don't normally type private API, these are used all over
Matplotlib, and it makes sense to type them for internal checking
purposes.
greglucas merged commit dd40f48 into matplotlib:main Jul 28, 2023
QuLogic deleted the api-types branch July 28, 2023 02:18
QuLogic added this to the v3.8.0 milestone Jul 28, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

3 participants


Back | FazBrowse Home | New Git URL