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

Add type hints for matplotlib.dates module#30126

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

Open
bandpooja wants to merge6 commits intomatplotlib:main
base:main
Choose a base branch
Loading
frombandpooja:main

Conversation

bandpooja
Copy link

@bandpoojabandpooja commentedJun 1, 2025
edited
Loading

PR Summary

This pull request introduces comprehensive type hint stubs (.pyi files) for thematplotlib.dates module.

Why this change is necessary:
By providing explicit type information, this enhancement significantly improves static type checking capabilities formatplotlib.dates. Developers using tools like MyPy, Pyright, or Ruff can now catch potential type-related errors earlier in the development cycle, leading to more robust and maintainable code.

Problem solved:
This PR addresses the current lack of precise type information formatplotlib.dates classes and functions during static analysis. It makes the module's API more explicit and easier for automated type-checking tools to understand and validate.

Implementation reasoning:
The implementation involves adding.pyi files that define the types for the core classes and functions withinmatplotlib.dates.py. This change directly contributes to Matplotlib's ongoing initiative to expand and improve type hinting coverage across its codebase, fostering better developer experience and code quality.

Closes#29994

PR Checklist

Test Result

mypy, ruff and Pyright with the newdates.pyi
Screenshot 2025-06-01 at 4 22 20 AM

johnthagen and Anshul22Verma reacted with heart emoji
@bandpoojabandpooja marked this pull request as draftJune 1, 2025 07:41
@bandpoojabandpooja marked this pull request as ready for reviewJune 1, 2025 07:42
Copy link

@github-actionsgithub-actionsbot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join uson gitter for real-time discussion.

For details on testing, writing docs, and our review process, please seethe developer guide

We strive to be a welcoming and open project. Please follow ourCode of Conduct.

@tacaswelltacaswell added this to thev3.11.0 milestoneJun 2, 2025
Copy link
Member

@QuLogicQuLogic left a comment

Choose a reason for hiding this comment

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

I've only done up to the formatters; please correct the default values in the rest.

@@ -0,0 +1,200 @@
import datetime
import numpy as np
Copy link
Member

Choose a reason for hiding this comment

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

NumPy is third-party and thus should be in the second block between stdlib and first-party imports.


def get_epoch() -> str: ...

def _dt64_to_ordinalf(d: np.datetime64 | np.ndarray) -> float | np.ndarray: ...
Copy link
Member

Choose a reason for hiding this comment

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

There seems to be a bug here; it doesn't actually acceptnp.datetime64 or return afloat, but crashes. Maybe something to be fixed, or else the type hint shouldn't mention them.

Comment on lines +25 to +31
def datestr2num(d: str | Sequence[str], default: datetime.datetime | None = ...) -> float | np.ndarray: ...

def date2num(d: datetime.datetime | np.datetime64 | Sequence[datetime.datetime | np.datetime64]) -> float | np.ndarray: ...

def num2date(x: float | Sequence[float], tz: str | datetime.tzinfo | None = ...) -> datetime.datetime | list[datetime.datetime]: ...

def num2timedelta(x: float | Sequence[float]) -> datetime.timedelta | list[datetime.timedelta]: ...
Copy link
Member

Choose a reason for hiding this comment

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

We may want overloads on these for single input vs sequence input.

# --- Formatter Classes ---

class DateFormatter(Formatter):
tz: str | datetime.tzinfo | None
Copy link
Member

Choose a reason for hiding this comment

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

This is passed through_get_tz_info before assignment, so should always be adatetime.tzinfo.

class DateFormatter(Formatter):
tz: str | datetime.tzinfo | None
fmt: str
_usetex: bool | None
Copy link
Member

Choose a reason for hiding this comment

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

NeverNone, though since this is private and not part of the API, I'm not sure we should type hint it..

class ConciseDateFormatter(Formatter):
_locator: Locator
_tz: str | datetime.tzinfo | None
formats: List[str]
Copy link
Member

Choose a reason for hiding this comment

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

Missingdefaultfmt?

Comment on lines +78 to +83
def __init__(self, locator: Locator,
tz: str | datetime.tzinfo | None = None,
defaultfmt: str = '%Y-%m-%d',
*, usetex: bool | None = None) -> None: ...
def _set_locator(self, locator: Locator) -> None: ...
def __call__(self, x: float, pos: int | None = 0) -> str: ...
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
def__init__(self,locator:Locator,
tz:str|datetime.tzinfo|None=None,
defaultfmt:str='%Y-%m-%d',
*,usetex:bool|None=None)->None: ...
def_set_locator(self,locator:Locator)->None: ...
def__call__(self,x:float,pos:int|None=0)->str: ...
def__init__(self,locator:Locator,
tz:str|datetime.tzinfo|None=...,
defaultfmt:str=...,
*,usetex:bool|None=...)->None: ...
def_set_locator(self,locator:Locator)->None: ...
def__call__(self,x:float,pos:int|None=...)->str: ...

def set(self, **kwargs: Any) -> None: ...
def _update_rrule(self, **kwargs: Any) -> None: ...
def _attach_tzinfo(self, dt: datetime.datetime, tzinfo: datetime.tzinfo) -> datetime.datetime: ...
def _aware_return_wrapper(self, f: Callable[..., Any], returns_list: bool = False) -> Callable[..., Any]: ...
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
def_aware_return_wrapper(self,f:Callable[...,Any],returns_list:bool=False)->Callable[...,Any]: ...
def_aware_return_wrapper(self,f:Callable[...,Any],returns_list:bool=...)->Callable[...,Any]: ...

defaultfmt: str
_formatter: DateFormatter
_usetex: bool | None
scaled: dict
Copy link
Member

Choose a reason for hiding this comment

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

Missing key/value types.

def _set_locator(self, locator: Locator) -> None: ...
def __call__(self, x: float, pos: int | None = 0) -> str: ...

# --- rrulewrapper (assuming it's in rrule.py and this is its stub) ---
Copy link
Member

Choose a reason for hiding this comment

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

I don't know what this parentheses means.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@QuLogicQuLogicQuLogic left review comments

@timhoffmtimhoffmtimhoffm left review comments

@github-actionsgithub-actions[bot]github-actions[bot] left review comments

At least 1 approving review is required to merge this pull request.

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

Successfully merging this pull request may close these issues.

Missing type hints formatplotlib.dates.DateFormatter
4 participants
@bandpooja@QuLogic@timhoffm@tacaswell

[8]ページ先頭

©2009-2025 Movatter.jp