Skip to content

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

Merged
csmarchbanks merged 7 commits into
masterfrom
type-everything-from-init
Feb 11, 2022
Merged

Add type annotations to everything referenced in __init__.py#771
csmarchbanks merged 7 commits into
masterfrom
type-everything-from-init

Conversation

@csmarchbanks

Copy link
Copy Markdown
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],

Copy link
Copy Markdown

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
Copy Markdown
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.

Comment thread prometheus_client/exposition.py Outdated
gateway: str,
job: str,
registry: CollectorRegistry,
grouping_key: Optional[Dict[str, str]] = None,

Copy link
Copy Markdown

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
Copy Markdown
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.

Copy link
Copy Markdown

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
Copy Markdown
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,

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

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
Copy Markdown
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 :).

Comment thread prometheus_client/samples.py

@tommyjcarpenter tommyjcarpenter left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

thanks so much!

@csmarchbanks csmarchbanks left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the review!

Comment thread prometheus_client/exposition.py Outdated
gateway: str,
job: str,
registry: CollectorRegistry,
grouping_key: Optional[Dict[str, str]] = None,

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 :).

Comment thread prometheus_client/samples.py
@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