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
Also update built in collectors to use the new Abstract Base Class.

Signed-off-by: Chris Marchbanks <[email protected]>
Signed-off-by: Chris Marchbanks <[email protected]>
Signed-off-by: Chris Marchbanks <[email protected]>
@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