Skip to content

Create category.pyi #30093

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 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/matplotlib/category.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Plotting of string "category" data: ``plot(['d', 'f', 'a'], [1, 2, 3])`` will
plot three points with x-axis values of 'd', 'f', 'a'.

See :doc:`/gallery/lines_bars_and_markers/categorical_variables` for an
example.

The module uses Matplotlib's `matplotlib.units` mechanism to convert from
strings to integers and provides a tick locator, a tick formatter, and the
`.UnitData` class that creates and stores the string-to-integer mapping.
"""
Comment on lines +1 to +11
Copy link
Member

Choose a reason for hiding this comment

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

The .pyi files don't typically have doc strings


from __future__ import annotations
from matplotlib.units import ConversionInterface, AxisInfo
from matplotlib import ticker
from matplotlib.axis import Axis
from collections import OrderedDict
import numpy as np
from typing import Any, Dict, Iterable, List, Optional, Union

class StrCategoryConverter(ConversionInterface):
@staticmethod
def convert(
value: Union[str, Iterable[str]],

Check failure on line 24 in lib/matplotlib/category.pyi

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/category.pyi" range:{start:{line:24 column:42} end:{line:24 column:43}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:24 column:42} end:{line:24 column:43}}}
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
value: Union[str, Iterable[str]],
value: str | Iterable[str],

The .pyi files typically use the union operator rather than explicit union, please make this change to all the unions in this file
https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html

unit: UnitData,

Check failure on line 25 in lib/matplotlib/category.pyi

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/category.pyi" range:{start:{line:25 column:24} end:{line:25 column:25}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:25 column:24} end:{line:25 column:25}}}
axis: Axis
) -> Union[float, np.ndarray]: ...
@staticmethod
def axisinfo(unit: UnitData, axis: Axis) -> AxisInfo: ...
@staticmethod
def default_units(
data: Union[str, Iterable[str]],

Check failure on line 32 in lib/matplotlib/category.pyi

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/category.pyi" range:{start:{line:32 column:41} end:{line:32 column:42}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:32 column:41} end:{line:32 column:42}}}
axis: Axis
) -> UnitData: ...
@staticmethod
def _validate_unit(unit: UnitData) -> None: ...

class StrCategoryLocator(ticker.Locator):
def __init__(self, units_mapping: Dict[str, int]) -> None: ...
def __call__(self) -> List[int]: ...
def tick_values(self, vmin: Any, vmax: Any) -> List[int]: ...

class StrCategoryFormatter(ticker.Formatter):
def __init__(self, units_mapping: Dict[str, int]) -> None: ...
def __call__(self, x: float, pos: Optional[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.

Suggested change
def __call__(self, x: float, pos: Optional[int] = None) -> str: ...
def __call__(self, x: float, pos: int | None) -> str: ...

also use the | convetion for optional, see https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html

def format_ticks(self, values: Iterable[float]) -> List[str]: ...
@staticmethod
def _text(value: Union[str, bytes]) -> str: ...

class UnitData:
_mapping: OrderedDict[Union[str, bytes], int]
def __init__(
Copy link
Member

Choose a reason for hiding this comment

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

I think ._counter also needs to be documented

self,

Check failure on line 53 in lib/matplotlib/category.pyi

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/category.pyi" range:{start:{line:53 column:14} end:{line:53 column:15}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:53 column:14} end:{line:53 column:15}}}
data: Optional[Iterable[Union[str, bytes]]] = None
) -> None: ...
def update(self, data: Iterable[Union[str, bytes]]) -> None: ...
@staticmethod
def _str_is_convertible(val: Union[str, bytes]) -> bool: ...
Loading