Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
76178c2
Initial mypy type hint for ddtrace core
Yun-Kim Mar 12, 2021
bcb73a9
Initial mypy type checks for ddtrace core
Yun-Kim Mar 12, 2021
9eb80e1
Merge branch 'master' of github.com:DataDog/dd-trace-py into mypy-core
Yun-Kim Mar 12, 2021
aa4151b
Added type hinting to tracer, filters
Yun-Kim Mar 13, 2021
e392434
Addressed PR reviews
Yun-Kim Mar 15, 2021
103d01c
Merge branch 'master' into mypy-core
Yun-Kim Mar 15, 2021
40f48d9
Changed optional type for tracer.write and writer.write, added return…
Yun-Kim Mar 15, 2021
51f43d9
Added type hinting to compat.py, added return type hint to tracer.trace
Yun-Kim Mar 15, 2021
cc5cb2e
Merge branch 'master' into mypy-core
Yun-Kim Mar 15, 2021
66b1318
Added return type hinting for wrap()
Yun-Kim Mar 15, 2021
fb324fb
Merge branch 'mypy-core' of github.com:Yun-Kim/dd-trace-py into mypy-…
Yun-Kim Mar 15, 2021
d874958
Merge branch 'master' into mypy-core
Yun-Kim Mar 15, 2021
b439189
Moved Span import to avoid circular import.
Yun-Kim Mar 15, 2021
c8b9856
Fixed flake8
Yun-Kim Mar 15, 2021
38fe255
Removed circular import from compat.py, reverted previous change
Yun-Kim Mar 15, 2021
d04dc2c
Flake8 reformatting
Yun-Kim Mar 15, 2021
b48cd80
Merge branch 'master' into mypy-core
Yun-Kim Mar 16, 2021
eb8f842
Address PR comments
Yun-Kim Mar 16, 2021
4a26990
Merge branch 'master' into mypy-core
Yun-Kim Mar 16, 2021
8eb130a
Reverted tracer.write and writer.write to accept Optional[List[Span]]
Yun-Kim Mar 16, 2021
1d0ef8d
Merge branch 'master' into mypy-core
Yun-Kim Mar 16, 2021
d7894fc
Added return type hinting
Yun-Kim Mar 16, 2021
61aa5b0
Merge branch 'mypy-core' of github.com:Yun-Kim/dd-trace-py into mypy-…
Yun-Kim Mar 16, 2021
b18f7d0
Black reformatting
Yun-Kim Mar 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ddtrace/_hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import collections
from copy import deepcopy
from typing import DefaultDict
from typing import Set

from .internal.logger import get_logger
from .vendor import attr
Expand All @@ -20,7 +22,7 @@ def on_request(span, request, response):
pass
"""

_hooks = attr.ib(init=False, factory=lambda: collections.defaultdict(set))
_hooks = attr.ib(init=False, factory=lambda: collections.defaultdict(set), type=DefaultDict[str, Set])

def __deepcopy__(self, memodict=None):
hooks = Hooks()
Expand Down
32 changes: 20 additions & 12 deletions ddtrace/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import sys
import textwrap
import threading
from typing import Any
from typing import AnyStr
from typing import Text

from ddtrace.vendor import six

Expand All @@ -29,17 +32,17 @@
PYTHON_INTERPRETER = platform.python_implementation()

try:
StringIO = six.moves.cStringIO
StringIO = six.moves.cStringIO # type: ignore[attr-defined]
except ImportError:
StringIO = six.StringIO

httplib = six.moves.http_client
urlencode = six.moves.urllib.parse.urlencode
parse = six.moves.urllib.parse
Queue = six.moves.queue.Queue
httplib = six.moves.http_client # type: ignore[attr-defined]
urlencode = six.moves.urllib.parse.urlencode # type: ignore[attr-defined]
parse = six.moves.urllib.parse # type: ignore[attr-defined]
Queue = six.moves.queue.Queue # type: ignore[attr-defined]
iteritems = six.iteritems
reraise = six.reraise
reload_module = six.moves.reload_module
reload_module = six.moves.reload_module # type: ignore[attr-defined]

stringify = six.text_type
string_type = six.string_types[0]
Expand All @@ -52,10 +55,11 @@
if PYTHON_VERSION_INFO >= (3, 7):
pattern_type = re.Pattern
else:
pattern_type = re._pattern_type
pattern_type = re._pattern_type # type: ignore[misc,attr-defined]


def is_integer(obj):
# type: (Any) -> bool
"""Helper to determine if the provided ``obj`` is an integer type or not"""
# DEV: We have to make sure it is an integer and not a boolean
# >>> type(True)
Expand All @@ -71,6 +75,7 @@ def is_integer(obj):
from time import time as _time

def time_ns():
# type: () -> int
return int(_time() * 10e5) * 1000


Expand All @@ -85,15 +90,17 @@ def time_ns():
except ImportError:

def monotonic_ns():
# type: () -> int
return int(monotonic() * 1e9)


try:
from time import process_time_ns
except ImportError:
from time import clock as _process_time
from time import clock as _process_time # type: ignore[attr-defined]

def process_time_ns():
# type: () -> int
return int(_process_time() * 1e9)


Expand All @@ -104,10 +111,10 @@ def process_time_ns():


if sys.version_info.major < 3:
if isinstance(threading.current_thread(), threading._MainThread):
if isinstance(threading.current_thread(), threading._MainThread): # type: ignore[attr-defined]
main_thread = threading.current_thread()
else:
main_thread = threading._shutdown.im_self
main_thread = threading._shutdown.im_self # type: ignore[attr-defined]
else:
main_thread = threading.main_thread()

Expand Down Expand Up @@ -152,7 +159,7 @@ def func_wrapper(*args, **kwargs):
# asyncio is missing so we can't have coroutines; these
# functions are used only to ensure code executions in case
# of an unexpected behavior
def iscoroutinefunction(fn):
def iscoroutinefunction(fn): # type: ignore
return False

def make_async_decorator(tracer, fn, *params, **kw_params):
Expand All @@ -161,6 +168,7 @@ def make_async_decorator(tracer, fn, *params, **kw_params):

# DEV: There is `six.u()` which does something similar, but doesn't have the guard around `hasattr(s, 'decode')`
def to_unicode(s):
# type: (AnyStr) -> Text
""" Return a unicode string for the given bytes or string instance. """
# No reason to decode if we already have the unicode compatible object we expect
# DEV: `six.text_type` will be a `str` for python 3 and `unicode` for python 2
Expand Down Expand Up @@ -200,7 +208,7 @@ def get_connection_response(conn):
try:
import contextvars # noqa
except ImportError:
from ddtrace.vendor import contextvars # noqa
from ddtrace.vendor import contextvars # type: ignore # noqa

CONTEXTVARS_IS_AVAILABLE = False
else:
Expand Down
3 changes: 2 additions & 1 deletion ddtrace/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .ext import http


class TraceFilter(six.with_metaclass(abc.ABCMeta)):
class TraceFilter(six.with_metaclass(abc.ABCMeta)): # type: ignore[misc]
Copy link
Member

Choose a reason for hiding this comment

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

What was the type error here?

Copy link
Member

Choose a reason for hiding this comment

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

Seems like all the ABCs have type errors 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the mypy error: error: Unsupported dynamic base class "six.with_metaclass" [misc] This might be an issue with the ABCMeta rather than with_metaclass

@abc.abstractmethod
def process_trace(self, trace):
# type: (List[Span]) -> Optional[List[Span]]
Expand Down Expand Up @@ -52,6 +52,7 @@ def __init__(self, regexps):
self._regexps = [re.compile(regexp) for regexp in regexps]

def process_trace(self, trace):
# type: (List[Span]) -> Optional[List[Span]]
"""
When the filter is registered in the tracer, process_trace is called by
on each trace before it is sent to the agent, the returned value will
Expand Down
6 changes: 6 additions & 0 deletions ddtrace/internal/_encoding.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import Any
from typing import Union

class MsgpackEncoder(object):
content_type: str
def _decode(self, data: Union[str, bytes]) -> Any: ...
2 changes: 2 additions & 0 deletions ddtrace/internal/_rand.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def seed() -> None: ...
def rand64bits(check_pid: bool = True) -> int: ...
4 changes: 2 additions & 2 deletions ddtrace/internal/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def stop(self, timeout=None):

@abc.abstractmethod
def write(self, trace):
# type: (List[Span]) -> None
# type: (Optional[List[Span]]) -> None
pass


Expand All @@ -159,7 +159,7 @@ def stop(self, timeout=None):
return

def write(self, spans):
# type: (List[Span]) -> None
# type: (Optional[List[Span]]) -> None
if not spans:
return

Expand Down
2 changes: 1 addition & 1 deletion ddtrace/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
_DD_CONTEXTVAR = contextvars.ContextVar("datadog_contextvar", default=None)


class BaseContextProvider(six.with_metaclass(abc.ABCMeta)):
class BaseContextProvider(six.with_metaclass(abc.ABCMeta)): # type: ignore[misc]
"""
A ``ContextProvider`` is an interface that provides the blueprint
for a callable class, capable to retrieve the current active
Expand Down
4 changes: 2 additions & 2 deletions ddtrace/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
KNUTH_FACTOR = 1111111111111111111


class BaseSampler(six.with_metaclass(abc.ABCMeta)):
class BaseSampler(six.with_metaclass(abc.ABCMeta)): # type: ignore[misc]
@abc.abstractmethod
def sample(self, span):
pass


class BasePrioritySampler(six.with_metaclass(abc.ABCMeta)):
class BasePrioritySampler(six.with_metaclass(abc.ABCMeta)): # type: ignore[misc]
@abc.abstractmethod
def update_rate_by_service_sample_rates(self, sample_rates):
pass
Expand Down
Loading