Skip to content

Use object instead of Any for labels() parameters#1186

Open
gregoiredx wants to merge 1 commit into
prometheus:masterfrom
gregoiredx:gdeveaux/labels-object-typing
Open

Use object instead of Any for labels() parameters#1186
gregoiredx wants to merge 1 commit into
prometheus:masterfrom
gregoiredx:gdeveaux/labels-object-typing

Conversation

@gregoiredx

@gregoiredx gregoiredx commented Jun 26, 2026

Copy link
Copy Markdown

I'm working on a project where we try to reduce lines flagged as Any by mypy line precision report.

The type of labels() parameters is Any, reducing the whole line as Any (mypy takes the worst of the line typing for its report).

Using object here looks like the right choice.

From https://mypy.readthedocs.io/en/stable/kinds_of_types.html#the-any-type

This should not be confused with the object type, which represents the set of all values. Unlike object, Any introduces type unsafety — see Any vs. object for more.

I first thought it would even be str since it's the natural type of labels, but labels() transforms parameters to strings automatically (labelvalues = tuple(str(labelkwargs[l]) for l in self._labelnames) and labelvalues = tuple(str(l) for l in labelvalues)).

So we mean "anything that can be stred", object looks like a good candidate for that type.

Note: I had to introduce str_labelvalues to comply with mypy (since now labelvalues has type tuple[object, ...] which is not compatible with tuple[str, ...]).

@gregoiredx gregoiredx force-pushed the gdeveaux/labels-object-typing branch from 9c154a6 to 49c6c19 Compare June 26, 2026 10:27
@gregoiredx gregoiredx marked this pull request as ready for review June 26, 2026 10:27
@gregoiredx

Copy link
Copy Markdown
Author

Hello @csmarchbanks 👋 Following the CONTRIBUTING.md, I should ping you to get your review for a trivial change.

This is just a type change from Any to object. What do you think about it?

Signed-off-by: Grégoire Deveaux <gregoire.deveaux@gitguardian.com>
@gregoiredx gregoiredx force-pushed the gdeveaux/labels-object-typing branch from 49c6c19 to 757abe8 Compare June 26, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant