MNT: Add type information to rcParams - #32159
Conversation
1c203be to
6e24b6c
Compare
The added `_Param.type` attribute is currently used only for documentation, not for validation. This allows to be more flexible and conveniently use type annotations such as int and list[float] | None, but also strings such as ":mpltype:`color`".
There was a problem hiding this comment.
validate_fontsize() performs a s.lower(), so technically isn't this list too narrow? I don't think there's a way to specify a case-insensitive string as a type, however.
There was a problem hiding this comment.
Correct. Here I define the "normalized" version as recommended spelling. It's ok if the parser is more permissive. For now, this is just for documentation, so a user is directed to use these forms. Even if we at some point in the future use this for type checking, it may prompt corrections in some cases, where they are not strictly necessary, but IMHO this is bearable and perferrable compared to doing a permissive specification.
The lower() is a historic artifact. I wouldn't do these nowadays because it's unnecessarily lax. But it's not important enough to narrow down the implementation and break user code.
There was a problem hiding this comment.
That makes a lot of sense! I figured that this was the case, but I wanted to mention every case where the type and the validator were mismatched.
| "mathtext.fallback", | ||
| default="cm", | ||
| type=Literal["cm", "stix", "stixsans"] | None, | ||
| validator=_validate_mathtext_fallback, |
There was a problem hiding this comment.
_validate_mathtext_fallback also does a s.lower()
There was a problem hiding this comment.
As above. If in doubt, make the specification more strict and the implementation more permissive.
| "xtick.minor.ndivs", | ||
| default="auto", | ||
| type=int | Literal["auto"], | ||
| validator=_validate_minor_tick_ndivs, |
There was a problem hiding this comment.
_validate_minor_tick_ndivs() calls cbook._str_lower_equal
| "center", "top", "bottom", "baseline", "center_baseline"], | ||
|
|
||
| "grid.color": validate_color, # grid color | ||
| "grid.linestyle": _validate_linestyle, # solid |
There was a problem hiding this comment.
I think this is the mismatch causing the current test failure. I'm not sure if this can be _LineStyleType or not.
There was a problem hiding this comment.
Whoops, the quick fix was too quick. I intended to update the type not the validator. Fixed.
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
That can get encapsulated in a proper datatype though? (the |
|
I don't follow. You could create a type LimitedFloat that does only accept this, but as far as I understand typing, a user supplied But that's all for later PRs. |
Yeah, was just mentioning it as I think there's a way to encapsulate those constraints in types, with the implication being it's fine if this less precise form gets dropped now. Going down this rabbit hole, looks like Annotated is the way to do this cleanly. |
story645
left a comment
There was a problem hiding this comment.
Mostly questions/nits but useful info for docs.
There was a problem hiding this comment.
Just to make sure I'm understanding, this is why you can't use the colortypes in typing and the like and are using the doc types instead?
* MNT: Add type information to rcParams The added `_Param.type` attribute is currently used only for documentation, not for validation. This allows to be more flexible and conveniently use type annotations such as int and list[float] | None, but also strings such as ":mpltype:`color`". * Apply suggestions from code review Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Add review suggestions

PR summary
The added
_Param.typeattribute is currently used only for documentation, not for validation.This allows to be more flexible and conveniently use type annotations such as
intandlist[float] | None, but also strings such as":mpltype:`color`".Additional notes:
typehas a strong overlap with validators. Keeping them separate and have the duplication for now is a conscious design decision. In addtion to type checking, validators do more: They convert from string, because they are primarily built as a tooling to parse matplotlibrc. They can also do value validation beyond type validation (e.g._validate_greaterthan_minushalf).It is furthermore intentional to mostly standardize
typetowards type annotation and not use free text, even if that means we cannot express "greater thatn minus half". It is anticipated that we may use this information for type checking / validation eventually, e.g. we could build a TypedDict from this information, or maybe at some point we want to switch from a_Paramspecification to a specification via dataclasses. Proper type annotations will facilitate this.AI Disclosure
no AI
PR quality check