Add type hints for matplotlib.dates module#30126
Add type hints for matplotlib.dates module#30126bandpooja wants to merge 9 commits intomatplotlib:mainfrom
Conversation
There was a problem hiding this comment.
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.
QuLogic
left a comment
There was a problem hiding this comment.
I've only done up to the formatters; please correct the default values in the rest.
|
Thanks for the review @QuLogic. |
Please handle the rest of these. |
|
Done thanks again !! |
| 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: ... |
There was a problem hiding this comment.
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: ... |
| defaultfmt: str = ..., | ||
| *, usetex: bool | None = ...) -> None: ... | ||
| def _set_locator(self, locator: Locator) -> None: ... | ||
| def __call__(self, x: float, pos: int | None = ...) -> str: ... |
| 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]: ... |
| 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: ... |
| @staticmethod | ||
| def axisinfo(unit: datetime.tzinfo | None, axis: Axis) -> None: ... |
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
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.
| @staticmethod | ||
| def axisinfo(unit: datetime.tzinfo | None, axis: Axis) -> None: ... |
There was a problem hiding this comment.
Same as above, isn't static and doesn't return None.
|
|
||
| class _SwitchableDateConverter: | ||
| @staticmethod | ||
| def _get_converter() -> "ConciseDateConverter" | "DateConverter": ... |
There was a problem hiding this comment.
Note sure why these are quoted.
| @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: ... |
|
👋 @bandpooja I accidentally started work on this too at #30385 - are you still interested in finishing off this pull request? If you are, please let us know if you have any questions or can help at all. If not, it would be good to know and we can finish off the work you started :) |
PR Summary
This pull request introduces comprehensive type hint stubs (
.pyifiles) for thematplotlib.datesmodule.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.datesclasses 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
.pyifiles 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
matplotlib.dates.DateFormatter#29994" is in the body of the PR description to link the related issuemypyor similar type checkers to ensure no errors are introduced.Test Result
mypy, ruff and Pyright with the new

dates.pyi