Skip to content
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

Add type annotations to everything referenced in __init__.py #771

Merged
merged 7 commits into from
Feb 11, 2022

Conversation

csmarchbanks
Copy link
Member

Fixes: #760

This should cover most of the public surface area used by others. There is more typing work to be done before we can enable stricter types, but this will be a good next step.

def _make_handler(
url: str,
method: str,
timeout: Optional[float],

Choose a reason for hiding this comment

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

Seems fine to me, but most cases where I see Optional, there is typically a default of =None; but in this case I guess the caller of this is explicitly required to pass in an explicit None..

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this one is an internal and requires that we explicitly pass timeout though even if the value is None (or a default value) from one of the other handlers. A bit unusual I agree.

gateway: str,
job: str,
registry: CollectorRegistry,
grouping_key: Optional[Dict[str, str]] = None,

Choose a reason for hiding this comment

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

if grouping key is reused below, if it has a constant structure, may want to use a TypedDict

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think it it has a constant structure, and TypedDict isn't available earlier than 3.8 anyway :(. I really want to use Protocol in a few places as well but cannot yet.

Choose a reason for hiding this comment

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

It looks like you can use typing-extensions for TypedDict and Protocol.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have been avoiding adding typing-extensions as a dependency (we have no dependencies now), but if it becomes too painful I would considering adding it.

job: str,
registry: CollectorRegistry,
grouping_key: Optional[Dict[str, str]] = None,
timeout: Optional[float] = 30,

Choose a reason for hiding this comment

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

here timeout has a default.

@@ -169,10 +201,17 @@ def __init__(self, name, documentation, count_value=None, sum_value=None, labels
if labels is None:
labels = []
self._labelnames = tuple(labels)

Choose a reason for hiding this comment

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

maybe self._labelnames = tuple(labels) if labels else ()

Copy link
Member Author

Choose a reason for hiding this comment

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

Decent idea as a future cleanup, going to leave it out of this PR to keep the focus on types clear :).

Copy link

@tommyjcarpenter tommyjcarpenter left a comment

Choose a reason for hiding this comment

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

thanks so much!

Copy link
Member Author

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

Thanks for the review!

gateway: str,
job: str,
registry: CollectorRegistry,
grouping_key: Optional[Dict[str, str]] = None,
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think it it has a constant structure, and TypedDict isn't available earlier than 3.8 anyway :(. I really want to use Protocol in a few places as well but cannot yet.

def _make_handler(
url: str,
method: str,
timeout: Optional[float],
Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this one is an internal and requires that we explicitly pass timeout though even if the value is None (or a default value) from one of the other handlers. A bit unusual I agree.

@@ -169,10 +201,17 @@ def __init__(self, name, documentation, count_value=None, sum_value=None, labels
if labels is None:
labels = []
self._labelnames = tuple(labels)
Copy link
Member Author

Choose a reason for hiding this comment

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

Decent idea as a future cleanup, going to leave it out of this PR to keep the focus on types clear :).

prometheus_client/samples.py Show resolved Hide resolved
@csmarchbanks csmarchbanks force-pushed the type-everything-from-init branch from 28bc122 to 60bc25d Compare February 9, 2022 21:06
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
@csmarchbanks csmarchbanks force-pushed the type-everything-from-init branch from 60bc25d to 7790582 Compare February 9, 2022 22:05
Also update built in collectors to use the new Abstract Base Class.

Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
@csmarchbanks csmarchbanks force-pushed the type-everything-from-init branch from 7790582 to 35a48c2 Compare February 9, 2022 22:23
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
@csmarchbanks csmarchbanks force-pushed the type-everything-from-init branch from 35a48c2 to b3a00ea Compare February 10, 2022 21:00
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
@csmarchbanks csmarchbanks force-pushed the type-everything-from-init branch from f277116 to c350e5c Compare February 10, 2022 21:15
@csmarchbanks csmarchbanks merged commit 4e0e7ff into master Feb 11, 2022
@csmarchbanks csmarchbanks deleted the type-everything-from-init branch February 11, 2022 19:03
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.

Histogram is not typed, and due to partial typing, this makes mypy --strict fail
3 participants