Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

bandpooja
Copy link

@bandpooja bandpooja commented Jun 1, 2025

PR Summary

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

Why this change is necessary:
By providing explicit type information, this enhancement significantly improves static type checking capabilities for matplotlib.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 for matplotlib.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 within matplotlib.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 new dates.pyi
Screenshot 2025-06-01 at 4 22 20 AM

@bandpooja bandpooja marked this pull request as draft June 1, 2025 07:41
@bandpooja bandpooja marked this pull request as ready for review June 1, 2025 07:42
Copy link

@github-actions github-actions bot 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 us on gitter for real-time discussion.

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

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

@tacaswell tacaswell added this to the v3.11.0 milestone Jun 2, 2025
Copy link
Member

@QuLogic QuLogic 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.

@bandpooja
Copy link
Author

Thanks for the review @QuLogic.

@QuLogic
Copy link
Member

QuLogic commented Jun 10, 2025

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

Please handle the rest of these.

@bandpooja
Copy link
Author

Done thanks again !!
Learning to write .pyi files. 😅

fmt: str

def __init__(self, fmt: str, tz: str | datetime.tzinfo | None = ..., *, usetex: bool | None = ...) -> None: ...
def __call__(self, x: float, pos: int | None = ...) -> str: ...
Copy link
Member

Choose a reason for hiding this comment

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

This inherits from Formatter, so can be dropped.

zero_formats: Sequence[str] | None = ...,
show_offset: bool = ...,
*, usetex: bool | None = ...) -> None: ...
def __call__(self, x: float, pos: int | None = ...) -> str: ...
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

defaultfmt: str = ...,
*, usetex: bool | None = ...) -> None: ...
def _set_locator(self, locator: Locator) -> None: ...
def __call__(self, x: float, pos: int | None = ...) -> str: ...
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

show_offset: bool = ...,
*, usetex: bool | None = ...) -> None: ...
def __call__(self, x: float, pos: int | None = ...) -> str: ...
def format_ticks(self, values: Sequence[float]) -> 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.

Also inherited.

def __call__(self, x: float, pos: int | None = ...) -> str: ...
def format_ticks(self, values: Sequence[float]) -> list[str]: ...
def get_offset(self) -> str: ...
def format_data_short(self, value: float) -> str: ...
Copy link
Member

Choose a reason for hiding this comment

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

Also inherited.

Comment on lines +184 to +185
@staticmethod
def axisinfo(unit: datetime.tzinfo | None, axis: Axis) -> 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 isn't a static method, and doesn't return None.

@staticmethod
def axisinfo(unit: datetime.tzinfo | None, axis: Axis) -> None: ...
@staticmethod
def convert(obj: datetime.datetime | datetime.date | float | np.datetime64 | Sequence[datetime.datetime | datetime.date | float | np.datetime64],
Copy link
Member

Choose a reason for hiding this comment

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

obj is named value in the source. Also where did float and datetime.date input come from? This goes through date2num which takes datetime.datetime | np.datetime64.

And probably this should be overloaded like date2num.

Comment on lines +198 to +199
@staticmethod
def axisinfo(unit: datetime.tzinfo | None, axis: Axis) -> None: ...
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, isn't static and doesn't return None.


class _SwitchableDateConverter:
@staticmethod
def _get_converter() -> "ConciseDateConverter" | "DateConverter": ...
Copy link
Member

Choose a reason for hiding this comment

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

Note sure why these are quoted.

Comment on lines +205 to +212
@staticmethod
def axisinfo(unit: datetime.tzinfo | None, axis: Axis) -> AxisInfo: ...
@staticmethod
def default_units(x: datetime.datetime | datetime.date | float | np.ndarray | Sequence[datetime.datetime | datetime.date | float | np.datetime64],
axis: Axis) -> datetime.tzinfo | None: ...
@staticmethod
def convert(value: datetime.datetime | datetime.date | float | np.datetime64 | Sequence[datetime.datetime | datetime.date | float | np.datetime64],
unit: datetime.tzinfo | None, axis: Axis) -> 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.

None of these are static.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

Missing type hints for matplotlib.dates.DateFormatter
4 participants