Skip to content

Commit

Permalink
removed unused sys.version_info with python versions <3.8 around the …
Browse files Browse the repository at this point in the history
…codebase
  • Loading branch information
rafsaf committed Jul 16, 2023
1 parent 4d31b8d commit e136d82
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 63 deletions.
9 changes: 3 additions & 6 deletions prometheus_client/context_managers.py
@@ -1,13 +1,10 @@
import sys
from timeit import default_timer
from types import TracebackType
from typing import (
Any, Callable, Optional, Tuple, Type, TYPE_CHECKING, TypeVar, Union,
Any, Callable, Literal, Optional, Tuple, Type, TYPE_CHECKING, TypeVar,
Union,
)

if sys.version_info >= (3, 8, 0):
from typing import Literal

from .decorator import decorate

if TYPE_CHECKING:
Expand All @@ -23,7 +20,7 @@ def __init__(self, counter: "Counter", exception: Union[Type[BaseException], Tup
def __enter__(self) -> None:
pass

def __exit__(self, typ: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> "Literal[False]":
def __exit__(self, typ: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> Literal[False]:
if isinstance(value, self._exception):
self._counter.inc()
return False
Expand Down
63 changes: 18 additions & 45 deletions prometheus_client/decorator.py
Expand Up @@ -35,40 +35,18 @@

import collections
import inspect
from inspect import getfullargspec
import itertools
import operator
import re
import sys

__version__ = '4.0.10'

if sys.version_info >= (3,):
from inspect import getfullargspec

def get_init(cls):
return cls.__init__

def get_init(cls):
return cls.__init__
else:
class getfullargspec(object):
"A quick and dirty replacement for getfullargspec for Python 2.X"

def __init__(self, f):
self.args, self.varargs, self.varkw, self.defaults = \
inspect.getargspec(f)
self.kwonlyargs = []
self.kwonlydefaults = None

def __iter__(self):
yield self.args
yield self.varargs
yield self.varkw
yield self.defaults

getargspec = inspect.getargspec


def get_init(cls):
return cls.__init__.__func__

# getargspec has been deprecated in Python 3.5
ArgSpec = collections.namedtuple(
Expand Down Expand Up @@ -113,26 +91,21 @@ def __init__(self, func=None, name=None, signature=None,
setattr(self, a, getattr(argspec, a))
for i, arg in enumerate(self.args):
setattr(self, 'arg%d' % i, arg)
if sys.version_info < (3,): # easy way
self.shortsignature = self.signature = (
inspect.formatargspec(
formatvalue=lambda val: "", *argspec)[1:-1])
else: # Python 3 way
allargs = list(self.args)
allshortargs = list(self.args)
if self.varargs:
allargs.append('*' + self.varargs)
allshortargs.append('*' + self.varargs)
elif self.kwonlyargs:
allargs.append('*') # single star syntax
for a in self.kwonlyargs:
allargs.append('%s=None' % a)
allshortargs.append('%s=%s' % (a, a))
if self.varkw:
allargs.append('**' + self.varkw)
allshortargs.append('**' + self.varkw)
self.signature = ', '.join(allargs)
self.shortsignature = ', '.join(allshortargs)
allargs = list(self.args)
allshortargs = list(self.args)
if self.varargs:
allargs.append('*' + self.varargs)
allshortargs.append('*' + self.varargs)
elif self.kwonlyargs:
allargs.append('*') # single star syntax
for a in self.kwonlyargs:
allargs.append('%s=None' % a)
allshortargs.append('%s=%s' % (a, a))
if self.varkw:
allargs.append('**' + self.varkw)
allshortargs.append('**' + self.varkw)
self.signature = ', '.join(allargs)
self.shortsignature = ', '.join(allshortargs)
self.dict = func.__dict__.copy()
# func=None happens when decorating a caller
if name:
Expand Down
6 changes: 1 addition & 5 deletions prometheus_client/exposition.py
Expand Up @@ -38,7 +38,6 @@

CONTENT_TYPE_LATEST = 'text/plain; version=0.0.4; charset=utf-8'
"""Content type of the latest text format"""
PYTHON376_OR_NEWER = sys.version_info > (3, 7, 5)


class _PrometheusRedirectHandler(HTTPRedirectHandler):
Expand Down Expand Up @@ -545,10 +544,7 @@ def _use_gateway(
) -> None:
gateway_url = urlparse(gateway)
# See https://bugs.python.org/issue27657 for details on urlparse in py>=3.7.6.
if not gateway_url.scheme or (
PYTHON376_OR_NEWER
and gateway_url.scheme not in ['http', 'https']
):
if not gateway_url.scheme or gateway_url.scheme not in ['http', 'https']:
gateway = f'http://{gateway}'

gateway = gateway.rstrip('/')
Expand Down
10 changes: 3 additions & 7 deletions prometheus_client/metrics.py
@@ -1,11 +1,10 @@
import os
import sys
from threading import Lock
import time
import types
from typing import (
Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Type,
TypeVar, Union,
Any, Callable, Dict, Iterable, List, Literal, Optional, Sequence, Tuple,
Type, TypeVar, Union,
)

from . import values # retain this import style for testability
Expand All @@ -18,9 +17,6 @@
from .samples import Exemplar, Sample
from .utils import floatToGoString, INF

if sys.version_info >= (3, 8, 0):
from typing import Literal

T = TypeVar('T', bound='MetricWrapperBase')
F = TypeVar("F", bound=Callable[..., Any])

Expand Down Expand Up @@ -361,7 +357,7 @@ def __init__(self,
unit: str = '',
registry: Optional[CollectorRegistry] = REGISTRY,
_labelvalues: Optional[Sequence[str]] = None,
multiprocess_mode: "Literal['all', 'liveall', 'min', 'livemin', 'max', 'livemax', 'sum', 'livesum']" = 'all',
multiprocess_mode: Literal['all', 'liveall', 'min', 'livemin', 'max', 'livemax', 'sum', 'livesum'] = 'all',
):
self._multiprocess_mode = multiprocess_mode
if multiprocess_mode not in self._MULTIPROC_MODES:
Expand Down

0 comments on commit e136d82

Please sign in to comment.