Skip to content

Add typing for internal helpers #26367

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

Merged
merged 1 commit into from
Jul 28, 2023
Merged

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Jul 21, 2023

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 QuLogic force-pushed the api-types branch 2 times, most recently from defee83 to 648bd55 Compare July 21, 2023 07:22
@ksunden
Copy link
Member

ksunden commented Jul 21, 2023

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.

@QuLogic
Copy link
Member Author

QuLogic commented Jul 22, 2023

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 QuLogic force-pushed the api-types branch 6 times, most recently from 969c653 to ba2da1c Compare July 26, 2023 04:07
@QuLogic
Copy link
Member Author

QuLogic commented Jul 26, 2023

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.


@overload
def rename_parameter(
since: str, old: str, new: str, func: Literal[None] = 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
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
Member

Choose a reason for hiding this comment

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

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

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 greglucas merged commit dd40f48 into matplotlib:main Jul 28, 2023
@QuLogic QuLogic deleted the api-types branch July 28, 2023 02:18
@QuLogic QuLogic added this to the v3.8.0 milestone Jul 28, 2023
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